the second Objects lab.

TicketMachine.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Created variable status per exercise 2.15
  21. private int status;
  22. //created variable saving for exercise 2.49
  23. private int saving;
  24. //created count variable and mean variable for exercise 2.50
  25. private int count;
  26. private int mean;
  27. //create budget variable for exercise 2.51
  28. private int budget;
  29. /**
  30. * Create a machine that issues tickets of the given price.
  31. * Note that the price must be greater than zero, and there
  32. * are no checks to ensure this.
  33. */
  34. public TicketMachine(int newPrice)
  35. {
  36. price = newPrice;
  37. balance = 0;
  38. total = 0;
  39. //added budget field to ticketMachine object
  40. budget = 1000;
  41. }
  42. //Creation of second constructor with set ticket price per exercise 2.42
  43. public TicketMachine()
  44. {
  45. price = 1000;
  46. balance = 0;
  47. total = 0;
  48. }
  49. // create prompt method per exercise 2.33
  50. public void prompt()
  51. {
  52. System.out.println("Please insert the correct amount of money.");
  53. }
  54. // create showPrice method per eercise 2.34
  55. public void showPrice()
  56. {
  57. System.out.println("Current Price is "+ price + " cents");
  58. //System.out.println("# price cents.");
  59. }
  60. /**
  61. * Return the price of a ticket.
  62. */
  63. public int getPrice()
  64. {
  65. return price;
  66. }
  67. //Create set price method per exercise 2.30/2.41 apparently
  68. public int setPrice(int newPrice)
  69. {
  70. price = newPrice;
  71. return price;
  72. }
  73. /**
  74. * Return the amount of money already inserted for the
  75. * next ticket.
  76. */
  77. public int getAmount()
  78. {
  79. return balance;
  80. }
  81. /**
  82. * Receive an amount of money in cents from a customer.
  83. */
  84. public void insertMoney(int amount)
  85. {
  86. //per exercise 2.44 created if statement
  87. if(amount >= 0){
  88. balance = balance + amount;
  89. }
  90. }
  91. //Create empty method per exercise 2.40
  92. public void empty()
  93. {
  94. total = 0;
  95. }
  96. //Create getTotal method per exercise 2.24
  97. public int getTotal()
  98. {
  99. return total;
  100. }
  101. /**
  102. * Print a ticket.
  103. * Update the total collected and
  104. * reduce the balance to zero.
  105. */
  106. public void printTicket()
  107. {
  108. int amountLeftToPay = price - balance;
  109. if(amountLeftToPay <= 0) {
  110. System.out.println("##################");
  111. System.out.println("# The BlueJ Line");
  112. System.out.println("# Ticket");
  113. System.out.println("# " + price + " cents.");
  114. System.out.println("##################");
  115. System.out.println();
  116. // Update the total collected with the price.
  117. total = total + price;
  118. // Reduce the balance by the price.
  119. balance = balance - price;
  120. }
  121. else {
  122. System.out.println("You must insert at least: " +
  123. (amountLeftToPay) + " cents.");
  124. } }
  125. /**
  126. * Return the money in the balance.
  127. * The balance is cleared.
  128. */
  129. public int refundBalance() {
  130. int amountToRefund; amountToRefund = balance; balance = 0;
  131. return amountToRefund;
  132. }
  133. // create saving method per exercise 2.49
  134. public int saving(int discount)
  135. {
  136. return saving = total * discount;
  137. }
  138. //create mean method per exercise 2.50
  139. public int mean(int count)
  140. {
  141. return mean = total / count;
  142. }
  143. //create budget check method per exercise 2.51
  144. public void budgetCheck(int price)
  145. {
  146. if (budget < price){
  147. System.out.println("Too Expensive");
  148. System.out.println("Budget is: " + budget);
  149. }else{System.out.println("Just Right");
  150. }
  151. }
  152. //create emptyMachine method per exercise 2.55
  153. public int emptyMachine()
  154. {
  155. total = 0;
  156. return total;
  157. }
  158. }