the second Objects lab.

TicketMachine.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. // Exercise 2.15
  21. private int status;
  22. /**
  23. * Create a machine that issues tickets of the given price.
  24. * Note that the price must be greater than zero, and there
  25. * are no checks to ensure this.
  26. */
  27. public TicketMachine()
  28. {
  29. //2.39 set price to 1000 and remove input parameter
  30. //2.42 constructor with parameter to specify price
  31. //2.58 Better ticket machine
  32. price = 0;
  33. balance = 0;
  34. total = 0;
  35. }
  36. //public TicketMachine()
  37. //{
  38. //2.42 constructor with default value of price
  39. //price = 500;
  40. //}
  41. /*
  42. * Exercise 2.30/2.41: Assign a value to the price field
  43. */
  44. public void setPrice(int ticketCost)
  45. {
  46. price = ticketCost;
  47. }
  48. /*
  49. * Exercise 2.31: Add a value to the score field
  50. */
  51. public void increase(int points)
  52. {
  53. int score = points;
  54. }
  55. /*
  56. * Exercise 2.32: Subtract a value from the price field
  57. */
  58. public void discount(int amount)
  59. {
  60. price = price - amount;
  61. }
  62. /**
  63. * Return the price of a ticket.
  64. */
  65. public int getPrice()
  66. {
  67. return price;
  68. }
  69. /**
  70. * Return the amount of money already inserted for the
  71. * next ticket.
  72. */
  73. public int getBalance()
  74. {
  75. return balance;
  76. }
  77. /**
  78. * Receive an amount of money in cents from a customer.
  79. */
  80. public void insertMoney(int amount)
  81. {
  82. if(amount >= 0){
  83. balance = balance + amount;
  84. }
  85. }
  86. /**
  87. * Return the total amount of money collected by this machine.
  88. */
  89. //Exercise 2.24
  90. public int getTotal()
  91. {
  92. return total;
  93. }
  94. /**
  95. * Print message when incorrect amount of money is inserted
  96. */
  97. //Exercise 2.33
  98. public void prompt()
  99. {
  100. System.out.println("Please insert the correct amount of money.");
  101. }
  102. /**
  103. * Print message showing price of a ticket
  104. */
  105. //Exercise 2.34
  106. public void showPrice()
  107. {
  108. System.out.println("The price of a ticket is " + price + " cents.");
  109. }
  110. /**
  111. * Print message showing price of a ticket
  112. */
  113. //Exercise 2.40
  114. public void empty()
  115. {
  116. total = 0;
  117. }
  118. /**
  119. * Print message showing price of a ticket
  120. */
  121. //Exercise 2.55
  122. public int emptyMachine()
  123. {
  124. return total;
  125. //total=0;
  126. }
  127. /**
  128. * Print a ticket.
  129. * Update the total collected and
  130. * reduce the balance to zero.
  131. */
  132. public void printTicket()
  133. {
  134. /*
  135. // Simulate the printing of a ticket.
  136. System.out.println("##################");
  137. System.out.println("# The BlueJ Line");
  138. System.out.println("# Ticket");
  139. System.out.println("# " + price + " cents.");
  140. System.out.println("##################");
  141. System.out.println();
  142. // Update the total collected with the balance.
  143. total = total + balance;
  144. // Clear the balance.
  145. balance = 0;
  146. */
  147. /*
  148. if(balance >= price) {
  149. System.out.println("##################");
  150. System.out.println("# The BlueJ Line");
  151. System.out.println("# Ticket");
  152. System.out.println("# " + price + " cents.");
  153. System.out.println("##################");
  154. System.out.println();
  155. // Update the total collected with the price.
  156. total = total + price;
  157. // Reduce the balance by the price.
  158. balance = balance - price;
  159. }
  160. else {
  161. System.out.println("You must insert at least: " +
  162. (price - balance) + " more cents.");
  163. }
  164. */
  165. int amountLeftToPay = price - balance;
  166. if(amountLeftToPay <=0){
  167. System.out.println("##################");
  168. System.out.println("# The BlueJ Line");
  169. System.out.println("# Ticket");
  170. System.out.println("# " + price + " cents.");
  171. System.out.println("##################");
  172. System.out.println();
  173. // Update the total collected with the price.
  174. total = total + price;
  175. // Reduce the balance by the price.
  176. balance = balance - price;
  177. } else {
  178. System.out.println("You must insert at least: " +
  179. (price - balance) + " more cents.");
  180. }
  181. }
  182. }
  183. /* EXERCISES
  184. * 2.1: call void printTicket()
  185. * 2.2: price that was entered
  186. * 2.3: The ticket prints regardless
  187. * 2.4: [practice ticket machine]
  188. * 2.5: Yes, price is different.
  189. * 2.6: public class Student; public class LabClass
  190. * 2.7: Order of statement matters
  191. * 2.8: It is possible to leave this out, the class will be assigned default access modifier
  192. * 2.9: fields: price, balance, total;
  193. * constructors: public TicketMachine(int ticketCost);
  194. * methods: getPrice, getBalance, insertMoney, printTicket
  195. * 2.10: no return type defined
  196. * 2.11: int, Student, Server
  197. * 2.12: alive, tutor, game
  198. * 2.13: Order of statement matters
  199. * 2.14: field declaration requires a semicolon
  200. * 2.15: Shown in code
  201. * 2.16: Student
  202. * 2.17: title of type String, and price of type double
  203. * 2.18: fields: title, price, author, publisher, publish date
  204. * types: string, double, string, string, date
  205. * 2.19: name = petsName;
  206. * 2.20: The problem when adding int is the ticketCost was not recognized as price
  207. * Assignments should not re-declare the field type. This needs to be done in fields section.
  208. * 2.21: getPrice returns a passed value and getBalance returns a set value.
  209. * 2.22: getBalance - "How much money do I owe or am I owed?"
  210. * 2.23: No, the variable does not need to have the same name as the method
  211. * 2.24: new getTotal method created above.
  212. * 2.25: error "missing return statement"
  213. * 2.26: getPrice returns an integer, printTicket does not return anything (void)
  214. * 2.27: They are actions and don't return anything.
  215. * 2.28: (Practicing methods)
  216. * 2.29: There is a return type included
  217. * 2.30-2.34: Shown in Code
  218. * 2.35: Different outputs because the price is variable
  219. * 2.36: "price" string would display instead of a variable
  220. * 2.37: Same output as 2.36
  221. * 2.38: No, they cannot show variable prices because they are outputing a string
  222. * 2.39: Objects cannot pass a parameter to the ticketmachine
  223. * 2.40: Mutator method
  224. * 2.41: Setter method
  225. * 2.42: Shown in code
  226. * 2.43: (Practicing methods)
  227. * 2.44: negative values do not affect balance
  228. * 2.45: Booleans are well suited to be controlled by a type with only two different values
  229. * 2.46: Shown in code
  230. * 2.47: No because there is a check to make sure price is not greater than balance
  231. * 2.48: +, -, /, *, %
  232. * 2.49: saving = price * discount;
  233. * 2.50: mean = total / count;
  234. * 2.51: if(price > budget){System.out.print("Too expensive")}else {System.out.print("Just right")}
  235. * 2.52: if(price > budget){System.out.print("Too expensive, your budget is only " + budget)}else {System.out.print("Just right")}
  236. * 2.53: it is setting balance as 0 before returning it.
  237. * 2.54: return statement ends the execution of a method
  238. * 2.55: Shown in code
  239. * 2.56: It is an accessor because method ends after return statement
  240. * 2.57: Shown in code
  241. * 2.58: setPrice method can be called to set price based on user selection
  242. */