Advertisement
zenados

TicketMachine Main.java

Sep 28th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.util.Scanner;  
  2. public class Main  
  3. {  
  4.     public static void main(String[]args)  
  5.     {  
  6.         Scanner sc = new Scanner(System.in);  
  7.         int cost, menu;
  8.         boolean exit = false;
  9.        
  10.         //takes price as input for current ticket machine
  11.         System.out.print("Input the price of the ticket : ");  
  12.         cost = sc.nextInt();  
  13.        
  14.         //instantiates a new ticketmachine instance
  15.         TicketMachine tiket = new TicketMachine(cost);  
  16.        
  17.         while(!exit)
  18.         {
  19.             System.out.println("\nTicket Machine Application");
  20.             System.out.println("1. Get Price");
  21.             System.out.println("2. Get Balance");
  22.             System.out.println("3. Insert Money");
  23.             System.out.println("4. Print Ticket");
  24.             System.out.println("5. Exit");
  25.             menu = sc.nextInt();
  26.  
  27.             int uang;
  28.             switch(menu)
  29.             {
  30.                 //if the user wants to see the ticket price
  31.                 //of the current ticket machine
  32.                 case 1:
  33.                     cost=tiket.getPrice();
  34.                     System.out.println("Cost : "+cost);
  35.                     break;
  36.                 //if the user wants to check the amount of
  37.                 //money already inserted into the machine
  38.                 case 2:
  39.                     System.out.println("Balance : "+tiket.getBalance());
  40.                     break;
  41.                 //if the user wants to input money into the machine
  42.                 case 3:  
  43.                     System.out.println("Input Money inserted : ");
  44.                     uang=sc.nextInt();
  45.                     tiket.insertMoney(uang);  
  46.                     break;  
  47.                 //if the use wants to buy and print a ticket from
  48.                 //the ticket machine
  49.                 case 4:  
  50.                     tiket.printTicket();  
  51.                     break;
  52.                 //when the user wants to exit the ticket machine program
  53.                 case 5:
  54.                     if(tiket.getBalance() > 0) {
  55.                         System.out.println("Giving Change for : " + tiket.getBalance());
  56.                     }
  57.                     System.out.println("Thank You and Good Bye");
  58.                     exit = true;
  59.                     break;
  60.                 //error handling if the user doesn't pick any of the
  61.                 //given options
  62.                 default:
  63.                     System.out.println("Input Not Defined");
  64.                     break;
  65.             }  
  66.         }  
  67.     }  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement