Senin, 17 September 2018

Tugas Ticket Machine

Simple program ticket machine PBO kelas B.

     1. Class Window













     2. Ticket Machine


  /**  
  * Ticket Machine  
  *  
  * @author (Chrisnady Anggaiswara)  
  * @version (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;   
     //buy ticket  
     private int Buy;  
     //sell ticket  
     private int Sell;  
     /**   
       * 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()   
     {   
       System.out.println(+ balance + " cents.");  
       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 amounts:" + amount);  
       }  
     }  
     public void Buy()   
     {   
      if(balance>=price){  
        balance = balance - price;  
      System.out.println(+ balance + " cents.");  
       }  
       else {  
       System.out.println("Insufficient Money");  
       }  
     }   
      public void Sell()   
     {   
        balance = balance + price;  
        System.out.println(+ balance + " cents.");  
     }   
     /**   
     * Print a ticket.   
     * Update the total collected and   
     * reduce the balance to zero.   
      */   
     public void printTicket()   
     {   
      // 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();   
     // Update the total collected with the balance.   
     total = total + balance;  
     balance = balance -price;  
     }  
     else{  
     System.out.println("You must insert at least:" +(price-balance)+  
     " more cents.");  
     }  
     // Clear the balance.   
     }   
  }   

     3. Main


   import java.util.Scanner;  
 /**  
  * Main untuk program Ticket Machine  
  *  
  * @author (Chrisnady Anggaiswara)  
  * @version (17 September 2018)  
  */  
 public class intMain  
 {   
  public static void main(String args[])   
  {   
  Scanner scan= new Scanner(System.in);   
  int cost,menu;  
  menu = 0;  
  System.out.println("Masukkan harga tiket \n");   
  cost=scan.nextInt();   
  TicketMachine ticket= new TicketMachine(cost);  
  while (menu!=7){  
   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. Buy Ticket");  
  System.out.println("6. Sell Ticket");  
  System.out.println("7. Exit");  
  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.Buy();  
   break;  
   case 6:  
   ticket.Sell();  
   break;  
   case 7:  
   break;  
  }   
   }  
  }    
 }  

     4. Hasil Screenshot Program



Tidak ada komentar:

Posting Komentar