Write a class named GroceryList that represents a list of it
Solution
GroceryItemOrder.java
public class GroceryItemOrder {
   
    //Declaring instance variables
    private String name;
    private int quantity;
    private double pricePerUnit;
   
    //Parameterized constructor
    public GroceryItemOrder(String name, int quantity, double pricePerUnit) {
        this.name = name;
        this.quantity = quantity;
        this.pricePerUnit = pricePerUnit;
    }
   
    //Getting the cost of each item
    public double getCost()
    {
        return quantity*pricePerUnit;
    }
   
    //Setter method which sets the quanity
 public void setQuantity(int quantity)
 {
    this.quantity=quantity;
 }
 
 /* toString() method which displays
 * the content of an object inside it
 */
 @Override
 public String toString() {
    return \"\ Name=\" + name + \"\ Quantity=\" + quantity
            + \"\ Price Per Unit=\" + pricePerUnit;
 }
 
 }
_______________________________
GroceryList.java
import java.util.Arrays;
public class GroceryList {
   
    //Declaring variables
    private GroceryItemOrder list[] = null;
    int no_of_items;
   //Zero argumented constructor
    public GroceryList() {
        //Creating an array of size 10
        list = new GroceryItemOrder[10];
       
        //initialize the no of items to zero
        no_of_items = 0;
    }
  
    //This method will add an item to the grocery list
    public void add(GroceryItemOrder item) {
        //Checking the no of items in the grocery list
        if (no_of_items < 10) {
            list[no_of_items] = item;
            no_of_items++;
   
        }
        else
            System.out.println(\"::Basket Full ::\");
    }
  
    /* This method will find the total cost
    * of all the items in the grocery list
    */
    public double getTotalCost() {
        double tot_cost = 0.0;
        for (int i = 0; i < no_of_items; i++) {
           
            //Calculating the total cost
            tot_cost += list[i].getCost();
        }
        return tot_cost;
    }
   //toString() method is used to read the contents of an object
    @Override
    public String toString() {
        for(int i=0;i<no_of_items;i++)
        {
            System.out.println(list[i].toString());
        }
        return \"\";
    }
}
____________________________
Test.java
public class Test {
    public static void main(String[] args) {
        //Creating an GroceryList Object
        GroceryList gl=new GroceryList();
       
        //Creating an GroceryItemOrder object by passing values as arguments
        GroceryItemOrder gio1=new GroceryItemOrder(\"Apples\",10,0.20);
       
        //Adding the Grocery item to the Grocery list
        gl.add(gio1);
        GroceryItemOrder gio2=new GroceryItemOrder(\"Strawberries\",5,5);
        gl.add(gio2);
        GroceryItemOrder gio3=new GroceryItemOrder(\"Chocolate\",50,1);
        gl.add(gio3);
        GroceryItemOrder gio4=new GroceryItemOrder(\"Pineapples\",5,5);
        gl.add(gio4);
        GroceryItemOrder gio5=new GroceryItemOrder(\"Milk\",2,3);
        gl.add(gio5);
        GroceryItemOrder gio6=new GroceryItemOrder(\"Chewing Gums\",20,0.20);
        gl.add(gio6);
        GroceryItemOrder gio7=new GroceryItemOrder(\"Lettuce\",2,1);
        gl.add(gio7);
        GroceryItemOrder gio8=new GroceryItemOrder(\"Yogurt\",3,0.50);
        gl.add(gio8);
        GroceryItemOrder gio9=new GroceryItemOrder(\"Chicken\",1,7);
        gl.add(gio9);
        GroceryItemOrder gio10=new GroceryItemOrder(\"Banana\",12,0.50);
        gl.add(gio10);
        System.out.println(\":: List Of Grocery Items ::\");
       
        //Displaying all the items in the Grocery list
        System.out.println(gl.toString());
       
        //Displaying the total cost of all the items in the Grocery List
        System.out.println(\"Toatl Cost of All Items :$\"+gl.getTotalCost());
       
}
}
____________________________
Output:
:: List Of Grocery Items ::
Name=Apples
 Quantity=10
 Price Per Unit=0.2
Name=Strawberries
 Quantity=5
 Price Per Unit=5.0
Name=Chocolate
 Quantity=50
 Price Per Unit=1.0
Name=Pineapples
 Quantity=5
 Price Per Unit=5.0
Name=Milk
 Quantity=2
 Price Per Unit=3.0
Name=Chewing Gums
 Quantity=20
 Price Per Unit=0.2
Name=Lettuce
 Quantity=2
 Price Per Unit=1.0
Name=Yogurt
 Quantity=3
 Price Per Unit=0.5
Name=Chicken
 Quantity=1
 Price Per Unit=7.0
Name=Banana
 Quantity=12
 Price Per Unit=0.5
Toatl Cost of All Items :$128.5
__________Thank You




