PBO A - Tugas 3 Ticket Machine

Tugas PBO A kali ini yaitu membuat simulasi ticket machine.

Nama : Ifta Jihan N
NRP : 05111740000034
PBO-A

Source code:

  • Class Ticket
 /**  
  * Write a description of class Ticket here.  
  *  
  * @author Ifta Jihan N (05111740000034)  
  * @version 17/09/2018  
  */  
 public class Ticket  
 {  
   private int price;    
   private int balance;   
   private int total;   
   private int refund;  
   private int tickets;  
    public Ticket(int ticketCost)   
    {   
    price = ticketCost;   
    balance = 0;   
    total = 0;   
    refund = 0;  
    tickets = 0;  
    }   
    public int getPrice()   
    {   
    return price;   
    }   
    public int getBalance()  
    {  
      return balance;  
    }  
    /**   
    * Receive an amount of money in cents from a customer.   
    */   
    public void insertMoney(int amount)   
    {   
     if(amount > 0)  
     {  
       balance = balance + amount;  
     }   
     else  
     {  
       System.out.println("Please insert a sensible amount of money.");  
     }  
    }  
    public void printTicket()   
    {   
     if(balance > 0)  
     {    
       if(balance >= price)  
       {  
         tickets = purchaseTickets();  
         int i = 1;  
         while(i <= tickets)  
         {   
           System.out.println("##################");   
           System.out.println("# The BlueJ Line");   
           System.out.println("# Ticket");   
           System.out.println("# " + price + " rupiahs.");   
           System.out.println("##################");   
           System.out.println();   
           i++;  
         }    
         total = total + balance;   
         refund = refundBalance();  
         if(refund == 0)  
         {  
           System.out.println("No change given.");  
         }   
         else  
         {  
           System.out.println("Amount to refund: " + refund + " rupiahs.");  
         }  
         balance = 0;  
       }  
       else  
       {  
         System.out.println("You must insert at least: " +   
                  (price - balance) + " rupiahs to continue.");  
       }  
    }  
    else  
    {  
      System.out.println("Please insert a positive amount of money");  
    }  
   }  
    public int refundBalance()  
    {  
      int amountToRefund;  
      amountToRefund = balance - price * tickets;  
      balance = 0;  
      return amountToRefund;  
    }  
    public int purchaseTickets()  
    {  
      int numberOfTickets;  
      numberOfTickets = balance/price;  
      return numberOfTickets;  
    }  
 }  


  • Int Main
 /**  
  * Write a description of class Main here.  
  *  
  * @author Ifta Jihan N (05111740000034)  
  * @version 17/09/2018  
  */  
  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();   
  Ticket ticket=new Ticket(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");   
  while(isPrinted != true)  
  {  
    menu = scan.nextInt();  
    switch(menu)   
    {   
     case 1:   
     cost=ticket.getPrice();   
     System.out.println("Price: Rp " +cost);   
     System.out.println("--------------------------------");  
     break;   
     case 2:  
     System.out.println("Balance: Rp "+ticket.getBalance());  
     System.out.println("--------------------------------");  
     break;  
     case 3:   
     System.out.println("Please insert your money: ");  
     int money=scan.nextInt();   
     ticket.insertMoney(money);   
     System.out.println("--------------------------------");  
     break;   
     case 4:   
     System.out.println("This is your ticket(s)");  
     ticket.printTicket();   
     isPrinted = true;  
     System.out.println("--------------------------------");  
     break;   
    }   
  }  
  }   
 }  

Output:


Terimakasih.

Comments

Popular Posts