Answered step by step
Verified Expert Solution
Link Copied!

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 addItem(String itemName, double price);
boolean removeItem(String itemName);
void displayCart();
}
// OnlineDeliveryBag implementing Bag interface
class OnlineDeliveryBag implements Bag {
private HashMap shoppingCart;
public OnlineDeliveryBag(){
shoppingCart = new HashMap<>();
}
@Override
public void addItem(String itemName, double price){
shoppingCart.put(itemName, price);
System.out.println(itemName +" added to the cart.");
}
@Override
public boolean removeItem(String itemName){
if (shoppingCart.containsKey(itemName)){
shoppingCart.remove(itemName);
System.out.println(itemName +" removed from the cart.");
return true;
} else {
System.out.println(itemName +" not found in the cart.");
return false;
}
}
@Override
public void displayCart(){
System.out.println("Items in the cart:");
for (Map.Entry entry : shoppingCart.entrySet()){
System.out.println(entry.getKey()+"- Price: $"+ entry.getValue());
}
}
}
// Main class to demonstrate the usage
public class Main {
public static void main(String[] args){
OnlineDeliveryBag shoppingCart = new OnlineDeliveryBag();
// Adding items to the cart
shoppingCart.addItem("Can of Soup", 4.00);
shoppingCart.addItem("Bread",2.50);
// Displaying the cart
shoppingCart.displayCart();
// Removing an item from the cart
shoppingCart.removeItem("Can of Soup");
// Displaying the updated cart
shoppingCart.displayCart();
}

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_2

Step: 3

blur-text-image_3

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 Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions