The PerishableItem Class Create a subclass of GroceryItem ca
The PerishableItem Class
Create a subclass of GroceryItem called PerishableItem that represents an item that can spoil.   Implement a public toString() method that returns a String representation of the PerishableItem as follows (making sure to make full use of inheritance):
Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99 (perishable)
 
 Remove the perishable attribute from the GroceryItem class.   You will need to modify the constructors (and delete one of them) as well as remove the get method.
Adjust the ShopperTestProgram (from the last assignment) to make use of the PerishableItem class now for all items that were perishable.   You will see a need to create a constructor in the PerishableItem class that makes full use of inheritance.
 
 Adjust your unpackPerishables() method in the GroceryBag class so that it works properly again by making proper use of inheritance.   The method MUST return an array of PerishableItem objects now instead of GroceryItem objects.
 
public class GroceryBag implements Carryable { public static final float MAX_WEIGHT = 5; // max weight allowed (kg) public static final int MAX_ITEMS = 25; // max # items allowed private GroceryItem[] items; // actual GroceryItems in bag private int numItems; // # of GroceryItems in bag private float weight; // current weight of bag public GroceryBag() { items = new GroceryItem[MAX_ITEMS]; numItems = 0; weight = 0; } public GroceryItem[] getItems() { return items; } public int getNumItems() { return numItems; } public float getWeight() { return weight; } public String toString() { if (weight == 0) return \"An empty grocery bag\"; return (\"A \" + weight + \"kg grocery bag with \" + numItems + \" items\"); } public boolean canHold(GroceryItem g) { return (((weight + g.getWeight()) <= MAX_WEIGHT) && (numItems <= MAX_ITEMS)); } public void addItem(GroceryItem g) { if (canHold(g)) { items[numItems++] = g; weight += g.getWeight(); } } public void removeItem(GroceryItem item) { for (int i = 0; i < numItems; i++) { if (items[i] == item) { weight -= items[i].getWeight(); items[i] = items[numItems - 1]; numItems -= 1; return; } } } // Finds and returns the heaviest item in the shopping cart public GroceryItem heaviestItem() { if (numItems == 0) return null; GroceryItem heaviest = items[0]; for (int i=0; i<numItems; i++) { if (items[i].getWeight() > heaviest.getWeight()) { heaviest = items[i]; } } return heaviest; } // Determines whether or not the given item in the shopping cart public boolean has(GroceryItem item) { for (int i = 0; i < numItems; i++) { if (items[i] == item) { return true; } } return false; } // Remove all perishables from the bag and return an array of them public GroceryItem[] unpackPerishables() { int perishableCount = 0; for (int i=0; i<numItems; i++) { if (items[i].isPerishable()) perishableCount++; } GroceryItem[] perishables = new GroceryItem[perishableCount]; perishableCount = 0; for (int i=0; i<numItems; i++) { if (items[i].isPerishable()) { perishables[perishableCount++] = items[i]; removeItem(items[i]); i--; } } return perishables; } @Override public String getContents() { String contents = \"\"; for(GroceryItem item:items){ contents+=\" \"+item.toString()+\"\ \"; } return contents; } @Override public String getDescription() { return \"GROCERY BAG (\"+weight+\" kg)\"; } @Override public float getPrice() { float price=0.0f; for(GroceryItem item:items){ price+=item.getPrice(); } return price; } }
INITIAL CART CONTENTS: Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99 Snack Pack 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 94.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 Snack Pack Pudding weighing 0.396kg with price $0.99 Snack Pack 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 90.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 44.94 Lean Ground Beef weighing 0.75kg with price 44.94 Gold Seal Salmon weighing 0.213kg with price $1.99Solution
//This is interface Carryable
package com.grocery;
public interface Carryable {
   public String getContents();
    public String getDescription();
    public String toString();
    float getPrice();
   
}
package com.grocery;
public class GroceryItem implements Carryable {
   protected String name;
    protected float price;
    protected float weight;
    //private boolean perishable;
   public GroceryItem() {
    name = \"?\";
    price = 0;
    weight = 0;
    //perishable = false;
    }
   public GroceryItem(String n, float p, float w) {
    name = n;
    price = p;
    weight = w;
    //perishable = false;
    }
   /*public GroceryItem(String n, float p, float w, boolean s) {
    name = n;
    price = p;
    weight = w;
    perishable = s;
    }*/
   public String getName() {
    return name;
    }
   @Override
    public String getContents() {
    if(name.equals(\"?\") && (price == 0) && (weight == 0) ){
        //&& (perishable == false)
    return \"\";
    }
    else{
    return name + \" weighing \" + weight + \"kg with price $\" + price;
    }
    }
   @Override
    public String getDescription() {
       
       
    return name;
    }
   public float getPrice() {
    return price;
    }
   public float getWeight() {
    return weight;
    }
     
    public String toString() {
    return name + \" weighing \" + weight + \"kg with price $\" + price;
    }
       public boolean isPerishable() {
            // TODO Auto-generated method stub
            return false;
        }
   
 }
//This is sub class of GroceryItem
package com.grocery;
public class PerishableItem extends GroceryItem {
   private boolean perishable;
      
    public PerishableItem() {
    name = \"?\";
    price = 0;
    weight = 0;
    perishable = false;
    }
   public PerishableItem(String n, float p, float w) {
    name = n;
    price = p;
    weight = w;
    perishable = false;
    }
      
    public PerishableItem(String n, float p, float w, boolean s){
        super();
        name = n;
    price = p;
    weight = w;
    perishable = s;
    }
      
    @Override
    public String toString() {
    return name + \" weighing \" + weight + \"kg with price $\" + price;
    }
   
    /*public boolean isPerishable() {
    return perishable;
    }*/
      
 }
//This is GroceryBag class implements by Carryable interface
package com.grocery;
public class GroceryBag implements Carryable {
   
    public static final float MAX_WEIGHT = 5;
    // max weight allowed (kg)
    public static final int MAX_ITEMS = 25;
    // max # items allowed
    private GroceryItem[] items;
    // actual GroceryItems in bag
    private int numItems;
    // # of GroceryItems in bag
    private float weight;
    // current weight of bag
    public GroceryBag() {
    items = new GroceryItem[MAX_ITEMS];
    numItems = 0; weight = 0;
    }
    public GroceryItem[] getItems() {
    return items;
    }
    public int getNumItems() {
    return numItems;
    }
    public float getWeight() {
    return weight;
    }
    public String toString() {
    if (weight == 0) return \"An empty grocery bag\"; return (\"A \" + weight + \"kg grocery bag with \" + numItems + \" items\");
    }
    public boolean canHold(GroceryItem g) {
    return (((weight + g.getWeight()) <= MAX_WEIGHT) && (numItems <= MAX_ITEMS));
    }
    public void addItem(GroceryItem g) {
    if (canHold(g)) { items[numItems++] = g; weight += g.getWeight();
    }
    }
    public void removeItem(GroceryItem item) {
    for (int i = 0; i < numItems; i++) {
    if (items[i] == item) {
    weight -= items[i].getWeight(); items[i] = items[numItems - 1]; numItems -= 1; return;
    } } }
    // Finds and returns the heaviest item in the shopping cart
    public GroceryItem heaviestItem() {
    if (numItems == 0)
    return null;
    GroceryItem heaviest = items[0];
    for (int i=0; i<numItems; i++) {
    if (items[i].getWeight() > heaviest.getWeight()) {
    heaviest = items[i];
    } }
    return heaviest;
    }
    // Determines whether or not the given item in the shopping cart
    public boolean has(GroceryItem item) {
    for (int i = 0; i < numItems; i++) {
    if (items[i] == item) {
    return true;
    } }
    return false;
    }
    // Remove all perishables from the bag and return an array of them
    public GroceryItem[] unpackPerishables() {
    int perishableCount = 0;
    for (int i=0; i<numItems; i++) {
    if (items[i].isPerishable()) perishableCount++;
    }
    GroceryItem[] perishables = new GroceryItem[perishableCount];
    perishableCount = 0;
    for (int i=0; i<numItems; i++) {
    if (items[i].isPerishable()) {
    perishables[perishableCount++] = items[i];
    removeItem(items[i]); i--;
    } }
    return perishables;
    }
    @Override
    public String getContents() {
    String contents = \"\";
    for(GroceryItem item:items){
    contents+=\" \"+item.toString()+\"\ \";
    }
    return contents;
    }
    @Override
    public String getDescription() {
    return \"GROCERY BAG (\"+weight+\" kg)\";
    }
    @Override
    public float getPrice() {
    float price=0.0f;
    for(GroceryItem item:items){
    price+=item.getPrice();
    }
    return price;
    }
   
}
package com.grocery;
public class Shopper {
public static final int MAX_CART_ITEMS = 100; // max # items allowed
private Carryable[] cart; // items to be purchased
 private int numItems; // #items to be purchased
public Shopper() {
 cart = new GroceryItem[MAX_CART_ITEMS];
 numItems = 0;
 }
public Carryable [] 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 += ((GroceryItem) 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 = (GroceryItem) 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];
 return result;
 }
   
 }
//This is Test class which execute all above code
package com.grocery;
public class ShopperTest {
   public static void main(String[] args) {
        // TODO Auto-generated method stub
   
        GroceryItem g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11;
   g1 = new PerishableItem(\"Smart-Ones Frozen Entrees\", 1.99f, 0.311f,true);
    g2 = new GroceryItem(\"SnackPack Pudding\", 0.99f, 0.396f);
    g3 = new PerishableItem(\"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 PerishableItem(\"Lean Ground Beef\", 4.94f, 0.75f,true);
    g9 = new PerishableItem(\"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]);
    }
   GroceryBag[] packedBags = c.packBags();
    for (int i = 0; i<packedBags.length; i++) {
    if(packedBags[i]!=null) {
    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]);
    }
}
}
The output above program is given below
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









