the second Objects lab.

TicketMachine.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * TicketMachine models a naive ticket machine that issues
  3. * flat-fare tickets.
  4. * The price of a ticket is specified via the constructor.
  5. * It is a naive machine in the sense that it trusts its users
  6. * to insert enough money before trying to print a ticket.
  7. * It also assumes that users enter sensible amounts.
  8. *
  9. * @author David J. Barnes and Michael Kolling
  10. * @version 2008.03.30
  11. */
  12. public class TicketMachine
  13. {
  14. // The price of a ticket from this machine.
  15. private int price;
  16. // The amount of money entered by a customer so far.
  17. private int balance;
  18. // The total amount of money collected by this machine.
  19. private int total;
  20. /**
  21. * Create a machine that issues tickets of price = 1000.
  22. * Note that the price must be greater than zero, and there
  23. * are no checks to ensure this.
  24. */
  25. public TicketMachine()
  26. {
  27. price = 1000;
  28. balance = 0;
  29. total = 0;
  30. }
  31. /**
  32. * Create a machine that issues tickets of the given price.
  33. * Note that the price must be greater than zero, and there
  34. * are no checks to ensure this.
  35. */
  36. public TicketMachine(int ticketCost)
  37. {
  38. price = ticketCost;
  39. balance = 0;
  40. total = 0;
  41. }
  42. public void setPrice(int ticketCost)
  43. {
  44. price = ticketCost;
  45. }
  46. /**
  47. * Return the price of a ticket.
  48. */
  49. public int getPrice()
  50. {
  51. return price;
  52. }
  53. /**
  54. * Return the amount of money already inserted for the
  55. * next ticket.
  56. */
  57. public int getBalance()
  58. {
  59. return balance;
  60. }
  61. /**
  62. * Return the amount of money already inserted for the
  63. * next ticket.
  64. */
  65. public int getTotal()
  66. {
  67. return total;
  68. }
  69. /**
  70. * Receive an amount of money in cents from a customer.
  71. */
  72. public void insertMoney(int amount)
  73. {
  74. balance = balance + amount;
  75. }
  76. /**
  77. * Print a message prompting the user to insert correct amount
  78. */
  79. public void prompt()
  80. {
  81. System.out.println("Please insert the correct amount of money.");
  82. }
  83. /**
  84. * Print a message letting the user know the ticket amount
  85. */
  86. public void showPrice()
  87. {
  88. System.out.println("The price of a ticket is " + price + " cents.");
  89. }
  90. /**
  91. * Print a message letting the user know the ticket amount
  92. */
  93. public void empty()
  94. {
  95. total=0;
  96. }
  97. /**
  98. * Print a ticket.
  99. * Update the total collected and
  100. * reduce the balance to zero.
  101. */
  102. public void printTicket()
  103. {
  104. // Simulate the printing of a ticket.
  105. System.out.println("##################");
  106. System.out.println("# The BlueJ Line");
  107. System.out.println("# Ticket");
  108. System.out.println("# " + price + " cents.");
  109. System.out.println("##################");
  110. System.out.println();
  111. // Update the total collected with the balance.
  112. total = total + balance;
  113. // Clear the balance.
  114. balance = 0;
  115. }
  116. }