Mesin tiket

Contoh implementasi dari mesin tiket :


dengan fungsi Main :

import java.util.Scanner;  
 public class IntMain  
 {  
 public static void main(String args[])  
 {  
  Scanner scan= new Scanner(System.in);  
  int cost,menu;  
  System.out.println("Masukkan harga tiket \n");  
  cost=scan.nextInt();  
  TicketMachine ticket=new TicketMachine(cost);
  
  while (true) 
  {
  System.out.println("1. Dapatkan harga");  
  System.out.println("2. Dapatkan uang");  
  System.out.println("3. Masukkan uang");  
  System.out.println("4. Print tiket");  
  System.out.println("5. Keluar");
  menu=scan.nextInt();  
  
  switch(menu)  
  {  
   case 1:  
   cost=ticket.getPrice();  
   System.out.println(cost);  
   break;  
   case 2:  
   ticket.getBalance();  
   break;  
   case 3:  
   int money=scan.nextInt();  
   ticket.insertMoney(money);  
   break;  
   case 4:  
   ticket.printTicket();  
   break;  
   case 5:
   ticket.exit();
   break;
  }  
  }  
 }
}  


dan fungsi untuk menu pendukung :




 public class TicketMachine  
 {   
 private int price;  
 private int balance;    
 private int total;  

 public TicketMachine(int ticketCost)  
 {  
  price = ticketCost;  
  balance = 0;  
  total = 0;  
 }  

 public int getPrice()  
 {  
  return price;  
 }  

 public int getBalance()  
 {  
   return balance;  
 }  

 public void insertMoney(int amount)  
 {  
   balance = balance + amount;  
 }  

 public void exit()
 {
     System.exit(0);
 }
 public void printTicket()  
 {  
   System.out.println("##################");  
   System.out.println("# The BlueJ Line");  
   System.out.println("# Ticket");  
   System.out.println("# " + price + " rupiah.");  
   System.out.println("##################");  
   System.out.println();    
   total = total + balance;  
   balance = 0;  
 }  
 } 

sehingga ketika dijalankan menghasilkan :





Comments