Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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("Harga Tiket:"+price+" cents");
- System.out.println ("##################");
- return price;
- }
- /**
- * Return the amount of money already inserted for the
- * next ticket.
- */
- public int getBalance()
- {
- System.out.println("Saldo anda:"+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 telah masuk ke saldo anda");
- 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 (+ 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 ("##################");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement