/**
 * 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 Kölling
 * @version 2011.07.31
 */
public class TicketMachine {
    private int price;    // The price of a ticket from this machine.
    private int balance;  // The amount of money entered by a customer so far.
    private int total;    // The total amount of money collected by this machine.

    /**
     * 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) {
        this.price = cost;
        this.balance = 0;
        this.total = 0;
    }

    /**
     * Return the price of a ticket.
     */
    public int getPrice() {
        return this.price;
    }

    /**
     * Return the amount of money already inserted for the
     * next ticket.
     */
    public int getBalance() {
        return this.balance;
    }

    /**
     * Receive an amount of money from a customer.
     */
    public void insertMoney(int amount) {
        this.balance = this.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("# " + this.price + " cents.");
        System.out.println("##################");
        System.out.println();

        // Update the total collected with the balance.
        this.total = this.total + this.balance;
        // Clear the balance.
        this.balance = 0;
    }
}
