Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. public class TicketMachine  
  2.  {  
  3.  // The price of a ticket from this machine.  
  4.  private int price;  
  5.  // The amount of money entered by a customer so far.  
  6.  private int balance;  
  7.  // The total amount of money collected by this machine.  
  8.  private 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.   return price;  
  26.  }  
  27.  /**  
  28.  * Return the amount of money already inserted for the  
  29.  * next ticket.  
  30.  */  
  31.  public int getBalance()  
  32.  {  
  33.    return balance;  
  34.  }  
  35.  /**  
  36.  * Receive an amount of money in cents from a customer.  
  37.  */  
  38.  public void insertMoney(int amount)  
  39.  {  
  40.    balance = balance + amount;  
  41.  }  
  42.  /**  
  43.  * Print a ticket.  
  44.  * Update the total collected and  
  45.  * reduce the balance to zero.  
  46.  */  
  47.  public void printTicket()  
  48.  {  
  49.    // Simulate the printing of a ticket.  
  50.    System.out.println("##################");  
  51.    System.out.println("# Ticket Machine");  
  52.    System.out.println("# Ticket");  
  53.    System.out.println("# " + price + " cents.");  
  54.    System.out.println("##################");  
  55.    System.out.println();  
  56.    // Update the total collected with the balance.  
  57.    total = total + balance;  
  58.    // Clear the balance.  
  59.    balance = 0;  
  60.  }  
  61.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement