/**
* a self-serving machine for train ticket purchasing. Machine to
* arrange ticket price on every destination, change money, and produce
* paper or receipt.
*
* yaniarpe
*/
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;
private int balance2;
// The total amount of money collected by this machine.
private int total;
// The total amount of change money
private int refund;
// The amount of tickets
private int tickets;
/**
* Create a machine that issues tickets of the given price.
* Note that the price must be greater than zero, and there
* are no checks to ensure this.
*/
public TicketMachine(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
refund = 0;
}
/**
* Return the price of a ticket.
*/
public int getPrice()
{
return price;
}
/**
* Return the amount of money already inserted for the
* next ticket.
*/
public int getBalance()
{
return balance;
}
/**
* Return the number of ticket wanted.
*/
public void getTickets(int tkt)
{
if(tkt >= 1)
{
tickets = tkt;
}
else
{
System.out.println("Please insert the right number of tickets.");
System.out.println();
}
}
/**
* Receive an amount of money in rupiah from a customer.
*/
public void insertMoney(int money)
{
if (money > 0)
{
balance = balance + money;
balance2 = balance;
}
else
{
System.out.println("Please insert the right amount of money");
System.out.println();
}
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public void printTickets()
{
// Simulate the printing of a ticket.
for(int i = 1; i <= tickets; i++)
{
System.out.println("--------------------------------");
System.out.println();
System.out.println(" -SUPER TRAIN TICKET- ");
System.out.println(" A ticket for 1 person. ");
System.out.println(" Ticket price: IDR "+ price +".");
System.out.println();
System.out.println("--------------------------------");
System.out.println();
}
// Simulate the printing of receipt.
System.out.println("--------------------------------");
System.out.println();
System.out.println(" RECEIPT ");
System.out.println(" -SUPER TRAIN- ");
System.out.println(" Ticket price: IDR "+ price +".");
System.out.println(" Number of Tickets: "+tickets);
System.out.println(" Total Price: IDR "+price*tickets+".");
System.out.println();
System.out.println("--------------------------------");
System.out.println();
// Update the total collected with the balance.
total = total + balance;
// Clear the balance.
balance = 0;
}
public int refundBalance()
{
int refundable;
refundable = balance2 - price * tickets;
balance2 = 0;
return refundable;
}
}