Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- author: kkosak
- program: PaintStore
- date: december.2016
- */
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.text.*;
- import java.text.DecimalFormat.*;
- public class PaintStore extends JFrame
- {
- //declarations
- Color black = new Color(0, 0, 0);
- Color white = new Color(255, 255, 255);
- JRadioButton deluxePaintJRadioButton;
- JRadioButton premiumPaintJRadioButton;
- JLabel amountOfGallonsJLabel;
- JTextField amountOfGallonsJTextField;
- JLabel amountOfSalesTaxJLabel;
- JTextField amountOfSalesTaxJTextField;
- JLabel totalAmountJLabel;
- JTextField totalAmountJTextField;
- JButton enterJButton;
- JButton clearJButton;
- JButton closeJButton;
- final float DELUXE_PAINT = 24.95f;
- final float PREMIUM_PAINT = 34.95f;
- final float TAX_RATE = .07f;
- float amountOfGallons;
- float paintPrice;
- float salesTotal;
- float totalAmount;
- ButtonGroup displayButtonGroup;
- DecimalFormat decimalFormat;
- PaintCalculator paintCalculator;
- public PaintStore()
- {
- createUserInterface();
- }
- public void createUserInterface()
- {
- Container contentPane = getContentPane();
- contentPane.setBackground(white);
- contentPane.setLayout(null);
- displayButtonGroup = new ButtonGroup();
- deluxePaintJRadioButton = new JRadioButton();
- deluxePaintJRadioButton.setBounds(150, 20, 100, 20);
- deluxePaintJRadioButton.setText("Deluxe Paint $24.95");
- deluxePaintJRadioButton.setSelected(true);
- deluxePaintJRadioButton.setForeground(black);
- deluxePaintJRadioButton.setBackground(white);
- displayButtonGroup.add(deluxePaintJRadioButton);
- contentPane.add(deluxePaintJRadioButton);
- premiumPaintJRadioButton = new JRadioButton();
- premiumPaintJRadioButton.setBounds(250, 20, 100, 20);
- premiumPaintJRadioButton.setText("Premium Paint $34.95");
- premiumPaintJRadioButton.setSelected(false);
- premiumPaintJRadioButton.setForeground(black);
- premiumPaintJRadioButton.setBackground(white);
- displayButtonGroup.add(premiumPaintJRadioButton);
- contentPane.add(deluxePaintJRadioButton);
- amountOfGallonsJLabel = new JLabel();
- amountOfGallonsJLabel.setBounds(100, 50, 30, 20);
- amountOfGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
- amountOfGallonsJLabel.setText("Amount of Gallons");
- amountOfGallonsJLabel.setForeground(black);
- amountOfGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
- contentPane.add(amountOfGallonsJLabel);
- amountOfGallonsJTextField = new JTextField();
- amountOfGallonsJTextField.setBounds(170, 50, 50, 20);
- amountOfGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
- amountOfGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
- amountOfGallonsJTextField.setForeground(black);
- amountOfGallonsJTextField.setBackground(white);
- amountOfGallonsJTextField.setEditable(true);
- contentPane.add(amountOfGallonsJTextField);
- amountOfSalesTaxJLabel = new JLabel();
- amountOfSalesTaxJLabel.setBounds(100, 100, 30, 20);
- amountOfSalesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
- amountOfSalesTaxJLabel.setText("Amount of Sales Tax");
- amountOfSalesTaxJLabel.setForeground(black);
- amountOfSalesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
- contentPane.add(amountOfSalesTaxJLabel);
- amountOfSalesTaxJTextField = new JTextField();
- amountOfSalesTaxJTextField.setBounds(170, 100, 50, 20);
- amountOfSalesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
- amountOfSalesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
- amountOfSalesTaxJTextField.setForeground(black);
- amountOfSalesTaxJTextField.setBackground(white);
- amountOfSalesTaxJTextField.setEditable(false);
- contentPane.add(amountOfSalesTaxJTextField);
- totalAmountJLabel = new JLabel();
- totalAmountJLabel.setBounds(100, 150, 30, 20);
- totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
- totalAmountJLabel.setText("Total Amount");
- totalAmountJLabel.setForeground(black);
- totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
- contentPane.add(totalAmountJLabel);
- totalAmountJTextField = new JTextField();
- totalAmountJTextField.setBounds(170, 150, 50, 20);
- totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
- totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
- totalAmountJTextField.setForeground(black);
- totalAmountJTextField.setBackground(white);
- totalAmountJTextField.setEditable(false);
- contentPane.add(totalAmountJTextField);
- enterJButton = new JButton();
- enterJButton.setBounds(30, 300, 100, 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(150, 300, 100, 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(270, 300, 100, 20);
- closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
- closeJButton.setText("Close");
- closeJButton.setForeground(black);
- closeJButton.setBackground(white);
- contentPane.add(clearJButton);
- clearJButton.addActionListener(
- new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- closeJButtonActionPerformed(event);
- }
- }
- );
- setTitle("PaintStore");
- setSize(400, 400);
- setVisible(true);
- }
- public static void main (String[] args)
- {
- PaintStore application = new PaintStore();
- application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void enterJButtonActionPerformed(ActionEvent event)
- {
- getAmountOfGallons();
- }
- public void getAmountOfGallons()
- {
- try
- {
- amountOfGallons = Float.parseFloat(amountOfGallonsJTextField.getText());
- numberOfGallonsNotZero();
- }
- catch(NumberFormatException exception)
- {
- JOptionPane.showMessageDialog(this,
- "Please enter a number greater than 0!",
- "Number Format Error", JOptionPane.ERROR_MESSAGE);
- amountOfGallonsJTextField.setText("");
- amountOfGallonsJTextField.requestFocusInWindow();
- }
- }
- public void numberOfGallonsNotZero()
- {
- if(amountOfGallons == 0)
- {
- JOptionPane.showMessageDialog(this,
- "Please enter a number greater than 0!",
- "Number Format Error", JOptionPane.ERROR_MESSAGE);
- amountOfGallonsJTextField.setText("");
- amountOfGallonsJTextField.requestFocusInWindow();
- }
- else
- {
- getTotalAmount();
- }
- }
- public void getTotalAmount()
- {
- if(deluxePaintJRadioButton.isSelected())
- {
- paintPrice = DELUXE_PAINT;
- }
- if(premiumPaintJRadioButton.isSelected())
- {
- paintPrice = PREMIUM_PAINT;
- }
- calculateTotalAmount();
- }
- public void calculateTotalAmount()
- {
- salesTotal = (amountOfGallons * paintPrice) * TAX_RATE;
- paintCalculator = new PaintCalculator(amountOfGallons, paintPrice);
- totalAmount = paintCalculator.getResults();
- displaySales();
- }
- public void displaySales()
- {
- decimalFormat = new DecimalFormat("$0.00");
- totalAmountJTextField.setText("" + decimalFormat.format(totalAmount));
- }
- public void clearJButtonActionPerformed()
- {
- amountOfGallonsJTextField.setText("");
- amountOfSalesTaxJTextField.setText("");
- totalAmountJTextField.setText("");
- amountOfGallonsJTextField.requestFocusInWindow();
- deluxePaintJRadioButton.setSelected(true);
- }
- public void closeJButtonActionPerformed()
- {
- PaintStore.this.dispose();
- }
- }
- class PaintCalculator
- {
- float numberOne;
- float numberTwo;
- public PaintCalculator(float firstValue, float secondValue)
- {
- numberOne = firstValue;
- numberTwo = secondValue;
- }
- public float getResults()
- {
- return numberOne * numberTwo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement