Advertisement
Guest User

iluvgrlzwglasses

a guest
Feb 21st, 2009
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.29 KB | None | 0 0
  1. //GasStationGUI, Michael Mohamed
  2. //ICS4M0
  3. //February 18th 2009
  4. //A GUI based 'Gas Station' applet
  5.  
  6. //NOTES:
  7. //1. Although the options for regular or waxed washing are checkboxes, when
  8. //added to a CheckboxGroup... they essentially become RadioButtons...
  9. //This makes sense though because you wouldn't really have both at once.
  10. //2. The RadioButtons for the fuel use discounting have been locked to the appropriate states.
  11. import java.applet.*;
  12. import javax.swing.*;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.text.*;
  16. import java.util.*;
  17. class GasStationGUI //The application class.
  18. {
  19.     public static void main (String[] args)  //The main method for the application
  20.     {
  21.         GasStation applet = new GasStation ();
  22.     }
  23. }
  24. class GasStation extends JFrame implements ActionListener, ItemListener
  25. {
  26.     //Here be variables - Alter with Caution :o
  27.     public static Container container;
  28.     private Image logo;
  29.     public double washTotal = 0, gasTotal = 0, netTotal = 0, gradeMult = 0, litres = 0, washType = 10, subTotal;
  30.     public boolean noneSelected = true;
  31.     public String gasMessage, gradeString, gasOptString = "No options", oilCheckString = "", shieldCleanString = "", washerFluidString = "", washOptString = "None";
  32.     public JLabel mikesGUI = new JLabel (("                                                           Michael Mohamed's GasStationGUI"));
  33.     public JLabel teacher = new JLabel ("    Mr. R. Prins");
  34.     public JButton mainBut = new JButton ("Main Screen");
  35.     public JButton totBut = new JButton ("Total");
  36.     public JButton exitBut = new JButton ("Exit");
  37.     public JButton carWashBut = new JButton ("Car Wash");
  38.     public JButton gasCalc = new JButton ("Purchase Gas");
  39.     public JButton gasClr = new JButton ("Clear Gas Choices");
  40.     public JLabel gasTitleLabel = new JLabel ("                                                            Welcome to Mike's Gas Station!");
  41.     Panel butPan, y, x;
  42.     public JLabel gradeLabel = new JLabel ("    Grade of Gas?");
  43.     public JRadioButton gold = new JRadioButton ("Gold (1.5x)", true);
  44.     public JRadioButton silver = new JRadioButton ("Silver (1.25x)");
  45.     public JRadioButton bronze = new JRadioButton ("Bronze (1.0x)");
  46.     public ButtonGroup gasGrade = new ButtonGroup ();
  47.     public JRadioButton selfServe = new JRadioButton ("Self Serve (No Charge)", true);
  48.     public JRadioButton fullServe = new JRadioButton ("Full Serve ($10 fee)");
  49.     public JLabel serveLabel = new JLabel ("    Service Style");
  50.     private ButtonGroup serveGroup = new ButtonGroup ();
  51.     public JLabel litresLabel = new JLabel ("    Litres of Gas Purchased ($0.80/L)");
  52.     public JLabel titleLabel = new JLabel ("    Mohamdu's Carwash");
  53.     public JLabel classLabel = new JLabel ("    ICS 4M0");
  54.     public JLabel dateLabel = new JLabel ("    February 20th, 2009");
  55.     public JLabel blank = new JLabel (" ");
  56.     public JTextField litresField = new JTextField ("0", 15);
  57.     public JLabel fullOptions = new JLabel ("    Full Serve Options");
  58.     public Checkbox oilCheck = new Checkbox ("Oil Check", false);
  59.     public Checkbox washerFluid = new Checkbox ("Washer Fluid", false);
  60.     public Checkbox shieldClean = new Checkbox ("Windshield Cleaning", false);
  61.     public CheckboxGroup optionsGroup = new CheckboxGroup ();
  62.     public JLabel fuelLabel = new JLabel ("    Fuel Purchased?");
  63.     public JRadioButton fuelYes = new JRadioButton ("Yes", false);
  64.     public JRadioButton fuelNo = new JRadioButton ("No", true);
  65.     public ButtonGroup fuelGroup = new ButtonGroup ();
  66.     public JLabel washTitle = new JLabel ("                                                                 Welcome to the Car Wash!");
  67.     public JLabel washLabel = new JLabel ("    Wash Type?");
  68.     public CheckboxGroup washGroup = new CheckboxGroup ();
  69.     public Checkbox regWash = new Checkbox ("Regular Wash", true, washGroup);
  70.     public Checkbox waxWash = new Checkbox ("Wash and Wax", false, washGroup);
  71.     public JButton washCalc = new JButton ("Calculate Car Wash Fees");
  72.     public JButton washClr = new JButton ("Clear Wash Values");
  73.     public JLabel totalTitle = new JLabel ("                                                     Please take a look at your transaction record");
  74.     public JLabel gasTotalLabel = new JLabel ("    Your Fuel Total: ");
  75.     public JLabel washTotalLabel = new JLabel ("    Your Car Wash Total: ");
  76.     public JLabel netTotalLabel = new JLabel ("    Your Net Total (Include Tax): ");
  77.     public JTextField gasTotalField = new JTextField ("$0");
  78.     public JTextField washTotalField = new JTextField ("$0");
  79.     public JTextField netTotalField = new JTextField ("$0");
  80.     public JLabel gasOptLabel = new JLabel ("    Full serve options:");
  81.     public JLabel washOptLabel = new JLabel ("    Car wash options:");
  82.     public JTextField gasOptField = new JTextField ("No options");
  83.     public JTextField washOptField = new JTextField ("None.");
  84.     public JLabel subTotalLabel = new JLabel ("    Your subtotal:");
  85.     public JTextField subTotalField = new JTextField ("$0");
  86.     public JButton feesBut = new JButton ("Pay your fees?");
  87.     public JButton restartBut = new JButton ("Restart transaction?");
  88.     public JTextField washPrice = new JTextField ("$0");
  89.     public JLabel washSubTotal = new JLabel ("    Your car wash subtotal:");
  90.     //From here on in, it's all smooth sailing :D
  91.  
  92.     public GasStation ()  //main applet method
  93.     {
  94.         super ("Gas Station GUI");  //set title
  95.         setSize (1100, 300);
  96.         //Create the main panel for buttons
  97.         butPan = new Panel ();
  98.         butPan.setLayout (new GridLayout (5, 1));
  99.         butPan.add (mikesGUI);
  100.         butPan.add (mainBut);
  101.         butPan.add (carWashBut);
  102.         butPan.add (totBut);
  103.         butPan.add (exitBut);
  104.         //set the ActionListener's for the buttons
  105.         feesBut.addActionListener (this);
  106.         restartBut.addActionListener (this);
  107.         mainBut.addActionListener (this);
  108.         carWashBut.addActionListener (this);
  109.         totBut.addActionListener (this);
  110.         exitBut.addActionListener (this);
  111.         oilCheck.addItemListener (this);
  112.         washerFluid.addItemListener (this);
  113.         shieldClean.addItemListener (this);
  114.         fuelYes.addActionListener (this);
  115.         fuelNo.addActionListener (this);
  116.         gasCalc.addActionListener (this);
  117.         gasClr.addActionListener (this);
  118.         selfServe.addActionListener (this);
  119.         regWash.addItemListener (this);
  120.         waxWash.addItemListener (this);
  121.         washClr.addActionListener (this);
  122.         washCalc.addActionListener (this);
  123.         //Create the primary container
  124.         container = getContentPane ();
  125.         container.setLayout (new GridLayout (1, 2));
  126.         //Adds buttons to the side
  127.         container.add (butPan);
  128.         //Starts up the title screen
  129.         titleScreen ();
  130.         setVisible (true);
  131.     }
  132.  
  133.  
  134.     public void titleScreen ()  //Opening Layout - Only shown once.
  135.     {
  136.         //Initialize the panel, set panel for removal and replacement.
  137.         Panel x = new Panel ();
  138.         y = x;
  139.         x.setLayout (new GridLayout (13, 1));
  140.         x.add (blank);
  141.         x.add (blank);
  142.         x.add (blank);
  143.         x.add (blank);
  144.         x.add (titleLabel);
  145.         x.add (classLabel);
  146.         x.add (dateLabel);
  147.         x.add (teacher);
  148.         validate ();
  149.         container.add (x);
  150.         setVisible (true);
  151.     }
  152.  
  153.  
  154.     public void mainScreen ()  //Designing the components for the gas purchasing screen
  155.     {
  156.         //Initialize the panel, set panel for removal and replacement.
  157.         Panel x = new Panel ();
  158.         y = x;
  159.         x.setLayout (new GridLayout (6, 1));
  160.         //Set gas title layout
  161.         Panel gasTitle = new Panel ();
  162.         gasTitle.setLayout (new GridLayout (1, 1));
  163.         gasTitle.add (gasTitleLabel);
  164.         //Set gas grade panel
  165.         Panel gradePan = new Panel ();
  166.         gradePan.setLayout (new GridLayout (1, 4));
  167.         gradePan.add (gradeLabel);
  168.         gradePan.add (gold);
  169.         gradePan.add (silver);
  170.         gradePan.add (bronze);
  171.         gasGrade.add (gold);
  172.         gasGrade.add (silver);
  173.         gasGrade.add (bronze);
  174.         //Set input for gas litres
  175.         Panel litrePan = new Panel ();
  176.         litrePan.setLayout (new GridLayout (1, 2));
  177.         litrePan.add (litresLabel);
  178.         litrePan.add (litresField);
  179.         //Set service style selection panel
  180.         Panel servePan = new Panel ();
  181.         servePan.setLayout (new GridLayout (1, 3));
  182.         serveGroup.add (selfServe);
  183.         serveGroup.add (fullServe);
  184.         servePan.add (serveLabel);
  185.         servePan.add (selfServe);
  186.         servePan.add (fullServe);
  187.         //Add the 'Full Serve Options' panel
  188.         Panel optionPan = new Panel ();
  189.         optionPan.setLayout (new GridLayout (1, 4));
  190.         optionPan.add (fullOptions);
  191.         optionPan.add (oilCheck);
  192.         optionPan.add (washerFluid);
  193.         optionPan.add (shieldClean);
  194.         //Add the Calculate and Clear Button panel
  195.         Panel gasButPan = new Panel ();
  196.         gasButPan.setLayout (new GridLayout (1, 2));
  197.         gasButPan.add (gasCalc);
  198.         gasButPan.add (gasClr);
  199.         //Add the various sub-panels into the main gas screen panel
  200.         x.add (gasTitle);
  201.         x.add (gradePan);
  202.         x.add (litrePan);
  203.         x.add (servePan);
  204.         x.add (optionPan);
  205.         x.add (gasButPan);
  206.         validate (); // :I
  207.         container.add (x); //Adds the main panel to the main container
  208.         setVisible (true);
  209.     }
  210.  
  211.  
  212.     public void washScreen ()
  213.     {
  214.         //Initialize the panel, set panel for removal and replacement.
  215.         Panel x = new Panel ();
  216.         y = x;
  217.         x.setLayout (new GridLayout (5, 1));
  218.         //Create the 'Fuel Purchased' panel and components
  219.         Panel purchasedPan = new Panel ();
  220.         purchasedPan.setLayout (new GridLayout (1, 3));
  221.         purchasedPan.add (fuelLabel);
  222.         purchasedPan.add (fuelYes);
  223.         purchasedPan.add (fuelNo);
  224.         fuelGroup.add (fuelYes);
  225.         fuelGroup.add (fuelNo);
  226.         //Create the 'Wash type' panel
  227.         Panel washTypePan = new Panel ();
  228.         washTypePan.setLayout (new GridLayout (1, 3));
  229.         washTypePan.add (washLabel);
  230.         washTypePan.add (regWash);
  231.         washTypePan.add (waxWash);
  232.         //Create the 'Wash Buttons' panel
  233.         Panel washButPan = new Panel ();
  234.         washButPan.setLayout (new GridLayout (1, 2));
  235.         washButPan.add (washCalc);
  236.         washButPan.add (washClr);
  237.         //Create the Wash text field panel
  238.         Panel washPricePan = new Panel ();
  239.         washPricePan.setLayout (new GridLayout (1, 2));
  240.         washPricePan.add (washSubTotal);
  241.         washPricePan.add (washPrice);
  242.         //These are used in locking the fuel discount options
  243.         //Add all the panels to the main panel
  244.         x.add (washTitle);
  245.         x.add (purchasedPan);
  246.         x.add (washTypePan);
  247.         x.add (washButPan);
  248.         x.add (washPricePan);
  249.         validate (); // :D :D :D
  250.         //Adds main panel to the main container
  251.         container.add (x);
  252.         setVisible (true);
  253.     }
  254.  
  255.  
  256.     public void actionPerformed (ActionEvent e) throws ClassCastException, NumberFormatException //The Action Listener response method
  257.     {
  258.         if (e.getSource () == mainBut) //Switch to main fuel screen
  259.         {
  260.             remove (y);
  261.             mainScreen ();
  262.         }
  263.         if (e.getSource () == carWashBut) //Switch to main car wash screen
  264.         {
  265.             remove (y);
  266.             washScreen ();
  267.         }
  268.         if (e.getSource () == totBut) //Switch to the total screen
  269.         {
  270.             remove (y);
  271.             totalScreen ();
  272.         }
  273.         if (e.getSource () == selfServe) //Makes sure that if Self serve is chosen, any Full serve options are reset
  274.         {
  275.             oilCheck.setState (false);
  276.             washerFluid.setState (false);
  277.             shieldClean.setState (false);
  278.         }
  279.         if (e.getSource () == exitBut) //Provides a goodbye message, quits.
  280.         {
  281.             //Insert some JDialog
  282.             JOptionPane.showMessageDialog (null, "Thank you for visiting Mohamdu's Gas Station!");
  283.             System.exit (0);
  284.         }
  285.         if (e.getSource () == gasCalc) //Calculates gas, sets options variables, calls sets the total screen. Buggy.
  286.         {
  287.             if (gold.isSelected ())
  288.                 gradeMult = 1.5;
  289.             else if (silver.isSelected ())
  290.                 gradeMult = 1.25;
  291.             else if (bronze.isSelected ())
  292.                 gradeMult = 1;
  293.             try //Catches any non-number input.
  294.             {
  295.                 litres = Double.parseDouble (litresField.getText ());
  296.             }
  297.             catch (NumberFormatException event)
  298.             {
  299.                 JOptionPane.showMessageDialog (null, "Please do not enter any unnecessary symbols, numbers only");
  300.                 litresField.setText ("0");
  301.             }
  302.             if (litres > 0)
  303.             {
  304.                 gasTotal = litres * 0.8 * gradeMult;
  305.                 if (fullServe.isSelected ())
  306.                     gasTotal += 10;
  307.                 gasTotal = roundUp (gasTotal);
  308.                 if (litres > 0)
  309.                     fuelYes.setSelected (true);
  310.                 else if (litres == 0)
  311.                     fuelNo.setSelected (true);
  312.                 setTotal ();
  313.                 JOptionPane.showMessageDialog (null, "You're gas subtotal has come to $" + gasTotal + ".\n" + "Check the Total page for more information and checkout.");
  314.             }
  315.             //Ensures that negative litres are paid for...
  316.             else if (litres < 0)
  317.             {
  318.                 JOptionPane.showMessageDialog (null, "You're GIVING us gas? Why thank you :D");
  319.                 litres = 0.000001;
  320.             }
  321.             else if (litres == 0)
  322.                 JOptionPane.showMessageDialog (null, "You did not purchase any gas. Please purchase gas or stop loitering and leave.");
  323.  
  324.         }
  325.         //These next two 'ifs' lock the 'Fuel Purchased' choice to be in the appropriate state
  326.         if (e.getSource () == fuelYes)
  327.         {
  328.             if (gasTotal == 0)
  329.                 fuelNo.setSelected (true);
  330.         }
  331.         if (e.getSource () == fuelNo)
  332.             if (gasTotal > 0)
  333.                 fuelYes.setSelected (true);
  334.         if (e.getSource () == washCalc) //Calculates pricing for the car wash
  335.         {
  336.             if (fuelYes.isSelected ())
  337.                 washTotal = 20;
  338.             else if (fuelNo.isSelected ())
  339.                 washTotal = 30;
  340.             washTotal += washType;
  341.             washPrice.setText ("$" + washTotal);
  342.             setTotal ();
  343.         }
  344.         if (e.getSource () == gasClr) //Resets the gas bar values if pushed.
  345.         {
  346.             gasTotal = 0;
  347.             litres = 0;
  348.             litresField.setText ("0.0");
  349.             gold.setSelected (true);
  350.             selfServe.setSelected (true);
  351.             fuelNo.setSelected (true);
  352.             shieldClean.setState (false);
  353.             washerFluid.setState (false);
  354.             oilCheck.setState (false);
  355.             setTotal ();
  356.         }
  357.         if (e.getSource () == washClr) //Resets car wash values if pushed.
  358.         {
  359.             washTotal = 0;
  360.             washPrice.setText ("$" + washTotal);
  361.             regWash.setState (true);
  362.             washType = 10;
  363.             setTotal ();
  364.         }
  365.         if (e.getSource () == restartBut) //Resets EVERYTHING if pushed.
  366.         {
  367.             litres = 0;
  368.             washTotal = 0;
  369.             gasTotal = 0;
  370.             litresField.setText ("" + litres);
  371.             washPrice.setText ("$" + washTotal);
  372.             gold.setSelected (true);
  373.             selfServe.setSelected (true);
  374.             fuelNo.setSelected (true);
  375.             regWash.setState (true);
  376.             noneSelected = true;
  377.             oilCheck.setState (false);
  378.             washerFluid.setState (false);
  379.             shieldClean.setState (false);
  380.             gasOptString = "No options.";
  381.             setTotal ();
  382.         }
  383.         if (e.getSource () == feesBut) //Says thank you, closes window, creates new window. Pretty buggy.
  384.         {
  385.             JOptionPane.showMessageDialog (null, "We kindly appreciate your service!");
  386.             dispose ();
  387.             new GasStation ();
  388.         }
  389.     }
  390.  
  391.  
  392.     //An attempt at a logo...
  393.     // public void paint (Graphics g)
  394.     // {
  395.     //     try
  396.     //     {
  397.     //         logo = getImage (getDocumentBase (), "logo.gif");
  398.     //         g.drawImage (logo, 140, 160, size ().width - 290, size ().height - 290, this);
  399.     //     }
  400.     //     catch (Exception e)
  401.     //     {
  402.     //     }
  403.     // }
  404.  
  405.  
  406.     public void setTotal ()  //Set the total screen properties - works well for on the fly calculations and such
  407.     {
  408.         subTotal = washTotal + gasTotal;
  409.         netTotal = roundUp (subTotal * 1.13);
  410.         //Checks to see which options were chosen for full serve
  411.         if (noneSelected == true)
  412.             gasOptString = "No options.";
  413.         else if (noneSelected == false)
  414.             gasOptString = (oilCheckString + washerFluidString + shieldCleanString);
  415.         //Sets values
  416.         gasTotalField.setText ("$" + gasTotal);
  417.         washTotalField.setText ("$" + washTotal);
  418.         netTotalField.setText ("$" + netTotal);
  419.         subTotalField.setText ("$" + subTotal);
  420.         gasOptField.setText (gasOptString);
  421.         //Checks to see if car wash was chosen, sets appropriate values
  422.         if (washTotal > 0)
  423.             washOptField.setText (washOptString);
  424.         if (washTotal == 0)
  425.             washOptField.setText ("None.");
  426.     }
  427.  
  428.  
  429.     public void totalScreen ()  //Set up the total screen
  430.     {
  431.         //Initialize the panel, set panel for removal and replacement.
  432.         Panel x = new Panel ();
  433.         y = x;
  434.         x.setLayout (new GridLayout (8, 1));
  435.         x.add (totalTitle);
  436.         //set up the Fuel Total panel
  437.         Panel fuelOutput = new Panel ();
  438.         fuelOutput.setLayout (new GridLayout (1, 2));
  439.         fuelOutput.add (gasTotalLabel);
  440.         fuelOutput.add (gasTotalField);
  441.         x.add (fuelOutput);
  442.         //set up the Wash Total panel
  443.         Panel washOutput = new Panel ();
  444.         washOutput.setLayout (new GridLayout (1, 2));
  445.         washOutput.add (washTotalLabel);
  446.         washOutput.add (washTotalField);
  447.         x.add (washOutput);
  448.         //set up the Gas Option panel
  449.         Panel gasOptPan = new Panel ();
  450.         gasOptPan.setLayout (new GridLayout (1, 2));
  451.         gasOptPan.add (gasOptLabel);
  452.         gasOptPan.add (gasOptField);
  453.         x.add (gasOptPan);
  454.         //set up the wash Option panel
  455.         Panel washOptPan = new Panel ();
  456.         washOptPan.setLayout (new GridLayout (1, 2));
  457.         washOptPan.add (washOptLabel);
  458.         washOptPan.add (washOptField);
  459.         x.add (washOptPan);
  460.         //set up the Subtotal panel
  461.         Panel subOutput = new Panel ();
  462.         subOutput.setLayout (new GridLayout (1, 2));
  463.         subOutput.add (subTotalLabel);
  464.         subOutput.add (subTotalField);
  465.         x.add (subOutput);
  466.         //set up the Net Total panel
  467.         Panel netOutput = new Panel ();
  468.         netOutput.setLayout (new GridLayout (1, 2));
  469.         netOutput.add (netTotalLabel);
  470.         netOutput.add (netTotalField);
  471.         x.add (netOutput);
  472.         //set up the total buttons panel
  473.         Panel totalButPan = new Panel ();
  474.         totalButPan.setLayout (new GridLayout (1, 2));
  475.         totalButPan.add (feesBut);
  476.         totalButPan.add (restartBut);
  477.         x.add (totalButPan);
  478.         container.add (x);
  479.         validate ();
  480.         setVisible (true);
  481.     }
  482.  
  483.  
  484.     public double roundUp (double num)  //Some quick and dirty rounding
  485.     {
  486.         num = (Math.round (100 * num));
  487.         num = num / 100;
  488.         return num;
  489.     }
  490.  
  491.  
  492.     public void itemStateChanged (ItemEvent e)  //The Item Listener response method
  493.     {
  494.         //Locks the full serve options
  495.         if (fullServe.isSelected () == false)
  496.         {
  497.             oilCheck.setState (false);
  498.             washerFluid.setState (false);
  499.             shieldClean.setState (false);
  500.         }
  501.         if (regWash.getState ())
  502.         {
  503.             washType = 10;
  504.             washOptString = "Regular Wash";
  505.         }
  506.         if (waxWash.getState ())
  507.         {
  508.             washType = 30;
  509.             washOptString = "Wax and wash";
  510.         }
  511.         if (oilCheck.getState ())
  512.         {
  513.             oilCheckString = "Oil Check, ";
  514.             noneSelected = false;
  515.         }
  516.         if (oilCheck.getState () == false)
  517.             oilCheckString = "";
  518.         if (washerFluid.getState ())
  519.         {
  520.             washerFluidString = "Fluid Check, ";
  521.             noneSelected = false;
  522.         }
  523.         if (washerFluid.getState () == false)
  524.             washerFluidString = "";
  525.         if (shieldClean.getState ())
  526.         {
  527.             shieldCleanString = "Windshield Cleaned.";
  528.             noneSelected = false;
  529.         }
  530.         if (shieldClean.getState () == false)
  531.             shieldCleanString = "";
  532.         //Makes sure at least one option is selected, otherwise just sets "no options"
  533.         if ((shieldClean.getState () == false) && (oilCheck.getState () == false) && (washerFluid.getState () == false))
  534.             noneSelected = true;
  535.     }
  536. }
  537.  
  538.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement