/** * TicketMachine models a naive ticket machine that issues * flat-fare tickets. * The price of a ticket is specified via the constructor. * It is a naive machine in the sense that it trusts its users * to insert enough money before trying to print a ticket. * It also assumes that users enter sensible amounts. * * @author David J. Barnes and Michael Kolling * @version 2008.03.30 */ public class TicketMachine { // The price of a ticket from this machine. private int price; // The amount of money entered by a customer so far. private int balance; // The total amount of money collected by this machine. private int total; // Exercise 2.15 private int status; /** * Create a machine that issues tickets of the given price. * Note that the price must be greater than zero, and there * are no checks to ensure this. */ public TicketMachine() { //2.39 set price to 1000 and remove input parameter //2.42 constructor with parameter to specify price //2.58 Better ticket machine price = 0; balance = 0; total = 0; } //public TicketMachine() //{ //2.42 constructor with default value of price //price = 500; //} /* * Exercise 2.30/2.41: Assign a value to the price field */ public void setPrice(int ticketCost) { price = ticketCost; } /* * Exercise 2.31: Add a value to the score field */ public void increase(int points) { int score = points; } /* * Exercise 2.32: Subtract a value from the price field */ public void discount(int amount) { price = price - amount; } /** * Return the price of a ticket. */ public int getPrice() { return price; } /** * Return the amount of money already inserted for the * next ticket. */ public int getBalance() { return balance; } /** * Receive an amount of money in cents from a customer. */ public void insertMoney(int amount) { if(amount >= 0){ balance = balance + amount; } } /** * Return the total amount of money collected by this machine. */ //Exercise 2.24 public int getTotal() { return total; } /** * Print message when incorrect amount of money is inserted */ //Exercise 2.33 public void prompt() { System.out.println("Please insert the correct amount of money."); } /** * Print message showing price of a ticket */ //Exercise 2.34 public void showPrice() { System.out.println("The price of a ticket is " + price + " cents."); } /** * Print message showing price of a ticket */ //Exercise 2.40 public void empty() { total = 0; } /** * Print message showing price of a ticket */ //Exercise 2.55 public int emptyMachine() { return total; //total=0; } /** * Print a ticket. * Update the total collected and * reduce the balance to zero. */ public void printTicket() { /* // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the balance. total = total + balance; // Clear the balance. balance = 0; */ /* if(balance >= price) { System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the price. total = total + price; // Reduce the balance by the price. balance = balance - price; } else { System.out.println("You must insert at least: " + (price - balance) + " more cents."); } */ int amountLeftToPay = price - balance; if(amountLeftToPay <=0){ System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the price. total = total + price; // Reduce the balance by the price. balance = balance - price; } else { System.out.println("You must insert at least: " + (price - balance) + " more cents."); } } } /* EXERCISES * 2.1: call void printTicket() * 2.2: price that was entered * 2.3: The ticket prints regardless * 2.4: [practice ticket machine] * 2.5: Yes, price is different. * 2.6: public class Student; public class LabClass * 2.7: Order of statement matters * 2.8: It is possible to leave this out, the class will be assigned default access modifier * 2.9: fields: price, balance, total; * constructors: public TicketMachine(int ticketCost); * methods: getPrice, getBalance, insertMoney, printTicket * 2.10: no return type defined * 2.11: int, Student, Server * 2.12: alive, tutor, game * 2.13: Order of statement matters * 2.14: field declaration requires a semicolon * 2.15: Shown in code * 2.16: Student * 2.17: title of type String, and price of type double * 2.18: fields: title, price, author, publisher, publish date * types: string, double, string, string, date * 2.19: name = petsName; * 2.20: The problem when adding int is the ticketCost was not recognized as price * Assignments should not re-declare the field type. This needs to be done in fields section. * 2.21: getPrice returns a passed value and getBalance returns a set value. * 2.22: getBalance - "How much money do I owe or am I owed?" * 2.23: No, the variable does not need to have the same name as the method * 2.24: new getTotal method created above. * 2.25: error "missing return statement" * 2.26: getPrice returns an integer, printTicket does not return anything (void) * 2.27: They are actions and don't return anything. * 2.28: (Practicing methods) * 2.29: There is a return type included * 2.30-2.34: Shown in Code * 2.35: Different outputs because the price is variable * 2.36: "price" string would display instead of a variable * 2.37: Same output as 2.36 * 2.38: No, they cannot show variable prices because they are outputing a string * 2.39: Objects cannot pass a parameter to the ticketmachine * 2.40: Mutator method * 2.41: Setter method * 2.42: Shown in code * 2.43: (Practicing methods) * 2.44: negative values do not affect balance * 2.45: Booleans are well suited to be controlled by a type with only two different values * 2.46: Shown in code * 2.47: No because there is a check to make sure price is not greater than balance * 2.48: +, -, /, *, % * 2.49: saving = price * discount; * 2.50: mean = total / count; * 2.51: if(price > budget){System.out.print("Too expensive")}else {System.out.print("Just right")} * 2.52: if(price > budget){System.out.print("Too expensive, your budget is only " + budget)}else {System.out.print("Just right")} * 2.53: it is setting balance as 0 before returning it. * 2.54: return statement ends the execution of a method * 2.55: Shown in code * 2.56: It is an accessor because method ends after return statement * 2.57: Shown in code * 2.58: setPrice method can be called to set price based on user selection */