In Java please The GroceryBag and Grocerie item class is giv

In Java please. The GroceryBag and Grocerie item class is given. Please show the code and the given result of question 3 .

Q3 -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 GroceryItem 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 toString() method that returns a string representation of the shopper in the form: \"Shopper with shopping cart containing 10 items\".
addItem(): adds a given GroceryItem to the shopping cart. removeItem(GroceryItem 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.ca/~lanthier/teaching/COMP1405/Notes/COMP1405_Ch5_ArraysAndSe arching.pdf
A packBags() method which returns an array of packed GroceryBag objects and possibly some unpacked GroceryItem 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 removeItem() method). Therefore, the use of the removeItem() 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.
Make use of the following test program:
public class ShopperTestProgram {     public static void main(String args[]) {         GroceryItem g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11;

        g1 = new GroceryItem(\"Smart-Ones Frozen Entrees\",1.99f,0.311f, true);         g2 = new GroceryItem(\"SnackPack Pudding\",0.99f,0.396f);         g3 = new GroceryItem(\"Breyers Chocolate Icecream\",2.99f,2.27f, true);         g4 = new GroceryItem(\"Nabob Coffee\",3.99f,0.326f);         g5 = new GroceryItem(\"Gold Seal Salmon\",1.99f,0.213f);         g6 = new GroceryItem(\"Ocean Spray Cranberry Cocktail\",2.99f,2.26f);         g7 = new GroceryItem(\"Heinz Beans Original\",0.79f,0.477f);         g8 = new GroceryItem(\"Lean Ground Beef\",4.94f,0.75f, true);         g9 = new GroceryItem(\"5-Alive Frozen Juice\",0.75f,0.426f, true);         g10 = new GroceryItem(\"Coca-Cola 12-pack\",3.49f,5.112f);         g11 = new GroceryItem(\"Toilet Paper - 48 pack\",40.96f,10.89f);

        // Make a new customer and add some items to his/her shopping cart         Shopper c = new Shopper();         c.addItem(g1); c.addItem(g2); c.addItem(g3); c.addItem(g4);         c.addItem(g5); c.addItem(g6); c.addItem(g7); c.addItem(g8);         c.addItem(g9); c.addItem(g10); c.addItem(g1); c.addItem(g6);         c.addItem(g2); c.addItem(g2); c.addItem(g3); c.addItem(g3);         c.addItem(g3); c.addItem(g3); c.addItem(g3); c.addItem(g10);         c.addItem(g11); c.addItem(g9); c.addItem(g5); c.addItem(g6);         c.addItem(g7); c.addItem(g8); c.addItem(g8); c.addItem(g8);         c.addItem(g5);

        System.out.println(\"\ INITIAL CART CONTENTS:\");         for (int i=0; i<c.getNumItems(); i++) {             System.out.println(\"   \" + c.getCart()[i]);         }

        // Pack the bags and show the contents         GroceryBag[] packedBags = c.packBags();         for (int i=0; i<packedBags.length; i++) {             System.out.println(\"\ BAG \" + (i+1) + \" (Total Weight = \" +       packedBags[i].getWeight() + \"kg) CONTENTS:\");             for (int j=0; j<packedBags[i].getNumItems(); j++) {                 System.out.println(\"   \" + packedBags[i].getItems()[j]);             }         }         System.out.println(\"\ REMAINING CART CONTENTS:\");         for (int i=0; i<c.getNumItems(); i++) {             System.out.println(\"   \" + c.getCart()[i]);         }     } }
Here is the expected result (although the order of the items being packed may vary according to your algorithm).
INITIAL CART CONTENTS:    Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99    SnackPack Pudding weighing 0.396kg with price $0.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Nabob Coffee weighing 0.326kg with price $3.99    Gold Seal Salmon weighing 0.213kg with price $1.99    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Heinz Beans Original weighing 0.477kg with price $0.79    Lean Ground Beef weighing 0.75kg with price $4.94    5-Alive Frozen Juice weighing 0.426kg with price $0.75    Coca-Cola 12-pack weighing 5.112kg with price $3.49    Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    SnackPack Pudding weighing 0.396kg with price $0.99    SnackPack Pudding weighing 0.396kg with price $0.99
   Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Coca-Cola 12-pack weighing 5.112kg with price $3.49    Toilet Paper - 48 pack weighing 10.89kg with price $40.96    5-Alive Frozen Juice weighing 0.426kg with price $0.75    Gold Seal Salmon weighing 0.213kg with price $1.99    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Heinz Beans Original weighing 0.477kg with price $0.79    Lean Ground Beef weighing 0.75kg with price $4.94    Lean Ground Beef weighing 0.75kg with price $4.94    Lean Ground Beef weighing 0.75kg with price $4.94    Gold Seal Salmon weighing 0.213kg with price $1.99

BAG 1 (Total Weight = 3.251kg) CONTENTS:    Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99    Gold Seal Salmon weighing 0.213kg with price $1.99    Lean Ground Beef weighing 0.75kg with price $4.94    Lean Ground Beef weighing 0.75kg with price $4.94    Lean Ground Beef weighing 0.75kg with price $4.94    Heinz Beans Original weighing 0.477kg with price $0.79

BAG 2 (Total Weight = 3.295kg) CONTENTS:    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Gold Seal Salmon weighing 0.213kg with price $1.99    5-Alive Frozen Juice weighing 0.426kg with price $0.75    SnackPack Pudding weighing 0.396kg with price $0.99

BAG 3 (Total Weight = 4.54kg) CONTENTS:    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99

BAG 4 (Total Weight = 4.54kg) CONTENTS:    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99

BAG 5 (Total Weight = 4.936kg) CONTENTS:    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    SnackPack Pudding weighing 0.396kg with price $0.99

BAG 6 (Total Weight = 4.946kg) CONTENTS:    SnackPack Pudding weighing 0.396kg with price $0.99    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99    Nabob Coffee weighing 0.326kg with price $3.99    5-Alive Frozen Juice weighing 0.426kg with price $0.75    Lean Ground Beef weighing 0.75kg with price $4.94    Heinz Beans Original weighing 0.477kg with price $0.79

BAG 7 (Total Weight = 2.473kg) CONTENTS:    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Gold Seal Salmon weighing 0.213kg with price $1.99

REMAINING CART CONTENTS:    Toilet Paper - 48 pack weighing 10.89kg with price $40.96    Coca-Cola 12-pack weighing 5.112kg with price $3.49    Coca-Cola 12-pack weighing 5.112kg with price $3.49


Finally, write an unpackPerishables() method in the GroceryBag class that removes all the perishable items from the bag. Note that the GroceryBag should be modified (changed) by this method in that it will likely have less in it afterwards. The method should not print anything out (that belongs in the testing code) but instead should return an array of perishable GroceryItem objects.  


Add the following to the ShopperTestProgram so that it tests your method:

System.out.println(\"\ UNPACKING PERISHABLES:\"); for (int i=0; i<packedBags.length; i++) {     GroceryItem[] perishables = packedBags[i].unpackPerishables();     for (int j=0; j<perishables.length; j++) {         System.out.println(\"   \" + perishables[j]);     } } System.out.println(\"\ REMAINING BAG CONTENTS:\"); for (int i=0; i<packedBags.length; i++) {     System.out.println(\"\ BAG \" + (i+1) + \" (Total Weight = \" +      packedBags[i].getWeight() + \"kg) CONTENTS:\");     for (int j=0; j<packedBags[i].getNumItems(); j++) {         System.out.println(\"   \" + packedBags[i].getItems()[j]);     } }
Here is the result which should appear at the end of your output (although this may vary according to how you packed the bags earlier):
UNPACKING PERISHABLES:    Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99    Lean Ground Beef weighing 0.75kg with price $4.94    Lean Ground Beef weighing 0.75kg with price $4.94    Lean Ground Beef weighing 0.75kg with price $4.94    5-Alive Frozen Juice weighing 0.426kg with price $0.75    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Breyers Chocolate Icecream weighing 2.27kg with price $2.99    Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99    5-Alive Frozen Juice weighing 0.426kg with price $0.75    Lean Ground Beef weighing 0.75kg with price $4.94

REMAINING BAG CONTENTS:

BAG 1 (Total Weight = 0.69000006kg) CONTENTS:    Heinz Beans Original weighing 0.477kg with price $0.79    Gold Seal Salmon weighing 0.213kg with price $1.99

BAG 2 (Total Weight = 2.869kg) CONTENTS:    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Gold Seal Salmon weighing 0.213kg with price $1.99    SnackPack Pudding weighing 0.396kg with price $0.99

BAG 3 (Total Weight = 0.0kg) CONTENTS:

BAG 4 (Total Weight = 0.0kg) CONTENTS:

BAG 5 (Total Weight = 0.3959999kg) CONTENTS:    SnackPack Pudding weighing 0.396kg with price $0.99

BAG 6 (Total Weight = 3.459kg) CONTENTS:    SnackPack Pudding weighing 0.396kg with price $0.99    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Heinz Beans Original weighing 0.477kg with price $0.79    Nabob Coffee weighing 0.326kg with price $3.99

BAG 7 (Total Weight = 2.473kg) CONTENTS:    Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99    Gold Seal Salmon weighing 0.213kg with price $1.99

GroceryItem.java

Solution

Shopper.java

import java.util.*;
public class Shopper{
   
    private static final int MAX_CART_ITEM = 100; // max item can be in cart
    private GroceryItem[] cart;
    private static int cart_size;
   
    public Shopper getInstance()
    {
        return this;
    }
   
    public Shopper()   // zero argument constructor
    {
        cart = new GroceryItem[100];
        cart_size=0;
    }
    public int getNumItems()
    {
        return cart_size;
    }
   
    public GroceryItem[] getCart()
    {
        return cart;
       
    }
   
    @Override
    public String toString() {
   
        StringBuilder str = new StringBuilder();
        str.append(\"Shopper with shopping cart containing \" + cart.length +\" items\");
        str.append(\"\ \");
        return str.toString();
    }
    public void addItem(GroceryItem item)
    {
        cart[cart_size]=new GroceryItem(item.getName(),item.getPrice(),item.getWeight());

        cart_size++;
    }
    public static GroceryItem[] removeElement(GroceryItem[] original, GroceryItem element)
    {
        GroceryItem[] n = new GroceryItem[original.length - 1];
        int i;
        GroceryBag gb = new GroceryBag();
        for(i=0;i<original.length;i++)
        {
            if(gb.isItemEqual(original[i],element))
                break;
        }
       
        System.arraycopy(original, 0, n, 0, i-1);
        System.arraycopy(original, i+1, n, i, original.length - i-1);
        return n;
    }
    public void removeItem(GroceryItem item)
    {
        cart = removeElement(cart,item);
    }

    public List<GroceryBag> packBags()
    {
        List<GroceryBag> gb = new ArrayList<GroceryBag>();
        //GroceryBag[] gb = new GroceryBag()[];
        int len=0,j;
        for(int i=0;i<=cart_size;i++)
        {
            List<GroceryItem> gi = new ArrayList<GroceryItem>();
            GroceryBag gbobj = new GroceryBag();
            for(j=0;j<25;j++)
            {
                GroceryItem item = cart[j];
                if(len !=0)
                if((gb.get(len)).getWeight() + item.getWeight() > 5)
                    break;
                gbobj.addItem(item);
                removeItem(item);
            }
           ;
            gb.add(gbobj);
        }
        return gb;
    }
    public void add(GroveryItem[] items)
    {
       
    }
}

ShopperTestProgram.java

import java.util.*;
public class ShopperTestProgram
{    
    public static void main(String args[])
    {        
        GroceryItem g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11;

         g1 = new GroceryItem(\"Smart-Ones Frozen Entrees\",1.99f,0.311f, true);       
         g2 = new GroceryItem(\"SnackPack Pudding\",0.99f,0.396f);        
         g3 = new GroceryItem(\"Breyers Chocolate Icecream\",2.99f,2.27f, true);        
         g4 = new GroceryItem(\"Nabob Coffee\",3.99f,0.326f);        
         g5 = new GroceryItem(\"Gold Seal Salmon\",1.99f,0.213f);        
         g6 = new GroceryItem(\"Ocean Spray Cranberry Cocktail\",2.99f,2.26f);        
         g7 = new GroceryItem(\"Heinz Beans Original\",0.79f,0.477f);        
         g8 = new GroceryItem(\"Lean Ground Beef\",4.94f,0.75f, true);        
         g9 = new GroceryItem(\"5-Alive Frozen Juice\",0.75f,0.426f, true);       
         g10 = new GroceryItem(\"Coca-Cola 12-pack\",3.49f,5.112f);        
         g11 = new GroceryItem(\"Toilet Paper - 48 pack\",40.96f,10.89f);

         // Make a new customer and add some items to his/her shopping cart        
         Shopper c = new Shopper();        
         c.addItem(g1);
         //c.addItem(g2); c.addItem(g3); c.addItem(g4);        
         /*c.addItem(g5); c.addItem(g6); c.addItem(g7); c.addItem(g8);        
         c.addItem(g9); c.addItem(g10); c.addItem(g1); c.addItem(g6);      
         c.addItem(g2); c.addItem(g2); c.addItem(g3); c.addItem(g3);        
         c.addItem(g3); c.addItem(g3); c.addItem(g3); c.addItem(g10);       
         c.addItem(g11); c.addItem(g9); c.addItem(g5); c.addItem(g6);       
         c.addItem(g7);
         c.addItem(g8);
         c.addItem(g8);
         c.addItem(g8);        
         c.addItem(g5);
*/
         System.out.println(\"\ INITIAL CART CONTENTS:\");        
         for (int i=0; i<c.getNumItems(); i++)
         {            
             System.out.println(\"   \" + c.getCart()[i]);        
            
         }

         // Pack the bags and show the contents
         List<GroceryBag> list= c.packBags();
         GroceryBag[] packedBags = list.toArray(new GroceryBag[list.size()]);        
         for (int i=0; i<packedBags.length; i++)
         {            
             System.out.println(\"\ BAG \" + (i+1) + \" (Total Weight = \" +packedBags[i].getWeight() + \"kg) CONTENTS:\");
             for (int j=0; j<packedBags[i].getNumItems(); j++)
             {                
                 System.out.println(\"   \" + packedBags[i].getItems()[j]);            
                
             }         }        
             System.out.println(\"\ REMAINING CART CONTENTS:\");        
             for (int i=0; i<c.getNumItems(); i++)
             {            
                 System.out.println(\"   \" + c.getCart()[i]);        
                
             }    
       
    }
   
}

In Java please. The GroceryBag and Grocerie item class is given. Please show the code and the given result of question 3 . Q3 -The Shopper Class Implement a cla
In Java please. The GroceryBag and Grocerie item class is given. Please show the code and the given result of question 3 . Q3 -The Shopper Class Implement a cla
In Java please. The GroceryBag and Grocerie item class is given. Please show the code and the given result of question 3 . Q3 -The Shopper Class Implement a cla
In Java please. The GroceryBag and Grocerie item class is given. Please show the code and the given result of question 3 . Q3 -The Shopper Class Implement a cla
In Java please. The GroceryBag and Grocerie item class is given. Please show the code and the given result of question 3 . Q3 -The Shopper Class Implement a cla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site