the second Objects lab.

Student.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Student{
  13. public class LabClass{
  14. }
  15. public class TicketMachine
  16. {
  17. // The price of a ticket from this machine.
  18. private int price;
  19. int status;
  20. // The amount of money entered by a customer so far.
  21. private int balance;
  22. // The total amount of money collected by this machine.
  23. private int total;
  24. public String Pet(String petsName){
  25. petsName = "Cujo";
  26. return petsName;
  27. }
  28. /**
  29. * Create a machine that issues tickets of the given price.
  30. * Note that the price must be greater than zero, and there
  31. * are no checks to ensure this.
  32. */
  33. public TicketMachine()
  34. {
  35. price = 1000;
  36. balance = 0;
  37. total = 0;
  38. }
  39. public void setPrice()
  40. {
  41. getPrice();
  42. }
  43. public void prompt()
  44. {
  45. System.out.println("Please insert the correct amount of money.");
  46. }
  47. public void showPrice()
  48. {
  49. System.out.println("The price of a ticket " + price +" cents." );
  50. }
  51. public void empty()
  52. {
  53. total = 0;
  54. }
  55. /**
  56. * Return the price of a ticket.
  57. */
  58. public int getPrice()
  59. {
  60. return price;
  61. }
  62. /**
  63. * Return the amount of money already inserted for the
  64. * next ticket.
  65. */
  66. public int getBalance()
  67. {
  68. return balance;
  69. }
  70. /**
  71. * Receive an amount of money in cents from a customer.
  72. */
  73. public void insertMoney(int amount)
  74. {
  75. balance = balance + amount;
  76. }
  77. /**
  78. * Print a ticket.
  79. * Update the total collected and
  80. * reduce the balance to zero.
  81. */
  82. public void printTicket()
  83. {
  84. // Simulate the printing of a ticket.
  85. System.out.println("##################");
  86. System.out.println("# The BlueJ Line");
  87. System.out.println("# Ticket");
  88. System.out.println("# " + "price" + " cents.");
  89. System.out.println("##################");
  90. System.out.println();
  91. // Update the total collected with the balance.
  92. total = total + balance;
  93. // Clear the balance.
  94. balance = 0;
  95. }
  96. }
  97. }