Ok so We were given a example GUI on a Gas Station Pump. It was just one gas sta
ID: 3648410 • Letter: O
Question
Ok so We were given a example GUI on a Gas Station Pump. It was just one gas station on one window. I have to use that one gas station GUI and upgrade it to:> Make least 3 gas pumps for the gas station. Each gas pump is in a separate window. A customer can select any available gas pump to purchase gas.
>Create a separate window for the central office to monitor and display the activities at each gas pump, including gas type, gas amount and purchase total. The window must also have a emergency shut-off button for each gas pump.
the One gas station pump what i have so far is (I have notes on the side of some stuff that help me know what it does.) I am not allowed to use any GUI tool for this assignment. However, I can use everything in Java Swing to improve the GUI. :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GasPump extends JFrame implements ActionListener
{
private Container contentPane;
private JPanel controlPanel;
private JPanel displayPanel;
private JPanel numberPanel;
private JPanel gasPanel;
private JTextField messageField;
private JTextField gasAmountField;
private JTextField payField;
private JTextField unitPriceField;
private JButton regularButton;
private JButton plusButton;
private JButton premiumButton;
private JButton startButton;
private JButton stopButton;
private JButton resetButton;
private final double REGULAR_PRICE = 3.45;
private final double PLUS_PRICE = 3.6;
private final double PREMIUM_PRICE = 3.95;
private double gasAmount;
private double pay;
private boolean stopButtonPressed;
private Timer delay;
public static void main (String [ ] args)
{
GasPump pump1 = new GasPump ( );
pump1.setVisible (true);
pump1.buttonControl (3);
}
public GasPump ( )
{
setSize (500,300); //set size
setTitle ("pump 1"); //name of application
contentPane = getContentPane ( ); //the whole panel
setDefaultCloseOperation (EXIT_ON_CLOSE); //Exit on close will kill the program on close because only one window is being used
contentPane.setLayout (new BorderLayout ( )); // the whole space is using a border layout, which consists of north, south, east, west, and center.
displayPanel = new JPanel ( ); //introduces the display panel
controlPanel = new JPanel ( ); //introduces the control panel
contentPane.add (controlPanel, BorderLayout.EAST); //tells the program that the control panel goes on the EAST side.
contentPane.add (displayPanel, BorderLayout.CENTER); //tells the program that the display panel is in the CENTER.
displayPanel.setLayout (new GridLayout (3,1)); //in the DISPLAY panel which is in the center, it creates a grid with 3 panels: 3 rows, only 1 column.
numberPanel = new JPanel ( ); //first row is a number panel which outputs the results
displayPanel.add (numberPanel); //this added the number panel to the DISPLAY panel
gasPanel = new JPanel ( ); //Second row is the gas panel, which allows you too choose the type of gas
displayPanel.add (gasPanel); //adds the gas panel to the Display
messageField = new JTextField ( ); //Third row is the message field, it is a TEXTFIELD, unlike the PAnels. IT displays a message. ITs NOT a panel, its nothing.
displayPanel.add (messageField); //adds the message field to the DISPLAY
//this focuses on the number panel ONLY
gasAmountField = new JTextField (10); //this makes the first text field and the 10 is the size
gasAmountField.setEditable (false); // allows you to make it editable or not.
payField = new JTextField ( 10); //this makes the second pay field and the size is 10
payField.setEditable (false); //same as above
unitPriceField = new JTextField (10); //makes a textfield which is a unit price field
unitPriceField.setEditable (false); //same as before
//adds the textfields to the number panel
numberPanel.add (gasAmountField); //adds
numberPanel.add (payField);
numberPanel.add (unitPriceField);
//focuses ONLY in the second panel which is the GAS PANEL
regularButton = new JButton ("regular"); //you created the regular button
regularButton.addActionListener (this); //the regular button is added to the ACTION LISTENTER
plusButton = new JButton ("plus"); //creates the plus button
plusButton.addActionListener (this); //adds to the action listener
premiumButton = new JButton ("premium"); //creates a the premium button
premiumButton.addActionListener (this); //adds it to the action listener
//These adds all the buttons to the panel
gasPanel.add (regularButton);
gasPanel.add (plusButton);
gasPanel.add (premiumButton);
//ONLY FOCUS ON CONTROL PANEL WHICH IS ON THE EAST
controlPanel.setLayout (new GridLayout (3,1)); //creates the grid, which has 3 rows, and 1 column
//All this will be added in ORDER, so the first thing is start, then stop, then reset.
startButton = new JButton ("start"); //creates a start button
startButton.addActionListener (this); //adds a action listener to the start button
controlPanel.add (startButton); //adds the start button to the control panel display
stopButton = new JButton ("stop"); //creates a stop button
controlPanel.add (stopButton); //adds to the control panel
stopButton.addActionListener (this); //adds the action listener
resetButton = new JButton ("reset"); //adds the reset button
controlPanel.add (resetButton); //adds to the control panel
resetButton.addActionListener (this); //adds action listener
messageField.setText ("welcome to pump1.... please choose gas type..."); //this was the message field created in the display panel. the words in quotes are the message
}
public void actionPerformed (ActionEvent x)
{
String which = x.getActionCommand ( );
if ((which.equals ("regular")) || (which.equals ("plus")) || (which.equals ("premium"))) //algorithm, if the person clicks on regular, or plus, or premium.
{
if (which.equals ("regular")) //if the person clicks on regular then it does this.
{
unitPriceField.setText (new Double (REGULAR_PRICE).toString ( )); //puts in the numbers in the unit price fields
messageField.setText ("gas type: regular"); //this changes the message in the test field to : gas type: regular
}
else if (which.equals ("plus"))
{
unitPriceField.setText (new Double (PLUS_PRICE).toString ( ));
messageField.setText ("gas type:plus");
}
else if (which.equals ("premium"))
{
unitPriceField.setText (new Double (PREMIUM_PRICE).toString ( ));
messageField.setText ("gas type: premium");
}
buttonControl (2); //calls teh button control method, which will allow the user to not allow user to click on some buttons.
resetGasAmountAndPay ( ); //calls the resetGasAmountMethod
}
else if (which.equals ("reset")) //click on this, calls the reset method
{
doReset ( );
}
else if (which.equals ("start")) //click on this, calls the start method
{
doStart ( );
}
else if (which.equals ("stop")) //click on this, calls the stop method
{
doStop ( );
}
}
public void buttonControl (int what) //controls what buttons can be clicked
{
switch (what)
{
case 3: startButton.setEnabled (false);
stopButton.setEnabled (false);
regularButton.setEnabled (true);
plusButton.setEnabled (true);
premiumButton.setEnabled (true);
break;
case 2: startButton.setEnabled (true);
stopButton.setEnabled (false);
resetButton.setEnabled (true);
regularButton.setEnabled (true);
plusButton.setEnabled (true);
premiumButton.setEnabled (true);
break;
case 0: startButton.setEnabled (false);
stopButton.setEnabled (true);
resetButton.setEnabled (false);
regularButton.setEnabled (false);
plusButton.setEnabled (false);
premiumButton.setEnabled (false);
break;
case 1: startButton.setEnabled (true);
stopButton.setEnabled (false);
resetButton.setEnabled (true);
regularButton.setEnabled (false);
plusButton.setEnabled (false);
premiumButton.setEnabled (false);
break;
}
}
public void doReset ( )
{
messageField.setText ("welcome to pump1.... please choose gas type...");
unitPriceField.setText ("");
payField.setText ("");
gasAmountField.setText ("");
buttonControl (3);
}
public void doStart ( ) //what happens when start is pressed
{
buttonControl (0); //does the 0 option in button control
resetGasAmountAndPay ( ); //this method will be used when you press reset
stopButtonPressed = false;
//using a local "ActionListener" independent of the global one in this class
//"delay.start" starts the delay for 0.1 seconds, after that, an event is fired
// to stop the timer (delay.stop) and update the "pay" and "gasAmount" fields
//if during this time, "stop" is pressed, exit this transaction
//the # of repetitions in the for loop does not affect the loop, which is
//rather conrolled by the "return" , why? yet to find out......but it works perfectly
for (int i = 1; i <= 10; i++)
{
delay = new Timer (100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
delay.stop ( );
if (stopButtonPressed)
return;
gasAmount += 1;
//regular for now, but uou ned to change it based on what the client selects
pay = (REGULAR_PRICE * gasAmount);
gasAmountField.setText (new Double (gasAmount).toString ( ));
payField.setText (new Double (pay).toString ( ));
} //end actionPerformed
});
delay.start ( );
}
}
public void doStop ( )
{
buttonControl (1);
stopButtonPressed = true;
}
public void resetGasAmountAndPay ( )
{
gasAmountField.setText ("0.00");
payField.setText ("0.00");
}
}
Explanation / Answer
import java.util.Random; public class Office { public static void main (String [ ] args) { double regularTank = 100; double plusTank = 100; double primiumTank = 100; GasPump pump1 = new GasPump ( ); GasPump2 pump2 = new GasPump2 ( ); Random gen = new Random (); //asuumint gas tanks always have gas, will upgrade later do { int whatHappens = gen.nextInt (100); if (whatHappens == 5) { System.out.println ("gas station closed"); break; } else if ((whatHappens % 10) == 0) { if (pump1.pumpAvailable ()) pump1.customerArrival (); else System.out.println ("a new customer arrives at pump 1, but is turned away, gas pump is busy"); } else { if (pump1.pumpAvailable ()) System.out.println ("no customer; waiting........."); } if (!pump1.pumpAvailable()) { if (pump1.updateClock () == 0) pump1.saleComplete (); } } while (true); do system.exit(0) } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.