can someone help me fix my code were it compiles and the output is: printing cos
ID: 3529636 • Letter: C
Question
can someone help me fix my code were it compiles and the output is:
printing cost of 10 items
total cost of items added to clientgrocerylist 92.00
adding more than 10 item to test the grocery list
public class clientgrocerylist {
static int count;
private GroceryItemOrder [] GroceryItemOrder = GroceryItemOrder [10];
public GroceryList () {
public void add (GroceryItemOrder item) {
try {
if (count >=10)
throw new IllegalArgument Exception () ;
else {
GroceryItemOrder [ count] =item;
count++;
}
}
catch (Exception e) {
System.out.println(e);
}
}
public class clientgroceryOrder {
private String name;
private int quatnity;
privte double pricePerUnit;
public clientgroceryOrder(String name, int quantity, double pricePerUnit)
{
this.name = name;
this.pricePerUnit = pricePerUnit
setQuantity(quantity);
}
public double getCost ()
{
return quantity*pricePerUnit
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
}
public class clientgrocery {
public static void main (String[] args )
{
String itemName ="cookies";
int quantity = 4;
double pricePerItem = 2.30;
GroceryItemOrder itemOrder = new GroceryItemOrder( itemName,quantity,pricePerItem);
System.out.println(print int cost of 10 items ");
clientgrocerylist = new clientgrocerylist ();
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
list.add(itemOrder);
System.out.println(" Total cost of items added to " + clientgrocerylist " + "test the grocery list" );
list.add(itemOrder);
}
}
Explanation / Answer
Grosorylist class
-----------------------------------------------
import java.util.Scanner;
import java.util.ArrayList;
public class Grosorylist {
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());
}
}
----------------------------------------------------
GrosoryOrderedItem 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.