Write a small price calculator application with the below-mentioned flow:
- Read a value n indicating the total count of devices. This would be followed by the name and price of the device. The datatype for a name would be String and price would be the float.
- Build a hashmap containing the peripheral devices with the name as key and price as value.
- Read a value m indicating the number of devices for which the price has to be calculated. This would be followed by device names.
- For each device mentioned in the array calculate the total price.
- You decide to write a function cost estimator which takes the above hashmap and array as input and returns the total price (float) as output with two decimal points.
Input and Output Format:
Input consists of device details. The first number indicates the size of the devices. The next two values indicate the name, price.
This would be followed by m indicating the size of the device array. The next m values would be the device names.
The output consists of the total price in the float.
Refer sample output for formatting specifications.
Sample Input 1:
3
Monitor
1200.36
Mouse
100.42
Speakers
500.25
2
Speakers
Mouse
Sample Output 1:
600.67
price calculator application in java
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<String, String> m1=new HashMap<String, String>();
m1.put("monitor", "1200.36");
m1.put("mouse","100.42");
m1.put("speaker", "500.25");
String[] s={"speaker","mouse"};
System.out.println(getTheTotalCostOfPheripherals(m1,s));
}
public static float getTheTotalCostOfPheripherals(HashMap<String,String> m1,String[] s) {
Float f=(float) 0;
Iterator<String> i=m1.keySet().iterator();
while(i.hasNext()){
String s1=(String) i.next();
Float f1=Float.parseFloat(m1.get(s1));
for(int j=0;j<s.length;j++)
if(s[j].equals(s1))
f+=f1; }
return f;
}}