Minggu, 16 September 2018

Tugas 3 PBO-A

Tugas 3 PBO-A --> Ticket Machine

Hari ini, 7 September 2018, kelas PBO-A diberi penugasan untuk membuat program ticket machine

Source Code

1. 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. Get Price");   
      System.out.println("2. Get Balance");   
      System.out.println("3. Insert Money");   
      System.out.println("4. Print Ticket");   
      System.out.println("5. New Price (Don't forget to insert money again)");  
      System.out.println("6. Exit");  
      menu=scan.nextInt();   
      if(menu == 1){  
        cost = ticket.getPrice();  
        System.out.println(cost);  
      }  
      else if(menu == 2){  
        int uang = ticket.getBalance();  
        System.out.println(uang);  
      }  
      else if(menu == 3){  
        int money = scan.nextInt();  
        ticket.insertMoney(money);  
      }  
      else if(menu == 4){  
        ticket.printTicket();  
      }  
      else if(menu == 5){  
        cost = scan.nextInt();  
        ticket=new TicketMachine(cost);  
      }  
      else{  
        break;  
      }  
    }   
  }  
  }   

2. Ticket Machine
 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)   
  {   
   balance = balance + amount;   
  }   
  /**   
  * Print a ticket.   
  * Update the total collected and   
  * reduce the balance to zero.   
  */   
  public void printTicket()   
  {   
   // Simulate the printing of a ticket.   
   System.out.println("##################");   
   System.out.println("# The BlueJ Line");   
   System.out.println("# " + (balance/price) + " 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 = 0;   
  }   
  }   

Output


Tidak ada komentar:

Posting Komentar