Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Scanner;
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class kur {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         LinkedHashMap<String, LinkedHashMap<Double,Integer>> list = new LinkedHashMap<>();
  10.         String input = "";
  11.         AtomicInteger oldQuantity = new AtomicInteger();
  12.  
  13.         while(!"buy".equals(input = sc.nextLine())){
  14.             String[] data = input.split(" ");
  15.             String product = data[0];
  16.             double price = Double.parseDouble(data[1]);
  17.             int quantity = Integer.parseInt(data[2]);
  18.  
  19.             if (list.containsKey(product)) {
  20.  
  21.                 list.get(product).entrySet().forEach(e -> {
  22.                      oldQuantity.set(e.getValue());
  23.                 });
  24.  
  25.                 list.get(product).remove(price);
  26.                 list.put(product, new LinkedHashMap<>());
  27.                 list.get(product).put(price, oldQuantity.get() + quantity);
  28.  
  29.             }else {
  30.  
  31.                 list.putIfAbsent(product, new LinkedHashMap<>());
  32.                 list.get(product).put(price, quantity);
  33.             }
  34.  
  35.         }
  36.         list.entrySet().forEach(pair -> {
  37.             System.out.print(pair.getKey() + " -> ");
  38.             pair.getValue().entrySet().forEach(subentry -> {
  39.                 System.out.printf("%.2f\n", subentry.getValue() * subentry.getKey());;
  40.             });
  41.             });
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement