PBO3-Membuat Simulasi Tiket Mesin

Tugas 3 PBO-A
Pada kali ini saya ditugaskan untuk membuat simulasi sebuah tiket mesin. Sebelum diberi tugas ini kami diperlihatkan sebuah video bagaimana sebuah tiket mesin bekerja. Lalu kami membuat simulasinya dalam BlueJ.
Disini saya menggunakan hanya 2 class yaitu:

  1. TicketMachine
  2. IntMain

  • Ticket Machine

 /**  
  * Ticket Machine (methods)  
  * Samuel Marcellinus  
  * 05111740000134  
  */  
 import java.util.Scanner;   
 public class TicketMachine  
 {  
   private int balance;  
   private int price;  
   private int total;  
   /**  
    * Constructor for objects of class TicketMachine  
    */  
   public TicketMachine(int ticketCost)  
   {  
     // initialise instance variables  
     price=ticketCost;  
     balance=0;  
     total=0;  
   }  
     public int getPrice(){  
     //menunjukkan harga tiket  
     System.out.println("Your Ticket Price = "+price);  
     return price;  
   }  
   public int getBalance(){  
     //menunjukkan balance yang kita punya  
     System.out.println("Your Current Balance = "+balance);  
     return balance;  
   }  
   public void insertMoney(int amount){  
     if(amount>0){  
      balance=balance+amount;  
      System.out.println("Inserted "+balance+" cents");  
     }  
     else {  
       System.out.println("Use a positive amount rather than; " +amount);  
     }  
   }  
   public void printTicket(){  
     //to simulate the printing of a ticket  
     if(balance>=price){  
     System.out.println("##################");  
     System.out.println("# The BlueJ Line ");  
     System.out.println("# Ticket     ");  
     System.out.println("# " + price + " cents.");  
     System.out.println("##################");  
     System.out.println();  
     total=total+price;  
     balance=balance-price;  
     if(balance>0)  
     {  
   int amountToRefund;  
   amountToRefund=balance;  
   balance=0;  
   }  
 }  
   else{  
     System.out.println("You must insert at least: "+(price-balance)+" cents.");  
   }  
 }  
 }  

  • IntMain

 /**  
  * Ticket Machine (main)  
  * Samuel Marcellinus  
  * 05111740000134  
  */  
 import java.util.Scanner;   
  public class IntMain   
  {   
  public static boolean isPrinted;  
  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);  
  System.out.println("1. Get Price");   
  System.out.println("2. Get Balance");   
  System.out.println("3. Insert Money");   
  System.out.println("4. Print Ticket");  
  //menu=scan.nextInt();  
  while (isPrinted!=true){  
  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();  
   isPrinted=true;  
   break;   
  }   
  }  
  }   
  }  

Maka jika saya coba compile dan jalankan akan muncul seperti ini:

  • Jika uang cukup untuk membeli tiket :

  • Jika uang tidak cukup untuk membeli tiket :

Comments

Popular posts from this blog

APSI 1 - Analisa Sistem Informasi Lazada

Tugas 3 PBKK