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

In java please. Both shopper and shopper test program is given. Plese show code

ID: 3789855 • Letter: I

Question

In java please. Both shopper and shopper test program is given. Plese show code and result.

The Pricing
Create a computeFreezerItemCost() method in the Shopper class that will return the total price of all FreezerItems in the cart. Note that some items will be packed and some will be loose in the cart.   
Create a computeTotalCost() method in the Shopper class that will return the total price of all items in the cart. Note that some items will be packed and some will be loose in the cart.   Your code must not use instanceof nor getClass() for this method.   
Insert the following code into the ShopperTestProgram after the bags are packed but before the unperishables are unpacked:
System.out.println(" TOTAL FREEZER ITEM COST: $" + c.computeFreezerItemCost()); System.out.println(" TOTAL CART CONTENTS COST: $" + c.computeTotalCost());
The code should produce an answers of $91.35 and $192.05 when you run it.

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]);      

   }

    }

}

Explanation / Answer

// Shopper.java

public class Shopper {
public static final int MAX_CART_ITEMS = 100; // max # items allowed

private GroceryItem[] cart; // items to be purchased
private int numItems; // #items to be purchased
public GroceryBag[] packedBagsInCart = new GroceryBag[MAX_CART_ITEMS];
public int packedBagCount = 0;
public Shopper() {
cart = new GroceryItem[MAX_CART_ITEMS];
numItems = 0;
}

public GroceryItem[] getCart() { return cart; }
public int getNumItems() { return numItems; }

public String toString() {
return "Shopper with shopping cart containing " + numItems + " items";
}

// Return the total cost of the items in the cart
public float totalCost() {
float total = 0;
for (int i=0; i<numItems; i++) {
total += cart[i].getPrice();
}
return total;
}

// Add an item to the shopper's shopping cart
public void addItem(GroceryItem g) {
if (numItems < MAX_CART_ITEMS)
cart[numItems++] = g;
}

// Removes the given item from the shopping cart
public void removeItem(GroceryItem g) {
for (int i=0; i<numItems; i++) {
if (cart[i] == g) {
cart[i] = cart[numItems - 1];
numItems -= 1;
return;
}
}
}

// Go through the shopping cart and pack all packable items into bags
public GroceryBag[] packBags() {
GroceryBag[] packedBags = new GroceryBag[numItems];
int bagCount = 0;

GroceryBag currentBag = new GroceryBag();
for (int i=0; i<numItems; i++) {
GroceryItem item = cart[i];
if (item.getWeight() <= GroceryBag.MAX_WEIGHT) {
if (!currentBag.canHold(item)) {
packedBags[bagCount++] = currentBag;
currentBag = new GroceryBag();
}
currentBag.addItem(item);
removeItem(item);
i--;
}
}
// Check this in case there were no bagged items
if (currentBag.getWeight() > 0)
packedBags[bagCount++] = currentBag;

// Now create a new bag array which is just the right size
GroceryBag[] result = new GroceryBag[bagCount];
for (int i=0; i<bagCount; i++)
result[i] = packedBags[i];
packedBagCount = bagCount;
packedBagsInCart = result;
  
return result;
}
  
public double computeFreezerItemCost()
{
   double totalCost = 0;
  
   for(int i = 0; i < numItems; i++)
   {
       if (cart[i].isPerishable())
       {
           totalCost += cart[i].getPrice();
       }
   }
  
   System.out.println(packedBagsInCart.length);
  
   for(int i = 0; i < packedBagCount; i++)
   {
       for(GroceryItem item : packedBagsInCart[i].getItems())
       {
           if (item != null && item.isPerishable())
           {
               totalCost += item.getPrice();
           }
       }
   }
   return totalCost;
}
  
public double computeTotalCost()
{
   double totalCost = 0;
  
   for(int i = 0; i < numItems; i++)
   {
       totalCost += cart[i].getPrice();
   }
  
   for(int i = 0; i < packedBagCount; i++)
   {
       for(GroceryItem item : packedBagsInCart[i].getItems())
       {
           if (item != null)
           totalCost += item.getPrice();
       }
   }
   return totalCost;
}
}

// ShopperTestProgram.java

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(" TOTAL FREEZER ITEM COST: $"
               + c.computeFreezerItemCost());
       System.out.println(" TOTAL CART CONTENTS COST: $"
               + c.computeTotalCost());
       System.out.println(" REMAINING CART CONTENTS:");
       for (int i = 0; i < c.getNumItems(); i++) {
           System.out.println(" " + c.getCart()[i]);
       }
   }
}

// Please note expected answer given in question is wrong as I tried calculating price manually and it is not maching up.

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