Write a class named GroceryList that represents a list of items to buy from the
ID: 3798201 • Letter: W
Question
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 (e.g., 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 GroceryltemOrder (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.Explanation / Answer
Java Code:
public class GroceryTester{ /*main class for testing*/
static class GroceryList{ /* grocery list class*/
GroceryItemOrder[] groceryList=null; /* array for storing grocery items*/
private int listSize;
public GroceryList(){ /*initiation*/
groceryList=new GroceryItemOrder[10];
listSize=0;
}
public void add(GroceryItemOrder item){ /*function to add groceryItem orders*/
if(listSize<10){ /* add to list if list is not full*/
groceryList[listSize]=item;
listSize++;
}
}
public double getTotalCost(){ /*function return the total cost of grocery items in the list*/
double totalCost=0;
for(int i=0;i<listSize;i++){
totalCost=totalCost+groceryList[i].getCost();
}
return totalCost;
}
}
static class GroceryItemOrder{ /* class grocery item order*/
String name;
int quantity;
double pricePerUnit;
public GroceryItemOrder(String nme,int qty,double ppu){
name=nme;
quantity=qty;
pricePerUnit=ppu;
}
public double getCost(){
return quantity*pricePerUnit;
}
public void setQuantity(int qty){
quantity=qty;
}
}
public static void main(String []args){
/* creating grocery list*/
GroceryList grocery=new GroceryList();
/*creating three grocery items*/
GroceryItemOrder orderOne=new GroceryItemOrder("cookies",4,2.5);
GroceryItemOrder orderTwo=new GroceryItemOrder("soap",6,9.5);
GroceryItemOrder orderThree=new GroceryItemOrder("toothpaste",1,15);
/* adding grocery items to grocery list*/
grocery.add(orderOne);
grocery.add(orderTwo);
grocery.add(orderThree);
/* print out the toal cost of items in the list*/
System.out.println("Total cost of items in grocery list:"+grocery.getTotalCost());
}
}
OUTPUT:
Total cost of items in grocery list: 82.0
USAGE: Copy the code to file named GroceryTester.java. Compile and run to get the above output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.