Write a class named GroceryList that represents a list of items to buy from the market, and another class named GroceryItemOrder that represents a request to purchase a particular item in a given quantity (example: four boxes of cookies). The GroceryList class should use an array field to store the grocery items and to keep track of its size (number of items in the list so far). Assume that a grocery list will have no more than 10 items. A GroceryList object should have the following methods:
public GroceryList()
Constructs a new empty grocery list.
public void add(GroceryItemOrder item)
Adds the given item order to this list if the list has fewer than 10 items.
public double getTotalCost()
Returns the total sum cost of all grocery item orders in this list.
The GroceryItemOrder class should store an item quantity and a price per unit. A GroceryItemOrder object
should have the following methods:
public GroceryItemOrder(String name, int quantity, double pricePerUnit)
Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.
public double getCost()
Returns the total cost of this item in its given quantity. For example, four boxes of cookies that cost 2.30 per unit
have a total cost of 9.20.
public void setQuantity(int quantity)
Sets this grocery item’s quantity to be the given value.
PROGRAM CODE:
import java.util.Arrays;
public class GroceryList {
private static final int MAX_ITEMS = 10;
private GroceryItemOrder[] items;
private int size;
// Constructs a new empty grocery list
public GroceryList() {
items = new GroceryItemOrder[MAX_ITEMS];
size = 0;
}
// Adds the given item order to this list if the list has fewer than 10 items
public void add(GroceryItemOrder item) {
if (size < MAX_ITEMS) {
items[size++] = item;
}
}
// Returns the total sum cost of all grocery item orders in this list
public double getTotalCost() {
double totalCost = 0;
for (int i = 0; i < size; i++) {
totalCost += items[i].getCost();
}
return totalCost;
}
@Override
public String toString() {
return "GroceryList{" +
"items=" + Arrays.toString(Arrays.copyOfRange(items, 0, size)) +
", size=" + size +
'}';
}
}
public class GroceryItemOrder {
private String name;
private int quantity;
private double pricePerUnit;
// Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.
public GroceryItemOrder(String name, int quantity, double pricePerUnit) {
this.name = name;
this.quantity = quantity;
this.pricePerUnit = pricePerUnit;
}
// Returns the total cost of this item in its given quantity
public double getCost() {
return quantity * pricePerUnit;
}
// Sets this grocery item’s quantity to be the given value
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "GroceryItemOrder{" +
"name='" + name + '\'' +
", quantity=" + quantity +
", pricePerUnit=" + pricePerUnit +
", cost=" + getCost() +
'}';
}
}
public class Main {
public static void main(String[] args) {
GroceryList groceryList = new GroceryList();
groceryList.add(new GroceryItemOrder("Cookies", 4, 2.30));
groceryList.add(new GroceryItemOrder("Milk", 1, 3.00));
groceryList.add(new GroceryItemOrder("Bread", 2, 2.50));
System.out.println("Total cost of grocery items: " + groceryList.getTotalCost());
}
}