Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public interface Carryable { public String getContents(); public String getDescription(); public float getPrice(); } public class FreezerItem extends PerishableItem { public FreezerItem(String n, float p,

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

public interface Carryable { public String getContents(); public String getDescription(); public float getPrice(); } 

public class FreezerItem extends PerishableItem { public FreezerItem(String n, float p, float w) { super(n, p, w); } public String toString () { return super.toString() + "[keep frozen]"; } } 

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()) public void addItem(GroceryItem g) { if (canHold(g)) { items[numItems++] = g; weight += g.getWeight(); } } public void removeItem(GroceryItem item) { for (int i = 0; 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; iif (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 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; iif (items[i] instanceof PerishableItem) perishableCount++; } PerishableItem[] perishables = new PerishableItem[perishableCount]; perishableCount = 0; for (int i=0; iif (items[i] instanceof PerishableItem) { perishables[perishableCount++] = (PerishableItem)items[i]; removeItem(items[i]); i--; } } return perishables; } public String getDescription() { return "GROCERY BAG (" + weight + "kg)"; } public String getContents(){ String result = ""; for (int i=0; i" " + items[i] + " "; return result; } public float getPrice() { float total = 0; for (int i=0; ireturn total; } } 

public class GroceryItem implements Carryable{ 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; } public String getDescription() { return name; } public String getContents(){ return ""; } } 

public abstract class PerishableItem extends GroceryItem { public PerishableItem(String n, float p, float w) { super(n, p, w); } public String toString () { return super.toString() + " (perishable)"; } } 

public class RefrigeratorItem extends PerishableItem { public RefrigeratorItem(String n, float p, float w) { super(n, p, w); } public String toString () { return super.toString() + "[keep refrigerated]"; } } 

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 Carryable[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; ireturn total; } // Add an item to the shopper's shopping cart  public void addItem(Carryable g) { if (numItems // Removes the given item from the shopping cart  public void removeItem(Carryable g) { for (int i=0; iif (cart[i] == g) { cart[i] = cart[numItems - 1]; numItems -= 1; return; } } } // Go through the shopping cart and pack all packable items into bags  public void packBags() { GroceryBag[] packedBags = new GroceryBag[numItems]; int bagCount = 0; GroceryBag currentBag = new GroceryBag(); for (int i=0; iif (item.getWeight() 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 add the bags to the cart  for (int i=0; i// Display the contents of the cart  public void displayCartContents() { for (int i=0; i// Remoce and return all PerishableItems from the Shopping Cart  public PerishableItem[] removePerishables() { PerishableItem[] pItems = new PerishableItem[MAX_CART_ITEMS]; int pItemCount = 0; PerishableItem[] perishables; for (int i=0; iif (cart[i] instanceof GroceryBag) { perishables = ((GroceryBag) cart[i]).unpackPerishables(); for (int j=0; jelse { if (cart[i] instanceof PerishableItem) { pItems[pItemCount++] = (PerishableItem) cart[i]; removeItem(cart[i]); i--; } } } // Now create the proper size array  PerishableItem[] result = new PerishableItem[pItemCount]; for (int i=0; ireturn result; } // Return the amount of money that would be lost if the freezer breaks down  public float computeFreezerItemCost() { float total = 0; for (int i=0; iif (cart[i] instanceof GroceryBag) { GroceryItem[] itemsInBag = ((GroceryBag)cart[i]).getItems(); for (GroceryItem item: itemsInBag) { if (item instanceof FreezerItem) total += item.getPrice(); } } if (cart[i] instanceof FreezerItem) total += cart[i].getPrice(); } return total; } // Return the total cost of all items in the packed cart  public float computeTotalCost() { float total = 0; for (int i=0; ireturn total; } } 

public class ShopperTestProgram { public static void main(String args[]) { GroceryItem g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12, g13, g14, g15; g1 = new FreezerItem("Smart-Ones Frozen Entrees",1.99f,0.311f); g2 = new GroceryItem("SnackPack Pudding",0.99f,0.396f); g3 = new FreezerItem("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 RefrigeratorItem("Lean Ground Beef",4.94f,0.75f); g9 = new FreezerItem("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); g12 = new RefrigeratorItem("2L Sealtest Milk",2.99f,2.06f); g13 = new RefrigeratorItem("Extra-Large Eggs",1.79f,0.77f); g14 = new RefrigeratorItem("Yoplait Yogurt 6-pack",4.74f,1.02f); g15 = new FreezerItem("Mega-Sized Chocolate Icecream",67.93f,15.03f); // 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); c.addItem(g12); c.addItem(g13); c.addItem(g14); c.addItem(g15); System.out.println(" INITIAL CART CONTENTS:"); for (int i=0; i" " + c.getCart()[i]); } // Pack the bags and show the contents  c.packBags(); System.out.println(" CART CONTENTS:"); c.displayCartContents(); System.out.println(" REMAINING CART CONTENTS:"); for (int i=0; i" " + c.getCart()[i]); } System.out.println(" TOTAL FREEZER ITEM COST: $" + c.computeFreezerItemCost()); System.out.println(" TOTAL CART CONTENTS COST: $" + c.computeTotalCost()); System.out.println(" UNPACKING PERISHABLES:"); PerishableItem[] perishables = c.removePerishables(); for (int j=0; j" " + perishables[j]); } System.out.println(" REMAINING CART CONTENTS:"); c.displayCartContents(); } } 

Need to be solved in Java, thank you.

C Home Chegg.com x Cou e: COMP 06B x D 95.105/115 Assignme x C file://IC/Users/victor0323/Downloads/COMP1406 A5 W2017-Posted%20X1).pdf (1) The GUI Create a class called ShoppingListApp which will construct the GUI shown below. You should make the window to be non-resizable. You may have to tweak the size a little bit in order to get the bordering margins correct. 10 10 120 15 65 100 10 C Grocery Store Application 10 pping Cart 35 300 10 Return checkout iota Price: Buy 10 a e A 459 1x NG 2017/2123

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions