Question
Invoice. Java package business; import java.text.NumberFormat; import java.util.Locale; public class Invoice { private String productName; private int quantityPurchased; private double pricePerItem; public Invoice() { setProductName();;
Invoice. Java
package business;
import java.text.NumberFormat;
import java.util.Locale;
public class Invoice {
private String productName;
private int quantityPurchased;
private double pricePerItem;
public Invoice() {
setProductName("");;
setQuantityPurchased(1);
setPricePerItem(10);
}
public Invoice(String productName, int quantityPurchased, double pricePerItem) {
setProductName(productName);
setQuantityPurchased(quantityPurchased);
setPricePerItem(pricePerItem);
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
if(productName != null && !productName.trim().isEmpty()) {
this.productName = productName;
}
else {
this.productName = "Unknown product";
}
}
public int getQuantityPurchased() {
return quantityPurchased;
}
public void setQuantityPurchased(int quantityPurchased) {
if (quantityPurchased >= 1 && quantityPurchased
this.quantityPurchased = quantityPurchased;
}
else if (quantityPurchased
this.quantityPurchased = 1;
}
else {
this.quantityPurchased = 1000;
}
}
public double getPricePerItem() {
return pricePerItem;
}
public void setPricePerItem(double pricePerItem) {
if (pricePerItem >= 10 && pricePerItem
this.pricePerItem = pricePerItem;
}
else if (pricePerItem
this.pricePerItem = 10;
}
else {
this.pricePerItem = 10000;
}
}
public double getTotalCost() {
return pricePerItem * quantityPurchased;
}
@Override
public String toString() {
NumberFormat cf = NumberFormat.getCurrencyInstance(Locale.US);
StringBuilder str = new StringBuilder();
str.append("Invoice Information:");
str.append(" Product Name: ");
str.append(productName);
str.append(" Number of items: ");
str.append(quantityPurchased);
str.append(" Price Per Item: ");
str.append(cf.format(pricePerItem));
str.append(" Total cost: ");
str.append(cf.format(getTotalCost()));
return str.toString();
}
}
You have to modify below code (only modify to do comment program work);
Invoice_GUI.java
package presentation;
import business.Invoice; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JTextField;
public class Invoice_GUI extends JFrame { public static final int WINDOW_WIDTH = 450; public static final int WINDOW_HEIGHT = 400; private Invoice aInvoice; private JButton btnCalculate; private JButton btnClear; private JButton btnExit; private JTextField txtProductName; private JTextField txtQuantity; private JTextField txtCostPerItem; private JTextArea txtTotalCost; public Invoice_GUI() { super(); createPanel(); setFrame(); } private void createPanel() { super.setLayout(new GridLayout(0, 2)); //TODO: create the GUI components and add them to the form for: /* 1. Product name label 2. Product name textfield (name: txtProductName) 3. Quantity label 4. Quanitity textfield (name: txtQuantity) 5. Item cost label 6. Item cost textfield (name: txtCost) 7. JButton to calculate the cost (name: btnCalculate 8. Add a CalculateHandler handler object to the btnCalculate addActionListener */ } private void setFrame() { Dimension windowSize = new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
super.setTitle("Week 2 Practice Program--Invoice"); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setSize(windowSize); super.setMinimumSize(windowSize); super.setMaximumSize(windowSize);
super.setLocationRelativeTo(null); super.setVisible(true); } private class CalculateHandler implements ActionListener { @Override public void actionPerformed(ActionEvent ae) { boolean valid; if (aInvoice == null) { //TODO: create a new invoice object aInvoice = new Invoice(); } aInvoice.setProductName(txtProductName.getText());
try { int quanity = Integer.parseInt(txtQuantity.getText()); aInvoice.setQuantityPurchased(quanity); valid = true; } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Quantity needs to be a number between 1 and 1000", "Illegal Quanity value", JOptionPane.ERROR_MESSAGE); valid = false; txtQuantity.setText(""); txtQuantity.requestFocus(); } if (valid) { try { double cost = 0; //TODO: write the state to extract the double value from the txtCost field aInvoice.setPricePerItem(cost); } catch (NumberFormatException ex) { valid = false; JOptionPane.showMessageDialog(null, "Costs needs to be a number between 10 and 10000", "Illegal Cost value", JOptionPane.ERROR_MESSAGE); valid = false; txtCostPerItem.setText(""); txtCostPerItem.requestFocus(); } } if (valid) { //TODO: using the toString method of the invoice object to set the output text } } } }
Week 2 Practice Program-Invoice Product Name Coffee Cup Product Quantity (1 to 1000) 100 Item cost (10 to 10000) 10 Invoice Information: Product Name: Coffee Cup Number of items: 100 Price Per Item: $10.00 Total cost: $1,000.00 Total cost Calculate Total Cost
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started