Answered You can hire a professional tutor to get the answer.
Thanks! In this practice program you are going to practice creating graphical user interface controls and placing them on a form.
Can you help as I been messing with this for 2 days with no success.
Thanks!
In this practice program you are going to practice creating graphical user interface controls and placing them on a form. You are given a working NetBeans project shell to start that works with a given Invoice object that keeps track of a product name, quantity of the product to be ordered and the cost of each item. You will then create the necessary controls to extract the user input and display the results of the invoice.
If you have any questions or need help post a question in the Practice/Programming Help in the Introduction & Resources module.
Review the Project Shell
Download the Netbeans Project WK2_Practice_Invoice - Shell.zip, unzip the project and open it up in NetBeans.
Carefully review the structure of the program and the given classes. Make sure you understand the structure before you attempt to modify the program. You can run the program, but you will see a blank form until you complete the steps below.
Modify the program code
In the program code you will see several "TODO" comments (you can select "Window|Action Items" to see a list of the TODOs, clicking on an item will jump you to the place in the code).
You will create the GUI components and add the controls to the form for: (naming of the controls as shown is important!)
- Product name label
- Product name textfield (name: txtProductName)
- Quantity label
- Quantity textfield (name: txtQuantity)
- Item cost label
- Item cost textfield (name: txtCost)
- JButton to calculate the cost (name: btnCalculate)
- Add a CalculateHandler handler object to the btnCalculate addActionListener
Your final working program will look something like the following (note this is an example only, your form will look different).
It has three classes:
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 <= 1000) {
this.quantityPurchased = quantityPurchased;
}
else if (quantityPurchased < 1) {
this.quantityPurchased = 1;
}
else {
this.quantityPurchased = 1000;
}
}
public double getPricePerItem() {
return pricePerItem;
}
public void setPricePerItem(double pricePerItem) {
if (pricePerItem >= 10 && pricePerItem <= 10000) {
this.pricePerItem = pricePerItem;
}
else if (pricePerItem < 10) {
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("nProduct Name: ");
str.append(productName);
str.append("nNumber of items: ");
str.append(quantityPurchased);
str.append("nPrice Per Item: ");
str.append(cf.format(pricePerItem));
str.append("nTotal cost: ");
str.append(cf.format(getTotalCost()));
return str.toString();
}
}
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: Modify 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
}
}
}
}
Invoice_Main.java
package presentation;
public class Invoice_Main {
public static void main(String[] args) {
new Invoice_GUI();
}
}