the second Objects lab.

TicketMachine.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 information.
  21. private int status;
  22. // The next.
  23. private String prompt;
  24. // The next2.0.
  25. private int showPrice;
  26. private int score;
  27. private int saving;
  28. private int mean;
  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()
  35. {
  36. price = 1000;
  37. balance = 0;
  38. total = 0;
  39. }
  40. public void getMean(){
  41. }
  42. public int setSaving(int amount){
  43. saving = this.price * (price - amount);
  44. return saving;
  45. }
  46. public TicketMachine(int price){
  47. }
  48. // public TicketMachine(){
  49. // price = 867459;
  50. // }
  51. public void setPrice(int ticketCost){
  52. price = ticketCost;
  53. }
  54. /**
  55. * Return the price of a ticket.
  56. */
  57. public int getPrice()
  58. {
  59. return price;
  60. }
  61. /**
  62. * Return the amount of money already inserted for the
  63. * next ticket.
  64. */
  65. public int getBalance()
  66. {
  67. return balance;
  68. }
  69. public void prompt()
  70. {
  71. System.out.println("Please insert the correct amount of money.");
  72. }
  73. public void showPrice()
  74. {
  75. System.out.println("The price of a ticket is " + price + " cents.");
  76. }
  77. /**
  78. * Receive an amount of money in cents from a customer.
  79. */
  80. public void insertMoney(int amount)
  81. {
  82. balance = balance + amount;
  83. }
  84. /**
  85. * Print a ticket.
  86. * Update the total collected and
  87. * reduce the balance to zero.
  88. */
  89. public void printTicket()
  90. {
  91. // Simulate the printing of a ticket.
  92. System.out.println("##################");
  93. System.out.println("# The BlueJ Line");
  94. System.out.println("# Ticket");
  95. System.out.println("# " + price + " cents.");
  96. System.out.println("##################");
  97. System.out.println();
  98. // Update the total collected with the balance.
  99. total = total + balance;
  100. // Clear the balance.
  101. balance = 0;
  102. }
  103. public int getTotal(){
  104. return total;
  105. }
  106. public void increase(int points){
  107. score = score + points;
  108. }
  109. public void discount(int amount){
  110. price = price - amount;
  111. }
  112. public void empty(){
  113. total = 0;
  114. }
  115. }