Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

** Modify the code of the insertMoney method to check that a user can only enter

ID: 3530915 • Letter: #

Question

** Modify the code of the insertMoney method to check that a user can only enter sensible amounts of money. The method should print a message (and not change the balance) if the amount parameter is less than or equal to 0. Optional: right click on the green class TicketMachineTest and select TestAll. This will execute a series of test cases to check your code. Testing will be covered later in the course, so don't worry if this part doesn't make sense for now. ** Modify the code of the printTicket method so that it will a) only print a ticket if enough money has been input b) prints an error message if there is not enough money c) only deducts the ticket price from the balance (possibly leaving change to be given). The code is given below. /** * TicketMachine models a ticket machine that issues * flat-fare tickets. * The price of a ticket is specified via the constructor. * Instances will check to ensure that a user only enters * sensible amounts of money, and will only print a ticket * if enough money has been input. * * @author David J. Barnes and Michael Koelling * @version 2011.07.31 */ 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; /** * 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(int cost) { price = cost; balance = 0; total = 0; } /** * Create a machine that issues tickets at default price * (of your choosing). */ public TicketMachine() { // add code here } /** * 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; } /** * Return the total amount of money collected by this machine. */ public int getTotal() { return total; } /** * Receive an amount of money from a customer. * Check amount is reasonable */ public void insertMoney(int amount) { balance = balance + amount; } /** * 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; } /** * Repay the current balance to the customer * and reduce the balance to zero. * @return int the amount paid back to the customer */ public int refundBalance() { return 0; //replace code } /** * Set the price of this machine's tickets to be cost (if reasonable) */ public void setPrice(int cost) { //add code here } /** * Discount the ticket price by amount. * Check amount makes sense. */ public void discount(int amount) { //add code here } /** * Print a message "The price of a method is xxx cents" * where xxx is the current ticket price */ public void showPrice() { //add code here } }

Explanation / Answer

public void insertMoney(int amount)

{

if(amount<=0)

{

System.out.println("Error, please enter amount greater than zero");

}

else

{

balance = balance + amount;

}

}

public void printTicket()

{

if(price<0)

{

System.out.println("Error! Please enter amount greater than zero");

}

else

{

System.out.println("##################");

System.out.println("# The BlueJ Line");

System.out.println("# Ticket");

System.out.println("# " + price + " cents.");

System.out.println("##################");

System.out.println();

balance = balance-price;

}

}