the second Objects lab.

TicketMachine.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 point,score=0;
  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. public TicketMachine(int k)
  33. {
  34. price = k;
  35. balance = 0;
  36. total = 0;
  37. }
  38. /**
  39. * Return the price of a ticket.
  40. */
  41. public int getPrice()
  42. {
  43. return price;
  44. }
  45. /**
  46. * Return the amount of money already inserted for the
  47. * next ticket.
  48. */
  49. public int getBalance()
  50. {
  51. return balance;
  52. }
  53. /**
  54. * Receive an amount of money in cents from a customer.
  55. */
  56. public void insertMoney(int amount)
  57. {
  58. balance = balance + amount;
  59. }
  60. public void setPrice(int ticketCost)
  61. {
  62. price=ticketCost;
  63. }
  64. public void increase(int points)
  65. {
  66. score=score+point;
  67. }
  68. public void discount(int amount)
  69. {
  70. price=price-amount;
  71. }
  72. public void prompt()
  73. {
  74. System.out.println("\nPlease insert the correct amount of money");
  75. }
  76. public void showprice()
  77. {
  78. System.out.printf("\nThe price of a ticket is %d cents",price);
  79. }
  80. public void empty()
  81. {
  82. total=0;
  83. }
  84. public int emptymachine(int total)
  85. {
  86. int actualtotal=total;
  87. total=0;
  88. return actualtotal;
  89. }
  90. /**
  91. * Print a ticket.
  92. * Update the total collected and
  93. * reduce the balance to zero.
  94. */
  95. public void printTicket()
  96. {
  97. int amountlefttopay;
  98. amountlefttopay=balance-price;
  99. // Simulate the printing of a ticket.
  100. System.out.println("##################");
  101. System.out.println("# The BlueJ Line");
  102. System.out.println("# Ticket");
  103. System.out.println("# " + price + " cents.");
  104. //System.out.println("# " + "price" + " cents.");
  105. System.out.println("##################");
  106. System.out.println();
  107. // Update the total collected with the balance.
  108. total = total + balance;
  109. // Clear the balance.
  110. balance = total-balance;
  111. }
  112. }