the second Objects lab.

TicketMachine.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. public class TicketMachine
  2. {
  3. private int price;
  4. private int balance;
  5. private int total;
  6. private int score;
  7. /**
  8. * Create a machine that issues tickets of the given price.
  9. * Note that the price must be greater than zero, and there
  10. * are no checks to ensure this.
  11. */
  12. public TicketMachine()
  13. {
  14. price = 1000;
  15. balance = 0;
  16. total = 0;
  17. }
  18. public TicketMachine(int ticketCost){}
  19. public void setPrie(int ticketCost){
  20. price = ticketCost;
  21. }
  22. public int getPrice()
  23. {
  24. return price;
  25. }
  26. /**
  27. * Return the amount of money already inserted for the
  28. * next ticket.
  29. */
  30. public int getBalance()
  31. {
  32. return balance;
  33. }
  34. public void increase(int points){
  35. score = score + points;
  36. }
  37. public void discount(int amount){
  38. price = price - amount;
  39. }
  40. public void promot(){
  41. System.out.println("Please insert the correct amount of money.");
  42. }
  43. public void showPrice(){
  44. System.out.println("The price of a ticke is" + price + "cents");
  45. }
  46. /**
  47. * Receive an amount of money in cents from a customer.
  48. */
  49. public void insertMoney(int amount)
  50. {
  51. balance = balance + amount;
  52. }
  53. public int getTotal(int amount){
  54. return total;
  55. }
  56. public void empty(){
  57. total = 0;
  58. }
  59. /**
  60. * Print a ticket.
  61. * Update the total collected and
  62. * reduce the balance to zero.
  63. */
  64. public void printTicket()
  65. {
  66. // Simulate the printing of a ticket.
  67. System.out.println("##################");
  68. System.out.println("# The BlueJ Line");
  69. System.out.println("# Ticket");
  70. System.out.println("# " + price + " cents.");
  71. System.out.println("##################");
  72. System.out.println();
  73. // Update the total collected with the balance.
  74. total = total + balance;
  75. // Clear the balance.
  76. balance = 0;
  77. }
  78. }