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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. private int balance;
  18. // The total amount of money collected by this machine.
  19. private int total;
  20. // The total number of tickets sold.
  21. private int sold;
  22. public void prompt()
  23. {
  24. System.out.println("Please insert money for ticket.");
  25. System.out.println();
  26. }
  27. public void showPrice()
  28. {
  29. System.out.println("The price of a ticket is " + price + " cents.");
  30. System.out.println();
  31. }
  32. /**
  33. * Create a machine that issues tickets of the given price.
  34. * Note that the price must be greater than zero, and there
  35. * are no checks to ensure this.
  36. */
  37. public TicketMachine()
  38. {
  39. price = 1000;
  40. balance = 0;
  41. total = 0;
  42. }
  43. /**
  44. * Return the price of a ticket.
  45. */
  46. public int getPrice()
  47. {
  48. return price;
  49. }
  50. /**
  51. * Return the amount of money already inserted for the
  52. * next ticket.
  53. */
  54. public int getAmount()
  55. {
  56. return balance;
  57. }
  58. public int getTotal()
  59. {
  60. return total;
  61. }
  62. /**
  63. * Receive an amount of money in cents from a customer.
  64. */
  65. public void insertMoney(int amount)
  66. {
  67. balance = balance + amount;
  68. }
  69. /**
  70. * Print a ticket.
  71. * Update the total collected and
  72. * reduce the balance to zero.
  73. */
  74. public void printTicket()
  75. {
  76. // Simulate the printing of a ticket.
  77. if (balance == price) {
  78. System.out.println("##################");
  79. System.out.println("# The BlueJ Line");
  80. System.out.println("# Ticket");
  81. System.out.println("# " + price + " cents.");
  82. System.out.println("##################");
  83. System.out.println();
  84. balance = 0;
  85. } else if (balance < price) {
  86. System.out.println("Not enough money inserted. Please insert " + (price - balance) + " more cents.");
  87. System.out.println();
  88. } else if (balance > price) {
  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. System.out.println((balance - price) + " cents change returned.");
  96. System.out.println();
  97. balance = 0;
  98. }
  99. // Update the total collected with the balance.
  100. total = (total + balance) - (balance - price);
  101. }
  102. public int ticketsSold()
  103. {
  104. // Calculates how many tickets have been sold.
  105. sold = total / price;
  106. return sold;
  107. }
  108. public void emptyMachine()
  109. {
  110. total = 0;
  111. }
  112. public void setPrice(int newPrice)
  113. {
  114. price = newPrice;
  115. }
  116. public void refeundBalance()
  117. {
  118. int amountToRefund;
  119. amountToRefund = balance;
  120. System.out.println(amountToRefund + " cents refunded.");
  121. System.out.println();
  122. }
  123. }