In java please Grocery item and bag class are given along si

In java please. Grocery item and bag class are given along side shopper test program.

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 {
    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             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             if (items[i].isPerishable())
                perishableCount++;
        }
        GroceryItem[] perishables = new GroceryItem[perishableCount];
        perishableCount = 0;
        for (int i=0; i             if (items[i].isPerishable()) {
                perishables[perishableCount++] = items[i];
                removeItem(items[i]);
                i--;
            }
        }
        return perishables;
    }
}
Grocery item class

public class GroceryBag {
    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             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             if (items[i].isPerishable())
                perishableCount++;
        }
        GroceryItem[] perishables = new GroceryItem[perishableCount];
        perishableCount = 0;
        for (int i=0; i             if (items[i].isPerishable()) {
                perishables[perishableCount++] = items[i];
                removeItem(items[i]);
                i--;
            }
        }
        return perishables;
    }
}
Shopper 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

     System.out.println(\"   \" + c.getCart()[i]);     

    }

        // Pack the bags and show the contents    

     GroceryBag[] packedBags = c.packBags();   

      for (int i=0; i

          System.out.println(\"\ BAG \" + (i+1) + \" (Total Weight = \" +       packedBags[i].getWeight() + \"kg) CONTENTS:\");    

         for (int j=0; j

        System.out.println(\"   \" + packedBags[i].getItems()[j]);         

    }      

   }       

System.out.println(\"\ REMAINING CART CONTENTS:\");    

     for (int i=0; i

        System.out.println(\"   \" + c.getCart()[i]);      

   }

    }

}

Grocery Item

Solution

// You missed to include shoper class (I had my own version answering as per that)

// GroceryBag.java

public class GroceryBag {
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 PerishableItem[] unpackPerishables() {
int perishableCount = 0;
for (int i=0; i < numItems; i++) {
if (items[i] instanceof PerishableItem)
perishableCount++;
}
PerishableItem[] perishables = new PerishableItem[perishableCount];
perishableCount = 0;
for (int i=0; i < numItems; i++) {
if (items[i] instanceof PerishableItem)
{
perishables[perishableCount++] = (PerishableItem)items[i];
removeItem(items[i]);
i--;
}
}
return perishables;
}
}

// GroceryItem.java

public class GroceryItem {
private String name;
private float price;
private float weight;

public GroceryItem() {
name = \"?\";
price = 0;
weight = 0;
}
public GroceryItem(String n, float p, float w) {
name = n;
price = p;
weight = w;
}

public String getName() { return name; }
public float getPrice() { return price; }
public float getWeight() { return weight; }

public String toString () {
return name + \" weighing \" + weight + \"kg with price $\" + price;
}
}

// ShopperTestProgram

public class ShopperTestProgram {
  
public static void main(String args[]) {
GroceryItem g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11;
  
g1 = new PerishableItem(\"Smart-Ones Frozen Entrees\",1.99f,0.311f);
g2 = new GroceryItem(\"SnackPack Pudding\",0.99f,0.396f);   
g3 = new PerishableItem(\"Breyers Chocolate Icecream\",2.99f,2.27f);
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);
g9 = new PerishableItem(\"5-Alive Frozen Juice\",0.75f,0.426f);   
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]);
}
}
}

// PerishableItem


public class PerishableItem extends GroceryItem{

public PerishableItem() {
super();
}
public PerishableItem(String n, float p, float w) {
super(n, p, w);
}
public PerishableItem(String n, float p, float w, boolean s) {
super(n,p,w);
}

public String toString () {
return getName() + \" weighing \" + getWeight() + \"kg with price $\" + getPrice() + \" (perishable)\";
}

}

// 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 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];
return result;
}
}

In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl
In java please. Grocery item and bag class are given along side shopper test program. The PerishableItem Class Create a subclass of GroceryItem called Perishabl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site