Studi Kasus : Mesin Tiket
Ticket Machine Case Study (Studi Kasus Mesin Tiket)
Ticket Machine adalah sebuah mesin seperti ATM, yang berfungsi melayani penjualan tiket kereta api dari satu tujuan ke tujuan yang lain. Di dalam Ticket Machine ada sebuah program atau perangkat lunak yang mengatur harga tiket di tiap tujuan, mengatur kembalian uang, dan juga mencetak receipt sebagai bukti pembelian tiket.
Berikut ini program TicketMachine dan program pengaplikasiannya (IntMain) :
Source code : Program TicketMachine
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()
{
System.out.println("################");
System.out.println("# The BlueJ Line");
System.out.println("# Ticket");
System.out.println("#" + price + " cents.");
System.out.println("#################");
System.out.println();
}
}
Source code : Program TicketMachine
//Main
import java.util.Scanner;
public class IntMain
{
public static int main(String args[])
{
Scanner scan= new Scanner(System.in);
int cost, menu, money;
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. 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:
money = scan.nextInt();
ticket.insertMoney(money);
break;
case 4:
ticket.printTicket();
break;
case 5:
return 0;
}
}
}
}
output :
Komentar
Posting Komentar