PB0-B Class dan Object studi kasus Ticket Machine

Nama : Putri Endah Puspitasari
NRP   : 05111740000039
Kelas  : PBO-B

Hari ini Senin, 17 Sepetember 2018 kelas kami mendapat tugas rumah untuk membuat class dan object studi kasus Ticket Machine menggunakan BlueJ.



Berikut merupakan codingan Ticket Machine :


 /**   
  * Write a description of class TicketMachine here.   
  *   
  * @author (Putri Endah Puspitasari)   
  * @version (Senin,17 September 2018)   
  */   
  public class TicketMachine    
  {    
   // The price of a ticket from this machine.    
   private int price;    
   // The amount of money entered by a customer so far.    
   private int balance;    
   // The total amount of money collected by this machine.    
   private int total;    
   /**    
   * Create a machine that issues tickets of the given price.    
   * Note that the price must be greater than zero, and there    
   * are no checks to ensure this.    
   */    
   public TicketMachine(int ticketCost)    
   {    
    price = ticketCost;    
    balance = 0;    
    total = 0;    
   }    
   /**    
   * Return the price of a ticket.    
   */    
   public int getPrice()    
   {    
    return price;    
   }    
   /**    
   * Return the amount of money already inserted for the    
   * next ticket.    
   */    
   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("Use a positive amount rather than: "+amount);   
    }   
   }    
   /**    
   * Print a ticket.    
   * Update the total collected and    
   * reduce the balance to zero.    
   */    
   public void printTicket()    
   {    
    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();    
     // Update the total collected with the balance.    
     total = total + balance;    
     // Clear the balance.    
     balance = balance-price;    
     if (balance==0)   
     {   
      System.out.println("No change given");   
     }   
     else   
     {   
      System.out.println("Amount to refund : " +balance+ "");   
     }   
    }   
    else   
    {   
     System.out.println("You must insert at least: "+ (price-balance) +" more cents.");   
    }   
   }   
   public int refundBalance()   
   {   
    int refund;   
    refund = balance;   
    return refund;   
   }   
  }   


Berikut merupakan codingan Main nya :

 /**   
  * Write a description of class IntMain here.   
  *   
  * @author (Putri Endah Puspitasari)   
  * @version (Senin,17 September 2018)   
  */   
  //Main    
  import java.util.Scanner;    
  public class IntMain    
  {    
   public static void main(String args[])    
   {    
    Scanner scan= new Scanner(System.in);    
    int cost,menu,refund;  
    System.out.println("1. Get Price");    
    System.out.println("2. Get Balance");    
    System.out.println("3. Insert Money");    
    System.out.println("4. Print Ticket");   
    System.out.println("5. Exit");  
    System.out.println("Masukkan harga tiket:\n");    
    cost=scan.nextInt();    
    TicketMachine ticket=new TicketMachine(cost);   
    while(true)  
    {  
      System.out.println("1. Get Price");    
      System.out.println("2. Get Balance");    
      System.out.println("3. Insert Money");    
      System.out.println("4. Print Ticket");   
      System.out.println("5. Exit");  
      menu=scan.nextInt();    
    switch(menu)    
    {    
     case 1:    
     cost=ticket.getPrice();    
     System.out.println(cost);   
     refund = ticket.refundBalance();   
     System.out.println(refund);   
     break;    
     case 2:    
     int balance=ticket.getBalance();  
     System.out.println(balance);  
     break;    
     case 3:    
     int money=scan.nextInt();    
     ticket.insertMoney(money);    
     break;    
     case 4:    
     ticket.printTicket();    
     break;  
     case 5:  
     break;  
    }   
    if(menu==5)  
    break;  
    }  
   }    
  }    

Berikut ini merupakan hasil akhir atau input dan output dari program saya 




Komentar

Postingan populer dari blog ini

ETS PBO - B Manless Parking System

Foxes and Rabbits Project

Evaluasi Akhir Semester PBO 2018 : Image Editor