the second Objects lab.

TicketMachine.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // The Status
  21. private int status;
  22. private int score = 100;
  23. private int discount = 20;
  24. private int saving;
  25. /**
  26. * Create a machine that issues tickets of the given price.
  27. * Note that the price must be greater than zero, and there
  28. * are no checks to ensure this.
  29. */
  30. public TicketMachine()
  31. {
  32. int price = 1000;
  33. balance = 0;
  34. total = 0;
  35. }
  36. /*public TicketMachine(int ticketCost)
  37. {
  38. price = ticketCost;
  39. balance = 0;
  40. total = 0;
  41. }*/
  42. /**
  43. * Return the price of a ticket.
  44. */
  45. public int getPrice()
  46. {
  47. return price;
  48. }
  49. /**
  50. * Return the amount of money already inserted for the
  51. * next ticket.
  52. */
  53. public int getAmount()
  54. {
  55. return balance;
  56. }
  57. /**
  58. * Receive an amount of money in cents from a customer.
  59. */
  60. public void insertMoney(int amount)
  61. {
  62. balance = balance + amount;
  63. }
  64. public int getTotal() {
  65. return total;
  66. }
  67. /**
  68. * Increase score by the given number of points.
  69. */
  70. public void increase(int points)
  71. {
  72. score += points;
  73. }
  74. /**
  75. * Reduce price by the given amount.
  76. */
  77. public void discount(int amount)
  78. {
  79. price -= amount;
  80. }
  81. // Prompt method
  82. public void prompt()
  83. {
  84. System.out.println("Please insert the correct amount of money.");
  85. }
  86. // Show price method
  87. public void showPrice()
  88. {
  89. System.out.println("The price of a ticket is " + price + " cents.");
  90. }
  91. // Empty method
  92. public void empty()
  93. {
  94. total = 0;
  95. }
  96. // Set Price method
  97. public int setPrice(int newPrice)
  98. {
  99. return newPrice;
  100. }
  101. public void savings()
  102. {
  103. saving = price * discount;
  104. }
  105. public int emptyMachine() {
  106. return total;
  107. }
  108. /**
  109. * Print a ticket.
  110. * Update the total collected and
  111. * reduce the balance to zero.
  112. */
  113. public void printTicket()
  114. {
  115. int amountLeftToPay = price - balance;
  116. // Simulate the printing of a ticket.
  117. System.out.println("##################");
  118. System.out.println("# The BlueJ Line");
  119. System.out.println("# Ticket");
  120. System.out.println("# " + price + " cents.");
  121. System.out.println("##################");
  122. System.out.println();
  123. // Update the total collected with the balance.
  124. total = total + balance;
  125. // Clear the balance.
  126. balance = 0;
  127. }
  128. }