Source Code :
import java.util.Scanner;
/**
* Write a description of class TicketMachine here.
*
* @author (Hafidz Firman A.)
* @version (17092018)
*/
public class TicketMachine
{
private int price;
private int balance;
private int total;
public TicketMachine(int cost)
{
price = cost;
balance = 0;
total = 0;
}
/**
* @Return The price of a ticket.
*/
public int getPrice()
{
return price;
}
public int getBalance()
{
return balance;
}
public void insertMoney(int amount)
{
if(amount > 0) {
balance = balance + amount;
}
else {
System.out.println("Use a positive amount rather than: " +
amount);
}
}
public void printTicket()
{
if(balance >= price) {
// 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 price.
total = total + price;
// Reduce the balance by the prince.
balance = balance - price;
}
else {
System.out.println("You must insert at least: " +
(price - balance) + " more cents.");
}
}
public int refundBalance()
{
int amountToRefund;
amountToRefund = balance;
balance = 0;
return amountToRefund;
}
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
int cost, menu;
boolean entry;
System.out.println("Masukkan harga tiket \n");
cost = scan.nextInt();
TicketMachine ticket = new TicketMachine(cost);
entry = true;
while(entry) {
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:
ticket.getBalance();
break;
case 3:
int money=scan.nextInt();
ticket.insertMoney(money);
break;
case 4:
ticket.printTicket();
break;
case 5:
entry = false;
break;
}
}
}
}
output :
Tidak ada komentar:
Posting Komentar