Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TicketMachine
- {
- private int price;
- private int balance;
- private int total;
- private int refund;
- private int tickets;
- public TicketMachine(int ticketCost)
- {
- price = ticketCost;
- balance = 0;
- total = 0;
- refund = 0;
- }
- public int getPrice()
- {
- return price;
- }
- public int getBalance()
- {
- return balance;
- }
- public void getTickets(int tkt)
- {
- if(tkt >= 1)
- {
- tickets = tkt;
- }
- else
- {
- System.out.println("Please insert the right number of tickets.\n");
- }
- }
- public void insertMoney(int money)
- {
- if (money > 0)
- {
- balance = balance + money;
- }
- else
- {
- System.out.println("Please insert the right amount of money.\n");
- }
- }
- /**
- * Print a ticket.
- * Update the total collected and
- * reduce the balance to zero.
- */
- public void purchaseTickets()
- {
- System.out.println ("Ticket cost = $"+price+".");
- System.out.println ("Your total amount of money = $"+balance+".");
- if (balance > 0)
- {
- if(balance >= (price*tickets))
- {
- // Simulate the printing of a ticket.
- System.out.println("\n+++++++++++++++++++++++++++++++++\n");
- System.out.println("Mystery Train [Final Destination]");
- System.out.println("Ticket(s) : "+ tickets +".");
- System.out.println("Total ticket(s) cost : $"+ (price*tickets) +".\n");
- System.out.println("+++++++++++++++++++++++++++++++++");
- System.out.println();
- // Update the total collected with the balance.
- total = total + balance;
- refund = refundBalance();
- if (refund == 0)
- {
- System.out.println("No refundable money.\n");
- }
- else
- {
- System.out.println("Refundable money: $"+refund+".\n");
- }
- // Clear the balance.
- balance = 0;
- }
- else
- {
- System.out.println ("Ticket to buy : "+tickets);
- System.out.println("Please insert enough money. Insert $"
- +((price * tickets)-balance)+ " more.\n");
- }
- }
- else
- {
- System.out.println("Insert your money.\n");
- }
- }
- public int refundBalance()
- {
- int refundable;
- refundable = balance - price * tickets;
- balance = 0;
- return refundable;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement