Advertisement
ivanar_00

Untitled

Oct 2nd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. public class TicketMachine
  2. {
  3.     private int price;
  4.     private int balance;
  5.     private int total;
  6. /**
  7. * Create a machine that issues tickets of the given price.
  8. * Note that the price must be greater than zero, and there
  9. * are no checks to ensure this.
  10. */
  11. public TicketMachine(int ticketcost)
  12. {
  13.     price = ticketcost;
  14.     balance = 0;
  15.     total = 0;
  16. }
  17. /**
  18. * Return the price of a ticket.
  19. */
  20. public int getPrice()
  21. {
  22.     return price;
  23. }
  24. /**
  25. * Return the amount of money already inserted for the
  26. * next ticket.
  27. */
  28. public int getBalance()
  29. {
  30.     return balance;
  31. }
  32. /**
  33. * Receive an amount of money from a customer.
  34. */
  35. public void insertMoney(int amount)
  36. {
  37.     balance = balance + amount;
  38. }
  39. /**
  40. * Print a ticket
  41. * Update the total collected and
  42. * reduce the balance to zero.
  43. */
  44. public void printTicket()
  45. {
  46. // Simulate the printing of a ticket.
  47.     System.out.println("##################");
  48.     System.out.println("# The BlueJ Line");
  49.     System.out.println("# Ticket");
  50.     System.out.println("# " + price + " cents.");
  51.     System.out.println("##################");
  52.     System.out.println();
  53. // Update the total collected with the balance.
  54.     total = total + balance;
  55. // Clear the balance.
  56.     balance = 0;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement