the second Objects lab.

TicketMachine.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. */ public class TicketMachine
  12. {
  13. // The price of a ticket from this machine.
  14. private int price ;
  15. // The amount of money entered by a customer so far.
  16. private int balance;
  17. // The total amount of money collected by this machine.
  18. private int total;
  19. /**
  20. * Exercise 2.15 Write in full the
  21. * declaration for a field of type
  22. * int whose name is status.
  23. */
  24. private int status;
  25. /**
  26. * Create a machine that issues tickets of the given price.
  27. * Note that the price must be greater than zero, and there
  28. * are no checks to ensure this.
  29. * Exercise 2.39
  30. */
  31. public TicketMachine()
  32. {
  33. price = 1000;
  34. balance = 0;
  35. total = 0;
  36. }
  37. /**Exercise 2.42
  38. *
  39. */
  40. public void TicketSet(int ticketCost)
  41. {
  42. price = ticketCost;
  43. }
  44. /**
  45. * Return the price of a ticket.
  46. */
  47. public int getPrice()
  48. {
  49. return price;
  50. }
  51. /**Exercise 2.33
  52. * Request the correct amount of money
  53. */
  54. public void prompt()
  55. {
  56. System.out.println("Please insert the correct amount of money.");
  57. }
  58. /**Exercise 2.34
  59. * Show price
  60. */
  61. public void showPrice()
  62. {
  63. System.out.println("The price of the ticket is " + price);
  64. }
  65. /**
  66. * Return the amount of money already inserted for the
  67. * next ticket.
  68. */
  69. public int getBalance()
  70. {
  71. return balance;
  72. }
  73. /**
  74. * Receive an amount of money in cents from a customer.
  75. */
  76. public void insertMoney(int amount)
  77. {
  78. balance = balance + amount;
  79. }
  80. /**Exercise 2.24 Define an accessor method, getTotal,
  81. * that returns the value of the total field.
  82. */
  83. public int getTotal()
  84. {
  85. return total;
  86. }
  87. /**
  88. * Print a ticket.
  89. * Update the total collected and
  90. * reduce the balance to zero.
  91. */
  92. public void printTicket()
  93. {
  94. // Simulate the printing of a ticket.
  95. System.out.println("##################");
  96. System.out.println("# The BlueJ Line");
  97. System.out.println("# Ticket");
  98. System.out.println("# " + price + " cents.");
  99. System.out.println("##################");
  100. System.out.println();
  101. // Update the total collected with the balance.
  102. total = total + balance;
  103. // Clear the balance.
  104. balance = 0;
  105. }
  106. /**
  107. * Exercise 2.30 & 2.41
  108. * Set Price
  109. */
  110. public void setPrice(int newValue)
  111. {
  112. price = newValue;
  113. //return price;
  114. }
  115. /**
  116. * Exercise 2.32
  117. * Reduce price by the given amount.
  118. */
  119. public void discount(int amount)
  120. {
  121. price = price - amount;
  122. }
  123. /**Exercise 2.40
  124. * Empty machine
  125. */
  126. public void empty()
  127. {
  128. total = 0;
  129. }
  130. }