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:
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 = new Supplies(1, "whiteout", 5, 1.00);

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

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

        Supplies = new Supplies(4, "box of paper", 10, 20.00);

        Supplies = new Supplies(5, "sharpie", 100, 1.00);

        Supplies = new Supplies(6, "coffee cup", 5, 5.00);

        Supplies = new Supplies(7, "sticky note", 30, 1.00);

        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
2 Years
Discussion Span
Last Post by LVZombie

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

Edited by JamesCherrill


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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.