final Map p1 = new HashMap<>(); //Example Prices p1.put(\"apples\", 2.); p1.put(
ID: 3809495 • Letter: F
Question
final Map p1 = new HashMap<>();
//Example Prices
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 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 p3 = new HashMap<>();
p3.put("oranges", 0.75);
p3.put("limes", 0.5);
// Example Shopping Lists
final Map le1 = new HashMap<>();
le1.put("apples", 1);
le1.put("cranberries", 2);
final Map le2 = new HashMap<>();
le2.put("bananas", 3);
le2.put("limes", 4);
final Map l1 = new HashMap<>();
l1.put("apples", 1);
final Map l2 = new HashMap<>();
l2.put("oranges", 2);
l2.put("limes", 4);
final Map l3 = new HashMap<>();
l3.put("apples", 2);
l3.put("pears", 3);
l3.put("limes", 4);
final Map l4 = new HashMap<>();
l4.put("apples", 5);
l4.put("pears", 4);
l4.put("limes", 3);
l4.put("oranges", 2);
final Map l5 = new HashMap<>();
l5.put("strawberries", 1);
l5.put("apples", 5);
l5.put("pears", 4);
l5.put("limes", 3);
l5.put("oranges", 2);
final Map lb1 = new HashMap<>();
lb1.put("mangoes", 1);
final Map lb2 = new HashMap<>();
lb2.put("apples", 1);
lb2.put("blueberries", 2);
final Map lb3 = new HashMap<>();
lb3.put("apples", 2);
lb3.put("limes", 4);
lb3.put("watermelons", 1);
final Map lb4 = new HashMap<>();
lb4.put("apples", 2);
lb4.put("limes", 4);
lb4.put("pears", 4);
lb4.put("blueberries", 8);
final Map lb5 = new HashMap<>();
lb5.put("strawberries", 1);
lb5.put("apples", 5);
lb5.put("pears", 4);
lb5.put("limes", 3);
lb5.put("passionfruits", 2);
Testing getCost
_testCost(p1, lb1, null); //no mangoes
_testCost(p1, lb2, null); //no blueberries
_testCost(p1, lb3, null); //no watermelons
_testCost(p1, lb4, null); //no pears & blueberries
_testCost(p1, l1, 2.);
_testCost(p1, l2, 6.);
_testCost(p1, l3, 12.25);
_testCost(p1, l4, 22.25);
_testCost(p1, l5, 23.25);
_testCost(p2, l1, 1.);
_testCost(p2, l2, 16.);
_testCost(p2, l3, 17.35);
_testCost(p2, l4, 24.05);
_testCost(p2, l5, 26.05);
// Example of stores for best cost
final List> pl0 = new ArrayList<>();
final List> pl1 = new ArrayList<>();
pl1.add(p1);
final List> pl2 = new ArrayList<>();
pl2.add(p2);
final List> pl3 = new ArrayList<>();
pl3.add(p3);
final List> pl12 = new ArrayList<>();
pl12.add(p1);
pl12.add(p2);
final List> pl13 = new ArrayList<>();
pl13.add(p1);
pl13.add(p3);
final List> pl23 = new ArrayList<>();
pl23.add(p2);
pl23.add(p3);
final List> pl123 = new ArrayList<>();
pl123.add(p1);
pl123.add(p2);
pl123.add(p3);
// Testing bestCost
_testBest(pl1, lb2, null);
_testBest(pl2, lb2, null);
_testBest(pl2, l1, p2);
_testBest(pl2, l2, p2);
_testBest(pl3, le1, null);
_testBest(pl3, le2, null);
_testBest(pl12, le1, p1);
_testBest(pl12, le2, p2);
_testBest(pl13, le1, p1);
_testBest(pl13, le2, null);
_testBest(pl23, le1, null);
_testBest(pl23, le2, p2);
_testBest(pl123, le1, p1);
_testBest(pl123, le2, p2);
Thank you
Explanation / Answer
public static Double getCost(Map<String, Double> prices, Map<String, Integer> shoppingList)
{
Double total = 0.0;
Double cost = 0.0;
Integer qty = 0;
if(prices == null){
return null;
}
Set set= shoppingList.entrySet();
Iterator itr = set.iterator();
while(itr.hasNext())
{
cost = 0.0;
Map.Entry value = (Map.Entry)itr.next();
cost = (Double)prices.get((String)value.getKey());
if(cost == null)
return null;
cost = cost * (Integer)(value.getValue());
total += cost;
}
return total;
}
public static Map<String, Double> bestPrices(List<Map<String, Double>> stores, Map<String, Integer> shoppingList)
{
Double lowestCost = null;
Double tempCost = 0.0;
Map<String,Double> bestStore = null;
if(stores == null)
return null;
for(Map<String,Double> store : stores){
tempCost = getCost(store, shoppingList);
if(tempCost != null){
if(lowestCost == null){
lowestCost = tempCost;
bestStore = store;
}
else if(tempCost < lowestCost){
lowestCost = tempCost;
bestStore = store;
}
}
}
return bestStore;
}
Case checked :
public static void main(String []args)
{
Map<String,Double> p1 = new HashMap<String,Double>();
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.);
Map<String,Double> p2 = new HashMap<String,Double>();
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.);
Map<String,Double> p3 = new HashMap<String,Double>();
p3.put("oranges", 0.75);
p3.put("limes", 0.5);
Map<String,Integer> le1 = new HashMap<String,Integer>();
le1.put("apples", 1);
le1.put("cranberries", 2);
Map<String,Integer> le2 = new HashMap<String,Integer>();
le2.put("bananas", 3);
le2.put("limes", 4);
List<Map<String,Double>> pl12 = new ArrayList<>();
pl12.add(p2);
pl12.add(p1);
List<Map<String,Double>> pl13 = new ArrayList<>();
pl13.add(p1);
pl13.add(p3);
List<Map<String,Double>> pl23 = new ArrayList<>();
pl23.add(p2);
pl23.add(p3);
System.out.println("List : " + bestPrices(pl23,le1));
System.out.println("List : " + bestPrices(pl13,le2));
System.out.println("List : " + bestPrices(pl12,le1));
System.out.println("List : " + bestPrices(pl13,le1));
System.out.println("List : " + bestPrices(pl12,le2));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.