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

(3) The Shopper Class Implement a class called Shopper which represents a person

ID: 3787299 • Letter: #

Question

(3) The Shopper Class Implement a class called Shopper which represents a person that buys grocery items. The Shopper should maintain an array (called cart) that contains all the loose Groceryltem objects that the shopper is planning to purchase. You should create (and make use of) a MAX CART ITEMS static constant to represent the maximum number of items that can be in the cart at any time (set to 100) Create the following methods: public get methods for the instance variables. A zero-argument constructor. A toString0 method that returns a string representation of the shopper in the form Shopper with shopping cart containing 10 items additem(0: adds a given Groce em to the shopping cart. removeltem (Groceryltem item): removes the given item from the shopping cart. Make sure that the array does not have gaps in it (i.e., move the last item in the cart into the removed item's position). (Look at my COMP1405 Java Notes 5 pages 138-141 to avoid messing up the array while removing from it) http://people.scs.carleton.cal lanthieriteaching/COMP1405/NotesiCOMP1405 Ch5 ArraysAndSe arching. pd A packBags() method which returns an array of packed GroceryBag objects and possibly some unpacked Groceryltem objects (e.g., a case of coke). The method should use the following packing algorithm: Fill as many bags as are necessary one at a time, each to just below its weight limit. That is take items in order from the cart and place them into an initially empty bag and stop when the next item will cause the bag to exceed its weight limit. Then make a new bag and pack that one until it is full. The items are taken sequentially from the cart, although as each item is packed it should be removed from the cart (use the removeltem0 method). Therefore, the use of the removeltem0 method will affect the order of the items as they are packed. If an item is too heavy (i.e., exceeds the MAX WEIGHT) to be placed in a bag (e.g., a case of coke), then it should be left in the cart. In theory, all items in the cart may be too heavy, which means no bags may be packed. Or, all items in the cart are exactly the bag limit in weight, which means the number of bags will equal the number of items initially in the cart. Either way, your method should return an array with a size equal to the number of packed bags.

Explanation / Answer


public class GroceryItem {

   public String itemName;
   public int itemPrice;
   public float weight;
   public boolean perishable;

   public GroceryItem(String itemName, int itemPrice, float weight,
           boolean perishable) {
       this.itemName = itemName;
       this.itemPrice = itemPrice;
       this.weight = weight;
       this.perishable = perishable;
   }

   public GroceryItem(String itemName, int itemPrice, float weight) {
       this.itemName = itemName;
       this.itemPrice = itemPrice;
       this.weight = weight;
   }

   public GroceryItem() {
       itemName = "";
       itemPrice = 0;
       weight = 0;
       perishable = false;

   }

   public String getItemName() {
       return itemName;
   }

   public int getItemPrice() {
       return itemPrice;
   }

   public float getWeight() {
       return weight;
   }

   public boolean isPerishable() {
       return perishable;
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result
               + ((itemName == null) ? 0 : itemName.hashCode());
       result = prime * result + itemPrice;
       result = prime * result + (perishable ? 1231 : 1237);
       result = prime * result + Float.floatToIntBits(weight);
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       GroceryItem other = (GroceryItem) obj;
       if (itemName == null) {
           if (other.itemName != null)
               return false;
       } else if (!itemName.equals(other.itemName))
           return false;
       if (itemPrice != other.itemPrice)
           return false;
       if (perishable != other.perishable)
           return false;
       if (Float.floatToIntBits(weight) != Float.floatToIntBits(other.weight))
           return false;
       return true;
   }

   @Override
   public String toString() {
       return "GroceryItem [itemName=" + itemName + ", itemPrice=" + itemPrice
               + ", weight=" + weight + ", perishable=" + perishable + "]";
   }

}

*********************************************************************

import java.util.ArrayList;
import java.util.List;

public class Shopper {

   public final static int MAX_CART_ITEMS = 100;

   public List<GroceryItem> cartItems;

   public List<GroceryItem> getCartItems() {
       return cartItems;
   }

   public static int getMaxCartItems() {
       return MAX_CART_ITEMS;
   }

   public Shopper() {
       cartItems = new ArrayList<GroceryItem>();
   }

   @Override
   public String toString() {
       return "Shopper with shopping cart contains 10 items";
   }

   public void addItem() {
       if (cartItems.size() < 100) {
           GroceryItem item = new GroceryItem();
           cartItems.add(item);
       } else
           System.out.println("shop is full with grocery items");
   }

   public int removeItem(GroceryItem item) {
       int count = 0;

       for (GroceryItem item1 : cartItems) {
           if (item1.itemName.equals(item.itemName)) {
               cartItems.remove(item1);
               count++;
           }

       }

       return count;
   }

   public int packBags() {
       float weightLimit = 50.0f;
       int count = 0;
       List<GroceryItem> packbag1 = new ArrayList<GroceryItem>();
       for (GroceryItem item : cartItems) {
           if (item.weight > weightLimit) {
               List<GroceryItem> packbag = new ArrayList<GroceryItem>();
               count++;
           } else {
               if (item.weight < weightLimit) {
                   if (item.itemName.equals(item.itemName)) {
                       packbag1.add(item);
                       count++;
                   } else {
                       List<GroceryItem> packbag = new ArrayList<GroceryItem>();
                       count++;
                   }
               }
           }

       }
       return count;

   }

}