Advertisement
GSculerlor

TicketMachine.java

Sep 21st, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicketMachine
  4. {
  5.     private int price;
  6.     private int balance;
  7.     private int total;
  8.     public TicketMachine(int cost)
  9.     {
  10.         price = cost;
  11.         balance = 0;
  12.         total = 0;
  13.     }
  14.     /**
  15.      * @Return The price of a ticket.
  16.      */
  17.     public int getPrice()
  18.     {
  19.         return price;
  20.     }
  21.     public int getBalance()
  22.     {
  23.         return balance;
  24.     }
  25.     public void insertMoney(int amount)
  26.     {
  27.         if(amount > 0) {
  28.             balance = balance + amount;
  29.         }
  30.         else {
  31.             System.out.println("Use a positive amount rather than: " +
  32.                                amount);
  33.         }
  34.     }
  35.     public void printTicket()
  36.     {
  37.         if(balance >= price) {
  38.             // Simulate the printing of a ticket.
  39.             System.out.println("##################");
  40.             System.out.println("# The BlueJ Line");
  41.             System.out.println("# Ticket");
  42.             System.out.println("# " + price + " cents.");
  43.             System.out.println("##################");
  44.             System.out.println();
  45.  
  46.             // Update the total collected with the price.
  47.             total = total + price;
  48.             // Reduce the balance by the prince.
  49.             balance = balance - price;
  50.         }
  51.         else {
  52.             System.out.println("You must insert at least: " +
  53.                                (price - balance) + " more cents.");
  54.                    
  55.         }
  56.     }
  57.  
  58.     public int refundBalance()
  59.     {
  60.         int amountToRefund;
  61.         amountToRefund = balance;
  62.         balance = 0;
  63.         return amountToRefund;
  64.     }
  65.  
  66.     public static void main(String args[]){  
  67.         Scanner scan= new Scanner(System.in);  
  68.         int cost, menu;
  69.         boolean entry;
  70.    
  71.         System.out.println("Masukkan harga tiket \n");  
  72.         cost = scan.nextInt();
  73.    
  74.         TicketMachine ticket = new TicketMachine(cost);
  75.         entry = true;
  76.        
  77.         while(entry) {
  78.             System.out.println("1. Get Price");  
  79.             System.out.println("2. Get Balance");  
  80.             System.out.println("3. Insert Money");  
  81.             System.out.println("4. Print Ticket");  
  82.             System.out.println("5. Exit");
  83.            
  84.             menu=scan.nextInt();  
  85.             switch(menu){  
  86.                 case 1:  
  87.                     cost=ticket.getPrice();  
  88.                     System.out.println(cost);
  89.                     break;
  90.                 case 2:  
  91.                     ticket.getBalance();
  92.                     break;
  93.                 case 3:  
  94.                     int money=scan.nextInt();  
  95.                     ticket.insertMoney(money);
  96.                     break;
  97.                 case 4:  
  98.                     ticket.printTicket();
  99.                     break;
  100.                 case 5:
  101.                     entry = false;
  102.                     break;
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement