Advertisement
Nostrada1

ticket machine 0.1

Jan 23rd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  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 Kölling
  10. * @version 2016.02.29
  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. // Exercise 2.17
  21. private int status;
  22.  
  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()
  29. {
  30. price = 1000;
  31. balance = 0;
  32. total = 0;
  33. }
  34.  
  35. public TicketMachine(int cost)
  36. {
  37. price = cost;
  38. balance = 0;
  39. total = 0;
  40. }
  41.  
  42. public void empty()
  43. {
  44. total = 0;
  45. }
  46.  
  47. public int getTotal()
  48. {
  49. return total;
  50. }
  51.  
  52. /**
  53. * Return the price of a ticket.
  54. */
  55. public int getPrice()
  56. {
  57. return price;
  58. }
  59.  
  60. /**
  61. * Return the amount of money already inserted for the
  62. * next ticket.
  63. */
  64. public int getBalance()
  65. {
  66. return balance;
  67. }
  68.  
  69. /**
  70. * Receive an amount of money from a customer.
  71. */
  72. public void insertMoney(int amount)
  73. {
  74. balance += amount;
  75. total += amount;
  76. }
  77.  
  78. public void prompt()
  79. {
  80. System.out.println("Please insert the correct amount of money.");
  81. }
  82.  
  83. public void showPrice()
  84. {
  85. System.out.println("The price of a ticket is " + price + " cents.");
  86. }
  87.  
  88. /**
  89. * Print a ticket.
  90. * Update the total collected and
  91. * reduce the balance to zero.
  92. */
  93. public void printTicket()
  94. {
  95. // Simulate the printing of a ticket.
  96. System.out.println("##################");
  97. System.out.println("# The BlueJ Line");
  98. System.out.println("# Ticket");
  99. System.out.println("# " + price + " cents.");
  100. System.out.println("##################");
  101. System.out.println();
  102.  
  103. // Update the total collected with the balance.
  104. total = total + balance;
  105. // Clear the balance.
  106. balance = 0;
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement