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

TicketMachine.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /**
  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(int ticketCost)
  26. {
  27. price = ticketCost;
  28. balance = 0;
  29. total = 0;
  30. prompt();
  31. }
  32. public TicketMachine()
  33. {
  34. price = 1000;
  35. balance = 0;
  36. total = 0;
  37. prompt();
  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. /**
  55. * Receive an amount of money in cents from a customer.
  56. */
  57. public void insertMoney(int amount)
  58. {
  59. if (amount > 0)
  60. {
  61. balance = balance + amount;
  62. }
  63. else
  64. System.out.println("Use a positive amount: " +amount);
  65. if (balance >= price)
  66. {
  67. total = total + price;
  68. balance = balance - price;
  69. }
  70. else
  71. System.out.println("You must insert at least " + (price-balance)+ " more cents.");
  72. balance = balance + amount;
  73. }
  74. /**
  75. * Print a ticket.
  76. * Update the total collected and
  77. * reduce the balance to zero.
  78. */
  79. public void printTicket()
  80. {
  81. int amountLeftToPay = price - balance;
  82. if (amountLeftToPay <=0)
  83. {
  84. // Simulate the printing of a ticket.
  85. System.out.println("##################");
  86. System.out.println("# The BlueJ Line");
  87. System.out.println("# Ticket");
  88. System.out.println("# " + price + " cents.");
  89. System.out.println("##################");
  90. System.out.println();
  91. }
  92. else
  93. System.out.println("An additional " +amountLeftToPay+ " cents is required.");
  94. // Update the total collected with the balance.
  95. total = total + balance;
  96. // Clear the balance.
  97. balance = 0;
  98. }
  99. public int getTotal()
  100. {
  101. return total;
  102. }
  103. public void prompt()
  104. {
  105. System.out.println("Please insert the correct amount of money.");
  106. }
  107. public void showPrice()
  108. {
  109. System.out.println("The price of a ticket is " + price + "cents.");
  110. }
  111. public void empty()
  112. {
  113. total = 0;
  114. }
  115. public void setPrice(int newPrice)
  116. {
  117. price = newPrice;
  118. }
  119. public int emptyMachine()
  120. {
  121. System.out.println("The total is " + total);
  122. total = 0;
  123. return total;
  124. }
  125. }