-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductPrice.java
38 lines (37 loc) · 1.24 KB
/
ProductPrice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
import java.io.*;
class ProductPrice{
public static float getPrice(String[] input, HashMap<String, String> mapObj){
Iterator<Map.Entry<String, String>> itr=mapObj.entrySet().iterator();
float price=0f;
while(itr.hasNext()){
Map.Entry<String, String> entry=itr.next();
for(String i: input){
if((entry.getKey()).equalsIgnoreCase(i)){
float p=Float.valueOf(entry.getValue());
price=price+p;
}
}
}
return price;
}
public static void main(String[] args) throws java.lang.Exception {
Scanner s=new Scanner(System.in);
HashMap<String, String> obj=new HashMap<String, String>();
System.out.println("\nEnter total number of products..");
int n=s.nextInt();
String[] productName=new String[n];
String[] productPrice=new String[n];
System.out.println("\nEnter Product name and price..");
for(int i=0;i<n;i++){
productName[i]=s.next();
productPrice[i]=s.next();
obj.put(productName[i], productPrice[i]);
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter product's names..");
String cart[] = br.readLine().split(" ");
float cost=getPrice(cart, obj);
System.out.println("\nCost: "+cost);
}
}