document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * a self-serving machine for train ticket purchasing. Machine to
  4.  * arrange ticket price on every destination, change money, and produce
  5.  * paper or receipt.
  6.  *
  7.  * yaniarpe
  8.  */
  9. public class TicketMachine
  10. {
  11.     // The price of a ticket from this machine.
  12.     private int price;
  13.     // The amount of money entered by a customer so far.
  14.     private int balance;
  15.     private int balance2;
  16.     // The total amount of money collected by this machine.
  17.     private int total;
  18.     // The total amount of change money
  19.     private int refund;
  20.     // The amount of tickets
  21.     private int tickets;
  22.     /**
  23.      * Create a machine that issues tickets of the given price.  
  24.      * Note that the price must be greater than zero, and there
  25.      * are no checks to ensure this.
  26.      */  
  27.     public TicketMachine(int ticketCost)
  28.     {
  29.         price = ticketCost;
  30.         balance = 0;
  31.         total = 0;
  32.         refund = 0;
  33.     }
  34.  
  35.     /**
  36.      * Return the price of a ticket.
  37.      */
  38.     public int getPrice()
  39.     {
  40.         return price;
  41.     }
  42.  
  43.     /**
  44.      * Return the amount of money already inserted for the
  45.      * next ticket.
  46.      */
  47.     public int getBalance()
  48.     {
  49.         return balance;
  50.     }
  51.    
  52.     /**
  53.      * Return the number of ticket wanted.
  54.      */
  55.     public void getTickets(int tkt)
  56.     {
  57.         if(tkt >= 1)
  58.         {
  59.             tickets = tkt;
  60.         }
  61.         else
  62.         {
  63.             System.out.println("Please insert the right number of tickets.");
  64.             System.out.println();
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Receive an amount of money in rupiah from a customer.
  70.      */
  71.     public void insertMoney(int money)
  72.     {
  73.         if (money > 0)
  74.         {
  75.             balance = balance + money;
  76.             balance2 = balance;
  77.         }
  78.         else
  79.         {
  80.             System.out.println("Please insert the right amount of money");
  81.             System.out.println();
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Print a ticket.
  87.      * Update the total collected and
  88.      * reduce the balance to zero.
  89.      */
  90.    
  91.     public void printTickets()
  92.     {
  93.         // Simulate the printing of a ticket.
  94.         for(int i = 1; i <= tickets; i++)
  95.         {
  96.             System.out.println("--------------------------------");
  97.             System.out.println();
  98.             System.out.println("      -SUPER TRAIN TICKET-      ");
  99.             System.out.println("     A ticket for 1 person.     ");
  100.             System.out.println("  Ticket price: IDR "+ price +".");
  101.             System.out.println();
  102.             System.out.println("--------------------------------");
  103.             System.out.println();
  104.         }
  105.         // Simulate the printing of receipt.    
  106.         System.out.println("--------------------------------");
  107.         System.out.println();
  108.         System.out.println("            RECEIPT             ");
  109.         System.out.println("         -SUPER  TRAIN-         ");
  110.         System.out.println("  Ticket price: IDR "+ price +".");
  111.         System.out.println("  Number of Tickets: "+tickets);
  112.         System.out.println("  Total Price: IDR "+price*tickets+".");
  113.         System.out.println();
  114.         System.out.println("--------------------------------");
  115.         System.out.println();
  116.            
  117.         // Update the total collected with the balance.
  118.         total = total + balance;
  119.         // Clear the balance.
  120.         balance = 0;
  121.     }
  122.    
  123.     public int refundBalance()
  124.     {
  125.         int refundable;
  126.         refundable = balance2 - price * tickets;
  127.         balance2 = 0;
  128.         return refundable;
  129.     }
  130. }
');