yoshifish
yoshifish

Reputation: 73

How to construct references for array?

public class ShoppingCart {

    //initialise the array

    Item[] myItemList = new Item[100];
    CartItem[] cartItem = new CartItem[100];
    /* Just some getter/setter methods ignore them for now.
    public Item[] getItemList () {
        return this.myItemList;
    }

    public CartItem[] getCartItem () {
        return this.cartItem;
    }
 */
    Random randomGenerator = new Random();
    public CartItem[] Shop (int numOfItems) {
        for (int i = 0; i < numOfItems; i++) {
            CartItem.cartItem[i] =randomGenerator.nextInt(numOfItems-1); 
        }

        return cartItem;
    }
}

Here's the explanation for the class:
ShoppingCart.java: This is the complete shopping cart of the shopper. In other words, the shopper deposits the items she purchases in an instance of this class. This class has two attributes, a reference to an array of cartItems called basket and a reference to an array of items available in the super market. The constructor takes the reference of an array of items as a parameter and initializes this attribute. The constructor also can construct the basket.

The shopper completes her shopping in this class. The policy for buying an item is the following. Suppose there are k items in the inventory, she generates a random number between 0 and k to choose an item and then buys 50 pieces of that item. She also has the responsibility to re-stock an item if there are less than 50 pieces of that item.

Upvotes: 0

Views: 88

Answers (2)

dacwe
dacwe

Reputation: 43504

You access CartItem.cartItem in a static way. Just remove CartItem. and you will be fine.


Apart from that you have some potential problems with your code:

  1. When you are doing CartItem[] cartItem = new CartItem[100]; you are not initializing the elements in the array. You need to loop over the items and initialize them otherwise they will be null..

  2. randomGenerator.nextInt(numOfItems-1);: the nextInt(n) method randomizes a number beteen 0 (included) and n (excluded). When you pass numOfItems - 1 you are potentially missing one element from your random (the last one).

  3. Shouldn't the Shop (or shop?) method take an array of items that it can shop instead of already having the array as an instance variable?

Upvotes: 2

Jon Newmuis
Jon Newmuis

Reputation: 26502

You should not specify the class when you say CartItem.cartItem[i] = randomGenerator.nextInt(numOfItems-1), since cartItem is not a static member. Instead, you should say cartItem[i] =randomGenerator.nextInt(numOfItems-1), which would result in:

public class ShoppingCart {


    Item[] myItemList = new Item[100];
    CartItem[] cartItem = new CartItem[100];
    /* Just some getter/setter methods
    public Item[] getItemList () {
        return this.myItemList;
    }

    public CartItem[] getCartItem () {
        return this.cartItem;
    }
 */
    Random randomGenerator = new Random();
    public CartItem[] Shop (int numOfItems) {
        for (int i = 0; i < numOfItems; i++) {
            cartItem[i] =randomGenerator.nextInt(numOfItems-1); 
        }

        return cartItem;
    }
}

Upvotes: 3

Related Questions