Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: sum of array values

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sum of array values

    hello. im having trouble getting the total inventory value in an array. It would loop the items in the choicebox by item but wouldn't add the item values together.

    itemsArray[0] = new Items(87, "Wrench", 5, 13.23);
    itemsArray[1] = new Items(126, "Hammer", 13, 12.37);
    itemsArray[2] = new Items(298, "Screwdriver", 21, 4.12);
    itemsArray[3] = new Items(315, "Pliers", 34, 5.18);

    double totalInventoryValue = 0;
    double itemInventoryValue = 0;

    for(int i = 0; i < itemsArray.length ; i++) //<---this works fine
    {
    itemInventoryValue = itemsArray[i].getItemInventoryValue(quantityOnHand, price);
    }

    for(int i = itemsChoiceBox.getSelectedIndex()-1; i < itemsArray.length ; i++) //<---- doesn't add item totals together
    {
    totalInventoryValue = getTotalInventoryValue(itemsArray[i].itemInventoryValue);
    }

    private double getTotalInventoryValue(double itemInventoryValue)
    {
    totalInventoryValue += itemInventoryValue;

    return totalInventoryValue;
    }

    public double getItemInventoryValue(int qOH, double prce)
    {
    itemInventoryValue = (qOH * prce);

    return itemInventoryValue;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    wouldn't add the item values together.
    Please explain what happens. If there are error messages, copy the full text and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    hi. sorry i fell asleep. it was really late. Thanks for the reply and I hope I didn't post in the wrong section. so when i select an item in the choice box it is supposed to calculate the sum of the values of each item in the array. Instead it only adds the sum of the item values without going to the next item in the array. How can i store the sum of the item values separately for each item? Thanks.

     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class InventoryControlGUIapp extends Frame implements ActionListener, ItemListener
    {
    	private static final long serialVersionUID = 1L;
     
    	int quantityOnHand;
    	double price;
    	static double totalInventoryValue;
    	int itemID; String item;
     
    	Choice itemsChoiceBox = new Choice(); 
    	Items[] itemsArray = new Items[4]; 
    	Label itemIDLabel;
    		TextField itemIDField;
    	Label itemLabel;
    		TextField itemField;
    	Label quantityLabel;
    		TextField quantityField;
    	Label priceLabel;
    		TextField priceField;
    	Label itemValueLabel;
    		TextField itemValueField;
    	Label totalItemsValueLabel;
    		TextField totalItemsValueField;
    	Button submitButton, cancelButton, editItemButton, sellItemButton, receiveStockButton;
     
    	public InventoryControlGUIapp() 
    	{
    		super ("INVENTORY CONTROL GUI APP"); 
     
    		itemsArray[0] = new Items(87, "Wrench", 5, 13.23); //4. Populate array reference with OBJECTS
    		itemsArray[1] = new Items(126, "Hammer", 13, 12.37);
    		itemsArray[2] = new Items(298, "Screwdriver", 21, 4.12);
    		itemsArray[3] = new Items(315, "Pliers", 34, 5.18);
     
    		itemsChoiceBox.addItem("Select an item"); 
     
    		for (int i= 0; i < itemsArray.length; i++) 
    		{
     
    			itemsChoiceBox.addItem(itemsArray[i].getItemID() + " - " + itemsArray[i].getItem());
    		}
     
    		setLayout(new GridLayout(9,1,0,0)); 
     
    		Panel itemsChoicePanel = new Panel(); 
    			itemsChoicePanel.setLayout(new FlowLayout()); 
    			itemsChoicePanel.add(itemsChoiceBox); 
    			itemsChoiceBox.addItemListener(this); 
    			add(itemsChoicePanel); 
     
    		etc..etc...
    	}
     
    	public void itemStateChanged(ItemEvent e)
    	{
    		quantityOnHand = itemsArray[itemsChoiceBox.getSelectedIndex()-1].getQuantityOnHand();
    		price = itemsArray[itemsChoiceBox.getSelectedIndex()-1].getPrice();
     
    		double totalInventoryValue = 0;
    		double itemInventoryValue = 0;
     
    		for(int i = 0; i < itemsArray.length ; i++) //<---this works fine
    		{
    			itemInventoryValue = itemsArray[i].getItemInventoryValue(quantityOnHand, price);
    		}
     
    		for(int i = itemsChoiceBox.getSelectedIndex()-1; i < itemsArray.length ; i++) //<---- doesn't add item totals together
    		{
     
    			totalInventoryValue = (itemsArray[i].getTotalInventoryValue(itemInventoryValue));
    		}
     
    		itemValueField.setText(Double.toString((itemInventoryValue)));
    		totalItemsValueField.setText(Double.toString((totalInventoryValue)));
     
    		if (itemsChoiceBox.getSelectedIndex() > 0)
    		{
     
    			itemIDField.setText(Integer.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getItemID()));
    			itemField.setText((itemsArray[itemsChoiceBox.getSelectedIndex()-1].getItem()));
    			quantityField.setText(Integer.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getQuantityOnHand()));
    			priceField.setText(Double.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getPrice()));
     
     
     
    			editItemButton.setEnabled(true);
    			sellItemButton.setEnabled(true);
    			receiveStockButton.setEnabled(true);
    		}
    		else
    		{
    			itemIDField.setText("");
    			itemField.setText("");
    			quantityField.setText("");
    			priceField.setText("");
    			itemValueField.setText("");
    			totalItemsValueField.setText("");
    			submitButton.setEnabled(false);
    			cancelButton.setEnabled(false);
    			editItemButton.setEnabled(false);
    			sellItemButton.setEnabled(false);
    			receiveStockButton.setEnabled(false);			
    		}
    	}
     
     
     
    	public void actionPerformed(ActionEvent e)
    	{
     
    	}
     
    	public static void main(String args[]) //executes at run time
    	{
    		InventoryControlGUIapp frame = new InventoryControlGUIapp(); //create Frame
    		frame.setSize(300, 400);
    		frame.setLocation(300, 170);
    		frame.setVisible(true);
    	}
     
     
    }
     
    class Items 
    {
     
    	private int itemID;
    	private String item;
    	private int quantityOnHand;
     
    	public double price;
     
    	double itemInventoryValue;
    	static double totalInventoryValue;
     
     
    	public Items(int iID, String itm, int qOH, double prce) 
    	{
    		itemID = iID;
    		item = itm;
    		quantityOnHand = qOH;
    		price = prce;
    	} 
    	public int getItemID()
    	{
    		return itemID;
    	}
    	public String getItem()
    	{
    		return item;
    	}
    	public int getQuantityOnHand()
    	{
    		return quantityOnHand;
    	}
    	public double getPrice()
    	{
    		return price;
    	}
    	public double getItemInventoryValue(int qOH, double prce)
    	{
    		itemInventoryValue = (qOH * prce);
     
    		return itemInventoryValue;
    	}
    	public void setItem(String itm) 
    	{
    		item = itm;
    	}
    	public void setQuantityOnHand(int qOH)
    	{
    		quantityOnHand = qOH;
    	}
    	public void setPrice(double prce)
    	{
    		price = prce;
    	}
    	double getTotalInventoryValue(double itemInventoryValue)
    	{
    		totalInventoryValue += itemInventoryValue;
     
    		return totalInventoryValue;
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    Can you post the program's current output and add some comments showing the desired output?

    To accumulate a sum, you add the next value to the current value. Where does the code do that?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    i populate my choicebox.

    public InventoryControlGUIapp() 
    	{
    		super ("INVENTORY CONTROL GUI APP"); 
     
    		itemsArray[0] = new Items(87, "Wrench", 5, 13.23); //4. Populate array reference with OBJECTS
    		itemsArray[1] = new Items(126, "Hammer", 13, 12.37);
    		itemsArray[2] = new Items(298, "Screwdriver", 21, 4.12);
    		itemsArray[3] = new Items(315, "Pliers", 34, 5.18);
     
    		itemsChoiceBox.addItem("Select an item"); 
     
    		for (int i= 0; i < itemsArray.length; i++) 
    		{
     
    			itemsChoiceBox.addItem(itemsArray[i].getItemID() + " - " + itemsArray[i].getItem());
    		}

    this is my choice box. when i select an item in the choice box, it will display the item properties.
    public void itemStateChanged(ItemEvent e)
    	{
    		quantityOnHand = itemsArray[itemsChoiceBox.getSelectedIndex()-1].getQuantityOnHand();
    		price = itemsArray[itemsChoiceBox.getSelectedIndex()-1].getPrice();
     
    		double totalInventoryValue = 0;
    		double itemInventoryValue = 0;
     
    		for(int i = 0; i < itemsArray.length ; i++) //<---this works fine
    		{
    			itemInventoryValue = itemsArray[i].getItemInventoryValue(quantityOnHand, price);
    		}
     
    		for(int i = itemsChoiceBox.getSelectedIndex()-1; i < itemsArray.length ; i++) //<---- doesn't add item totals together
    		{
     
    			totalInventoryValue = (itemsArray[i].getTotalInventoryValue(itemInventoryValue));
    		}
     
    		itemValueField.setText(Double.toString((itemInventoryValue)));
    		totalItemsValueField.setText(Double.toString((totalInventoryValue)));
     
    		if (itemsChoiceBox.getSelectedIndex() > 0)
    		{
     
    			itemIDField.setText(Integer.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getItemID()));
    			itemField.setText((itemsArray[itemsChoiceBox.getSelectedIndex()-1].getItem()));
    			quantityField.setText(Integer.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getQuantityOnHand()));
    			priceField.setText(Double.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getPrice()));
     
     
     
    			editItemButton.setEnabled(true);
    			sellItemButton.setEnabled(true);
    			receiveStockButton.setEnabled(true);
    		}
    		else
    		{
    			itemIDField.setText("");
    			itemField.setText("");
    			quantityField.setText("");
    			priceField.setText("");
    			itemValueField.setText("");
    			totalItemsValueField.setText("");
    			submitButton.setEnabled(false);
    			cancelButton.setEnabled(false);
    			editItemButton.setEnabled(false);
    			sellItemButton.setEnabled(false);
    			receiveStockButton.setEnabled(false);			
    		}
    	}


     
    class Items 
    {
     
    	private int itemID;
    	private String item;
    	private int quantityOnHand;
     
    	public double price;
     
    	double itemInventoryValue;
    	static double totalInventoryValue;
     
     
    	public Items(int iID, String itm, int qOH, double prce) 
    	{
    		itemID = iID;
    		item = itm;
    		quantityOnHand = qOH;
    		price = prce;
    	} 
    	public int getItemID()
    	{
    		return itemID;
    	}
    	public String getItem()
    	{
    		return item;
    	}
    	public int getQuantityOnHand()
    	{
    		return quantityOnHand;
    	}
    	public double getPrice()
    	{
    		return price;
    	}
    	public double getItemInventoryValue(int qOH, double prce)
    	{
    		itemInventoryValue = (qOH * prce);
     
    		return itemInventoryValue;
    	}
    	public void setItem(String itm) 
    	{
    		item = itm;
    	}
    	public void setQuantityOnHand(int qOH)
    	{
    		quantityOnHand = qOH;
    	}
    	public void setPrice(double prce)
    	{
    		price = prce;
    	}
    	double getTotalInventoryValue(double itemInventoryValue)
    	{
    		totalInventoryValue += itemInventoryValue;
     
    		return totalInventoryValue;
    	}
    }

    this loop for getting total inventory value adds the total value for only one item in the choicebox. it doesn't go to the next item in the array.

    for(int i = itemsChoiceBox.getSelectedIndex()-1; i < itemsArray.length ; i++) //<---- doesn't add item totals together
    		{
     
    			totalInventoryValue = (itemsArray[i].getTotalInventoryValue(itemInventoryValue));
    		}

    outputs:
    on debugging when i select the wrench, the itemInventoryValue is correct (quantity on hand * price = 66.15). But the loop above for totalInventoryValue takes this value and multiplies it by the length of the array (which is 4) and gives an output of 264.60 (66.15 * 4). I need the loop to go to the next item in the array after it calculates the itemInventoryValue. So it should look like sumOfwrench + sumOfHammer + sumOfScrewdriver + sumOfPliers. I'm still working on this but I can't get it to work.

    this is how i pass itemInventoryValue to get totalInventoryValue
    double getTotalInventoryValue(double itemInventoryValue)
    	{
    		totalInventoryValue += itemInventoryValue;
     
    		return totalInventoryValue;
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    this loop for getting total inventory value adds
    Where does that code "add" anything? The loop assigns new values to the totalInventoryValue variable but does not add to it.
      			totalInventoryValue = (itemsArray[i].getTotalInventoryValue(itemInventoryValue));  //  assign a value


    This statement adds a value to the totalInventoryValue variable :
       totalInventoryValue += itemInventoryValue;   // add to the value
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    i've tried this statement many times before but when i select another item in the choice box the itemInventoryValue for the previous item gets added to the totalInventoryValue of the item multiplied by the length of the array (which is 4). For example, selecting wrench first in the choice box gives the totalInventoryValue as 264.60 then selecting hammer next the totalInventoryValue is 643.24 (13 * 12.37 * 4). The totalInventoryValue should not change when selecting an item in the choicebox since it should be adding the sum of itemInventoryValue in the array. I need a statement that goes to the next item in the array after looping to calculate the itemInventoryValue.

    i am so lost. i've tried this code too to get the next item in the array but i'm not any closer to figuring this out.

     
    int n = 1;
    		for(int i = itemsChoiceBox.getSelectedIndex()-1; i < itemsArray.length ; i++)
    		{
    			totalInventoryValue = (itemsArray[i].getTotalInventoryValue(itemInventoryValue));
    			totalInventoryValue += itemInventoryValue;
    			i = i + n;
     
    		}

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

            totalInventoryValue = (itemsArray[i].getTotalInventoryValue(itemInventoryValue));  // sets value of  totalInventoryValue
    	totalInventoryValue += itemInventoryValue;       // changes value of totalInventoryValue
    The second time through the loop, the value in totalInventoryValue is set to a new value. The value from the second line above is lost.
       x =  4;     // x is 4
       x += 3;    // x is  now 7
       // second time through loop
       x = 2;      // x is now 2, the value 7 is gone
       x += 3;    // x is now 5

    If you want to sum up the values, Why not use the += like this:
           totalInventoryValue += (itemsArray[i].getTotalInventoryValue(itemInventoryValue));  // add to total
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    i think i used that statement before but it would still sum up the itemInventoryValue then multiply by array.length then when i click on another item in the choicebox the totalInventoryValue would add the itemInventoryValue * array.length + the previous item's itemInventoryValue (that was multiplied by array.length).

    i'm a bit closer now with this code but it would not statically display the totalInventoryValue. however, the correct value will be displayed after I cycle through all the items in the choicebox.

    double eachItemInventoryValue;
     
    for(int i = itemsChoiceBox.getSelectedIndex()-1; i < itemsArray.length ; i++)
    		{
    			eachItemInventoryValue = (itemsArray[i].getTotalInventoryValue(itemInventoryValue));
     
    			for(i = 0; i < itemsArray.length ; i++)
     
    				itemInventoryValue = itemsArray[i].getItemInventoryValue(quantityOnHand, price);
     
    		}
     
    totalInventoryValue += eachItemInventoryValue;

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    Why have a loop here:
    for(i = 0; i < itemsArray.length ; i++)
     
    				itemInventoryValue = itemsArray[i].getItemInventoryValue(quantityOnHand, price);
    The value in itemInventoryValue will be from the last iteration of the loop. All its previous values will be overwritten by the following executions of the statement as the loop iterates.
    If you only want the last value, why have a loop, just assign the final value you want and skip the loop.

    The same for the other loop, just assign to eachItemInventoryValue the value you want it in and skip the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    I was working on taking the loops out. Unfortunately, each time I select an item in the choice box the totalInventoryValue gets summed up with the previous selections and it still doesn't remain static.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    the totalInventoryValue gets summed up with the previous selections
    What's it supposed to do?
    The code has no comments describing what it is supposed to do and how it is going to do it, so I have no idea what the code is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    the totalInventoryValue is the variable that stores the sum of each items itemInventoryValue (quantity*price). It is supposed to display the TOTAL sum of all itemInventoryValue regardless if any item is selected in the choice box the value should remain the same. Right now, the totalInventoryValue variable keeps adding to the sum when making a new selection in the choicebox.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    Can you post the new code with comments in the code describing what it is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    thanks very much for the help. I still couldn't get it to work properly. I tried moving the statement to the actionlistener and call it from the itemListener so it doesn't affect the value when the user changes the selection but it seems I've messed it up even more.

    import java.awt.*;
    import java.awt.event.*;
     
     
    public class InventoryControlGUIapp extends Frame implements ActionListener, ItemListener
    {
    	private static final long serialVersionUID = 1L;
     
    	int quantityOnHand;
    	double price;
    	static double totalInventoryValue;
    	int itemID; String item;
     
    	double itemInventoryValue;
    	double eachItemInventoryValue;
     
     
    	Choice itemsChoiceBox = new Choice(); //create choice box
    	Items[] itemsArray = new Items[4]; //create an array of item REFERENCES not item objects
    	Label itemIDLabel;
    		TextField itemIDField;
    	Label itemLabel;
    		TextField itemField;
    	Label quantityLabel;
    		TextField quantityField;
    	Label priceLabel;
    		TextField priceField;
    	Label itemValueLabel;
    		TextField itemValueField;
    	Label totalItemsValueLabel;
    		TextField totalItemsValueField;
    	Button submitButton, cancelButton, editItemButton, sellItemButton, receiveStockButton;
     
    	public InventoryControlGUIapp() 
    	{
    		super ("INVENTORY CONTROL GUI APP"); 
     
    		itemsArray[0] = new Items(87, "Wrench", 5, 13.23); 
    		itemsArray[1] = new Items(126, "Hammer", 13, 12.37);
    		itemsArray[2] = new Items(298, "Screwdriver", 21, 4.12);
    		itemsArray[3] = new Items(315, "Pliers", 34, 5.18);
     
    		itemsChoiceBox.addItem("Select an item"); 
     
    		for (int i= 0; i < itemsArray.length; i++) 
    		{
    			itemsChoiceBox.addItem(itemsArray[i].getItemID() + " - " + itemsArray[i].getItem());
    		}
     
    		setLayout(new GridLayout(9,1,0,0)); 
     
    		Panel itemsChoicePanel = new Panel(); 
    			itemsChoicePanel.setLayout(new FlowLayout()); //set panel layout left to right
    			itemsChoicePanel.add(itemsChoiceBox); //add the choice box in this panel
    			itemsChoiceBox.addItemListener(this); //(this) references the Frame class.
    			add(itemsChoicePanel); //add panel to grid on row 1
     
    		Panel itemsIDPanel = new Panel(); //11. create panels to put on the rows in the grid layout
    			itemsIDPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); //set panel layout left justified
    			itemIDLabel = new Label("Item ID");
    			itemIDField = new TextField(5);
    			itemIDField.setEditable(false);
    			itemsIDPanel.add(itemIDLabel);
    			itemsIDPanel.add(itemIDField);
    			add(itemsIDPanel);
     
    		Panel itemsPanel = new Panel(); 
    			itemsPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    			itemLabel = new Label("Item description");
    			itemField = new TextField(15);
    			itemField.setEditable(false);
    			itemsPanel.add(itemLabel);
    			itemsPanel.add(itemField);
    			add(itemsPanel);
     
    		Panel quantityPanel = new Panel(); 
    			quantityPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    			quantityLabel = new Label("Quantity on hand");
    			quantityField = new TextField(5);
    			quantityField.setEditable(false);
    			quantityPanel.add(quantityLabel);
    			quantityPanel.add(quantityField);
    			add(quantityPanel);
     
    		Panel pricePanel = new Panel(); 
    			pricePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    			priceLabel = new Label("Item price");
    			priceField = new TextField(5);
    			priceField.setEditable(false);
    			pricePanel.add(priceLabel);
    			pricePanel.add(priceField);
    			add(pricePanel);
     
    		Panel itemValuePanel = new Panel(); 
    			itemValuePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    			itemValueLabel = new Label("Inventory value");
    			itemValueField = new TextField(5);
    			itemValueField.setEditable(false);
    			itemValuePanel.add(itemValueLabel);
    			itemValuePanel.add(itemValueField);
    			add(itemValuePanel);
     
    		Panel totalInventoryValuePanel = new Panel();
    			totalInventoryValuePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    			totalItemsValueLabel = new Label("Total Inventory value");
    			totalItemsValueField = new TextField(5);
    			totalItemsValueField.setEditable(false);
    			totalInventoryValuePanel.add(totalItemsValueLabel);
    			totalInventoryValuePanel.add(totalItemsValueField);
    			add(totalInventoryValuePanel);
     
    		Panel submitCancelEditSellPanel = new Panel();
    			submitCancelEditSellPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    			submitButton = new Button("Submit");
    			cancelButton = new Button("Cancel");
    			editItemButton = new Button("Edit");
    			sellItemButton = new Button("Sell");
    			submitButton.addActionListener(this);
    			cancelButton.addActionListener(this);
    			editItemButton.addActionListener(this);
    			sellItemButton.addActionListener(this);
    			submitCancelEditSellPanel.add(submitButton);
    			submitCancelEditSellPanel.add(cancelButton);
    			submitCancelEditSellPanel.add(editItemButton);
    			submitCancelEditSellPanel.add(sellItemButton);
    			add(submitCancelEditSellPanel);
     
    		Panel receivePanel = new Panel(); 
    			receivePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    			receiveStockButton = new Button("Receive");
    			receiveStockButton.addActionListener(this);
    			receivePanel.add(receiveStockButton);
    			add(receivePanel);
     
    			//disable all buttons on run time
    			submitButton.setEnabled(false);
    			cancelButton.setEnabled(false);
    			editItemButton.setEnabled(false);
    			sellItemButton.setEnabled(false);
    			receiveStockButton.setEnabled(false);
     
     
    		addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				System.exit(0);
    			}
    		});		
    	}
     
    	public void itemStateChanged(ItemEvent e)
    	{
    		quantityOnHand = itemsArray[itemsChoiceBox.getSelectedIndex()-1].getQuantityOnHand();
    		price = itemsArray[itemsChoiceBox.getSelectedIndex()-1].getPrice();
     
    		int i=0;	
    		itemInventoryValue = itemsArray[i].getItemInventoryValue(quantityOnHand, price); //get sum of item value
     
    		itemValueField.setText(Double.toString((itemInventoryValue)));
     
    		eachItemInventoryValue +=  itemInventoryValue; //sum each item value in a variable
     
    		totalInventoryValue += eachItemInventoryValue; //get total items value
    		totalItemsValueField.setText(Double.toString((totalInventoryValue)));
     
    		if (itemsChoiceBox.getSelectedIndex() > 0)
    		{
     
    			itemIDField.setText(Integer.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getItemID()));
    			itemField.setText((itemsArray[itemsChoiceBox.getSelectedIndex()-1].getItem()));
    			quantityField.setText(Integer.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getQuantityOnHand()));
    			priceField.setText(Double.toString(itemsArray[itemsChoiceBox.getSelectedIndex()-1].getPrice()));
     
     
     
    			editItemButton.setEnabled(true);
    			sellItemButton.setEnabled(true);
    			receiveStockButton.setEnabled(true);
    		}
    		else
    		{
    			itemIDField.setText("");
    			itemField.setText("");
    			quantityField.setText("");
    			priceField.setText("");
    			itemValueField.setText("");
    			totalItemsValueField.setText("");
    			submitButton.setEnabled(false);
    			cancelButton.setEnabled(false);
    			editItemButton.setEnabled(false);
    			sellItemButton.setEnabled(false);
    			receiveStockButton.setEnabled(false);			
    		}
    	}
     
     
    	public void actionPerformed(ActionEvent e)
    	{
     
     
    	}
     
    	public static void main(String args[]) //executes at run time
    	{
    		InventoryControlGUIapp frame = new InventoryControlGUIapp(); //create Frame
    		frame.setSize(300, 400);
    		frame.setLocation(300, 170);
    		frame.setVisible(true);
     
     
    	}
    }
     
    class Items 
    {
    	private int itemID;
    	private String item;
    	private int quantityOnHand;
     
    	public double price;
     
    	double itemInventoryValue;
    	static double totalInventoryValue;
     
     
    	public Items(int iID, String itm, int qOH, double prce)
    	{ 
    		itemID = iID;
    		item = itm;
    		quantityOnHand = qOH;
    		price = prce;
    	}
    	public int getItemID()
    	{
    		return itemID;
    	}
    	public String getItem()
    	{
    		return item;
    	}
    	public int getQuantityOnHand()
    	{
    		return quantityOnHand;
    	}
    	public double getPrice()
    	{
    		return price;
    	}
    	public double getItemInventoryValue(int qOH, double prce)
    	{
    		itemInventoryValue = (qOH * prce);
     
    		return itemInventoryValue;
    	}
    	public void setItem(String itm) 
    	{
    		item = itm;
    	}
    	public void setQuantityOnHand(int qOH)
    	{
    		quantityOnHand = qOH;
    	}
    	public void setPrice(double prce)
    	{
    		price = prce;
    	}
    	public static double getTotalInventoryValue()
    	{
    		return totalInventoryValue;
    	}
     
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    I see lots of code in the itemStateChanged method but I do not see any comments describing what that code is trying to do.
    There are several variables declared at the start of the class without comments saying what they are used for.

    One problem I see is totalInventoryValue is declared as static. Why is that?

    What is the program supposed to do? Again no comments.
    What does the user input into the program to make it do something?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sum of array values

    it says in the book we have to set it to static. Right now i am updating my array to take a double itemInventoryValue field. Then maybe i can use that value to get the totalInventoryValue .

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sum of array values

    it says in the book we have to set it to static
    Does it give a reason for having static variables?
    Using static incorrectly can cause problems.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: April 26th, 2019, 04:16 PM
  2. how to find 2nd largest array if array values like{10,20,92,81,92,34}
    By anjijava16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 04:59 PM
  3. Need Help Trying to set an array for multiple RGB values
    By JavaClass in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2013, 07:28 PM
  4. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 09:47 PM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM

Tags for this Thread