Saya diberi tugas untuk membuat suatu vending ticket machine menggunakan java, disini saya menggunakan BlueJ. Berikut source codenya:
public class Naufal_TicketMachine
{
private int price;
private int balance;
private int total;
public Naufal_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;
}
}
Berikut hasil implementasi dari source code tersebut :
0 comments:
Post a Comment