Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default adding entire inventory in array?

    HEllo again,
    Thanks for all the help I have gotten almost all the bugs worked out. I am trying to get the Sum of the the entire inventory of my program. I keep running into road blocks.

    This is my thoughts for code. I will include the java code I have with array.

    Java Code:
            
    public double getValue() {
            double value = 0.00;
            for (int i = 0; i < count; i++) {
                value + manufacture[i].totalInventoryField;
            }
            return value;
            }
    Java Code:
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent; // required for click events
    import java.awt.event.ActionListener; // required for click events
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.util.Arrays;  // required to sort array
    import javax.swing.ImageIcon;   // regired to access images
    import java.text.NumberFormat;  // import Java package used to format currency
    
    
    public class FrameHelper extends JFrame
    {
        NumberFormat nf = NumberFormat.getCurrencyInstance();  // used to format currency
        private int count;
         // Declare Class Variables
    
          // Declare two panels. gridPanel is main panel. panel is inserted into gridPanel.
    
          private JPanel gridPanel = new JPanel(); // one panel that contains my gridlayout
          private JPanel panel = new JPanel();      // panel used to contain buttons, labels and text fields
    
          // Declare Buttons
    
          JButton firstButton; // first button
          JButton nextButton; // next button
          JButton previousButton; // previous button
          JButton lastButton; // last button
    
          // Declare Text Fields
    
          JTextField itemNumberField; // name field
          JTextField productNameField; // name field
          JTextField numberOfUnitsInStockField; // age field
          JTextField priceOfUnitField; // age field
          JTextField totalInventoryField;  // sum of the ages of the manufactures
          JTextField manufactureField; // hair color field
          JTextField restockingFeeField; // age in ten years field
          JTextField sumOfInventoryField;  // sum of the ages of the manufactures
    
          // Declare Labels
    
           JLabel lblItemNumber; // name label
           JLabel lblProductName; // name label
           JLabel lblNumberOfUnitsInStock; // age labe
           JLabel lblPriceOfUnit; // age labe
           JLabel lblTotalInventory; // age labe
           JLabel lblManufacture ; // hair color label
           JLabel lblRestockingFee; // age of manufacture in ten years label
           JLabel lblSumOfInventory;  // sum of all the manufacture's ages
           JLabel lblLogo; // logo label
           JLabel lblCompanyName; // company name label
    
          Manufacture manufacture1;
          Manufacture manufacture2;
          Manufacture manufacture3;
          Manufacture manufacture4;
          Manufacture manufacture5;
    
          public static final int MAX_MANUFACTURES = 5; // set maximum size for Item Array
          Manufacture[] item = new Manufacture[MAX_MANUFACTURES];   // create Item Array object
          public static int ArrayIndex = 0; // index used to track position of array.
    
          public FrameHelper() // constructor
          {
               // create and initialize five new Manufacture objects
    
              manufacture1 = new Manufacture();
              manufacture1.setItemNumber(1);
              manufacture1.setProductName("Slinks");
              manufacture1.setNumberOfUnitsInStock(4);
              manufacture1.setPriceOfUnit(26.00);
              manufacture1.setManufacture("PD");
              //manufacture1.setRestockingFee(10.00);
    
              manufacture2 = new Manufacture();
              manufacture2.setItemNumber(2);
              manufacture2.setProductName("Googles");
              manufacture2.setNumberOfUnitsInStock(6);
              manufacture2.setPriceOfUnit(15.00);
              manufacture2.setManufacture("Flex Vision");
    
              manufacture3 = new Manufacture();
              manufacture3.setItemNumber(3);
              manufacture3.setProductName("LogBook");
              manufacture3.setNumberOfUnitsInStock(6);
              manufacture3.setPriceOfUnit(15.00);
              manufacture3.setManufacture("Log3");
    
              manufacture4 = new Manufacture();
              manufacture4.setItemNumber(4);
              manufacture4.setProductName("Gloves");
              manufacture4.setNumberOfUnitsInStock(4);
              manufacture4.setPriceOfUnit(12.00);
              manufacture4.setManufacture("Converse");
    
              manufacture5 = new Manufacture();
              manufacture5.setItemNumber(5);
              manufacture5.setProductName("Closing Loop");
              manufacture5.setNumberOfUnitsInStock(18);
              manufacture5.setPriceOfUnit(2.00);
              manufacture5.setManufacture("Here Hopin it Opens");
    
                // add Manufacture objects to Array of Manufactures object
    
                item[0] = manufacture1;
                item[1] = manufacture2;
                item[2] = manufacture3;
                item[3] = manufacture4;
                item[4] = manufacture5;
    
                gridPanel.setLayout(new BorderLayout()); // create a border layout
                gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel
                gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel
                gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel
                add(gridPanel);   // add gridpanel to the frame
          }
    
          // helper method creates a panel for the button
    
          private JPanel createButtonPanel()
    
          {
                ActionListener btnListen = new ButtonListener(); // create my listerner
    
                // Create button objects
    
                firstButton = new JButton("First"); // create button object
                firstButton.setActionCommand("First"); // add actionComman
                firstButton.addActionListener(btnListen); // add Listener command
    
                nextButton = new JButton("Next"); // create button object
                nextButton.setActionCommand("Next"); // add actionCommand
                nextButton.addActionListener(btnListen); // add Listener command
    
                previousButton = new JButton("Previous"); // create button object
                previousButton.setActionCommand("Previous"); // add actionCommand
                previousButton.addActionListener(btnListen); // add Listener command
    
                lastButton = new JButton("Last"); // create button object
                lastButton.setActionCommand("Last"); // add actionCommand
                lastButton.addActionListener(btnListen); // add Listener command
    
                // Create panel object
    
                JPanel panel = new JPanel();
    
                panel.add(firstButton); // add firt button to panel
                panel.add(nextButton); // add next button to panel
                panel.add(previousButton); // add previous button to panel
                panel.add(lastButton); // add last button to panel
    
                return panel; // return the panel
    
          }   // end createButtonPaenl method
    
          private JPanel createLabelPanel()
          {
                // Create label objects
    
                ImageIcon icon = new ImageIcon(".\\src\\manufacturesLogo.jpg");   // load the image file
                JLabel lblLogo = new JLabel(icon); // add the image to the label
    
                lblItemNumber = new JLabel("Item Number:"); // label for name of item
                lblProductName = new JLabel("Product Name:"); // label for name of item
                lblNumberOfUnitsInStock = new JLabel("Units in Stock:"); // label for age of item
                lblPriceOfUnit = new JLabel("PRice of Items:"); // label for age of item
                lblTotalInventory = new JLabel("Item Total:"); // label for age of item
                lblManufacture = new JLabel("Manufacture:"); // label for hair color
                lblRestockingFee = new JLabel("Restock Fee:"); // label for age in ten years
                lblSumOfInventory = new JLabel("Sum of Inventory:");  // age total label, sum of all the manufacture's ages
    
                panel = new JPanel(); // create panel object
                panel.setLayout(new GridLayout(9, 1));
    
                // add labels to the panel
    
                 panel.add(lblLogo);
                 panel.add(lblItemNumber);
                 panel.add(lblProductName);
                 panel.add(lblNumberOfUnitsInStock);
                 panel.add(lblPriceOfUnit);
                 panel.add(lblTotalInventory);
                 panel.add(lblManufacture);
                 panel.add(lblRestockingFee);
                 panel.add(lblSumOfInventory);
    
                return panel;
    
          } // end createLabelPanel method
    
          private JPanel createTextPanel()
          {
                //create instances of text box objects
    
                JLabel lblCompanyName = new JLabel("      Far North"); // label for company name
    
                itemNumberField = new JTextField(); // name text field
                itemNumberField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                productNameField = new JTextField(); // name text field
                productNameField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                numberOfUnitsInStockField = new JTextField(); // age text field
                numberOfUnitsInStockField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                priceOfUnitField = new JTextField(); // age text field
                priceOfUnitField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                totalInventoryField = new JTextField(); // age text field
                totalInventoryField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                restockingFeeField = new JTextField(); // age in ten years field
                restockingFeeField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                manufactureField = new JTextField();   // hair color text field
                manufactureField.setEditable(false);   // set field to not editable to prevent user from changing the data
    
                sumOfInventoryField = new JTextField();    // age total text field to sum the total of the manufacture's ages.
                sumOfInventoryField.setEditable(false);   // set field to not editable to prevent user from changing the data
    
                panel = new JPanel(); // creat panel object
                panel.setLayout(new GridLayout(9, 1)); // create grid layout for fields.
    
                panel.add(lblCompanyName);   // logo field
                panel.add(itemNumberField); // add the name field to the grid layout
                panel.add(productNameField); // add the name field to the grid layout
                panel.add(numberOfUnitsInStockField); // add the age field to the grid layout
                panel.add(priceOfUnitField); // add the age field to the grid layout
                panel.add(totalInventoryField); // add the age field to the grid layout
                panel.add(manufactureField); // add the hair color field to the grid layout
                panel.add(restockingFeeField); // add the age in ten years field to the grid layout
                panel.add(sumOfInventoryField); // add the age total field to the grid layout
    
                return panel; // return the panel
    
          } // end createTextPanel method
    
    
          // nested ButtonListener class
    
          class ButtonListener implements ActionListener
    
          {
                public void actionPerformed(ActionEvent e)
    
                {
    
                      // Parameters for the sort method are the manufactures array and an instance of my FirstNameComparator class
    
                      Arrays.sort(item, new FirstNameComparator()); // Sort the manufactures array
                      sumOfInventoryField.setText(String.valueOf(getSumOfInventory()));
    
                      //sumOfInventoryField.setText(String.valueOf(nf.format(Item.getSumOfInventory())));  // display the sum of the manufacture's age
    
                      //add button functions
    
                      if (e.getActionCommand() == "First") // if First button is clicked
    
                      {
                          ArrayIndex = 0;
                          displayItem();
                    }
    
                      if (e.getActionCommand() == "Next") // if Next button is clicked
                      {
                          ArrayIndex = ArrayIndex + 1;
                         displayItem();
                      }
    
                      if (e.getActionCommand() == "Previous") // if Previous button is clicked
                      {
                          ArrayIndex = ArrayIndex - 1;
                          displayItem();
                      }
    
                      if (e.getActionCommand() == "Last") // if Last button is clicked
    
                      {
                          ArrayIndex = 4;
                          displayItem();
                      }
    
                } // end actionPerformed
    
          } // end class ButonListerner
            private void displayItem() {
               
            if (ArrayIndex == -1) {
                // no product
                itemNumberField.setText(null);
                productNameField.setText(null);
                manufactureField.setText(null);
                numberOfUnitsInStockField.setText(null);
                priceOfUnitField.setText(null);
                restockingFeeField.setText(null);
                totalInventoryField.setText(null);
            } else {
                // get the product by its index
                Manufacture manufacture = (Manufacture) item[ArrayIndex];
                        //item[ArrayIndex];
                // update the fields
                itemNumberField.setText(String.valueOf(manufacture.getItemNumber()));
                productNameField.setText(manufacture.getProductName());
                manufactureField.setText(manufacture.getManufacture());
                numberOfUnitsInStockField.setText(String.valueOf(manufacture.getNumberOfUnitsInStock()));
                totalInventoryField.setText(String.valueOf(nf.format(manufacture.getValueOfInventory())));
                priceOfUnitField.setText(String.valueOf(nf.format(manufacture.getPriceOfUnit())));
                restockingFeeField.setText(String.valueOf(nf.format(manufacture.getRestockingFee())));
    
            }
            }
            public double getValue() {
            double value = 0.00;
            for (int i = 0; i < count; i++) {
                value + manufacture[i].totalInventoryField;
            }
            return value;
            }
    } // end FrameHelperClass

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Do you have a question or a problem?
    Should the following:
    value + manufacture[i].totalInventoryField;
    be
    value += manufacture[i].totalInventoryField; // accumulate total in value

  3. #3
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    I had a question and your post answered it for me. i was not able to get the total inventory value to compute right thanks.

Similar Threads

  1. adding in array String
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 04:28 PM
  2. adding to an array
    By mayhewj7 in forum New To Java
    Replies: 14
    Last Post: 02-19-2009, 05:41 AM
  3. adding GUI to Inventory
    By voyager91 in forum New To Java
    Replies: 8
    Last Post: 02-01-2009, 05:20 AM
  4. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •