Question
Define a GroceryItem class that represents a request to purchase a particular item at the grocery store in a given quantity (for example, two boxes
Define a GroceryItem class that represents a request to purchase a particular item at the grocery store in a given quantity (for example, two boxes of cereal). A GroceryItem object should store an item name (a String), an item quantity (an int), and a unit price (a double). Your GroceryItem class should contain the following methods: * A public constructor that takes an item name, quantity, and unit price, and assigns them to the appropriate instance variables. * A getCost() method that returns the total cost of this item (its unit price times its quantity). For example, three cans of soup that cost 1.50 each would have a total cost of 4.50. * A setQuantity() method that sets the GroceryItem's quantity to the supplied value. Youmay assume that the argument is always greater than 0. Next, define a GroceryList class that represents a list of items to buy from the grocery store. Your GroceryList should contain an array that holds GroceryItem objects, along with a variable that tracks the current number of items in the list. You may assume that the GroceryList will never contain more than 10 items. The GroceryList class should have the following methods: * A public constructor that creates a new, empty grocery list. * An add() method that takes a GroceryItem object as its argument. This method adds this item to this list if the list has fewer than 10 items. * A getTotalCost() method that adds up and returns the total cost of every item in the list. Write a driver program (either a new class, or a main() method in your GroceryList class) that creates a new GroceryList, populates it with several GroceryItems, and then prints the total cost of the list contents.
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