Chapter 2 of the BlueJ book. Walk through many of the chapters exercises.

TicketMachine.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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,fgghf
  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. private int balance;
  18. // The total amount of money collected by this machine.
  19. private int total;
  20. /**
  21. * Create a machine that issues tickets of the given price.
  22. * Note that the price must be greater than zero, and there
  23. * are no checks to ensure this.
  24. */
  25. public TicketMachine()
  26. {
  27. price = 1000;
  28. balance = 0;
  29. total = 0;
  30. }
  31. public TicketMachine(int ticketPrice)
  32. {
  33. price = ticketPrice;
  34. balance = 0;
  35. total = 0;
  36. }
  37. public int emptyMachine()
  38. {
  39. int finalTotal = total;
  40. total = 0;
  41. return finalTotal;
  42. }
  43. public void checkBudget(int budget) {
  44. if(price > budget) {
  45. System.out.println("Too expensive! Your budget is only " + budget);
  46. } else {
  47. System.out.println("Just Right!");
  48. }
  49. }
  50. public void prompt()
  51. {
  52. System.out.println("Please insert the correct amount of money.");
  53. }
  54. public void showPrice()
  55. {
  56. System.out.println("The price of a ticket is " + price + " cents.");
  57. }
  58. /**
  59. * Return the price of a ticket.
  60. */
  61. public int getPrice()
  62. {
  63. return price;
  64. }
  65. public void empty()
  66. {
  67. total = 0;
  68. }
  69. public void setPrice(int newPrice)
  70. {
  71. price = newPrice;
  72. }
  73. /**
  74. * Return the amount of money already inserted for the
  75. * next ticket.
  76. */
  77. public int getBalance()
  78. {
  79. return balance;
  80. }
  81. /**
  82. * Receive an amount of money in cents from a customer.
  83. */
  84. public void insertMoney(int amount)
  85. {
  86. balance = balance + amount;
  87. }
  88. public int getTotal()
  89. {
  90. return total;
  91. }
  92. /**
  93. * Print a ticket.
  94. * Update the total collected and
  95. * reduce the balance to zero.
  96. */
  97. public void printTicket()
  98. {
  99. // Simulate the printing of a ticket.
  100. int amountLeftToPay = price - balance;
  101. if(amountLeftToPay <= 0) {
  102. System.out.println("##################");
  103. System.out.println("# The BlueJ Line");
  104. System.out.println("# Ticket");
  105. System.out.println("# " + price + " cents.");
  106. System.out.println("##################");
  107. System.out.println();
  108. // Update the total collected with the balance.
  109. total = total + balance;
  110. // Clear the balance.
  111. balance = balance - price;
  112. } else {
  113. System.out.println("You must insert at least: " + amountLeftToPay + " cents.");
  114. }
  115. }
  116. }