Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * A self-serving machine for train ticket purchasing
  4.  *
  5.  * APTX-4869
  6.  * 09-29-2019
  7.  */
  8. public class TicketMachine
  9. {
  10.     private int price;
  11.     private int balance;
  12.     private int total;
  13.     private int refund;
  14.     private int tickets;
  15.    
  16.     public TicketMachine(int ticketCost)
  17.     {
  18.         price = ticketCost;
  19.         balance = 0;
  20.         total = 0;
  21.         refund = 0;
  22.     }
  23.  
  24.     public int getPrice()
  25.     {
  26.         return price;
  27.     }
  28.  
  29.     public int getBalance()
  30.     {
  31.         return balance;
  32.     }
  33.    
  34.     public void getTickets(int tkt)
  35.     {
  36.         if(tkt >= 1)
  37.         {
  38.             tickets = tkt;
  39.         }
  40.         else
  41.         {
  42.             System.out.println("Please insert the right number of tickets.\n");
  43.         }
  44.     }
  45.  
  46.  
  47.     public void insertMoney(int money)
  48.     {
  49.         if (money > 0)
  50.         {
  51.             balance = balance + money;
  52.         }
  53.         else
  54.         {
  55.             System.out.println("Please insert the right amount of money.\n");
  56.         }
  57.     }
  58.  
  59.     /**
  60.      * Print a ticket.
  61.      * Update the total collected and
  62.      * reduce the balance to zero.
  63.      */
  64.    
  65.     public void purchaseTickets()
  66.     {
  67.         System.out.println ("Ticket cost = $"+price+".");
  68.         System.out.println ("Your total amount of money = $"+balance+".");
  69.         if (balance > 0)
  70.         {
  71.             if(balance >= (price*tickets))
  72.             {
  73.                 // Simulate the printing of a ticket.
  74.                 System.out.println("\n+++++++++++++++++++++++++++++++++\n");
  75.                
  76.                 System.out.println("Mystery Train [Final Destination]");
  77.                 System.out.println("Ticket(s) : "+ tickets +".");
  78.                 System.out.println("Total ticket(s) cost : $"+ (price*tickets) +".\n");
  79.                 System.out.println("+++++++++++++++++++++++++++++++++");
  80.                 System.out.println();
  81.                
  82.                 // Update the total collected with the balance.
  83.                 total = total + balance;
  84.                 refund = refundBalance();
  85.                 if (refund == 0)
  86.                 {
  87.                     System.out.println("No refundable money.\n");
  88.                 }
  89.                 else
  90.                 {
  91.                     System.out.println("Refundable money: $"+refund+".\n");
  92.                 }
  93.                 // Clear the balance.
  94.                 balance = 0;
  95.             }
  96.             else
  97.             {
  98.                 System.out.println ("Ticket to buy : "+tickets);
  99.                 System.out.println("Please insert enough money. Insert $"
  100.                 +((price * tickets)-balance)+ " more.\n");
  101.             }
  102.         }
  103.         else
  104.         {
  105.             System.out.println("Insert your money.\n");
  106.         }
  107.     }
  108.    
  109.     public int refundBalance()
  110.     {
  111.         int refundable;
  112.         refundable = balance - price * tickets;
  113.         balance = 0;
  114.         return refundable;
  115.     }
  116. }