Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.HashMap; import java.util.Map; / / Bag Interface interface Bag { void addItem ( String itemName, double price ) ; boolean removeItem ( String itemName
import java.util.HashMap;
import java.util.Map;
Bag Interface
interface Bag
void addItemString itemName, double price;
boolean removeItemString itemName;
void displayCart;
OnlineDeliveryBag implementing Bag interface
class OnlineDeliveryBag implements Bag
private HashMap shoppingCart;
public OnlineDeliveryBag
shoppingCart new HashMap;
@Override
public void addItemString itemName, double price
shoppingCart.putitemName price;
System.out.printlnitemName added to the cart.";
@Override
public boolean removeItemString itemName
if shoppingCartcontainsKeyitemName
shoppingCart.removeitemName;
System.out.printlnitemName removed from the cart.";
return true;
else
System.out.printlnitemName not found in the cart.";
return false;
@Override
public void displayCart
System.out.printlnItems in the cart:";
for MapEntry entry : shoppingCart.entrySet
System.out.printlnentrygetKey Price: $ entry.getValue;
Main class to demonstrate the usage
public class Main
public static void mainString args
OnlineDeliveryBag shoppingCart new OnlineDeliveryBag;
Adding items to the cart
shoppingCart.addItemCan of Soup", ;
shoppingCart.addItemBread;
Displaying the cart
shoppingCart.displayCart;
Removing an item from the cart
shoppingCart.removeItemCan of Soup";
Displaying the updated cart
shoppingCart.displayCart;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started