Shopping! In the getCost method, you are supplied a price list (items mapped to
ID: 3807189 • Letter: S
Question
Shopping!
In the getCost method, you are supplied a price list (items mapped to prices) and a shopping list (items mapped to quantities). You are to return the cost of purchasing the items according to these prices.
In the bestPrices method, you are supplied a list of stores (each is a price list, like in getCost above) and a shopping list (like in getCost above). You are to return the price list that results in the lowest cost.
final Map<String, Double> p1 = new HashMap<>();
p1.put("apples", 2.);
p1.put("oranges", 1.5);
p1.put("pears", 1.75);
p1.put("limes", 0.75);
p1.put("strawberries", 1.);
p1.put("cranberries", 4.);
final Map<String, Double> p2 = new HashMap<>();
p2.put("apples", 1.);
p2.put("oranges", 2.5);
p2.put("pears", 1.45);
p2.put("limes", 2.75);
p2.put("strawberries", 2.);
p2.put("bananas", 1.);
final Map<String, Double> p3 = new HashMap<>();
p3.put("oranges", 0.75);
p3.put("limes", 0.5);
/**
* Calculates the cost of purchasing
* a list of items given item prices
*
* @param prices item prices (name -> cost)
* @param shoppingList list to purchase (name -> quantity)
* @return total cost of purchasing the shoppingList according to prices, null if prices does not contain the item
*/
public static Double getCost(Map<String, Double> prices, Map<String, Integer> shoppingList) {
// Suggested algorithm:
// 1. Initialize cost to be 0
// 2. Loop through the shopping list, for each item...
// a) Look up the cost in the price map (return null if not found)
// b) Add to cost quantity * associated price
// 3. Return cost
return null;
}
/**
* Determines the best store in which to
* purchase a list of items
*
* @param stores list of prices per store (each, name -> cost)
* @param shoppingList list to purchase (name -> quantity)
* @return pricing scheme with lowest total cost, null if no stores that can fully satisfy the list
*/
public static Map<String, Double> bestPrices(List<Map<String, Double>> stores, Map<String, Integer> shoppingList) {
// Suggested algorithm:
// 1. Keep track of the best store and the associated best cost (starts with nothing)
// 2. Loop over all stores, for each...
// a) Get the associated cost (use getCost())
// b) If the store can service the order, see if cost is lower than best so far
// c) If so, replace best map/cost
// 3. Return best price list
return null;
}
Explanation / Answer
//packages
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import java.util.*;
//Shopping class
class Shopping{
//to check a store can service a shopping list
static boolean canService=true;
//main method
public static void main(String args[]){
//price list
final Map<String, Double> p1 = new HashMap<>();
p1.put("apples", 2.);
p1.put("oranges", 1.5);
p1.put("pears", 1.75);
p1.put("limes", 0.75);
p1.put("strawberries", 1.);
p1.put("cranberries", 4.);
final Map<String, Double> p2 = new HashMap<>();
p2.put("apples", 1.);
p2.put("oranges", 2.5);
p2.put("pears", 1.45);
p2.put("limes", 2.75);
p2.put("strawberries", 2.);
p2.put("bananas", 1.);
final Map<String, Double> p3 = new HashMap<>();
p3.put("oranges", 0.75);
p3.put("limes", 0.5);
//list of stores
List<Map<String, Double>> stores=new ArrayList<Map<String, Double>> ();
stores.add(p1);
stores.add(p2);
stores.add(p3);
//shopping list
final Map<String, Integer> sp1 = new HashMap<>();
sp1.put("apples", 20);
sp1.put("oranges", 15);
sp1.put("pears", 10);
sp1.put("limes", 5);
sp1.put("strawberries", 1);
//calling bestPrices
System.out.println("Best Sotre List:::");
Map<String,Double> result=bestPrices(stores,sp1);
String item;
double val;
for(Map.Entry<String, Double> entry : result.entrySet()){
//retrieving item and its price
item=entry.getKey().toString();
val=entry.getValue();
System.out.println(item+" "+val);
}
}
public static Double getCost(Map<String, Double> prices, Map<String, Integer> shoppingList) {
// 1. Initialize cost to be 0
double cost=0;
String item;
int quantity;
double price;
canService=true;
// 2. Loop through the shopping list, for each item...
for(Map.Entry<String, Integer> entry : shoppingList.entrySet()){
//retrieving item and its price
item=entry.getKey().toString();
quantity=entry.getValue();
//Look up the cost in the price map (return null if not found)
if(prices.get(item)!=null){
//retrieving cost of item
price=prices.get(item);
// Add to cost quantity * associated price
cost+=quantity*price;
}
else{
canService=false;
}
}
// Return cost
return cost;
}
//bestPrice list
public static Map<String, Double> bestPrices(List<Map<String, Double>> stores, Map<String, Integer> shoppingList) {
//Keep track of the best store and the associated best cost (starts with nothing)
Map<String,Double> result=null;
double bestCost=345346324;
double cost;
// Loop over all stores, for each...
for(int i=0;i<stores.size();i++){
//Get the associated cost (use getCost())
cost=getCost(stores.get(i),shoppingList);
// If the store can service the order, see if cost is lower than best so far
if(canService && cost<bestCost){
bestCost=cost;
result=stores.get(i);
}
}
// 3. Return best price list
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.