Advertisement
BAN-tux

Untitled

Dec 16th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public class TicketMachine
  2. {
  3. private int price;
  4. private int balance;
  5. private int total;
  6. private int refund;
  7. private int tickets;
  8.  
  9. public TicketMachine(int ticketCost)
  10. {
  11. price = ticketCost;
  12. balance = 0;
  13. total = 0;
  14. refund = 0;
  15. }
  16.  
  17. public int getPrice()
  18. {
  19. return price;
  20. }
  21.  
  22. public int getBalance()
  23. {
  24. return balance;
  25. }
  26.  
  27. public void getTickets(int tkt)
  28. {
  29. if(tkt >= 1)
  30. {
  31. tickets = tkt;
  32. }
  33. else
  34. {
  35. System.out.println("Please insert the right number of tickets.\n");
  36. }
  37. }
  38.  
  39.  
  40. public void insertMoney(int money)
  41. {
  42. if (money > 0)
  43. {
  44. balance = balance + money;
  45. }
  46. else
  47. {
  48. System.out.println("Please insert the right amount of money.\n");
  49. }
  50. }
  51.  
  52. /**
  53. * Print a ticket.
  54. * Update the total collected and
  55. * reduce the balance to zero.
  56. */
  57.  
  58. public void purchaseTickets()
  59. {
  60. System.out.println ("Ticket cost = $"+price+".");
  61. System.out.println ("Your total amount of money = $"+balance+".");
  62. if (balance > 0)
  63. {
  64. if(balance >= (price*tickets))
  65. {
  66. // Simulate the printing of a ticket.
  67. System.out.println("\n+++++++++++++++++++++++++++++++++\n");
  68.  
  69. System.out.println("Mystery Train [Final Destination]");
  70. System.out.println("Ticket(s) : "+ tickets +".");
  71. System.out.println("Total ticket(s) cost : $"+ (price*tickets) +".\n");
  72. System.out.println("+++++++++++++++++++++++++++++++++");
  73. System.out.println();
  74.  
  75. // Update the total collected with the balance.
  76. total = total + balance;
  77. refund = refundBalance();
  78. if (refund == 0)
  79. {
  80. System.out.println("No refundable money.\n");
  81. }
  82. else
  83. {
  84. System.out.println("Refundable money: $"+refund+".\n");
  85. }
  86. // Clear the balance.
  87. balance = 0;
  88. }
  89. else
  90. {
  91. System.out.println ("Ticket to buy : "+tickets);
  92. System.out.println("Please insert enough money. Insert $"
  93. +((price * tickets)-balance)+ " more.\n");
  94. }
  95. }
  96. else
  97. {
  98. System.out.println("Insert your money.\n");
  99. }
  100. }
  101.  
  102. public int refundBalance()
  103. {
  104. int refundable;
  105. refundable = balance - price * tickets;
  106. balance = 0;
  107. return refundable;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement