the second Objects lab.

TicketMachine.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 showPrice;
  22. private int prompt;
  23. /**
  24. * Create a machine that issues tickets of the given price.
  25. * Note that the price must be greater than zero, and there
  26. * are no checks to ensure this.
  27. */
  28. public TicketMachine(int ticketCost)
  29. {
  30. price = ticketCost;
  31. balance = 500;
  32. total = 500;
  33. }
  34. /**
  35. * Return the price of a ticket.
  36. */
  37. public int getPrice()
  38. {
  39. return price;
  40. }
  41. /**
  42. * Return the amount of money already inserted for the
  43. * next ticket.
  44. */
  45. public int getBalance()
  46. {
  47. return balance;
  48. }
  49. /**
  50. * Receive an amount of money in cents from a customer.
  51. */
  52. public void insertMoney(int amount)
  53. {
  54. balance = balance + amount;
  55. }
  56. public class Student {
  57. }
  58. public class LabClass {
  59. }
  60. public void Store() {
  61. }
  62. public void Mean() {
  63. }
  64. public void showPrice() {
  65. }
  66. public void prompt() {
  67. }
  68. /**
  69. * Print a ticket.
  70. * Update the total collected and
  71. * reduce the balance to zero.
  72. */
  73. public void printTicket()
  74. {
  75. // Simulate the printing of a ticket.
  76. System.out.println("##################");
  77. System.out.println("# The BlueJ Line");
  78. System.out.println("# Ticket");
  79. System.out.println("# " + price + " cents.");
  80. System.out.println("##################");
  81. System.out.println();
  82. // Update the total collected with the balance.
  83. total = total + balance;
  84. // Clear the balance.
  85. balance = 0;
  86. }
  87. }