TUGAS 3

IMPLEMENTASI STUDI KASUS TICKET MACHINE
Kode:

public class TicketMachine
{
    private int price; //harga kereta api
    private int total; //jumlah uang di mesin
    private int balance; //jumlah uang di transaksi
    /** 
     * Constructor for objects of class TicketMachine
     */
    public TicketMachine(int cost)
    {
        price=cost;
        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()
    {
        System.out.println("##########################");
        System.out.println("Ticket: " + price + " cents");
        System.out.println("##########################");
        total=balance+total;
        balance = 0;
    }
        
}


import java.util.Scanner;
public class ticketmain
{
    public static void main(String args[])
    {
       int cost, menu;
       int sum = 0;
       Scanner scan = new Scanner(System.in);
       System.out.println("Masukkan harga tiket: ");
       cost = scan.nextInt();
       TicketMachine tiket = 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");
           menu = scan.nextInt();
            switch (menu)
           {
               case 1:
               cost=tiket.getPrice();
               System.out.println("Ticket's price:" + cost + " cents");
               break;
               case 2:
               System.out.println("Current balance: " + tiket.getBalance());
               break;
               case 3:
               System.out.println("Please insert money: ");
               int amount = scan.nextInt();
               sum=sum+amount;
               int selisih = cost-sum;
               if(sum < cost)
               {
                   System.out.println("Not enough money! Please add " + selisih);
               }
               tiket.insertMoney(amount);
               break;
               case 4:
               if(tiket.getBalance() < cost)
               {
                   System.out.println("Transaction is not success!");
                }
               else
               {  
                   System.out.println("Transaction is success!");
                   tiket.printticket();
                }
               System.exit(0);
               break;
               default:
               System.out.println("The input is wrong!");
               System.out.println();
            }
        }
    }

}


Hasil run program:



Komentar

Postingan populer dari blog ini

TUGAS 1

ETS PBO

RESUME KULIAH TAMU