the second Objects lab.

TicketMachine.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  21. * Create a machine that issues tickets of the given price.
  22. * Note that the price must be greater than zero, and there
  23. * are no checks to ensure this.
  24. */
  25. public TicketMachine(int ticketCost)
  26. {
  27. price = ticketCost;
  28. balance = 0;
  29. total = 0;
  30. }
  31. /**
  32. * Return the price of a ticket.
  33. */
  34. public int getPrice()
  35. {
  36. return price;
  37. }
  38. /**
  39. * set the price
  40. */
  41. public void setPrice(int ticketCost){
  42. price = ticketCost;
  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. /**
  53. * return the total
  54. */
  55. public int getTotal(){
  56. return total;
  57. }
  58. /**
  59. * reduce the price by amount
  60. */
  61. public void discount(int amount){
  62. price -= amount;
  63. }
  64. /**
  65. * Receive an amount of money in cents from a customer.
  66. */
  67. public void insertMoney(int amount)
  68. {
  69. if (amount > 0){
  70. balance = balance + amount;
  71. }
  72. else{
  73. System.out.println("Enter a positive amount: " + amount);
  74. }
  75. }
  76. /**
  77. * refund the balance
  78. */
  79. public int refundBalance(){
  80. int amountToReturn = balance;
  81. balance = 0;
  82. return amountToReturn;
  83. }
  84. /**
  85. * print a prompt
  86. */
  87. public void prompt(){
  88. System.out.println("Please insert the correct amount of money");
  89. }
  90. /**
  91. * show the price of the ticket
  92. */
  93. public void showPrice(){
  94. System.out.println("The price of the ticket " + price + " cents");
  95. }
  96. /**
  97. * Print a ticket.
  98. * Update the total collected and
  99. * reduce the balance to zero.
  100. */
  101. public void printTicket()
  102. {
  103. int amountLeftToPay = price - balance;
  104. if (balance >= price){
  105. // Simulate the printing of a ticket.
  106. System.out.println("##################");
  107. System.out.println("# The BlueJ Line");
  108. System.out.println("# Ticket");
  109. System.out.println("# " + price + " cents.");
  110. System.out.println("##################");
  111. System.out.println();
  112. // Update the total collected with the balance.
  113. total = total + price;
  114. // Clear the balance.
  115. balance = balance - price;
  116. }
  117. else{
  118. System.out.println("You must enter atleast: " + (amountLeftToPay) + " cents");
  119. }
  120. }
  121. /**
  122. * emptying machine
  123. */
  124. public int emptyMachine(){
  125. int moneyInMachine = total;
  126. total = 0;
  127. return moneyInMachine;
  128. }
  129. }