Advertisement
BAN-tux

Untitled

Dec 16th, 2019
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1.  public class TicketMachine  
  2.   {  
  3.   // The price of a ticket from this machine.  
  4.   public int price;  
  5.   // The amount of money entered by a customer so far.  
  6.   public int balance;  
  7.   // The total amount of money collected by this machine.  
  8.   public int total;  
  9.   /**  
  10.   * Create a machine that issues tickets of the given price.  
  11.   * Note that the price must be greater than zero, and there  
  12.   * are no checks to ensure this.  
  13.   */  
  14.   public TicketMachine(int ticketCost)  
  15.   {  
  16.   price = ticketCost;  
  17.   balance = 0;  
  18.   total = 0;  
  19.   }  
  20.   /**  
  21.   * Return the price of a ticket.  
  22.   */  
  23.   public int getPrice()  
  24.   {  
  25.   System.out.println("Harga Tiket:"+price+" cents");  
  26.   System.out.println ("##################");  
  27.   return price;  
  28.   }  
  29.   /**  
  30.   * Return the amount of money already inserted for the  
  31.   * next ticket.  
  32.   */  
  33.   public int getBalance()  
  34.   {  
  35.    System.out.println("Saldo anda:"+balance+" cents");  
  36.    System.out.println("You need to insert "+(price-balance)+" cents more");  
  37.    System.out.println ("##################");  
  38.    return balance;  
  39.   }  
  40.   /**  
  41.   * Receive an amount of money in cents from a customer.  
  42.   */  
  43.   public void insertMoney(int amount)  
  44.   {  
  45.    balance = balance + amount;  
  46.    System.out.println (+amount+" cents telah masuk ke saldo anda");  
  47.    System.out.println ("##################");  
  48.   }  
  49.   /**  
  50.   * Print a ticket.  
  51.   * Update the total collected and  
  52.   * reduce the balance to zero.  
  53.   */  
  54.   public void printTicket()  
  55.   {  
  56.     if(balance>=price)  
  57.     {  
  58.    // Simulate the printing of a ticket.  
  59.    System.out.println ("##################");  
  60.    System.out.println ("The KA Line");    
  61.    System.out.println (+ price +" cents.");  
  62.    System.out.println ("Enjoy your Trip!!!");  
  63.    System.out.println ("##################");  
  64.    // Update the total collected with the balance.  
  65.    total = total + balance;  
  66.    // Clear the balance.  
  67.    balance = 0;  
  68.  }  
  69.    else{  
  70.   System.out.println("Ticket can't be printed");  
  71.   System.out.println("You must insert at least: " +(price-balance)+" cents.");  
  72.   System.out.println ("##################");      
  73.    }  
  74.   }  
  75.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement