SHARE
TWEET

Untitled

a guest Jan 14th, 2020 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * TicketMachine models a naive ticket machine that issues
  3.  * flat-fare tickets.
  4.  * The price of a ticket is specified via the constructor.
  5.  * It is a naive machine in the sense that it trusts its users
  6.  * to insert enough money before trying to print a ticket.
  7.  * It also assumes that users enter sensible amounts.
  8.  *
  9.  * @author David J. Barnes and Michael K├Âlling
  10.  * @version 2016.02.29
  11.  */
  12. public class TicketMachine
  13. {
  14.     // The price of a ticket from this machine.
  15.     private int price;
  16.     // The amount of money entered by a customer so far.
  17.     private int balance;
  18.     // The total amount of money collected by this machine.
  19.     private int total;
  20.  
  21.     /**
  22.      * Create a machine that issues tickets of the given price.
  23.      * Note that the price must be greater than zero, and there
  24.      * are no checks to ensure this.
  25.      */
  26.     public TicketMachine(int cost)
  27.     {
  28.         price = cost;
  29.         balance = 0;
  30.         total = 0;
  31.     }
  32.  
  33.     /**
  34.      * Return the price of a ticket.
  35.      */
  36.     public int getPrice()
  37.     {
  38.         return price;
  39.     }
  40.  
  41.     /**
  42.      * Return the amount of money already inserted for the
  43.      * next ticket.
  44.      */
  45.     public int getBalance()
  46.     {
  47.         return balance;
  48.     }
  49.    
  50.     /**
  51.      * Exercise 2.26
  52.      */
  53.     public int getTotal()
  54.     {
  55.         return total;
  56.     }
  57.    
  58.     /**
  59.      * Receive an amount of money from a customer.
  60.      */
  61.     public void insertMoney(int amount)
  62.     {
  63.         balance = balance + amount;
  64.     }
  65.    
  66.     /**
  67.      * Exercise 2.32
  68.      */
  69.     public void setPrice(int cost)
  70.     {
  71.         price = cost;
  72.     }
  73.  
  74.     /**
  75.      * Print a ticket.
  76.      * Update the total collected and
  77.      * reduce the balance to zero.
  78.      */
  79.     public void printTicket()
  80.     {
  81.         // Simulate the printing of a ticket.
  82.         System.out.println("##################");
  83.         System.out.println("# The BlueJ Line");
  84.         System.out.println("# Ticket");
  85.         System.out.println("# " + price + " cents.");
  86.         System.out.println("##################");
  87.         System.out.println();
  88.  
  89.         // Update the total collected with the balance.
  90.         total = total + balance;
  91.         // Clear the balance.
  92.         balance = 0;
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top