175k views
4 votes
Write a class named grocerylist that represents a list of items to buy from the market. write another class called item that represents a request to purchase a particular item in a given quantity (for example four boxes of cookies). the grocerylist class should use an array field to store items and to keep track of its size (number of the items in the list so far). assume that a grocery list will have no more than 50 items.

User Robdodson
by
8.1k points

1 Answer

3 votes
class Item {
// item class attributes
string itemName;
int itemQuantity;
double itemPrice;
...
}

class grocerylist {

// you can use arrays or any other containers like ArrayList, Vectors,...etc depends on programming language you use

Item[50] itemList;
int size;
public grocerylist () {
this.size = 0;
}
public void addItem(Item i) {
itemList[size] = i;
size = size +1; // Or size++
}
}
User SlappyTheFish
by
8.3k points