Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class exercises_Map2 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Map<String, Double> products = new LinkedHashMap<>();
- Map<String, Integer> productsQuanity = new LinkedHashMap<>();
- while(true){
- List<String> prodInfo = Arrays.asList(scan.nextLine().split(" "));
- if(prodInfo.contains("buy")){
- break;
- }
- String prodName = prodInfo.get(0);
- double price = Double.parseDouble(prodInfo.get(1));
- int quantity = Integer.parseInt(prodInfo.get(2));
- if(!products.containsKey(prodName)){
- productsQuanity.put(prodName, quantity);
- products.put(prodName,price*quantity);
- }
- else{
- int totalQuantityTillNow = productsQuanity.get(prodName);
- int nextQuantity = totalQuantityTillNow + quantity;
- products.put(prodName, price*nextQuantity);
- productsQuanity.put(prodName,nextQuantity);
- }
- }
- for (Map.Entry<String, Double> entry : products.entrySet()) {
- System.out.printf("%s -> %.2f%n" , entry.getKey(), entry.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement