svetlozar_kirkov

Orders

Feb 6th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class Orders {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner input = new Scanner(System.in);
  10.         int n = input.nextInt();
  11.         Map<String,Map<String,Integer>> orders = new LinkedHashMap<>();
  12.         for (int i = 0; i < n; i++){
  13.             String name = input.next();
  14.             Integer count = input.nextInt();
  15.             String product = input.next();
  16.             if (!orders.containsKey(product)){
  17.                 Map<String,Integer> namesAndCount = new TreeMap<>();
  18.                 namesAndCount.put(name, count);
  19.                 orders.put(product, namesAndCount);
  20.             }
  21.             else {
  22.                 Map<String,Integer> oldNamesAndCount = orders.get(product);
  23.                 if (oldNamesAndCount.containsKey(name)){
  24.                     oldNamesAndCount.computeIfPresent(name, (k, v) -> v + count);
  25.                 }
  26.                 else{
  27.                     oldNamesAndCount.put(name, count);
  28.                 }
  29.                 orders.put(product, oldNamesAndCount);
  30.             }
  31.         }
  32.         for(Map.Entry<String,Map<String,Integer>> entry : orders.entrySet()) {
  33.             String product = entry.getKey();
  34.             Map<String,Integer> namesAndCounts = entry.getValue();
  35.             String result = namesAndCounts.toString().replace("{", "").replace("}", "").replaceAll("=", " ");
  36.             System.out.println(product + ": " + result);
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment