Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is to be in Java 2. Define a Groceryltem class that represents a re

ID: 3606485 • Letter: T

Question

This program is to be in Java

2. Define a Groceryltem class that represents a request to purchase a particular item at the grocery store in a given quantity (for example, two boxes of cereal). A Groceryltem object should store an item name (a String), an item quantity (an int), and a unit price (a double). Your Groceryltem class should contain the following methods: *A public constructor that takes an item name, quantity, and unit price, and assigns them to the appropriate instance variables. *A getCost() method that returns the total cost of this item (its unit price times its quantity). For example, three cans of soup that cost 1.50 each would have a total cost of 4.50 *A setQuantity() method that sets the Groceryltem's quantity to the supplied value. You

Explanation / Answer

GroceryItem.java

public class GroceryItem {

//Declaring instance variables

private String name;

private int quantity;

private double pricePerUnit;

//Parameterized constructor

public GroceryItem(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 GroceryItem list[] = null;

int no_of_items;

//Zero argumented constructor

public GroceryList() {

//Creating an array of size 10

list = new GroceryItem[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(GroceryItem 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

GroceryItem gio1=new GroceryItem("Apples",10,0.20);

//Adding the Grocery item to the Grocery list

gl.add(gio1);

GroceryItem gio2=new GroceryItem("Strawberries",5,5);

gl.add(gio2);

GroceryItem gio3=new GroceryItem("Chocolate",50,1);

gl.add(gio3);

GroceryItem gio4=new GroceryItem("Pineapples",5,5);

gl.add(gio4);

GroceryItem gio5=new GroceryItem("Milk",2,3);

gl.add(gio5);

GroceryItem gio6=new GroceryItem("Chewing Gums",20,0.20);

gl.add(gio6);

GroceryItem gio7=new GroceryItem("Lettuce",2,1);

gl.add(gio7);

GroceryItem gio8=new GroceryItem("Yogurt",3,0.50);

gl.add(gio8);

GroceryItem gio9=new GroceryItem("Chicken",1,7);

gl.add(gio9);

GroceryItem gio10=new GroceryItem("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

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote