the second Objects lab.

TicketMachine.java 2.9KB

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