Advertisement
dimipan80

Exam 4. Orders

Sep 15th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 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 _4_Orders {
  7.  
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Scanner scan = new Scanner(System.in);
  11.         int count = scan.nextInt();
  12.         scan.nextLine();
  13.  
  14.         Map<String, TreeMap<String, Integer>> orders = new LinkedHashMap<>();
  15.         for (int i = 0; i < count; i++) {
  16.             String[] inputs = scan.nextLine().split("[^A-Za-z0-9]+");
  17.             String product = inputs[2];
  18.             String customer = inputs[0];
  19.             Integer amount = Integer.parseInt(inputs[1]);
  20.             if (!orders.containsKey(product)) {
  21.                 orders.put(product, new TreeMap<String, Integer>());
  22.             }
  23.  
  24.             TreeMap<String, Integer> amounts = orders.get(product);
  25.             int previousAmount = 0;
  26.             if (amounts.containsKey(customer)) {
  27.                 previousAmount = amounts.get(customer);
  28.             }
  29.  
  30.             amounts.put(customer, previousAmount + amount);
  31.         }
  32.  
  33.         for (String product : orders.keySet()) {
  34.             System.out.print(product + ": ");
  35.             TreeMap<String, Integer> amounts = orders.get(product);
  36.             boolean first = true;
  37.             for (Map.Entry<String, Integer> pair : amounts.entrySet()) {
  38.                 if (!first) {
  39.                     System.out.print(", ");
  40.                 }
  41.                 first = false;
  42.                 String customer = pair.getKey();
  43.                 int amount = pair.getValue();
  44.                 System.out.print(customer + " " + amount);
  45.             }
  46.  
  47.             System.out.println();
  48.         }
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement