the second Objects lab.

TicketMachine.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 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. //Exercise 2.15ls
  21. private int status;
  22. private int score;
  23. /**
  24. * Create a machine that issues tickets of the given price.
  25. * Note that the price must be greater than zero, and there
  26. * are no checks to ensure this.
  27. */
  28. public TicketMachine(int ticketCost)
  29. {
  30. price = ticketCost;
  31. balance = 0;
  32. total = 0;
  33. }
  34. public TicketMachine()
  35. {
  36. price = 10000;
  37. balance = 0;
  38. total = 0;
  39. }
  40. public void setPrice(/*int ticketCost*/){
  41. price = 1000;//all tickets will begin with the state of 1000.
  42. }
  43. /**
  44. * Remove all money from machine.
  45. */
  46. public void empty(/* none needed. */)//mutates the state of total.
  47. {
  48. total = 0;
  49. }
  50. /**
  51. * Set the new price of a ticket.
  52. */
  53. public void setPrice2(int price2)//mutates the state of price.
  54. {
  55. price = price2;
  56. }
  57. /**
  58. * Return the price of a ticket.
  59. */
  60. public int getPrice()
  61. {
  62. return price;
  63. }
  64. //missing return statement
  65. /**
  66. * Return the amount of money already inserted for the
  67. * next ticket.
  68. */
  69. public int getAmount()
  70. {
  71. return balance;
  72. }
  73. /**
  74. * Return the total amount of money if additional money
  75. * is inserted to the balance.
  76. */
  77. public int getTotal()
  78. {
  79. return total;
  80. }
  81. /**
  82. * Receive an amount of money in cents from a customer.
  83. */
  84. public void insertMoney(int amount)
  85. {
  86. balance += amount;
  87. }
  88. /**
  89. * Increase score by the given number of points.
  90. */
  91. public void increase(int points)
  92. {
  93. score += points;
  94. }
  95. /**
  96. * Reduce price by the given amount.
  97. */
  98. public void discount(int amount)
  99. {
  100. price-= amount;
  101. }
  102. /**
  103. * Print the message to insert the correct amount of money.
  104. */
  105. public void prompt()
  106. {
  107. System.out.println("Please insert the correct amount of money.");
  108. }//2.36 Print out line: # price cents.
  109. //2.37 Prints the same at 2.36.
  110. // Neither option has the price variable. They have a price string.
  111. // String does not represent price's state and cannot be mutated.
  112. /**
  113. * Print the price of the ticket.
  114. */
  115. public void showPrice()
  116. {
  117. System.out.println("The price of a ticket is " + price + " cents.");
  118. }
  119. /**
  120. * Print a ticket.
  121. * Update the total collected and
  122. * reduce the balance to zero.
  123. */
  124. public void printTicket()
  125. {
  126. // Simulate the printing of a ticket.
  127. System.out.println("##################");
  128. System.out.println("# The BlueJ Line");
  129. System.out.println("# Ticket");
  130. System.out.println("# " + price + " cents.");
  131. System.out.println("##################");
  132. System.out.println();
  133. // Update the total collected with the balance.
  134. total = total + balance;
  135. // Clear the balance.
  136. balance = 0;
  137. }
  138. }