Chapter 2 of the BlueJ book. Walk through many of the chapters exercises.

TicketMachine.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // A discounted rate
  21. private int saving;
  22. //Number of tickets sold
  23. private int count;
  24. //average price of tickets
  25. private int mean;
  26. /**
  27. * Create a machine that issues tickets of the given price.
  28. * Note that the price must be greater than zero, and there
  29. * are no checks to ensure this.
  30. */
  31. public TicketMachine(int ticketCost)
  32. {
  33. price = ticketCost;
  34. balance = 0;
  35. total = 0;
  36. }
  37. /**
  38. * Return the price of a ticket.
  39. */
  40. public int getPrice()
  41. {
  42. return price;
  43. }
  44. /**
  45. * Return the amount of money already inserted for the
  46. * next ticket.
  47. */
  48. public int getBalance()
  49. {
  50. return balance;
  51. }
  52. public int getTotal()
  53. {
  54. return total;
  55. }
  56. /*
  57. * creates discounted price
  58. */
  59. public int discount(int discount)
  60. {
  61. return saving = price + discount;
  62. }
  63. /*
  64. * Calculates average cost of a ticket
  65. */
  66. public int mean()
  67. {
  68. return mean = total/count;
  69. }
  70. /*
  71. * Determines if there is enough money for tickets
  72. */
  73. public void budget(int canSpend)
  74. {
  75. if (price>canSpend) {
  76. System.out.println("Too expensive, must be " + canSpend + " or less");}
  77. else {System.out.println("Just right!");
  78. }
  79. }
  80. /**
  81. * Receive an amount of money in cents from a customer.
  82. */
  83. public void insertMoney(int amount)
  84. {
  85. if (amount>0) {
  86. balance += amount;
  87. }
  88. else {
  89. System.out.println("Use a positive amount: " + amount);
  90. }
  91. }
  92. /**
  93. * Print a ticket.
  94. * Update the total collected and
  95. * reduce the balance to zero.
  96. */
  97. public void printTicket()
  98. {
  99. int amountLeftToPay = price-balance;
  100. if (amountLeftToPay<=0) {
  101. // Simulate the printing of a ticket.
  102. System.out.println("##################");
  103. System.out.println("# The BlueJ Line");
  104. System.out.println("# Ticket");
  105. System.out.println("# " + price + " cents.");
  106. System.out.println("##################");
  107. System.out.println();
  108. // Reduce the balance.
  109. balance = balance - price;
  110. // Update the total collected with the price.
  111. total = total + price;}
  112. else {
  113. System.out.println("You must insert at least: " + (price - balance) + " cents.");
  114. }
  115. }
  116. /*return the money in the balance
  117. * balance is cleared
  118. */
  119. public int refundBalance()
  120. {
  121. int refundAmount =balance;
  122. balance = 0;
  123. return refundAmount;
  124. }
  125. /*
  126. * Remove all money from machine
  127. * total is cleared
  128. * returns amount you took from machine
  129. */
  130. public int emptyMachine()
  131. {
  132. int emptyAmount = total;
  133. total = 0;
  134. return emptyAmount;
  135. }
  136. }