import javax.swing.*;
import java.awt.event.*;
import java.text.*;
import java.awt.*;
public class Presentation extends JFrame implements ActionListener, ItemListener
{
// All the GUI objects
JPanel mainPanel = new JPanel();
JPanel discountPanel = new JPanel();
JLabel storeLabel = new JLabel(" OrderBook INC. ");
JTextField bookNameTextField = new JTextField(20);
JTextField quantityTextField = new JTextField(20);
JRadioButton discountARadioButton = new JRadioButton(" A ");
JRadioButton discountBRadioButton = new JRadioButton(" B ");
JRadioButton discountCRadioButton = new JRadioButton(" C ");
JRadioButton discountInvisibleRadioButton = new JRadioButton("");
ButtonGroup discountButtonGroup = new ButtonGroup();
JTextField priceTextField = new JTextField(20);
JButton calculateButton = new JButton(" Calculate ");
JTextArea outputTextArea = new JTextArea("Books Ordered", 10,25);
JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
//object of the font
Font storeFont = new Font("Arial",Font.BOLD,14);
public static void main(String[] args)
{
//create an object of the class and code the close button
Presentation myBooks = new Presentation();
myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Presentation()
{
designFrame();
add(mainPanel);
setTitle("OrderBook INC.");
setSize(304,600);
setVisible(true);
}
public void designFrame()
{
//set the font and the color
storeLabel.setFont(storeFont);
storeLabel.setForeground(Color.GRAY);
//add the components to the mainPanel
mainPanel.add(storeLabel);
mainPanel.add(new JLabel(" Book Name "));
mainPanel.add(bookNameTextField);
mainPanel.add(new JLabel(" Quantity Ordered "));
mainPanel.add(quantityTextField);
//adding discount radio buttons to car radio button group
mainPanel.add(new JLabel(" Discount Type "));
discountButtonGroup.add(discountARadioButton);
discountButtonGroup.add(discountBRadioButton);
discountButtonGroup.add(discountCRadioButton);
discountButtonGroup.add(discountInvisibleRadioButton);
//adding radio buttons to panel
mainPanel.add(discountARadioButton);
mainPanel.add(discountBRadioButton);
mainPanel.add(discountCRadioButton);
priceTextField.setEditable(false);
calculateButton.setEnabled(false);
mainPanel.add(new JLabel(" Price "));
mainPanel.add(priceTextField);
mainPanel.add(calculateButton);
mainPanel.add(outputScrollPane);
//add the listeners
priceTextField.addActionListener(this);
calculateButton.addActionListener(this);
discountARadioButton.addItemListener(this);
discountBRadioButton.addItemListener(this);
discountCRadioButton.addItemListener(this);
//add the panel to the frame
add(mainPanel);
setSize(300,400);
setVisible(true);
}
public void itemStateChanged(ItemEvent evt)
{
if(discountARadioButton.isSelected())
{
enabledComponents();
}
else if (discountBRadioButton.isSelected())
{
enabledComponents();
}
else if (discountCRadioButton.isSelected())
{
enabledComponents();
}
}
public void actionPerformed(ActionEvent evt)
{
getInput();
clear();
}
public void getInput()
{
//declaring local variables
int quantityInteger;
int discountTypeInteger;
double priceDouble;
double discountDouble;
String nameString;
char discountChar;
// get input
try
{
quantityInteger = Integer.parseInt(quantityTextField.getText());
try
{
priceDouble = Double.parseDouble(priceTextField.getText());
}
catch(NumberFormatException err)
{
JOptionPane.showMessageDialog(null, "Price is invalid");
priceTextField.selectAll();
priceTextField.requestFocus();
}
}
catch(NumberFormatException err)
{
JOptionPane.showMessageDialog(null, "Quantity is invalid");
quantityTextField.selectAll();
quantityTextField.requestFocus();
}
if(discountARadioButton.isSelected())
{
discountChar = 'a';
discountTypeInteger = getTypeOfDiscount(discountChar);
nameString = bookNameTextField.getText();
quantityInteger = Integer.parseInt(quantityTextField.getText());
priceDouble = Double.parseDouble(priceTextField.getText());
if((discountTypeInteger != -1))
{
if (!(nameString.equals("")))
{
Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble);
discountDouble = myOrder.getDiscount();
displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
clear();
}
else
{
JOptionPane.showMessageDialog(null, "Please Enter Book Name");
bookNameTextField.selectAll();
bookNameTextField.requestFocus();
}
}
}
else if(discountBRadioButton.isSelected())
{
discountChar = 'b';
discountTypeInteger = getTypeOfDiscount(discountChar);
nameString = bookNameTextField.getText();
quantityInteger = Integer.parseInt(quantityTextField.getText());
priceDouble = Double.parseDouble(priceTextField.getText());
if((discountTypeInteger != -1))
{
if (!(nameString.equals("")))
{
Calculation myOrder = new Calculation(quantityInteger,
discountTypeInteger, priceDouble);
discountDouble = myOrder.getDiscount();
displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
clear();
}
else
{
JOptionPane.showMessageDialog(null, "Please Enter Book Name");
bookNameTextField.selectAll();
bookNameTextField.requestFocus();
}
}
}
else if(discountCRadioButton.isSelected())
{
discountChar = 'c';
discountTypeInteger = getTypeOfDiscount(discountChar);
nameString = bookNameTextField.getText();
quantityInteger = Integer.parseInt(quantityTextField.getText());
priceDouble = Double.parseDouble(priceTextField.getText());
if((discountTypeInteger != -1))
{
if (!(nameString.equals("")))
{
Calculation myOrder = new
Calculation(quantityInteger,
discountTypeInteger, priceDouble);
discountDouble = myOrder.getDiscount();
displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
clear();
}
else
{
JOptionPane.showMessageDialog(null, "Please Enter Book Name");
bookNameTextField.selectAll();
bookNameTextField.requestFocus();
}
}
}
try
{
quantityInteger = Integer.parseInt(quantityTextField.getText());
try
{
priceDouble = Double.parseDouble(priceTextField.getText());
}
catch(NumberFormatException err)
{
JOptionPane.showMessageDialog(null, "Price is Invalid");
priceTextField.selectAll();
priceTextField.requestFocus();
}
}
catch(NumberFormatException err)
{
JOptionPane.showMessageDialog(null, "Quantity is Invalid");
quantityTextField.selectAll();
quantityTextField.requestFocus();
}
}
//set text fields visible once a discount is selected
public void enabledComponents()
{
priceTextField.setEditable(true);
calculateButton.setEnabled(true);
}
public int getTypeOfDiscount(char discountChar)
{
int discountTypeInteger = 0;
{
if(discountARadioButton.isSelected())
{
discountTypeInteger = 1;
}
else if(discountBRadioButton.isSelected())
{
discountTypeInteger = 2;
}
else if(discountCRadioButton.isSelected())
{
discountTypeInteger = 3;
}
else if(discountInvisibleRadioButton.isSelected())
{
discountTypeInteger = -1;
}
}
return discountTypeInteger;
}
public void displayOutput(int quantityInteger, double priceDouble,
double discountDouble, String nameString, char discountChar)
{
//object to format to currency
DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
DecimalFormat formatNumberFormat = new DecimalFormat("0");
Calculation myCalcs = new Calculation();
String outputString;
if(discountChar == 'a')
{
outputString = "Name of the Book: " + nameString + '\n' +
"Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
+ '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice())
+ '\n' + "Discount Percentage: 20% " +
formatDecimalFormat.format(myCalcs.getDiscount())
+ '\n' + "Subtotal: " +
formatDecimalFormat.format(myCalcs.getSubTotal())
+ '\n' + "Shipping: " +
formatDecimalFormat.format(myCalcs.getShipping())
+ '\n' + "Tax: " +
formatDecimalFormat.format(myCalcs.getTax()) + '\n'+
"Total: " + formatDecimalFormat.format(myCalcs.getTotal())+'\n' +'\n' +
"Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n'
+ "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())+ '\n';
outputTextArea.append(outputString);
}
else if(discountChar == 'b')
{
outputString = "Name of the Book: " + nameString + '\n' +
"Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
+ '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice())
+ '\n' + "Discount Percentage: 20% " +
formatDecimalFormat.format(myCalcs.getDiscount())
+ '\n' + "Subtotal: " +
formatDecimalFormat.format(myCalcs.getSubTotal())
+ '\n' + "Shipping: " +
formatDecimalFormat.format(myCalcs.getShipping())
+ '\n' + "Tax: " +
formatDecimalFormat.format(myCalcs.getTax()) + '\n'+
"Total: " + formatDecimalFormat.format(myCalcs.getTotal())+'\n' +'\n' +
"Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n'
+ "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())+ '\n';
outputTextArea.append(outputString);
}
else if(discountChar == 'c')
{
outputString = "Name of the Book: " + nameString + '\n' +
"Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
+ '\n' + "Price: " + formatDecimalFormat.format(myCalcs.getPrice())
+ '\n' + "Discount Percentage: 20% " +
formatDecimalFormat.format(myCalcs.getDiscount())
+ '\n' + "Subtotal: " +
formatDecimalFormat.format(myCalcs.getSubTotal())
+ '\n' + "Shipping: " +
formatDecimalFormat.format(myCalcs.getShipping())
+ '\n' + "Tax: " +
formatDecimalFormat.format(myCalcs.getTax()) + '\n'+
"Total: " + formatDecimalFormat.format(myCalcs.getTotal())+'\n' +'\n' +
"Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n'
+ "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())+ '\n';
outputTextArea.append(outputString);
}
}
}
public void clear()
{
//clear existing text from text fields and request cursor to top
bookNameTextField.setText("");
quantityTextField.setText("");
discountInvisibleRadioButton.setSelected(true);
priceTextField.setText("");
bookNameTextField.requestFocus();
priceTextField.setEditable(false);
calculateButton.setEnabled(false);
}
}