Question: https://gyazo.com/9a09020acff2b964b60a2ed3f212a918 Main Method: public class Main { public static void main(String[] args) { Item item1 = new Item(); item1.setName(Pencil); item1.setQuantity(10); //item1.setPrice(0.99); Item item2 = new

 https://gyazo.com/9a09020acff2b964b60a2ed3f212a918 Main Method: public class Main { public static void main(String[] https://gyazo.com/9a09020acff2b964b60a2ed3f212a918

Main Method:

public class Main {

public static void main(String[] args) { Item item1 = new Item(); item1.setName("Pencil"); item1.setQuantity(10); //item1.setPrice(0.99); Item item2 = new Item(); item2.setName("Notepad"); item2.setQuantity(3); //item2.setPrice(1.99); GroceryItem item3 = new GroceryItem(); item3.setName("Apple"); item3.setQuantity(5); item3.setExpiration("2017-11-11"); //item3.setPrice(0.49); ImportedGroceryItem item4 = new ImportedGroceryItem(); item4.setName("Mango"); item4.setQuantity(10); item4.setExpiration("2017-12-1"); item4.setCountryOfOrigin("Mexico"); //item4.setPrice(1.25); VirtualItem item5 = new VirtualItem(); item5.setName("Wonder Woman"); item5.setQuantity(100); item5.setSizeMB(2000); //item5.setPrice(9.99); ShoppingCart cart = new ShoppingCart(); cart.addItem(item1); cart.addItem(item2); cart.addItem(item3); cart.addItem(item4); cart.addItem(item5); cart.displayCart(); //System.out.println("Total price of all items is $" + cart.getTotal()); //System.out.println("Total price of grocery items is $" + cart.getGroceryTotal()); //System.out.println("Total price of non grocery items is $" + cart.getNonGroceryTotal()); }

}

ITEM:

public class Item {

protected String itemName; protected int itemQuantity; public void setName(String newName) { itemName = newName; return; }

public void setQuantity(int newQty) { itemQuantity = newQty; return; }

public void printItem() { System.out.println(this.toString()); //return; }

public String toString() { return itemName + " " + itemQuantity ; } }

GroceryItem:

public class GroceryItem extends Item { //implicitly all attributes and methods from Item are here private String expiration; public void setExpiration(String ex) { expiration = ex; }

public String toString() { String s1 = super.toString(); String s2 = s1 + " " + expiration; return s2; //return super.toString() + " " + expiration; }

}

IMPORTEDGROCERY ITEM

public class ImportedGroceryItem extends GroceryItem { private String countryOfOrigin; public String getCountryOfOrigin() { return countryOfOrigin; } public void setCountryOfOrigin(String coo) { countryOfOrigin = coo; } @Override public String toString() { return super.toString() + " imported from " + countryOfOrigin; } }

VIRTUALITEM:

public class VirtualItem extends Item { private double sizeMB; public double getSizeMB() { return sizeMB; } public void setSizeMB(double smb) { sizeMB = smb; } @Override public void setQuantity(int q) { if (q > 1) { System.out.println("A virtual item cannot have a quantity more than 1"); q = 1; } this.itemQuantity = q; } @Override public String toString() { return super.toString() + " " + sizeMB + "MB"; } }

SHOPPINGCART:

import java.util.ArrayList;

public class ShoppingCart { private ArrayList items; public ShoppingCart() { items = new ArrayList(); } public void addItem(Item item) { items.add(item); } public void displayCart() { for(Item item : items) item.printItem(); }

}

Java coding help. Very confused price of groceryon-grocery items. Once you implement the changes, you can uncomment the lines in Main and run, you should an output similar to: A virtual item cannot have a quantity more than 1 Pencil 10 $0.99 Notepad 3 $1.99 Apple 5 $0.49 2017-11-11 Mango 10 $1.25 2017-12-1 imported from Mexico Wonder Woman 1 $9.99 2000.0MB Total price of all items is $40.81 Total price of grocery items is $14.95 Total price of non grocery items is $25.860000000000003 1 Add an attribute price (double) to Item. Add a setter method. Note that since Item is the Base class of Groceryltem, ImportedGroceryltem, and Virtualltem, the attribute and the setter method are automatically inherited (how wonderful!) 2- Modify the method toString to add the price to the result 3 Next implement a method calculateTotal, also in item. This method returns the price quantity of the itenm In ShoppingCart, implement the method getTotal. This method returns the total price of all items in the cart Now, implement the method getGroceryTotal. This method returns the total price of grocery items in the cart Similarly, implement the method getNonGroceryTotal. This method returns the total price of non-grocery items in the cart 4- 5- 6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!