/*
program:AcmeSelfServe
name: Jacob Bosiljevac
date: July.2014
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.text.DateFormat;
import java.util.Date;
public class AcmeSelfServe extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
DecimalFormat decimalFormat;
DateFormat dateFormat;
SalesAmount salesAmount;
JRadioButton regularJRadioButton;
JLabel regularJLabel;
JRadioButton premiumJRadioButton;
JLabel premiumJLabel;
JRadioButton superJRadioButton;
JLabel superJLabel;
ButtonGroup displayButtonGroup;
JLabel numberOfGallonsJLabel;
JTextField numberOfGallonsJTextField;
JLabel totalJLabel;
JTextField totalJTextField;
// controls
JButton enterJButton;
JButton clearJButton;
JButton closeJButton;
// variables & constants
float saleAmount;
int quantityOfItem;
float gasRate;
double salesSubTotal;
float totalAmountOfSale;
public AcmeSelfServe()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
// inputs
numberOfGallonsJLabel = new JLabel();
numberOfGallonsJLabel.setBounds(50, 50, 150, 20);
numberOfGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberOfGallonsJLabel.setText("Number of Gallons");
numberOfGallonsJLabel.setForeground(black);
numberOfGallonsJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(numberOfGallonsJLabel);
numberOfGallonsJTextField = new JTextField();
numberOfGallonsJTextField.setBounds(210, 50, 50, 20);
numberOfGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
numberOfGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
numberOfGallonsJTextField.setForeground(black);
numberOfGallonsJTextField.setBackground(white);
numberOfGallonsJTextField.setEditable(true);
contentPane.add(numberOfGallonsJTextField);
displayButtonGroup = new ButtonGroup();
regularJRadioButton = new JRadioButton();
regularJRadioButton.setBounds(100, 80, 150, 20);
regularJRadioButton.setText("Regular $3.55");
regularJRadioButton.setSelected(true);
regularJRadioButton.setForeground(black);
regularJRadioButton.setBackground(white);
displayButtonGroup.add(regularJRadioButton);
contentPane.add(regularJRadioButton);
premiumJRadioButton = new JRadioButton();
premiumJRadioButton.setBounds(100, 110, 150, 20);
premiumJRadioButton.setText("Premium $4.95");
premiumJRadioButton.setForeground(black);
premiumJRadioButton.setBackground(white);
displayButtonGroup.add(premiumJRadioButton);
contentPane.add(premiumJRadioButton);
superJRadioButton = new JRadioButton();
superJRadioButton.setBounds(100, 140, 150, 20);
superJRadioButton.setText("Super $5.25");
superJRadioButton.setForeground(black);
superJRadioButton.setBackground(white);
displayButtonGroup.add(superJRadioButton);
contentPane.add(superJRadioButton);
// output
totalJLabel = new JLabel();
totalJLabel.setBounds(50, 200, 100, 20);
totalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalJLabel.setText("Total Sale");
totalJLabel.setForeground(black);
totalJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(totalJLabel);
totalJTextField = new JTextField();
totalJTextField.setBounds(210, 200, 50, 20);
totalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalJTextField.setHorizontalAlignment(JTextField.CENTER);
totalJTextField.setForeground(black);
totalJTextField.setBackground(white);
totalJTextField.setEditable(false);
contentPane.add(totalJTextField);
// control
enterJButton = new JButton();
enterJButton.setBounds(50, 300, 80, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(140, 300, 80, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
closeJButton = new JButton();
closeJButton.setBounds(230, 300, 80, 20);
closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
closeJButton.setText("Close");
closeJButton.setForeground(black);
closeJButton.setBackground(white);
contentPane.add(closeJButton);
closeJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeJButtonActionPerformed(event);
}
}
);
setTitle("Acme Self Serve");
setSize( 400, 400 );
setVisible(true);
}
// main method
public static void main(String[] args)
{
AcmeSelfServe application = new AcmeSelfServe();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
try
{
quantityOfItem = Integer.parseInt(numberOfGallonsJTextField.getText());
getGasRate();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter number of gallons!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
numberOfGallonsJTextField.setText("");
numberOfGallonsJTextField.requestFocusInWindow();
}
}
public void getGasRate()
{
if(regularJRadioButton.isSelected())
{
double gasRate = 3.55;
}
if(premiumJRadioButton.isSelected())
{
double gasRate = 4.95;
}
if(superJRadioButton.isSelected())
{
double gasRate = 5.25;
}
calculateSubTotal();
}
public void calculateSubTotal()
{
salesAmount = new SalesAmount(saleAmount, quantityOfItem);
salesSubTotal = salesAmount.getSubTotal();
}
public void displaySales()
{
decimalFormat = new DecimalFormat("$0.00");
totalJTextField.setText("" + decimalFormat.format(saleAmount));
totalJTextField.setText("" + salesSubTotal);
}
public void clearJButtonActionPerformed(ActionEvent event)
{
numberOfGallonsJTextField.setText("");
numberOfGallonsJTextField.requestFocusInWindow();
totalJTextField.setText("");
regularJRadioButton.setSelected(true);
}
public void closeJButtonActionPerformed(ActionEvent event)
{
AcmeSelfServe.this.dispose();
}
}
class SalesAmount
{
int quantityOfItems;
float salesSubTotal;
float gasRate;
public SalesAmount(float saleValue, int quantityValue)
{
gasRate = saleValue;
quantityOfItems = quantityValue;
salesSubTotal = gasRate * quantityOfItems;
}
public float getSubTotal()
{
return salesSubTotal;
}
}