Results 1 to 2 of 2
  1. #1
    sparkyii is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default How come it does not display the inventory

    In the InventoryMain I have 4 different list I want to dispaly. The program will display 4 list but all are only the HP information.

    What do I need to do to correct this?


    import java.util.Locale;
    import java.text.NumberFormat;


    // Class that represents a product in an inventory
    class Product implements Comparable
    {
    private int itemnum;
    private double prodprice;
    private int numinstock;
    private String prodname;

    public Product()
    {
    itemnum = 0;
    prodprice = 0.0;
    numinstock = 0;
    }




    // Method to set the Item number
    public void setItemNum(int num)
    {
    itemnum = num;
    }
    // Method to get the Item number
    public int getitemnumber()
    {
    return itemnum;
    }


    // Method to set the Price of the product
    public void setprodprice(double prod)
    {
    prodprice = prod;
    }
    // Method to get the Price of the product
    public double getprodprice()
    {
    return prodprice;
    }


    // Method to set the Price of the product
    public void setnuminstock(int numstock)
    {
    numinstock = numstock;
    }
    // Method to get the Price of the product
    public int getnuminstock()
    {
    return numinstock;
    }


    // Method to set the name of the product
    public void setprodname(String name)
    {
    prodname = name;
    }
    // Method to get the name of the product
    public String getprodname()
    {
    return prodname;
    }
    // Method to calculate the weekly pay
    public double calculateValue()
    {
    return prodprice * numinstock;
    }

    // the compareTo method to implement the Comparable interface. This enables us to sort a list of Products using Arrays.sort()
    // this method returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
    public int compareTo (Object o)
    {
    Product p = (Product) o;
    return prodname.compareTo(p.getprodname());
    }

    // returns a string representation of the product
    public String toString()
    {
    return "Stock Number : " + getitemnumber() + "\n"
    + "Brand Name : " + getprodname() + "\n"
    + "Qty in Stock : " + getnuminstock() + "\n"
    + "Product Price : " + NumberFormat.getCurrencyInstance(Locale.US).format (getprodprice()) + "\n"
    + "Value : " + NumberFormat.getCurrencyInstance(Locale.US).format (calculateValue());
    }


    }//end class Product






    import java.util.Arrays;
    import java.util.Locale;
    import java.text.NumberFormat;




    // Class that represents a collection of Products
    class Inventory
    {


    // place to store all the Products in the inventory
    Product[] inventory;

    // create an empty inventory capable of holding different Products
    public Inventory( )
    {
    inventory = new Product[0];
    }

    // add the Products to the end of the inventory
    public void addProduct(Product new_Product)
    {
    // create a new array of Products
    Product[] new_inventory = new Product[this.getSize() + 1];

    //copy all the old ones to the front of the new array
    for (int i = 0; i < this.getSize(); i++)
    {
    new_inventory[i] = inventory[i];

    }
    // add the new Product to the end of the new array
    new_inventory[this.getSize()] = new_Product;

    // replace the old inventory with the new one
    inventory = new_inventory;

    }
    // return the Products at the location index in the inventory
    public Product getProduct(int index)
    {
    return inventory[index];
    }

    // return the number of Products in the inventory
    public int getSize()
    {
    return inventory.length;
    }

    // run through all the Products in the inventory, and add up their value.
    public double getTotalValueOfAllInventory()
    {
    double tot = 0.00;

    for(int i = 0; i < getSize(); i++)
    {
    tot += inventory[i].calculateValue();
    }
    return tot;
    }

    // sort the Products in the inventory by movie title
    public void sortInventory()
    {
    Arrays.sort(inventory, 0, getSize());
    }

    }

    // Program that displays the value of a product in inventory.
    public class InventoryMain

    {
    public static void main( String args[] )
    {


    //Instantiate an Inventory object
    Inventory myInventory = new Inventory();

    Product myItem = new Product();


    int d = 0;
    myItem.setItemNum(d);


    myItem.setprodname("Dell");
    myItem.setItemNum(1);
    myItem.setnuminstock(37); myItem.setprodprice(Double.valueOf(486.99).doubleV alue());

    // add the newly created object to the existing inventory
    myInventory.addProduct(myItem);




    myItem.setprodname("Compaq");
    myItem.setItemNum(2);
    myItem.setnuminstock(22); myItem.setprodprice(Double.valueOf(383.91).doubleV alue());


    // add the newly created object to the existing inventory
    myInventory.addProduct(myItem);



    myItem.setprodname("Sony");
    myItem.setItemNum(3);
    myItem.setnuminstock(48); myItem.setprodprice(Double.valueOf(531.92).doubleV alue());

    // add the newly created object to the existing inventory
    myInventory.addProduct(myItem);



    myItem.setprodname("HP");
    myItem.setItemNum(4);
    myItem.setnuminstock(12); myItem.setprodprice(Double.valueOf(401.19).doubleV alue());


    // add the newly created object to the existing inventory
    myInventory.addProduct(myItem);

    // sort them by their name
    myInventory.sortInventory();

    // Display the inventory of Products on the screen,
    for (int i = 0; i < myInventory.getSize(); i++)
    {
    System.out.println(myInventory.getProduct(i));
    System.out.println();
    }

    // Display the total value of the inventory on the screen
    System.out.println("The Total Value of the inventory is: " + NumberFormat.getCurrencyInstance(Locale.US).format (myInventory.getTotalValueOfAllInventory()));


    } // end method main

    } // end class InventoryMain

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Can you show what the program does now and how you want to change that by giving examples?

    Please format your code when posting it by using the code tags. One of the icon buttons above the input box will do that.

Similar Threads

  1. Help with Errors in Inventory Program
    By ljk8950 in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2008, 12:49 AM
  2. Final Inventory tweeks
    By badness in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:18 AM
  3. Inventory Program modification help
    By badness in forum Java Applets
    Replies: 1
    Last Post: 01-17-2008, 06:24 AM
  4. Inventory part 2 help please
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 08:51 AM
  5. Inventory program
    By Nexcompac in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:51 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
  •