the second Objects lab.

TicketMachine.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 saving;
  21. private int status;
  22. private int count;
  23. private int mean;
  24. private int budget;
  25. /**
  26. * Create a machine that issues tickets of the given price.
  27. * Note that the price must be greater than zero, and there
  28. * are no checks to ensure this.
  29. */
  30. //public TicketMachine(int newPrice);
  31. public TicketMachine()
  32. {
  33. //price = newPrice;
  34. balance = 0;
  35. total = 0;
  36. budget = 1000;
  37. }
  38. public void emptry()
  39. {
  40. total = 0;
  41. }
  42. public int setPrice(int price){
  43. price = getPrice();
  44. return price;
  45. }
  46. public void prompt(){
  47. System.out.println("Please insert the correct amount of money.");
  48. }
  49. public void showPrice(){
  50. System.out.println("# price cents.");
  51. }
  52. /**
  53. * Return the price of a ticket.
  54. */
  55. public int getPrice()
  56. {
  57. return price;
  58. }
  59. /**
  60. * Return the amount of money already inserted for the
  61. * next ticket.
  62. */
  63. public int getBalance()
  64. {
  65. return balance;
  66. }
  67. /**
  68. * Receive an amount of money in cents from a customer.
  69. */
  70. public void insertMoney(int amount)
  71. {
  72. balance = balance + amount;
  73. }
  74. /*
  75. * Write an if statement that will compare the value in price against
  76. the value in budget. If price is greater than budget then print the message ‘Too
  77. expensive’, otherwise print the message ‘Just right’.
  78. */
  79. //if(price == budget){}
  80. /**
  81. * Print a ticket.
  82. * Update the total collected and
  83. * reduce the balance to zero.
  84. */
  85. public void printTicket()
  86. {
  87. // Simulate the printing of a ticket.
  88. System.out.println("##################");
  89. System.out.println("# The BlueJ Line");
  90. System.out.println("# Ticket");
  91. System.out.println("# " + price + " cents.");
  92. System.out.println("##################");
  93. System.out.println();
  94. // Update the total collected with the balance.
  95. total = total + balance;
  96. // Clear the balance.
  97. balance = 0;
  98. }
  99. }