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;
}
}