the second Objects lab.

TicketMachine.java 2.8KB

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