the second Objects lab.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. price = 1000;
  30. balance = 0;
  31. total = 0;
  32. }
  33. public TicketMachine(int ticketPrice){
  34. price = ticketPrice;
  35. balance = 0;
  36. total = 0;
  37. }
  38. /**
  39. * Return the price of a ticket.
  40. */
  41. public int getPrice()
  42. {
  43. return price;
  44. }
  45. /**
  46. * Return the amount of money already inserted for the
  47. * next ticket.
  48. */
  49. public int getAmount()
  50. {
  51. return balance;
  52. }
  53. /**
  54. * Returns the total amount of money inserted into the machine.
  55. */
  56. public int getTotal(){
  57. return total;
  58. }
  59. /**
  60. * Sets the price of the ticket.
  61. */
  62. public void setPrice(int ticketCost){
  63. price = ticketCost;
  64. }
  65. /**
  66. * Receive an amount of money in cents from a customer.
  67. */
  68. public void insertMoney(int amount)
  69. {
  70. balance = balance + amount;
  71. }
  72. /**
  73. * Reduces the price of the ticket.
  74. */
  75. public void discount(int amount){
  76. price = price - amount;
  77. }
  78. /**
  79. * Removes all money from the machine.
  80. */
  81. public void empty(){
  82. total = 0;
  83. }
  84. /**
  85. * Prompts the user to enter money.
  86. */
  87. public void prompt(){
  88. System.out.println("Please insert the correct amount of money.");
  89. }
  90. /**
  91. *
  92. */
  93. public void showPrice(){
  94. System.out.println("The price of a ticket is " + price);
  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. // Simulate the printing of a ticket.
  104. System.out.println("##################");
  105. System.out.println("# The BlueJ Line");
  106. System.out.println("# Ticket");
  107. System.out.println("# " + price + " cents.");
  108. System.out.println("##################");
  109. System.out.println();
  110. // Update the total collected with the balance.
  111. total = total + balance;
  112. // Clear the balance.
  113. balance = 0;
  114. }
  115. }