import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
public class Main extends JFrame implements ActionListener
{
// All the GUI objects
JPanel mainPanel = new JPanel();
JLabel storeLabel = new JLabel(" OrderBook INC. ");
JTextField bookNameTextField = new JTextField(20);
JTextField quantityTextField = new JTextField(20);
JTextField priceTextField = new JTextField(20);
JButton calculateButton = new JButton(" Calculate ");
JTextArea outputTextArea = new JTextArea("Books Ordered", 10, 25);
JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
int quantityInteger;
double priceDouble;
String nameString;
//object of the font
Font storeFont = new Font("Arial",Font.BOLD,14);
public static void main(String[] args)
{
Main myBooks = new Main();
myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Main()
{
designFrame();
}
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);
mainPanel.add(new JLabel(" Price "));
mainPanel.add(priceTextField);
mainPanel.add(calculateButton);
mainPanel.add(outputScrollPane);
//add the listeners
priceTextField.addActionListener(this);
calculateButton.addActionListener(this);
//add the panel to the frame
add(mainPanel);
setSize(300,400);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
getInput();
clear();
}
public void getInput()
{
nameString = bookNameTextField.getText();
quantityInteger = Integer.parseInt(quantityTextField.getText());
priceDouble = Double.parseDouble(priceTextField.getText());
Calculation myCalcs = new Calculation(quantityInteger, priceDouble);
displayOutput(myCalcs.getQuantity());
}
public void displayOutput(double CalculationDouble)
{
Calculation myOrder = new Calculation();
//object to format to currency
DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
DecimalFormat formatNumberFormat = new DecimalFormat("0");
//variables to store the retrieved values from the calculation class
double priceDouble, subTotalDouble, shippingDouble, taxDouble, totalDouble,
grandTotalDouble;
int totalNumberOfOrdersInteger;
priceDouble = myOrder.getPrice();
subTotalDouble = myOrder.getSubTotal();
shippingDouble = myOrder.getShipping();
taxDouble = myOrder.getTax();
totalDouble = myOrder.getTotal();
grandTotalDouble = myOrder.getGrandTotal();
totalNumberOfOrdersInteger = myOrder.getNumberOfOrders();
//display output
outputTextArea.append('\n' + "Name of the Book: " + nameString + '\n' +
"Quantity Ordered: " + formatNumberFormat.format(quantityInteger)
+ '\n' + "Price: " + formatDecimalFormat.format(priceDouble)+ '\n'
+ '\n' + "Subtotal: " + formatDecimalFormat.format(subTotalDouble)
+ '\n' + "Shipping: " + formatDecimalFormat.format(shippingDouble)
+ '\n' + "Tax: " + formatDecimalFormat.format(taxDouble) + '\n'+
"Total: " + formatDecimalFormat.format(totalDouble) + '\n' +'\n' +
"Total Quantity To-Date: " + totalNumberOfOrdersInteger + '\n' +
" Grand Total To-Date: " + formatDecimalFormat.format(grandTotalDouble)
+ '\n');
}
public void clear()
{
//clear existing text from text fields and request cursor to top
bookNameTextField.setText("");
quantityTextField.setText("");
priceTextField.setText("");
bookNameTextField.requestFocus();
}
}