Tuesday, January 17, 2012

Challenge 033: Ticket Machine


import java.util.Scanner;


public class TicketMachine
{
    // instance variables
    private int price;
    private int balance;
    private int total;
   

    /**
     * Constructor for objects of class challenge033
     * Price = cost of ticket
     * Balance = amount customer puts in
     * Total = total money ticket machine collects
     */
    public TicketMachine(int price)
    {
        // initialise instance variables
        this.price = price;
        this.balance = 0;
        this.total = 0;
    }
    /*
     * Allows customer to see price per ticket
     */
    public void getPrice()
    {
        System.out.println("The Price for one Ticket will be: " + price + " cents\n");
    }
    /*
     * Allows customer to see his current balance
     */
    public void getBalance()
    {
        System.out.println("Balance: " + balance + " cents\n");
    }  
    /*
     * Tells customer to insert money
     */
    public void insertMoney ()
    {
       
        Scanner input = new Scanner(System.in);
        System.out.print("\n");
        /*
         * The utility scanner breaks input into tokens. The tokens are then converted into values of
         * different types. For example a code can allow the user to read a number.
         */
        {
            System.out.println();
            System.out.println("Balance: " + balance + " cents");
            System.out.println("Put Your Money In Me: ");
            int userInput = input.nextInt();
           
            if (userInput > 0)
            {
                balance = balance + userInput;
                System.out.println();
                System.out.println("Your new Balance is: " + balance + " cents");
            }
           
            else
            {
                System.out.println();
                System.out.println("Insert Money Now");
                System.out.println("Click Refund to Receive Money");
            }
         
        }
    }
   
    public void refundBalance()
    {
        if (balance > price)
            System.out.println("Your Change: " + balance + " cents");
            balance = 0;
        }
    /*
     * This allows the customer to remove money if they wish to do so and also gives the costumer his
     * change while returning the balance to 0.
     */
    public void printTicket()
    {
        if(balance >= price)
        {
            System.out.println();
            System.out.println("Purchase Successful");
            System.out.println("--------------------------------");
            System.out.println("Greenman Transit Lines");
            System.out.println("Price: " + price + " cents");
            System.out.println("--------------------------------");
                /*
                 * This shows how the ticket will look when printed
                 */
            total = total + price;
            /*
             * This allows the machine to keep track of amount of money made becuase it takes the
             * previous value and add the cost of a new ticket each time a customer buys one.
             */
            balance = balance - price;
            /*
             * This allows the machine to return the costumer's change if he put too much money in.
             */
        }
        else
        {
            System.out.println();
            System.out.println("Insufficient Funds");
            System.out.println("Please insert " + (price - balance) + " cents.");
            /*
             * This tells the costumer that he is trying to but a ticket but does not have enough money,
             * therefore it does not print.
             */
        }
    }
}