123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /**
- * 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;
- // A discounted rate
- private int saving;
- //Number of tickets sold
- private int count;
- //average price of tickets
- private int mean;
-
-
- /**
- * 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 ticketCost)
- {
- price = ticketCost;
- balance = 0;
- total = 0;
- }
-
- /**
- * 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;
- }
-
- public int getTotal()
- {
- return total;
- }
-
- /*
- * creates discounted price
- */
- public int discount(int discount)
- {
- return saving = price + discount;
- }
-
- /*
- * Calculates average cost of a ticket
- */
- public int mean()
- {
- return mean = total/count;
- }
-
- /*
- * Determines if there is enough money for tickets
- */
- public void budget(int canSpend)
- {
- if (price>canSpend) {
- System.out.println("Too expensive, must be " + canSpend + " or less");}
- else {System.out.println("Just right!");
- }
- }
-
- /**
- * Receive an amount of money in cents from a customer.
- */
- public void insertMoney(int amount)
- {
- if (amount>0) {
- balance += amount;
- }
- else {
- System.out.println("Use a positive amount: " + amount);
- }
- }
-
- /**
- * Print a ticket.
- * Update the total collected and
- * reduce the balance to zero.
- */
- public void printTicket()
- {
- int amountLeftToPay = price-balance;
- if (amountLeftToPay<=0) {
- // 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();
- // Reduce the balance.
- balance = balance - price;
- // Update the total collected with the price.
- total = total + price;}
- else {
- System.out.println("You must insert at least: " + (price - balance) + " cents.");
- }
- }
-
- /*return the money in the balance
- * balance is cleared
- */
- public int refundBalance()
- {
- int refundAmount =balance;
- balance = 0;
- return refundAmount;
- }
-
- /*
- * Remove all money from machine
- * total is cleared
- * returns amount you took from machine
- */
- public int emptyMachine()
- {
- int emptyAmount = total;
- total = 0;
- return emptyAmount;
- }
- }
|