Write a class named GroceryList that represents a list of it

Write a class named GroceryList that represents a list of items to be purchased from a grocery store, and another class named GroceryItemOrder that represents a purchase item in a given quantity (example: four boxes of cookies). The GroceryList class should use an array to store the grocery items and an integer to keep track of the number of items in the list. Assume that a grocery list will have no more than 10 items. In other words, in the GroceryList class you should define two fields: one is an array that can accommodate 10 items and the other is an integer that represents the number of items are actually present in the array so far. Your GroceryList class should have the following constructor: public GroceryList () Constructs a new empty grocery list. A GroceryList object should have the following methods: public void add (GroceryItemOrder item) Adds the given item 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 information about a purchase item, including its name, quantity, and unit price. A GroceryItemOrder object should have the following constructor and methods: public GroceryItemOrder (String name, int quantity, double pricePerUnit) Constructs an item to purchase with the given name, in the given quantity and the give unit price. public double getCost() Returns the 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. public String toString() Returns string representation of this GroceryItemOrder object.

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

 Write a class named GroceryList that represents a list of items to be purchased from a grocery store, and another class named GroceryItemOrder that represents
 Write a class named GroceryList that represents a list of items to be purchased from a grocery store, and another class named GroceryItemOrder that represents
 Write a class named GroceryList that represents a list of items to be purchased from a grocery store, and another class named GroceryItemOrder that represents
 Write a class named GroceryList that represents a list of items to be purchased from a grocery store, and another class named GroceryItemOrder that represents

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site