Guest User

Untitled

a guest
Sep 21st, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Scanner;
  3. import java.util.TreeMap;
  4.  
  5.  
  6. public class Nuts {
  7.  
  8.     public static void main(String[] args) {
  9.        
  10.         Scanner input = new Scanner(System.in);
  11.        
  12.         int n = Integer.parseInt(input.nextLine());
  13.        
  14.         TreeMap<String, LinkedHashMap<String, Integer>> ordersMap = new TreeMap<>();
  15.        
  16.         for (int i = 0; i < n; i++) {
  17.             String[] line = input.nextLine().split(" ");
  18.            
  19.             String company = line[0];
  20.             String product = line[1];
  21.             String amount = line[2];
  22.             String amountDigit = "";
  23.             for (int j = 0; j < amount.length() - 2; j++) {
  24.                 amountDigit += amount.charAt(j);
  25.             }
  26.            
  27.             int amountDigitInt = Integer.parseInt(amountDigit);
  28.            
  29.             if (ordersMap.containsKey(company)) {
  30.                 if (ordersMap.get(company).containsKey(product)) {
  31.                     amountDigitInt += ordersMap.get(company).get(product);
  32.                 }
  33.                 ordersMap.get(company).put(product, amountDigitInt);
  34.             } else {
  35.                 LinkedHashMap<String, Integer> products = new LinkedHashMap<>();
  36.                 products.put(product, amountDigitInt);
  37.                 ordersMap.put(company, products);
  38.             }
  39.         }
  40.        
  41.         for (String company : ordersMap.keySet()) {
  42.             System.out.print(company + ": ");
  43.             LinkedHashMap<String, Integer> products = ordersMap.get(company);
  44.             int count = 1;
  45.             for (String product : products.keySet()) {
  46.                 if (count < products.keySet().size()) {
  47.                     System.out.printf("%s-%dkg, ", product, products.get(product));
  48.                 } else {
  49.                     System.out.printf("%s-%dkg", product, products.get(product));
  50.                 }
  51.                 count++;
  52.             }
  53.             System.out.println();
  54.         }
  55.  
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment