IMPLEMENTASI STUDI KASUS TICKET MACHINE
kode :
// untuk sebuah class ticket machine
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;
}
}
// untuk sebuah class ticket main
import java.util.Scanner;
public class ticketmain
{
public static void main(String args[])
{
int cost, menu;
Scanner scan = new Scanner(System.in);
System.out.println("Masukkan harga tiket: ");
cost = scan.nextInt();
TicketMachine tiket = new TicketMachine(cost);
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(cost);
break;
case 2:
tiket.getBalance();
break;
case 3:
int amount = scan.nextInt();
tiket.insertMoney(amount);
break;
case 4:
tiket.printticket();
break;
}
}
}
Komentar
Posting Komentar