Advertisement
dentia

Orders

Jun 17th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. package _27ExamPreparation;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class _04task {
  8.     public static void main(String[] args) {
  9.         Scanner in = new Scanner(System.in);
  10.         int n = Integer.parseInt(in.nextLine());
  11.  
  12.         LinkedHashMap<String, TreeMap<String, Integer>> productsInfo = new LinkedHashMap<>();
  13.  
  14.         String[] input;
  15.         for (int i = 0; i < n; i++) {
  16.             input = in.nextLine().split(" ");
  17.             String customer = input[0];
  18.             int amount = Integer.parseInt(input[1]);
  19.             String product = input[2];
  20.  
  21.             if (!productsInfo.containsKey(product)) {
  22.                 productsInfo.put(product, new TreeMap<String, Integer>());
  23.             }
  24.  
  25.             int oldAmount = 0;
  26.             TreeMap<String, Integer> info = productsInfo.get(product);
  27.             if (info.containsKey(customer)) {
  28.                 oldAmount = info.get(customer);
  29.             }
  30.             info.put(customer, oldAmount + amount);
  31.         }
  32.  
  33.         for (String prod : productsInfo.keySet()) {
  34.             System.out.print(prod + ": ");
  35.             System.out.println(productsInfo.get(prod)
  36.                     .toString()
  37.                     .replace("{", "")
  38.                     .replace("}", "")
  39.                     .replace("=", " "));
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement