Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GasStationGUI, Michael Mohamed
- //ICS4M0
- //February 18th 2009
- //A GUI based 'Gas Station' applet
- //NOTES:
- //1. Although the options for regular or waxed washing are checkboxes, when
- //added to a CheckboxGroup... they essentially become RadioButtons...
- //This makes sense though because you wouldn't really have both at once.
- //2. The RadioButtons for the fuel use discounting have been locked to the appropriate states.
- import java.applet.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.text.*;
- import java.util.*;
- class GasStationGUI //The application class.
- {
- public static void main (String[] args) //The main method for the application
- {
- GasStation applet = new GasStation ();
- }
- }
- class GasStation extends JFrame implements ActionListener, ItemListener
- {
- //Here be variables - Alter with Caution :o
- public static Container container;
- private Image logo;
- public double washTotal = 0, gasTotal = 0, netTotal = 0, gradeMult = 0, litres = 0, washType = 10, subTotal;
- public boolean noneSelected = true;
- public String gasMessage, gradeString, gasOptString = "No options", oilCheckString = "", shieldCleanString = "", washerFluidString = "", washOptString = "None";
- public JLabel mikesGUI = new JLabel ((" Michael Mohamed's GasStationGUI"));
- public JLabel teacher = new JLabel (" Mr. R. Prins");
- public JButton mainBut = new JButton ("Main Screen");
- public JButton totBut = new JButton ("Total");
- public JButton exitBut = new JButton ("Exit");
- public JButton carWashBut = new JButton ("Car Wash");
- public JButton gasCalc = new JButton ("Purchase Gas");
- public JButton gasClr = new JButton ("Clear Gas Choices");
- public JLabel gasTitleLabel = new JLabel (" Welcome to Mike's Gas Station!");
- Panel butPan, y, x;
- public JLabel gradeLabel = new JLabel (" Grade of Gas?");
- public JRadioButton gold = new JRadioButton ("Gold (1.5x)", true);
- public JRadioButton silver = new JRadioButton ("Silver (1.25x)");
- public JRadioButton bronze = new JRadioButton ("Bronze (1.0x)");
- public ButtonGroup gasGrade = new ButtonGroup ();
- public JRadioButton selfServe = new JRadioButton ("Self Serve (No Charge)", true);
- public JRadioButton fullServe = new JRadioButton ("Full Serve ($10 fee)");
- public JLabel serveLabel = new JLabel (" Service Style");
- private ButtonGroup serveGroup = new ButtonGroup ();
- public JLabel litresLabel = new JLabel (" Litres of Gas Purchased ($0.80/L)");
- public JLabel titleLabel = new JLabel (" Mohamdu's Carwash");
- public JLabel classLabel = new JLabel (" ICS 4M0");
- public JLabel dateLabel = new JLabel (" February 20th, 2009");
- public JLabel blank = new JLabel (" ");
- public JTextField litresField = new JTextField ("0", 15);
- public JLabel fullOptions = new JLabel (" Full Serve Options");
- public Checkbox oilCheck = new Checkbox ("Oil Check", false);
- public Checkbox washerFluid = new Checkbox ("Washer Fluid", false);
- public Checkbox shieldClean = new Checkbox ("Windshield Cleaning", false);
- public CheckboxGroup optionsGroup = new CheckboxGroup ();
- public JLabel fuelLabel = new JLabel (" Fuel Purchased?");
- public JRadioButton fuelYes = new JRadioButton ("Yes", false);
- public JRadioButton fuelNo = new JRadioButton ("No", true);
- public ButtonGroup fuelGroup = new ButtonGroup ();
- public JLabel washTitle = new JLabel (" Welcome to the Car Wash!");
- public JLabel washLabel = new JLabel (" Wash Type?");
- public CheckboxGroup washGroup = new CheckboxGroup ();
- public Checkbox regWash = new Checkbox ("Regular Wash", true, washGroup);
- public Checkbox waxWash = new Checkbox ("Wash and Wax", false, washGroup);
- public JButton washCalc = new JButton ("Calculate Car Wash Fees");
- public JButton washClr = new JButton ("Clear Wash Values");
- public JLabel totalTitle = new JLabel (" Please take a look at your transaction record");
- public JLabel gasTotalLabel = new JLabel (" Your Fuel Total: ");
- public JLabel washTotalLabel = new JLabel (" Your Car Wash Total: ");
- public JLabel netTotalLabel = new JLabel (" Your Net Total (Include Tax): ");
- public JTextField gasTotalField = new JTextField ("$0");
- public JTextField washTotalField = new JTextField ("$0");
- public JTextField netTotalField = new JTextField ("$0");
- public JLabel gasOptLabel = new JLabel (" Full serve options:");
- public JLabel washOptLabel = new JLabel (" Car wash options:");
- public JTextField gasOptField = new JTextField ("No options");
- public JTextField washOptField = new JTextField ("None.");
- public JLabel subTotalLabel = new JLabel (" Your subtotal:");
- public JTextField subTotalField = new JTextField ("$0");
- public JButton feesBut = new JButton ("Pay your fees?");
- public JButton restartBut = new JButton ("Restart transaction?");
- public JTextField washPrice = new JTextField ("$0");
- public JLabel washSubTotal = new JLabel (" Your car wash subtotal:");
- //From here on in, it's all smooth sailing :D
- public GasStation () //main applet method
- {
- super ("Gas Station GUI"); //set title
- setSize (1100, 300);
- //Create the main panel for buttons
- butPan = new Panel ();
- butPan.setLayout (new GridLayout (5, 1));
- butPan.add (mikesGUI);
- butPan.add (mainBut);
- butPan.add (carWashBut);
- butPan.add (totBut);
- butPan.add (exitBut);
- //set the ActionListener's for the buttons
- feesBut.addActionListener (this);
- restartBut.addActionListener (this);
- mainBut.addActionListener (this);
- carWashBut.addActionListener (this);
- totBut.addActionListener (this);
- exitBut.addActionListener (this);
- oilCheck.addItemListener (this);
- washerFluid.addItemListener (this);
- shieldClean.addItemListener (this);
- fuelYes.addActionListener (this);
- fuelNo.addActionListener (this);
- gasCalc.addActionListener (this);
- gasClr.addActionListener (this);
- selfServe.addActionListener (this);
- regWash.addItemListener (this);
- waxWash.addItemListener (this);
- washClr.addActionListener (this);
- washCalc.addActionListener (this);
- //Create the primary container
- container = getContentPane ();
- container.setLayout (new GridLayout (1, 2));
- //Adds buttons to the side
- container.add (butPan);
- //Starts up the title screen
- titleScreen ();
- setVisible (true);
- }
- public void titleScreen () //Opening Layout - Only shown once.
- {
- //Initialize the panel, set panel for removal and replacement.
- Panel x = new Panel ();
- y = x;
- x.setLayout (new GridLayout (13, 1));
- x.add (blank);
- x.add (blank);
- x.add (blank);
- x.add (blank);
- x.add (titleLabel);
- x.add (classLabel);
- x.add (dateLabel);
- x.add (teacher);
- validate ();
- container.add (x);
- setVisible (true);
- }
- public void mainScreen () //Designing the components for the gas purchasing screen
- {
- //Initialize the panel, set panel for removal and replacement.
- Panel x = new Panel ();
- y = x;
- x.setLayout (new GridLayout (6, 1));
- //Set gas title layout
- Panel gasTitle = new Panel ();
- gasTitle.setLayout (new GridLayout (1, 1));
- gasTitle.add (gasTitleLabel);
- //Set gas grade panel
- Panel gradePan = new Panel ();
- gradePan.setLayout (new GridLayout (1, 4));
- gradePan.add (gradeLabel);
- gradePan.add (gold);
- gradePan.add (silver);
- gradePan.add (bronze);
- gasGrade.add (gold);
- gasGrade.add (silver);
- gasGrade.add (bronze);
- //Set input for gas litres
- Panel litrePan = new Panel ();
- litrePan.setLayout (new GridLayout (1, 2));
- litrePan.add (litresLabel);
- litrePan.add (litresField);
- //Set service style selection panel
- Panel servePan = new Panel ();
- servePan.setLayout (new GridLayout (1, 3));
- serveGroup.add (selfServe);
- serveGroup.add (fullServe);
- servePan.add (serveLabel);
- servePan.add (selfServe);
- servePan.add (fullServe);
- //Add the 'Full Serve Options' panel
- Panel optionPan = new Panel ();
- optionPan.setLayout (new GridLayout (1, 4));
- optionPan.add (fullOptions);
- optionPan.add (oilCheck);
- optionPan.add (washerFluid);
- optionPan.add (shieldClean);
- //Add the Calculate and Clear Button panel
- Panel gasButPan = new Panel ();
- gasButPan.setLayout (new GridLayout (1, 2));
- gasButPan.add (gasCalc);
- gasButPan.add (gasClr);
- //Add the various sub-panels into the main gas screen panel
- x.add (gasTitle);
- x.add (gradePan);
- x.add (litrePan);
- x.add (servePan);
- x.add (optionPan);
- x.add (gasButPan);
- validate (); // :I
- container.add (x); //Adds the main panel to the main container
- setVisible (true);
- }
- public void washScreen ()
- {
- //Initialize the panel, set panel for removal and replacement.
- Panel x = new Panel ();
- y = x;
- x.setLayout (new GridLayout (5, 1));
- //Create the 'Fuel Purchased' panel and components
- Panel purchasedPan = new Panel ();
- purchasedPan.setLayout (new GridLayout (1, 3));
- purchasedPan.add (fuelLabel);
- purchasedPan.add (fuelYes);
- purchasedPan.add (fuelNo);
- fuelGroup.add (fuelYes);
- fuelGroup.add (fuelNo);
- //Create the 'Wash type' panel
- Panel washTypePan = new Panel ();
- washTypePan.setLayout (new GridLayout (1, 3));
- washTypePan.add (washLabel);
- washTypePan.add (regWash);
- washTypePan.add (waxWash);
- //Create the 'Wash Buttons' panel
- Panel washButPan = new Panel ();
- washButPan.setLayout (new GridLayout (1, 2));
- washButPan.add (washCalc);
- washButPan.add (washClr);
- //Create the Wash text field panel
- Panel washPricePan = new Panel ();
- washPricePan.setLayout (new GridLayout (1, 2));
- washPricePan.add (washSubTotal);
- washPricePan.add (washPrice);
- //These are used in locking the fuel discount options
- //Add all the panels to the main panel
- x.add (washTitle);
- x.add (purchasedPan);
- x.add (washTypePan);
- x.add (washButPan);
- x.add (washPricePan);
- validate (); // :D :D :D
- //Adds main panel to the main container
- container.add (x);
- setVisible (true);
- }
- public void actionPerformed (ActionEvent e) throws ClassCastException, NumberFormatException //The Action Listener response method
- {
- if (e.getSource () == mainBut) //Switch to main fuel screen
- {
- remove (y);
- mainScreen ();
- }
- if (e.getSource () == carWashBut) //Switch to main car wash screen
- {
- remove (y);
- washScreen ();
- }
- if (e.getSource () == totBut) //Switch to the total screen
- {
- remove (y);
- totalScreen ();
- }
- if (e.getSource () == selfServe) //Makes sure that if Self serve is chosen, any Full serve options are reset
- {
- oilCheck.setState (false);
- washerFluid.setState (false);
- shieldClean.setState (false);
- }
- if (e.getSource () == exitBut) //Provides a goodbye message, quits.
- {
- //Insert some JDialog
- JOptionPane.showMessageDialog (null, "Thank you for visiting Mohamdu's Gas Station!");
- System.exit (0);
- }
- if (e.getSource () == gasCalc) //Calculates gas, sets options variables, calls sets the total screen. Buggy.
- {
- if (gold.isSelected ())
- gradeMult = 1.5;
- else if (silver.isSelected ())
- gradeMult = 1.25;
- else if (bronze.isSelected ())
- gradeMult = 1;
- try //Catches any non-number input.
- {
- litres = Double.parseDouble (litresField.getText ());
- }
- catch (NumberFormatException event)
- {
- JOptionPane.showMessageDialog (null, "Please do not enter any unnecessary symbols, numbers only");
- litresField.setText ("0");
- }
- if (litres > 0)
- {
- gasTotal = litres * 0.8 * gradeMult;
- if (fullServe.isSelected ())
- gasTotal += 10;
- gasTotal = roundUp (gasTotal);
- if (litres > 0)
- fuelYes.setSelected (true);
- else if (litres == 0)
- fuelNo.setSelected (true);
- setTotal ();
- JOptionPane.showMessageDialog (null, "You're gas subtotal has come to $" + gasTotal + ".\n" + "Check the Total page for more information and checkout.");
- }
- //Ensures that negative litres are paid for...
- else if (litres < 0)
- {
- JOptionPane.showMessageDialog (null, "You're GIVING us gas? Why thank you :D");
- litres = 0.000001;
- }
- else if (litres == 0)
- JOptionPane.showMessageDialog (null, "You did not purchase any gas. Please purchase gas or stop loitering and leave.");
- }
- //These next two 'ifs' lock the 'Fuel Purchased' choice to be in the appropriate state
- if (e.getSource () == fuelYes)
- {
- if (gasTotal == 0)
- fuelNo.setSelected (true);
- }
- if (e.getSource () == fuelNo)
- if (gasTotal > 0)
- fuelYes.setSelected (true);
- if (e.getSource () == washCalc) //Calculates pricing for the car wash
- {
- if (fuelYes.isSelected ())
- washTotal = 20;
- else if (fuelNo.isSelected ())
- washTotal = 30;
- washTotal += washType;
- washPrice.setText ("$" + washTotal);
- setTotal ();
- }
- if (e.getSource () == gasClr) //Resets the gas bar values if pushed.
- {
- gasTotal = 0;
- litres = 0;
- litresField.setText ("0.0");
- gold.setSelected (true);
- selfServe.setSelected (true);
- fuelNo.setSelected (true);
- shieldClean.setState (false);
- washerFluid.setState (false);
- oilCheck.setState (false);
- setTotal ();
- }
- if (e.getSource () == washClr) //Resets car wash values if pushed.
- {
- washTotal = 0;
- washPrice.setText ("$" + washTotal);
- regWash.setState (true);
- washType = 10;
- setTotal ();
- }
- if (e.getSource () == restartBut) //Resets EVERYTHING if pushed.
- {
- litres = 0;
- washTotal = 0;
- gasTotal = 0;
- litresField.setText ("" + litres);
- washPrice.setText ("$" + washTotal);
- gold.setSelected (true);
- selfServe.setSelected (true);
- fuelNo.setSelected (true);
- regWash.setState (true);
- noneSelected = true;
- oilCheck.setState (false);
- washerFluid.setState (false);
- shieldClean.setState (false);
- gasOptString = "No options.";
- setTotal ();
- }
- if (e.getSource () == feesBut) //Says thank you, closes window, creates new window. Pretty buggy.
- {
- JOptionPane.showMessageDialog (null, "We kindly appreciate your service!");
- dispose ();
- new GasStation ();
- }
- }
- //An attempt at a logo...
- // public void paint (Graphics g)
- // {
- // try
- // {
- // logo = getImage (getDocumentBase (), "logo.gif");
- // g.drawImage (logo, 140, 160, size ().width - 290, size ().height - 290, this);
- // }
- // catch (Exception e)
- // {
- // }
- // }
- public void setTotal () //Set the total screen properties - works well for on the fly calculations and such
- {
- subTotal = washTotal + gasTotal;
- netTotal = roundUp (subTotal * 1.13);
- //Checks to see which options were chosen for full serve
- if (noneSelected == true)
- gasOptString = "No options.";
- else if (noneSelected == false)
- gasOptString = (oilCheckString + washerFluidString + shieldCleanString);
- //Sets values
- gasTotalField.setText ("$" + gasTotal);
- washTotalField.setText ("$" + washTotal);
- netTotalField.setText ("$" + netTotal);
- subTotalField.setText ("$" + subTotal);
- gasOptField.setText (gasOptString);
- //Checks to see if car wash was chosen, sets appropriate values
- if (washTotal > 0)
- washOptField.setText (washOptString);
- if (washTotal == 0)
- washOptField.setText ("None.");
- }
- public void totalScreen () //Set up the total screen
- {
- //Initialize the panel, set panel for removal and replacement.
- Panel x = new Panel ();
- y = x;
- x.setLayout (new GridLayout (8, 1));
- x.add (totalTitle);
- //set up the Fuel Total panel
- Panel fuelOutput = new Panel ();
- fuelOutput.setLayout (new GridLayout (1, 2));
- fuelOutput.add (gasTotalLabel);
- fuelOutput.add (gasTotalField);
- x.add (fuelOutput);
- //set up the Wash Total panel
- Panel washOutput = new Panel ();
- washOutput.setLayout (new GridLayout (1, 2));
- washOutput.add (washTotalLabel);
- washOutput.add (washTotalField);
- x.add (washOutput);
- //set up the Gas Option panel
- Panel gasOptPan = new Panel ();
- gasOptPan.setLayout (new GridLayout (1, 2));
- gasOptPan.add (gasOptLabel);
- gasOptPan.add (gasOptField);
- x.add (gasOptPan);
- //set up the wash Option panel
- Panel washOptPan = new Panel ();
- washOptPan.setLayout (new GridLayout (1, 2));
- washOptPan.add (washOptLabel);
- washOptPan.add (washOptField);
- x.add (washOptPan);
- //set up the Subtotal panel
- Panel subOutput = new Panel ();
- subOutput.setLayout (new GridLayout (1, 2));
- subOutput.add (subTotalLabel);
- subOutput.add (subTotalField);
- x.add (subOutput);
- //set up the Net Total panel
- Panel netOutput = new Panel ();
- netOutput.setLayout (new GridLayout (1, 2));
- netOutput.add (netTotalLabel);
- netOutput.add (netTotalField);
- x.add (netOutput);
- //set up the total buttons panel
- Panel totalButPan = new Panel ();
- totalButPan.setLayout (new GridLayout (1, 2));
- totalButPan.add (feesBut);
- totalButPan.add (restartBut);
- x.add (totalButPan);
- container.add (x);
- validate ();
- setVisible (true);
- }
- public double roundUp (double num) //Some quick and dirty rounding
- {
- num = (Math.round (100 * num));
- num = num / 100;
- return num;
- }
- public void itemStateChanged (ItemEvent e) //The Item Listener response method
- {
- //Locks the full serve options
- if (fullServe.isSelected () == false)
- {
- oilCheck.setState (false);
- washerFluid.setState (false);
- shieldClean.setState (false);
- }
- if (regWash.getState ())
- {
- washType = 10;
- washOptString = "Regular Wash";
- }
- if (waxWash.getState ())
- {
- washType = 30;
- washOptString = "Wax and wash";
- }
- if (oilCheck.getState ())
- {
- oilCheckString = "Oil Check, ";
- noneSelected = false;
- }
- if (oilCheck.getState () == false)
- oilCheckString = "";
- if (washerFluid.getState ())
- {
- washerFluidString = "Fluid Check, ";
- noneSelected = false;
- }
- if (washerFluid.getState () == false)
- washerFluidString = "";
- if (shieldClean.getState ())
- {
- shieldCleanString = "Windshield Cleaned.";
- noneSelected = false;
- }
- if (shieldClean.getState () == false)
- shieldCleanString = "";
- //Makes sure at least one option is selected, otherwise just sets "no options"
- if ((shieldClean.getState () == false) && (oilCheck.getState () == false) && (washerFluid.getState () == false))
- noneSelected = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement