(2) The GroceryBag Class Implement a class called GroceryBag that represents a b
ID: 3787559 • Letter: #
Question
(2) The GroceryBag Class Implement a class called GroceryBag that represents a bag which will hold Groceryltem objects. The class has the following private static constants: MAXWEIGHT of type double which indicates the maximum weight that the bag can hold (set it to 5 Kg). Make use of this value properly in the code below. MAXITEMS of type integer that indicates the maximum number of items that can be placed in the bag (set it to 25). Make use of this value properly in the code below. The class has the following private instance variables: tems which is an array that will hold Groceryltem objects. numItems which is the number of items in the bag. weight of type float which returns the total weight of all items currently in the bag. Create the following instance methods: Appropriate public get methods A zero-argument constructor. Atostring0 method that shows the number of items in the bag and the total weight of the bag or outputs An empty grocery bag A method called additem0 which adds a given Groce m to the bag only if the total weight of the bag is not exceeded by this item. A method called removeltem (Groceryltem item) which removes the given item from the bag. A heaviestitem( method that returns the heaviest item in the cart. Return null if bag is empty. A has(Groceryltem item) method that returns a boolean indicating whether or not the given item is currently in the bag.Explanation / Answer
// GroceryBag.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class GroceryBag {
private static double MAX_WEIGHT = 5;
private static int MAX_ITEMS = 25;
public GroceryBag()
{
numItems = 0;
weight = 0;
}
private GroceryItem heaviest;
private GroceryItem[] items = new GroceryItem[25];
private float weight;
private int numItems;
public GroceryItem[] getItems() {
return items;
}
public void addItem(GroceryItem item)
{
if (this.weight + item.getWeight() <= MAX_WEIGHT)
{
if (heaviest == null)
{
heaviest = item;
}
else
{
if (heaviest.getWeight() < item.getWeight())
{
heaviest = item;
}
}
items[numItems] = item;
weight += item.getWeight();
numItems++;
}
}
public boolean isItemEqual(GroceryItem item1, GroceryItem item2)
{
if ((item1.getName() == item2.getName()) &&
(item1.getWeight() == item2.getWeight()) &&
(item1.getPrice() == item2.getPrice()) &&
(item1.isPerishable() == item2.isPerishable()))
{
return true;
}
return false;
}
public void removeItem(GroceryItem item)
{
int i;
for(i = 0; i < numItems; i++)
{
if(isItemEqual(items[i], item))
{
items[i] = null;
for(int j = i; (j+1) < numItems; j++)
{
items[j] = items[j+1];
}
numItems--;
break;
}
}
}
public GroceryItem heaviestItem()
{
return heaviest;
}
public boolean has(GroceryItem item)
{
for(int i = 0; i < numItems; i++)
{
if (isItemEqual(item, items[i]))
{
return true;
}
}
return false;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public int getNumItems() {
return numItems;
}
public void setNumItems(int numItems) {
this.numItems = numItems;
}
@Override
public String toString() {
if (numItems == 0)
return "An empty grocery bag";
StringBuilder str = new StringBuilder();
str.append("A " + weight + "kg bag with " + numItems + " items");
str.append(" ");
for (int i = 0; i < numItems; i++)
{
str.append(items[i].toString());
str.append(" ");
}
return str.toString();
}
}
// GroceryItem.java
public class GroceryItem {
private float weight;
private String name;
private float price;
private boolean perishable;
public GroceryItem()
{
this.weight = 0;
this.name = "";
this.price = 0;
this.perishable = false;
}
public GroceryItem(String name, float price, float weight, boolean perishable)
{
this.weight = weight;
this.name = name;
this.price = price;
this.perishable = perishable;
}
public GroceryItem(String name, float price, float weight)
{
this.weight = weight;
this.name = name;
this.price = price;
this.perishable = false;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public boolean isPerishable() {
return perishable;
}
public void setPerishable(boolean perishable) {
this.perishable = perishable;
}
@Override
public String toString()
{
return name + " weighing " + weight + "kg with price $" + price;
}
public static void main(String[] args)
{
GroceryItem grocery = new GroceryItem("Cascade", 4.79f, 2.4f);
System.out.println(grocery);
}
}
// GroceryBagTestProgram.java
public class GroceryBagTestProgram {
public static void main(String[] args) {
GroceryItem g1, g2, g3, g4, g5, g6;
GroceryBag b1, b2;
g1 = new GroceryItem("Jumbo Cherries", 6.59f, 1.0f);
g2 = new GroceryItem("Smart-ones Frozen Entrees Cherries", 1.99f, 0.311f, false);
g3 = new GroceryItem("SnackPack Pudding", 0.99f, 0.396f);
g4 = new GroceryItem("Nabob Coffee", 3.99f, 0.326f);
g5 = new GroceryItem("Fresh Salmon", 4.99f, 0.413f, true);
g6 = new GroceryItem("Coca-Cola 12-pack", 3.99f, 5);
b1 = new GroceryBag();
b2 = new GroceryBag();
b1.addItem(g1);
b1.addItem(g2);
b1.addItem(g3);
b1.addItem(g4);
b1.addItem(g5);
b1.addItem(g5);
b1.addItem(g5);
System.out.println("BAG 1: " + b1);
for(int i = 0; i < b1.getNumItems(); i++)
{
System.out.println(" " + b1.getItems()[i]);
}
System.out.println("BAG 2: " + b2);
System.out.println(" Heaviest item in BAG 2: " + b2.heaviestItem());
b1.addItem(g6);
b2.addItem(g6);
System.out.println(" BAG 1: " + b1);
System.out.println("BAG 2: " + b2);
System.out.println(" BAG 1 contains Nabob Coffee: " + b1.has(g4));
System.out.println("BAG 1 contains a case of coke: " + b1.has(g6));
System.out.println("BAG 2 contains a case of coke: " + b2.has(g6));
System.out.println(" Heaviest item in BAG 1: " + b1.heaviestItem());
System.out.println(" Heaviest item in BAG 2: " + b2.heaviestItem());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.