All pastes #790321 Raw Edit

Ryan

public java v1 · immutable
·published 2007-11-20 08:44 UTC
rendered paste body
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;/** Creates a new instance of ticketMachine */public class lab7 extends JFrame implements ActionListener {        private JButton oneHours;    private JButton twoHours;    private JButton fourHours;    private JButton printTicket;    private JButton getMoneyBack;    private JTextField priceText;        private JLabel stepOne;    private JLabel stepTwo;    private JTextArea output;        private int price = 0;        private javax.swing.JTextArea setText;        private String m1;        private int moneyInserted;        TicketMachine myTicketMachine = new TicketMachine();               public static void main(String args[]) {       // lab7 demo = new TicketMachine();        lab7 demo = new lab7();        demo.setSize(300, 500);        demo.createGUI();        demo.show();    }        private void createGUI() {        setDefaultCloseOperation(EXIT_ON_CLOSE);        Container window = getContentPane();        window.setLayout(new FlowLayout());                        oneHours = new JButton("1 hour ----------- 2 dollars");        twoHours = new JButton("2 hours ---------- 4 dollars");        fourHours = new JButton("4 hours ---------- 8 dollars");        printTicket = new JButton("******* Print Ticket *******");        getMoneyBack = new JButton("Get Money Back");                        stepOne = new JLabel("Step1: Select number of hours ");        window.add(stepOne);                window.add(oneHours);        window.add(twoHours);        window.add(fourHours);                stepTwo = new JLabel("Step2: Insert money ");        window.add(stepTwo);                priceText = new JTextField(5);        window.add(priceText); //Input dollars                        window.add(printTicket);        window.add(getMoneyBack);                JTextArea output;        output = new JTextArea(10,20);        window.add(output);        Date date = new Date();        //date.setTime(System.currentTimeMillis());        output.append("##################\n");        output.append("#Camosun's Parking\n");        output.append("# " + date.toString() + "\n");        //int price = 269;        output.append("# " + moneyInserted + " dollars.\n");        output.append("##################\n");        output.append("\n");                oneHours.addActionListener(this);        twoHours.addActionListener(this);        fourHours.addActionListener(this);        printTicket.addActionListener(this);        getMoneyBack.addActionListener(this);            }        public void actionPerformed(ActionEvent event) {        Object source = event.getSource();                if (source == oneHours) {            myTicketMachine.setTicketPrice(2);        }else if (source == twoHours){            myTicketMachine.setTicketPrice(4);        }else if (source == fourHours){            myTicketMachine.setTicketPrice(8);        }else if (source == printTicket){	    myTicketMachine.printTicket(output);	}    }            public class TicketMachine {            /*Constructor: Constructs TicketMachine object. Initializes the ticket    price, balance of money inserted and total amount collected to zero.*/                int ticketPrice;  //instance variables        int moneyInserted;        int moneyBack;        int totalMoney;                        public TicketMachine() { //constructor                        ticketPrice = 0;            moneyInserted = 0;            moneyBack = 0;            totalMoney = 0;        }                /* Sets the ticket price to the value send by the parameter*/        public void setTicketPrice(int price){            ticketPrice=price;        }                /*Returns the current price of the ticket */        public int getTicketPrice(){            return ticketPrice;        }                /*Returns the amount of money inserted by the user for a single ticket.*/        public int getBalance(){            String m1 = priceText.getText();            moneyBack= Integer.parseInt(m1);            return moneyBack;        }            /*Receives the amount of money in dollars from a customer. Increments the balance of the amount inserted    by the amount send by parameter, providing that amount inserted is positive.*/        public void insertMoney(int amount){            if (amount > 0){                moneyInserted = moneyInserted + amount;            }        }            /* Prints the ticket providing that enough money has been inserted and that ticket price is not zero.    After the ticket is printed the total amount collected is incremented by the price of the ticket, the current    balance is reduced by the ticket price and the ticket price is set back to zero. If amount inserted is smaller    then the ticket price a message is displayed asking for the remaining amount.    Receives: JTextArea object to print the ticket or the error message*/        	public void printTicket(JTextArea output){            if (moneyInserted>=ticketPrice){                // somehow we print?                moneyInserted=moneyInserted+ticketPrice;                moneyBack=moneyBack-ticketPrice;                ticketPrice=0;            }else{                moneyInserted = (-1)*(moneyInserted-ticketPrice);                //display moneyInserted                    }                /*Displays the balance amount. Clears the balance to zero.*/        public int refundBalance(){            if (moneyInserted>=ticketPrice){                // somehow we print?                moneyInserted=moneyInserted+ticketPrice;                moneyBack=moneyBack-ticketPrice;                ticketPrice=0;            }else{                moneyInserted = (-1)*(moneyInserted-ticketPrice);                //display moneyInserted            }        return moneyBack;        }    }}