Berikut saya sertakan codingan saya dalam membuat Ticket Machine di BluJ
Saya membuat 2 class, class pertama untuk TicketMachine, class 2 untuk main.
Class Ticket Machine, memiliki codingan seperti ini:
Class main, memiliki codingan seperti ini
Outputnya adalah:
Nama : Bobbi Adittya
NRP : 05111740000099
Kelas : PBO A
Saya membuat 2 class, class pertama untuk TicketMachine, class 2 untuk main.
Class Ticket Machine, memiliki codingan seperti ini:
/**
* Write a description of class ticket_machine here.
*
* @author Bobbi Aditya
* @version 1.0
*/
public class TicketMachine
{
// The price of a ticket from this machine.
public int price;
// The amount of money entered by a customer so far.
public int balance;
// The total amount of money collected by this machine.
public int total;
/**
* Create a machine that issues tickets of the given price.
* Note that the price must be greater than zero, and there
* are no checks to ensure this.
*/
public TicketMachine(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
}
/**
* Return the price of a ticket.
*/
public int getPrice()
{
System.out.println("Ticket Price:"+price+" cents");
System.out.println ("##################");
return price;
}
/**
* Return the amount of money already inserted for the
* next ticket.
*/
public int getBalance()
{
System.out.println("Your Balance:"+balance+" cents");
System.out.println("You need to insert "+(price-balance)+" cents more");
System.out.println ("##################");
return balance;
}
/**
* Receive an amount of money in cents from a customer.
*/
public void insertMoney(int amount)
{
balance = balance + amount;
System.out.println (+amount+" cents inserted");
System.out.println ("##################");
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public void printTicket()
{
if(balance>=price)
{
// Simulate the printing of a ticket.
System.out.println ("##################");
System.out.println ("The KA Line");
System.out.println ("Ticket go to AA station");
System.out.println (+ price +" cents.");
System.out.println ("Enjoy your trip!!");
System.out.println ("##################");
// Update the total collected with the balance.
total = total + balance;
// Clear the balance.
balance = 0;
}
else{
System.out.println("Ticket can't be printed");
System.out.println("You must insert at least: " +(price-balance)+" cents.");
System.out.println ("##################");
}
}
}
Class main, memiliki codingan seperti ini
/**
* Write a description of class main here.
*
* @author Bobbi Aditya
* @version 1.0
*/
import java.util.Scanner;
public class IntMain
{
public static void main(String args[])
{
Scanner scan= new Scanner(System.in);
int cost,menu,balance;
System.out.println("Masukkan harga tiket \n");
cost=scan.nextInt();
TicketMachine ticket=new TicketMachine(cost);
balance=ticket.balance;
//cost=ticket.getPrice();
System.out.println("1. Get Price");
System.out.println("2. Get Balance");
System.out.println("3. Insert Money");
System.out.println("4. Print Ticket");
while(true)
{
menu=scan.nextInt();
if(menu==1)
{
ticket.getPrice();
}
else if(menu==2)
{
ticket.getBalance();
}
else if(menu==3)
{
int money=scan.nextInt();
ticket.insertMoney(money);
}
else if(menu==4)
{
ticket.printTicket();
break;
}
}
}
}
Outputnya, saya menggunakan harga tiket 300, kemudian saya mengecek balance saya. Setelah itu saya mengisikan uang sejumlah 150. kemudian saya kembali mengecek balance saya. Kemudian saya mengisikan kembali uang sejumlah 150. Dan yang terakhir saya mengeprint ticket yang sudah dibayarkan.Outputnya adalah:
Nama : Bobbi Adittya
NRP : 05111740000099
Kelas : PBO A
Comments
Post a Comment