Hello all,

I am working on assignment for class and I thought I had this one but something is wrong. This is an inventory control program that needs to display 7 items with the item number, name, number in stock, price and value. The program works for item 7 but all items before are not showing. I am sure this is a dumb error but any assistance in dumbed down language would be much appreciated.
This is how it runs:
run:
inventory1.Supplies@15db9742
inventory1.Supplies@6d06d69c
inventory1.Supplies@7852e922
inventory1.Supplies@4e25154f
inventory1.Supplies@70dea4e
inventory1.Supplies@5c647e05
inventory1.Supplies@33909752
The item number is 7.0
Name of the item is sticky note
The price of each item is 1.0
The number of items in stock are 30.0
The value of the inventory is 30.0
BUILD SUCCESSFUL (total time: 0 seconds)

This is my code:

public class Inventory1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Supplies


        Supplies;
        Supplies = new Supplies(1, "whiteout", 5, 1.00);
        System.out.println(Supplies);

        Supplies = new Supplies(2, "Pen", 50, .50);
        System.out.println(Supplies);

        Supplies = new Supplies(3, "pencil", 50, .25);
        System.out.println(Supplies);

        Supplies = new Supplies(4, "box of paper", 10, 20.00);
        System.out.println(Supplies);

        Supplies = new Supplies(5, "sharpie", 100, 1.00);
        System.out.println(Supplies);

        Supplies = new Supplies(6, "coffee cup", 5, 5.00);
        System.out.println(Supplies);

        Supplies = new Supplies(7, "sticky note", 30, 1.00);
        System.out.println(Supplies);

        System.out.println("The item number is " + Supplies.getItemNumber());
        System.out.println("Name of the item is " + Supplies.getProductName());
        System.out.println("The price of each item is " + Supplies.getItemPrice());
        System.out.println("The number of items in stock are " + Supplies.getInStock());
        System.out.println("The value of the inventory is " + Supplies.value());


    }// end main

}//end inventory class
//create class
class Supplies{
   private double itemNumber;
   private String productName;
   private double inStock;
   private double itemPrice;

   public Supplies(double number, String name, double stock, double price){
      itemNumber = number;
      productName = name;
      inStock = stock;
      itemPrice = price;

   } //end constructor 


   public void setProductName(String name) {
        productName = name;
   }// end set 

   public String getProductName(){
      return productName;
   }//end get

   public void setItemNumber(double number){
       itemNumber = number;
   } //end set

   public double getItemNumber(){
        return itemNumber;
   }//end get

   public void setInStock(double stock){
       inStock = stock;
   } //end set

   public double getInStock(){
        return inStock;
   }//end get

   public void setItemPrice(double price){
       itemPrice = price;
   } //end set

   public double getItemPrice(){
        return itemPrice;
   }//end get

   //inventory value calculation
   public double value(){
        return itemPrice * inStock;
   }//end value calculation


}// end class

Recommended Answers

All 6 Replies

I did fix the order of the instock and price too. I know those were out of order.

7 works because on lines 32-36 you print all its fields explicitly. For the others you use System.out.println(Supplies);

This is all about the toString() method...

Every Java class inherits a toString() method from the Object class, so methods like println use that to convert the object you want to print into a String. The inherited method prints the class name and the object's hash (normally the same as its address). If you want to display something more helpful, override the toString method in your class to return a useful string. Eg

class Person {
    private String givenName, familyName;
    ...
    @Override
    public String toString() {
        return "Person: " + givenName + " " + familyName;
    }

You should do this for every class you create so when you print an instance you get useful output.

You only create one Supplies object and then proceed to make it equal to product1, then product2, then product3, etc. At no time in your code do you have two Supplies objects in existence, you just keep resetting the one.
This is why item 7 works, it is the only one that survives your set up.

You probably need a loop that creates items 1 (through to 7), outputs its values then loops around creating the next item up - although this depends on what exactly it is you need to achieve.
Does that make sense?

^ well spotted, but that description not quite exactly correct - "Supplies" is a reference variable, not an object. The code creates 7 objects, and keeps changing the reference variable to refer to the most recent one, so it has no way to access the first 6. Eventually the first 6 will be garbage collected because there are no surving references to them.

ps: having a variable with exactly the same name as the class isn't a Java error, but its really really confusing. Java naming convention is that class names should start with an upper-case letter, but variables should start with a lower case letter

Thank you both, maybe this will make more sense after I sleep on it. I will check back in the morning and try again.

I missed a huge step, some sleep helped LOL! I am debugging my programs while I sleep now haha. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.