Advertisement
mnaufaldillah

TicketMachineTugas 4

Nov 1st, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. /**
  2.  * Class ticketMachine yang mengeluarkan tiket dengan harga tetap
  3.  * Harga tiket ditentukan dengan input user
  4.  *
  5.  * @author Muhammad Naufaldillah
  6.  * @version 1 November 2020
  7.  */
  8. public class TicketMachine
  9. {
  10.     // The price of a ticket from this machine.
  11.     private int price;
  12.     // The amount of money entered by a customer so far.
  13.     private int balance;
  14.     // The total amount of money collected by this machine.
  15.     private int total;
  16.  
  17.     /**
  18.      * Creat a machine that issues tickets at given price.
  19.      * Note that the price must be greater than zero, and there
  20.      * are no checks to ensure this.
  21.      */
  22.     public TicketMachine(int cost)
  23.     {
  24.         price = cost;
  25.         balance = 0;
  26.         total = 0;
  27.     }
  28.  
  29.     /**
  30.      * Return the price of a ticket
  31.      */
  32.     public int getPrice()
  33.     {
  34.         return price;
  35.     }
  36.    
  37.     /**
  38.      * Return the amount of money already inserted for the
  39.      * next ticket.
  40.      */
  41.     public int getBalance()
  42.     {
  43.         return balance;
  44.     }
  45.    
  46.     /**
  47.      * Receive an amount of money from a customer
  48.      */
  49.     public void insertMoney(int amount)
  50.     {
  51.         balance = balance + amount;
  52.     }
  53.    
  54.     /**
  55.      * Print a ticket
  56.      * Update the total collected
  57.      * reduce the balnce to zero.
  58.      */
  59.     public void printTicket()
  60.     {
  61.         //Simulate the printing of a ticket
  62.         System.out.println("##################");
  63.         System.out.println("# 16th National middle school tournament Final");
  64.         System.out.println("# Ticket");
  65.         System.out.println("# " + price + " yen");
  66.         System.out.println("# Enjoy the match");
  67.         System.out.println("##################");
  68.        
  69.         // Update the total collected with the balance.
  70.         total = total + balance;
  71.        
  72.         // Clear the balance.
  73.         balance = 0;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement