/** * 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; // Created variable status per exercise 2.15 private int status; //created variable saving for exercise 2.49 private int saving; //created count variable and mean variable for exercise 2.50 private int count; private int mean; //create budget variable for exercise 2.51 private int budget; /** * 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 newPrice) { price = newPrice; balance = 0; total = 0; //added budget field to ticketMachine object budget = 1000; } //Creation of second constructor with set ticket price per exercise 2.42 public TicketMachine() { price = 1000; balance = 0; total = 0; } // create prompt method per exercise 2.33 public void prompt() { System.out.println("Please insert the correct amount of money."); } // create showPrice method per eercise 2.34 public void showPrice() { System.out.println("Current Price is "+ price + " cents"); //System.out.println("# price cents."); } /** * Return the price of a ticket. */ public int getPrice() { return price; } //Create set price method per exercise 2.30/2.41 apparently public int setPrice(int newPrice) { price = newPrice; return price; } /** * Return the amount of money already inserted for the * next ticket. */ public int getAmount() { return balance; } /** * Receive an amount of money in cents from a customer. */ public void insertMoney(int amount) { //per exercise 2.44 created if statement if(amount >= 0){ balance = balance + amount; } } //Create empty method per exercise 2.40 public void empty() { total = 0; } //Create getTotal method per exercise 2.24 public int getTotal() { return total; } /** * Print a ticket. * Update the total collected and * reduce the balance to zero. */ public void printTicket() { 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: " + (amountLeftToPay) + " cents."); } } /** * Return the money in the balance. * The balance is cleared. */ public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } // create saving method per exercise 2.49 public int saving(int discount) { return saving = total * discount; } //create mean method per exercise 2.50 public int mean(int count) { return mean = total / count; } //create budget check method per exercise 2.51 public void budgetCheck(int price) { if (budget < price){ System.out.println("Too Expensive"); System.out.println("Budget is: " + budget); }else{System.out.println("Just Right"); } } //create emptyMachine method per exercise 2.55 public int emptyMachine() { total = 0; return total; } }