dim4o

_04_OfficeStuff

Sep 24th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 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 _04_OfficeStuff {
  7.  
  8.     public static void main(String[] args) {
  9.         @SuppressWarnings("resource")
  10.         Scanner input = new Scanner(System.in);
  11.         int numOflines = Integer.parseInt(input.nextLine());
  12.         TreeMap<String, LinkedHashMap<String, Integer>> productMap = new TreeMap<String, LinkedHashMap<String, Integer>>();
  13.  
  14.         for (int i = 0; i < numOflines; i++) {
  15.             String currLineString = input.nextLine();
  16.             currLineString = currLineString.replaceAll("[|]", "");
  17.             String[] info = currLineString.split("[- ]+");
  18.             //System.out.println(Arrays.toString(info));
  19.             String companyName = info[0];
  20.             Integer amount = Integer.parseInt(info[1]);
  21.             String productName = info[2];
  22.            
  23.             if (productMap.containsKey(companyName)) {
  24.                 LinkedHashMap<String, Integer> amountMap = productMap.get(companyName);
  25.                 if (amountMap.containsKey(productName)) {
  26.                     Integer oldValue = amountMap.get(productName);
  27.                     amountMap.put(productName, oldValue + amount);
  28.                 } else {
  29.                     amountMap.put(productName, amount);
  30.                 }
  31.                 productMap.put(companyName, amountMap);
  32.                
  33.             } else {
  34.                 LinkedHashMap<String, Integer> amountMap = new LinkedHashMap<String, Integer>();
  35.                 amountMap.put(productName, amount);
  36.                 productMap.put(companyName, amountMap);
  37.             }
  38.         }
  39.         //System.out.println(productMap.toString());
  40.         for (Map.Entry<String, LinkedHashMap<String, Integer>> entry1 : productMap.entrySet()) {
  41.             LinkedHashMap<String, Integer> amountMap = entry1.getValue();
  42.             System.out.printf("%s: ", entry1.getKey());
  43.             String productString = "";
  44.             for (Map.Entry<String, Integer> entry2 : amountMap.entrySet()) {
  45.                 productString += String.format("%s-%s, ", entry2.getKey(), entry2.getValue());
  46.             }
  47.             System.out.println(productString.substring(0, productString.length() - 2));
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment