import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Garage2 extends JApplet implements ActionListener { //declare variables for labels, text fields and buttons JLabel welcome, prompt,amount; JTextField hoursTxt,price; JButton process, quit; public void init(){ // initialize applet Container c = getContentPane();//create an area for the applet c.setLayout (new FlowLayout());// set a layout for the objects //create objects to be on your applet welcome = new JLabel ("-Welcome to the City Central Car Park-"); welcome.setFont(new Font("Arial", Font.BOLD, 14)); prompt = new JLabel ("Please enter hours parked:"); hoursTxt = new JTextField(5); amount = new JLabel ("Amount Due is:"); price = new JTextField(7); process = new JButton("Process Payment"); quit = new JButton("Quit"); //add the objects to the applet c.add(welcome); c.add(prompt); c.add(hoursTxt); c.add(amount); c.add(price); price.setEditable(false); c.add(process); c.add(quit); process.addActionListener(this); hoursTxt.addActionListener(this); quit.addActionListener(this); price.addActionListener(this); } public void actionPerformed(ActionEvent evt){ // end program if quit button is clicked if(evt.getSource()== quit){ System.exit(0); } //process payment if process button is clicked if(evt.getSource() == process){ calculate(); } } public void calculate(){ // pass the contents of the text field into a variable String hoursStr = hoursTxt.getText(); //convert from string to double double hours = Double.parseDouble (hoursStr); //set variables for calculations double overpay=0, fixedRate = 0,max = 15.5,min = 3,total=0; while (hours < 0){ if (hours >= min){ overpay = ((hours - 3)*2); fixedRate = overpay + 5; price.setText(" " + fixedRate +" €"); hoursTxt.setText(""); hoursTxt.requestFocus(); } if (hours >= max){ price.setText(" 30.00 €"); hoursTxt.setText(""); hoursTxt.requestFocus(); fixedRate = 30.00; } if (hours < min){ price.setText(" 5.00 €"); hoursTxt.setText(""); hoursTxt.requestFocus(); fixedRate = 5.00; } customer++; //show results in the status bar showStatus ("Customers today: " + customer +" Total: "+fixedRate+" €"); } } }
This post has been edited by William_Wilson: 05 April 2007 - 08:33 AM