0

Okay. I'm fairly new in this Java thing, but i'm desperately trying to learn. I've come upon somewhat of a deadend. I'm making an inventory program as part of school and i have a superclass Items with 4 instance variables. No problem there. I have 4 subclasses, two of which is mandatory Food class, which have a futher 2 variables and Nonfood class, which have one more variable. My problems is this.

Right now i'm working with an ArrayList (this is what i know so far) but i'm seriously considering working with a map or a linkedMap.

I have based my ArrayList on my superClass Items, but i'm having trouble getting my subclass variables into my ArrayList. Any idea how that's done. Using relative simple solutions (remember i'm new at this)

I have not yet got my id working. I've, in the spirit of the shop terminology, called it barCode. It's part of my Superclass, and i can't seem to initialize it in my main class.

//constructer from superclass

public Items (int barCode, String itemName, String itemSupplier, double itemPrice, int stock)
{
    this.itemName = itemName;

    barCode = GenerateBarCode();

    this.itemSupplier = itemSupplier;

    this.itemPrice = itemPrice;

    this.stock = stock;

// getter method for barCode

protected int getBarCode()
{
    return barCode;
}

// method for generating barcode

private int GenerateBarCode()
{
    Random barCode = new Random();    

    int barCode1 = barCode.nextInt(100000);
    return barCode1;
}

If any more code i needed, let me know. I'm working on getting it a bit prettyer.

3
  • 3
    Did you intend to write this.barCode = GenerateBarCode();? Commented Oct 18, 2018 at 9:18
  • 1
    To be more specific, barcode is generated internally, and thus should not be provided as a parameter to constructor. Commented Oct 18, 2018 at 9:31
  • Aha that is were i went wrong. I made a normal array where i had a constructor for the id last week. So hoped it could be done the same way. Thanks. Very helpfull Commented Oct 18, 2018 at 13:53

1 Answer 1

1

You have a local variable barCode in your constructor, and it is hiding your instance member also called barCode. Parameters are really local variables, and locals get precedence over fields. This local variable gets the random value, and then disappears at the end of the constructor, like all locals, leaving your field also called barCode with its original value.

You could fix it by changing the parameter to be called barCodeIn, or changing the statement to this.barCode = GenerateBarCode()

However the real solution is to remove the barCode parameter from the items constructor.

Since you are generating it with the GenerateBarCode() function, you don't really need to pass it in from the outside.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.