Advertisement
nmnikolov

JavaExam21Sep_04_Nuts

Sep 22nd, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 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.  
  7. public class _04_Nuts {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner input = new Scanner(System.in);
  11.         int n = Integer.parseInt(input.nextLine());
  12.         Map<String, LinkedHashMap<String, Integer>> nuts = new TreeMap<String, LinkedHashMap<String, Integer>>();
  13.        
  14.         for (int i = 0; i < n; i++) {
  15.             String[] line = input.nextLine().split(" ");
  16.             String company = line[0];
  17.             String nut = line[1];
  18.             String a = line[2].substring(0, line[2].length() - 2);
  19.             int amount = Integer.parseInt(a);
  20.                
  21.             if (nuts.containsKey(company)) {
  22.                 if (nuts.get(company).containsKey(nut)) {
  23.                     int count = nuts.get(company).get(nut).intValue();
  24.                     count += amount;
  25.                     nuts.get(company).put(nut, count);
  26.                    
  27.                 } else {                   
  28.                     nuts.get(company).put(nut, amount);
  29.                 }              
  30.             } else {
  31.                 nuts.put(company, new LinkedHashMap <String, Integer>());
  32.                 nuts.get(company).put(nut, amount);
  33.             }
  34.         }
  35.        
  36.         for (String company :  nuts.keySet()) {
  37.             String output = company + ":";
  38.            
  39.             for (String nut : nuts.get(company).keySet()) {        
  40.                 output += " " + nut + "-" + nuts.get(company).get(nut).intValue() + "kg,";
  41.             }
  42.            
  43.             output = output.substring(0,output.length() - 1);              
  44.             System.out.println(output);
  45.         }  
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement