Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In JAVA Modify the program code In the program code, you will see several TODO comments (you can select Window|Action Items to see

In JAVA

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!)

  1. Product name label
  2. Product name textfield (name: txtProductName)
  3. Quantity label
  4. Quantity 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

Your final working program will look something like the following (note this is an example only, your form will look different).

image text in transcribed

Here is the code Please modify it so the output looks like the example output shown above. Thank you

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 } } } } //*************************************************************************************************************************************************************************************************

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 = 10 && pricePerItem

//************************************************************************************************************************************************************************************************

Invoice_Main.java

//*********************************************************************************************************************************************************************************************

package presentation;

public class Invoice_Main { public static void main(String[] args) { new Invoice_GUI(); } } //***********************************************************************************************************************************************************************************************************

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions