[Solved]Java Assignment Following Classes Need Modified Public Class Item Protected String Itemnam Q37227975
For this Java assignment, I have the following classes that needto be modified.
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 ;
}
}
import java.util.ArrayList;
public class ShoppingCart {
private ArrayList<Item> items;
public ShoppingCart() {
items = newArrayList<Item>();
}
public void addItem(Item item) {
items.add(item);
}
public void displayCart() {
for(Item item : items)
item.printItem();
}
}
- Add an attribute price (double) to Item. Add a setter method.Note that since Item is the Base class of GroceryItem,ImportedGroceryItem, and VirtualItem, the attribute and the settermethod are automatically inherited (how wonderful!)
- Modify the method toString to add the price to the result
- Next implement a method calculateTotal, also in item. Thismethod returns the price * quantity of the item
- In ShoppingCart, implement the method getTotal. This methodreturns the total price of all items in the cart
- Now, implement the method getGroceryTotal. This method returnsthe total price of grocery items in the cart
- Similarly, implement the method getNonGroceryTotal. This methodreturns the total price of non-grocery items in the cart
Hint to differentiate between groceryand non-grocery items, you can use the instanceof operator objectinstanceof Class evaluates to true or false. For example, if(iteminstanceof GroceryItem) {….}
I need to print something output something like this belowhighlighted in black.
A virtual item cannot have a quantity more than1
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.992000.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
Expert Answer
Answer to For this Java assignment, I have the following classes that need to be modified. public class Item { protected String it… . . .
OR

