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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package NaiveTicket;
  2. /**
  3. * TicketMachine models a naive ticket machine that issues
  4. * flat-fare tickets.
  5. * The price of a ticket is specified via the constructor.
  6. * It is a naive machine in the sense that it trusts its users
  7. * to insert enough money before trying to print a ticket.
  8. * It also assumes that users enter sensible amounts.
  9. *
  10. * @author David J. Barnes and Michael Kolling
  11. * @version 2008.03.30
  12. */
  13. public class TicketMachine
  14. {
  15. // The price of a ticket from this machine.
  16. private int price;
  17. // The amount of money entered by a customer so far.
  18. private int balance;
  19. // The total amount of money collected by this machine.
  20. private int total;
  21. /**
  22. * Create a machine that issues tickets of the given price.
  23. * Note that the price must be greater than zero, and there
  24. * are no checks to ensure this.
  25. */
  26. /*public TicketMachine()
  27. {
  28. price = 1000;
  29. balance = 0;
  30. total = 0;
  31. }
  32. */
  33. public TicketMachine(int ticketCost)
  34. {
  35. price = ticketCost;
  36. }
  37. public TicketMachine()
  38. {
  39. price = 700;
  40. }
  41. /**
  42. * Return the price of a ticket.
  43. */
  44. public int getPrice()
  45. {
  46. return price;
  47. }
  48. /**
  49. * Return the amount of money already inserted for the
  50. * next ticket.
  51. */
  52. public int getBalance()
  53. {
  54. return balance;
  55. }
  56. /**
  57. * Define getTotal
  58. */
  59. public int getTotal()
  60. {
  61. return total;
  62. }
  63. /**
  64. * Receive an amount of money in cents from a customer.
  65. */
  66. public void insertMoney(int amount)
  67. {
  68. balance = balance + amount;
  69. total += amount;
  70. }
  71. /**
  72. * Complete setPrice
  73. */
  74. public void setPrice(int ticketCost)
  75. {
  76. price = ticketCost;
  77. }
  78. /**
  79. * Implement empty method
  80. */
  81. public void empty()
  82. {
  83. total = 0;
  84. balance = 0;
  85. }
  86. /**
  87. * Print a ticket.
  88. * Update the total collected and
  89. * reduce the balance to zero.
  90. */
  91. public void printTicket()
  92. {
  93. // Simulate the printing of a ticket.
  94. System.out.println("##################");
  95. System.out.println("# The BlueJ Line");
  96. System.out.println("# Ticket");
  97. System.out.println("# " + price + " cents.");
  98. System.out.println("##################");
  99. System.out.println();
  100. // Update the total collected with the balance.
  101. total = total + balance;
  102. // Clear the balance.
  103. balance = 0;
  104. }
  105. public void prompt(){
  106. System.out.println("Please insert the correct amount of money");
  107. }
  108. public void showPrice(){
  109. System.out.println("The price of a ticket is " + price + " cents.");
  110. }
  111. }