Question
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
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.
Here are the UML for the two classes
Class Item
- String name - Int quantity - double pricePerUnit - int ItemCount // number of items left in the inventory +Item(String name, int quantity, double pricePerUnit) //this method must make sure that enough items are in the inventory before ordering the item
+getName() : String
+getQuantity(): int +getPricePerUnit() : int
+getTotalCost(): double //returns the total cost of the item purchased
+getItemCount() : int
+setQuantity(int newQuantity): void
+setPricePerUnit(int newPrice): void +setName(String newName): String
+equals(Item other):boolan
+toString(): String Class GroceryList
GroceryList
+item[] items +static int count //static variable to keep track of the number of the items ordered. Every time an item is added to the list this variable will be incremented by 1. +GroceryList() // Constructs a new empty grocery list
+getList() : item[] //returns the list of the items ordered
+add(Item newItem): void //adds the given item to the list +remove(Item oldItem) :boolean //removes the item from the list by setting it to null and returns true. If the item is not in the list returns false
+getTotalCost() : double //returns the total cost for all the items in the list. This method must call the method getTotalCost from the item class
+find(String name): int // finds the item in the list and returns the index of the item. If the item is not in the list returns -1.
+toString(): string // returns a string representing all the items in the list. This method must call the toString method in the Item class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started