Hello developers,
I have a Product class defined. Then have an applet extending this class. This applet lets me key in: 1.Ordered item 2. Quantity to order then display total amount payable for each item ordered plus the total amount of all items ordered.
I am learning to use array in Java. So the array has been defined. The program can now give the total amount payable(using Units ordered*Quantity*Unit price). Now in the listbox, I want to give the user the total amount payable for all the items she has ordered.
The problem is I don't understand why can't I use "total" two times in my button2_actionPerformed code. This is very frustrating, something so simple, Im not good at logic, so if anyone can look at that section of my code and how can I just take the total amount of each item and add it to the final total to be displayed in the listbox, it's very much appreciated. The error message Im getting is for this line" theProducts[index].sumTotal(total);", the compiler said that "total is already defined in button2_actionPerformed". Thanks in advance for your help. Im going to look at this somemore. Meanwhile if you cud tell me what should I do with the section I've indicated as "put total of all products in the list", I would be very happy.
Product class:
public class Product {
private String name;
private double price;
private int amount;
private double finalTotal;
public Product() {
name = "";
price = 0.0;
amount = 0;
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Product(String theName, double thePrice)
{
name = theName;
price = thePrice;
amount = 0;
}
public String getName()
{
return name;
}
public int getAmount()
{
return amount;
}
public double getPrice()
{
return price;
}
public double getFinalTotal(){
return finalTotal;
}
//make here a public method which returns the price
public void orderArticle(int theNumber)
{
//fill in you code
amount = amount + theNumber;
}
public void sumTotal(double theItemTotal){
finalTotal = finalTotal + theItemTotal;
}
private void jbInit() throws Exception {
}
}
Shopping List class:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
public class ShoppingList extends Applet {
boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
TextField textField1 = new TextField();
XYLayout xYLayout1 = new XYLayout();
TextField textField2 = new TextField();
TextField textField3 = new TextField();
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
TextField textField4 = new TextField();
Label label4 = new Label();
Button button1 = new Button();
Button button2 = new Button();
List list1 = new List();
int amt;
//own attributes
Product[] theProducts = new Product[3]; //my Array
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public ShoppingList() {
}
//Initialize the applet
public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
textField1.setText("");
this.setLayout(xYLayout1);
textField2.setText("");
textField3.setText("");
label1.setText("Product to order");
label2.setText("You have ordered");
label3.setText("Amount to order");
textField4.setText("");
label4.setText("Amount ordered");
button1.setLabel("Order");
button1.addActionListener(new ShoppingList_button1_actionAdapter(this));
button2.setLabel("Total");
button2.addActionListener(new ShoppingList_button2_actionAdapter(this));
this.add(textField1, new XYConstraints(12, 14, 180, 36));
this.add(textField2, new XYConstraints(205, 12, 163, 35));
this.add(label1, new XYConstraints(14, 60, 92, -1));
this.add(label2, new XYConstraints(217, 56, -1, -1));
this.add(label3, new XYConstraints(16, 149, 87, 20));
this.add(textField3, new XYConstraints(14, 103, 177, 39));
this.add(textField4, new XYConstraints(203, 102, 168, 40));
this.add(label4, new XYConstraints(209, 152, 86, -1));
this.add(button1, new XYConstraints(34, 188, -1, 30));
this.add(button2, new XYConstraints(34, 234, 62, 31));
this.add(list1, new XYConstraints(205, 184, 177, 87));
initializeProducts();
}
private void initializeProducts() {
theProducts[0] = new Product("Bread", 1.34);
theProducts[1] = new Product("Beans", 0.88);
theProducts[2] = new Product("Coffee", 2.35);
//Add some own products and add just the boudaries in the declaration
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
ShoppingList applet = new ShoppingList();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400, 320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void button1_actionPerformed(ActionEvent e) {
boolean articleFound = false;
int index = 0;
//theProducts.length
for (int i=0; i<3; i++){
if (theProducts[i].getName().equals(textField1.getText())) {
articleFound = true;
index = i;
}
}
if (articleFound) {
//get text from “amount to order” textField
//convert read text to integer number
String inputAmt;
inputAmt = textField3.getText();
int amt = Integer.parseInt(inputAmt);
//get the total amount of the selected product
theProducts[index].orderArticle(amt);
//order the selected product from the standardlist theProducts
//display name of selected product in the “you have ordered”-field
//display the total amount in the “amount ordered”-field
textField2.setText("You have chosen" + theProducts[index].getName());
textField4.setText(String.valueOf(theProducts[index].getAmount()));
} else {
//display an error message in the “product to order”-field
textField1.setText("Article not found");
}
}
public void button2_actionPerformed(ActionEvent e) {
double total; //declarations of the neccesary variables
int amt;
double unitPrice,sum;
String product;
String ttl;
int index=0;
double total=0;
for (int i=0; i <theProducts.length; i++)
{
//get the name of the i-th product
product=theProducts[i].getName();
//get the amount of the i-th product
amt=theProducts[i].getAmount();
//get the price of the i-th product
unitPrice = theProducts[i].getPrice();
//calculate the total for the i-th product
index=i;
total = amt * unitPrice;
ttl = String.valueOf(total);
// put the name and the total in the list
// (use addItem-method of List)
list1.addItem(product + " " + ttl)
}
//put total of all product in the list}
theProducts[index].sumTotal(total);
}
class ShoppingList_button1_actionAdapter implements ActionListener {
private ShoppingList adaptee;
ShoppingList_button1_actionAdapter(ShoppingList adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
}
class ShoppingList_button2_actionAdapter implements ActionListener {
private ShoppingList adaptee;
ShoppingList_button2_actionAdapter(ShoppingList adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}