can some help me fix my code were it compiles and it: Anyone can help me out thi
ID: 3529650 • Letter: C
Question
can some help me fix my code were it compiles and it:
Anyone can help me out this problem?
Write a class named GroceryList that represents a person's list ofitems to buy from the market and another class namedGroceryItemOrder that represents a request to purchase a particularitem in a given quantity (example: four boxes of cookies).
public GroceryList()
Constructs a new empty grocery list.
public void add ( GroceryItemOrdre item)
Adds the given item order to this list, if the list is not full(i.e, has fewer than 10 items).
public double getTotalCost()
Returns the total sum cost of all grocery item orders in thislist.
The GroceryItemOrder class should store an item quantity anda price per unit. A GroceryItemOrder object should have thefollowing methods:
public GroceryItemOrder( String name, int quantity, doublepricePerUnit)
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. Forxeample, four boxes of cookies that cost 2.30 per unit have a totalcost of 9.20
public void setQuantity( int quantity)
Sets this grocery item's quantity to be the given value.
clientgrocerylist2.java:
public class clientgrocerylist2 {
private GroceryItemOrder [] items;
int itemCount;
public groceryList() {
itemCount = 0;
items = new GroceryItemOrder[10];
}
public void add ( GroceryItemOrder item) {
items[itemCount] = item;
itemCount++;
}
public double getTotalCost() {
double totalCost = 0;
for (int i=0; i<itemCount; i++) {
totalCost += items[i].getCost();
}
return totalCost;
}
public static void main(String[] args) {
// Some test code
GroceryList gl = new GroceryList();
gl.add(new GroceryItemOrder("cookies",4,.5));
gl.add(new GroceryItemOrder("crackers",6,1));
System.out.println("Total cost of groceries: "+gl.getTotalCost());
}
}
groceryItemOrder.java:
public class GroceryItemOrder {
double quantity;
String name;
double pricePerUnit;
public GroceryItemOrder( String name , int quantity , double pricePerUnit) {
this.pricePerUnit = pricePerUnit;
this.name = name;
this.quantity = quantity;
}
public double getCost () {
return pricePerUnit*quantity;
}
public void setQuantity ( int quantity ) {
this.quantity = quantity;
}
}
Explanation / Answer
Hey
This is the exact answer for your question..Please Rate...
Code Start From here
GrosoryList Class
import java.util.Scanner;
import java.util.ArrayList;
public class GrosoryList {
/**
* @param args
*/
private ArrayList<GrosoryOrderedItem> glist;
public GrosoryList()
{
super();
glist=new ArrayList<GrosoryOrderedItem>(10);
}
public ArrayList<GrosoryOrderedItem> getGlist() {
return glist;
}
public void setGlist(ArrayList<GrosoryOrderedItem> glist) {
this.glist = glist;
}
public void add(GrosoryOrderedItem g)
{
if(glist.size() < 10)
{
if(glist.add(g))
{
System.out.println("Item is successfully added to List");
}
}
else
{
System.out.println("Grosory List is full");
}
}
public double getTotalCost()
{
double totalCost=0;
for(GrosoryOrderedItem g:glist)
{
totalCost+=g.getCost();
}
return totalCost;
}
public static void main(String []args)
{
GrosoryList gl=new GrosoryList();
GrosoryOrderedItem i1=new GrosoryOrderedItem("Item1",2,10);
GrosoryOrderedItem i2=new GrosoryOrderedItem("Item2",3,30);
GrosoryOrderedItem i3=new GrosoryOrderedItem("Item3",4,10);
GrosoryOrderedItem i4=new GrosoryOrderedItem("Item4",2,30);
GrosoryOrderedItem i5=new GrosoryOrderedItem("Item5",5,10);
GrosoryOrderedItem i6=new GrosoryOrderedItem("Item6",7,30);
GrosoryOrderedItem i7=new GrosoryOrderedItem("Item7",2,20);
GrosoryOrderedItem i8=new GrosoryOrderedItem("Item8",3,30);
GrosoryOrderedItem i9=new GrosoryOrderedItem("Item9",8,10);
GrosoryOrderedItem i10=new GrosoryOrderedItem("Item10",3,30);
GrosoryOrderedItem i11=new GrosoryOrderedItem("Item11",2,10);
gl.add(i1);
gl.add(i2);
gl.add(i3);
gl.add(i4);
gl.add(i5);
gl.add(i6);
gl.add(i7);
gl.add(i8);
gl.add(i9);
gl.add(i10);
gl.add(i11);
System.out.println("*****************************!!!GENERATING BILL!!!*******************************");
System.out.println();
System.out.println("The cost for the Item "+i1.getName()+" is : "+i1.getCost());
System.out.println("The cost for the Item "+i2.getName()+" is : "+i2.getCost());
System.out.println("The cost for the Item "+i3.getName()+" is : "+i3.getCost());
System.out.println("The cost for the Item "+i4.getName()+" is : "+i4.getCost());
System.out.println("The cost for the Item "+i5.getName()+" is : "+i5.getCost());
System.out.println("The cost for the Item "+i6.getName()+" is : "+i6.getCost());
System.out.println("The cost for the Item "+i7.getName()+" is : "+i7.getCost());
System.out.println("The cost for the Item "+i8.getName()+" is : "+i8.getCost());
System.out.println("The cost for the Item "+i9.getName()+" is : "+i9.getCost());
System.out.println("The cost for the Item "+i10.getName()+" is : "+i10.getCost());
System.out.println("The cost for the Item "+i11.getName()+" is : "+i11.getCost());
System.out.println("______________________________________________________________________________________");
System.out.println("The Total cost is : "+gl.getTotalCost());
}
}
GrosortOrderedItem Class
public class GrosoryOrderedItem {
private String name;
private int quantity;
private double pricePerUnit;
public GrosoryOrderedItem() {
super();
}
public GrosoryOrderedItem(String name, int quantity, double pricePerUnit) {
super();
this.name = name;
this.quantity = quantity;
this.pricePerUnit = pricePerUnit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPricePerUnit() {
return pricePerUnit;
}
public void setPricePerUnit(double pricePerUnit) {
this.pricePerUnit = pricePerUnit;
}
public double getCost()
{
return this.getQuantity() * this.getPricePerUnit();
}
}
OUTPUt :
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Item is successfully added to List
Grosory List is full
*****************************!!!GENERATING BILL!!!*******************************
The cost for the Item Item1 is : 20.0
The cost for the Item Item2 is : 90.0
The cost for the Item Item3 is : 40.0
The cost for the Item Item4 is : 60.0
The cost for the Item Item5 is : 50.0
The cost for the Item Item6 is : 210.0
The cost for the Item Item7 is : 40.0
The cost for the Item Item8 is : 90.0
The cost for the Item Item9 is : 80.0
The cost for the Item Item10 is : 90.0
The cost for the Item Item11 is : 20.0
______________________________________________________________________________________
The Total cost is : 770.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.