Selasa, 14 Maret 2017

PBO Ticket Machine (tugas 3)

Studi Kasus Ticket Machine

Ticket Machine adalah sebuah mesin seperti ATM, yang berfungsi melayani penjualan tiket kereta api dari satu tujuan ke tujuan yang lain. Mesin ini berisi informasi harga tiket ke berbagai tujuan, menghitung kembalian, dan mencetak bukti pembelian tiket.

Class TicketMachine
/**
 * TicketMachine
 * Monica I H
 * PBO
 */
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;
   
   public TicketMachine (int ticketCost)
   {
       price=ticketCost;
       balance=0;
       total=0;
   }
   
   public int getPrice()
   {
       return price;
   }
   
   public int getBalance()
   {
       return balance;
   }
   
   public void insertMoney(int amount)
   {
       balance=balance+amount;
   }
   
   public void printTicket()
   {
        // Simulate the printing of a ticket.
       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=0;
   }
}

Main Program

/**
 * TicketMachine-Main
 * Monica I H
 * PBO
 */
import java.util.Scanner;
public class IntMain
{
    public static int main()
    {
        Scanner scan= new Scanner(System.in);  
        int cost,menu;  
        System.out.println("Masukkan harga tiket: ");  
        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);  
                        break;  
                    case 2:  
                        System.out.println(ticket.getBalance());  
                        break;  
                    case 3:  
                        int money=scan.nextInt();  
                        ticket.insertMoney(money);  
                        break;  
                    case 4:  
                        ticket.printTicket();  
                        break;  
                    case 5:
                        return 0;
                }
        }
    }
}

Output:

Tidak ada komentar:

Posting Komentar