Implement a shopping list program
Goal
The goal of this assignment is to practice using different implementations of Maps and Collections.
You will simulate a person shopping for groceries at a store.
Below you will find items that the store has and a shopping list of items that a person must acquire.
When adding an item to the cart you will need to be able to keep track of the total and the number of items (represented as a Collection) in it.
Store Items
Lettuce – $2.50/ea
Tomatoes – $2.33/lb
Cucumber – $1/ea
Radish – $0.50/ea
Garlic – $0.50/ea
Ground Beef – $3.50/lb
Chicken Breast – $4/lb
Frozen Mixed Veggies – $1.45/ea
Ice Cream – $4/ea
Frozen Pizza – $6.50/ea
Milk – $2
Orange Juice – $3
Butter – $4/pk
Cheese – $4/pk
Bread – $2
Candy – $3
Grocery List
1 Lettuce
2lb of Tomatoes
3 Cucumber
3 Radishes
3lb of Ground Beef
2 Frozen Pizzas
1 Bread
Any quantity of one item of your choice!
Instructions
Viewing the Grocery List above you will need to create a class ShoppingBag that will represent the items that you place in your shopping cart that are a part of the list. A ShoppingBag extends the following abstract class Bag (which you will need to create):
public abstract class Bag {
public abstract double getTotal();
public abstract void addItem(String item, int quantity);
}
A ShoppingBag will override the getTotal() method that will calculate the total cost to purchase all items present in it. For example, if your bag has 2lb of Tomatoes then the total returned should be 4.66. Your answer should show at least two decimal places (no rounding).
A ShoppingBag will also override the addItem(..) method that represents the action of placing an item in the ShoppingBag. You’ll need to keep track of all items (and quantities of each) in your ShoppingBag.
Lastly, a ShoppingBag should override the toString() method to print its contents of items. For example, if a Bag has 3 Radishes and 2 Frozen Pizzas, then it might print a message like this:
[Bag: 3 radishes, 2 frozen pizzas].
There is also the ShoppingBagTest class which you will need to update to simulate the experience of adding items to the ShoppingBag, printing its contents and the total cost of all items.
NOTE: You don’t need a special class to represent an item. We’ll represent each as a String. Ex: Tomatoes is represented as the String, “Tomatoes”.
Helpful Hints
You may wish to store the Store Items and prices in a Map for easy lookup of an item’s cost.
Order doesn’t matter when placing items in a bag.
You can declare any other variables or methods as needed.
What you will Turn in
You will turn in a completed Project with implementations for the inherited methods in the ShoppingBag class as well as a completed ShoppingBagTest class that simulates adding all of the items in the grocery list to the ShoppingBag and printing its contents and total.
Bag.java
package com.yourname;
public abstract class Bag {
public abstract double getTotal();
public abstract void addItem(String item, int quantity);
}
ShoppingBag.java
package com.yourname;
import java.util.HashMap;
import java.util.Map;
public class ShoppingBag extends Bag {
// TODO Declare any instance variables here
Map<String , double>priceMap = new HashMap<>();
Map<String, Integer>quantityMap = new HashMap<>();
private Object priceMap;{
// TODO Implement this method
this.quantityMap.put(“Lettuce”,1);
this.quantityMap.put(“Tomatoes”,2);
this.quantityMap.put(“Cucumber”, 3);
this.quantityMap.put(“Radishes”,3);
this.quantityMap.put(“Ground Beef”,3);
this.quantityMap.put(“Frozen Pizzas”,2);
this.quantityMap.put(“Bread”,1);
this.quantityMap.put(“Candy”,20);
}
}
public ShoppingBag() {
this.priceMap.put (“Lettuce”, 2.50);
this.priceMap.put(“Tomatoes”,2.33);
this.priceMap.put(“Cucumber”,1.00);
this.priceMap.put(“Radish”,0.50);
this.priceMap.put(“Garlic”,0.50);
this.priceMap.put(“Ground Beef”,3.50);
this.priceMap.put(“Chicken Breast”,4.00);
this.priceMap.put(“Frozen Mixed Vegitables”,1.45);
this.priceMap.put(“Ice Cream”,4.00);
this.priceMap.put(“Frozen Pizza”,6.50);
this.priceMap.put(“Milk”,2.00);
this.priceMap.put(“Orange Juice”,3.00);
this.priceMap.put(“Butter”,4.00);
this.priceMap.put(“Cheese”,4.00);
this.priceMap.put(“Bread”,2.00);
this.priceMap.put(“Candy”,3.00);
}
@Override
public String toString() {
// TODO Implement this method
return qauntityMap.toString();
}
@Override
public double getTotal() {
// TODO Auto-generated method stub
Set<String> keys= quantityMap.valueset * priceMap.valueset();
for (String s: keys) {
System.out.println(“key: ” + s);
}
}
}
ShoppingBagTest.java
package com.yourname;
public class ShoppingBagTest {
public static void main(String[] args) {
ShoppingBag bag = new ShoppingBag();
// TODO Add the rest of the items to the bag according to the shopping list
bag.addItem(“Tomatoes”, 2);
bag.addItem(“Lettuce”, 1);
bag.addItem(“Cucumber”, 3);
bag.addItem(“Radishes”, 3);
bag.addItem(“Ground Beef”, 3);
bag.addItem(“Frozen Pizza”, 2);
bag.addItem(“Bread”, 1);
bag.addItem(“Candy”,20);
// Print the Bag
System.out.println(bag);
// Print the Bag’s total cost
System.out.println(bag.getTotal());
}
}
Solution
Bag.java
package com.yourname;
public abstract class Bag {
public abstract double getTotal();
public abstract void addItem(String item, int quantity);
}
ShoppingBag.java
package com.yourname;
import java.util.HashMap;
import java.util.Map;
public class ShoppingBag extends Bag {
// Declare Data Structures
Map<String, Double>priceMap;
Map<String, Integer>quantityMap;
// Constructor
public ShoppingBag() {
// Initialise Data Structures
priceMap = new HashMap<>();
quantityMap = new HashMap<>();
this.priceMap.put(“Lettuce”, 2.50);
this.priceMap.put(“Tomatoes”, 2.33);
this.priceMap.put(“Cucumber”, 1.00);
this.priceMap.put(“Radish”, 0.50);
this.priceMap.put(“Garlic”, 0.50);
this.priceMap.put(“Ground Beef”, 3.50);
this.priceMap.put(“Chicken Breast”, 4.00);
this.priceMap.put(“Frozen Mixed Vegetables”, 1.45);
this.priceMap.put(“Ice Cream”, 4.00);
this.priceMap.put(“Frozen Pizza”, 6.50);
this.priceMap.put(“Milk”, 2.00);
this.priceMap.put(“Orange Juice”, 3.00);
this.priceMap.put(“Butter”, 4.00);
this.priceMap.put(“Cheese”, 4.00);
this.priceMap.put(“Bread”, 2.00);
this.priceMap.put(“Candy”, 3.00);
}
@Override
public String toString() {
// This will be the string we return. It starts with “Bag: ”
String bagContent = “Bag: “;
// We iterate through the whole quantityMap
for (Map.Entry<String, Integer> article : quantityMap.entrySet()) {
// And for each article in the quantity map we add the quantity and article name
// to the string
bagContent += article.getValue() + ” ” + article.getKey() + “, “;
}
// And we return a substring of bagContent up to length() -2! This is because
// in our last iteration of the for loop we added a “,” at the end, and there
// should not be a “,” at the end
return bagContent.substring(0, bagContent.length() – 2);
}
@Override
public double getTotal() {
// This will be our total price
double totalCost = 0;
// We iterate through all the articles in the quantity map
for (Map.Entry<String, Integer> article : quantityMap.entrySet()) {
// And we get the name and quantity of each article
String articleName = article.getKey();
int quantity = article.getValue();
// Now we will get the price from the price map,
// using the article name to find the price
Double price = priceMap.get(articleName);
// And we add price*quantity to the total costs
totalCost += price * quantity;
}
// Return the total cost
return totalCost;
}
@Override
public void addItem(String item, int quantity) {
// We check if the item is already in the bag.
// If the item is already in the bag, we get the
// quantity in the bag and add the new quantity to it!
if (quantityMap.containsKey(item)) {
intoldQuantity = quantityMap.get(item);
quantityMap.put(item, oldQuantity + quantity);
// If the item is not already in the bag, we just put
// it in the map.
} else {
quantityMap.put(item, quantity);
}
}
}
ShoppingBagTest.java
package com.yourname;
public class ShoppingBagTest {
public static void main(String[] args) {
ShoppingBag bag = new ShoppingBag();
// TODO Add the rest of the items to the bag according to the shopping list
bag.addItem(“Tomatoes”, 2);
bag.addItem(“Lettuce”, 1);
bag.addItem(“Cucumber”, 3);
bag.addItem(“Radish”, 3);
bag.addItem(“Ground Beef”, 3);
bag.addItem(“Frozen Pizza”, 2);
bag.addItem(“Bread”, 1);
bag.addItem(“Candy”, 20);
// Print the Bag
System.out.println(bag);
// Print the Bag’s total cost
System.out.println(“Total price: ” + bag.getTotal());
}
}