veselinsavov

Untitled

Sep 10th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.TreeMap;
  3.  
  4. public class LogsAggregator {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner input = new Scanner(System.in);
  9.         int n = input.nextInt();
  10.         input.nextLine();
  11.         TreeMap<String, TreeMap<String, Integer>> allUsers = new TreeMap<>();
  12.         // fill map with ips,names,duration of all inputLines
  13.         for (int i = 0; i < n; i++) {
  14.             String[] inputLine = input.nextLine().split(" ");
  15.             String ip = inputLine[0];
  16.             String name = inputLine[1];
  17.             int duration = Integer.parseInt(inputLine[2]);
  18.             if (!allUsers.containsKey(name)) {
  19.                 allUsers.put(name, new TreeMap<>());
  20.                 allUsers.get(name).put(ip, duration);
  21.             } else if (allUsers.get(name).containsKey(ip)) {
  22.                 int currentDuration = allUsers.get(name).get(ip);
  23.                 allUsers.get(name).put(ip, currentDuration + duration);
  24.  
  25.             } else {
  26.                 allUsers.get(name).put(ip, duration);
  27.             }
  28.         }
  29.         // print user-duration-ips
  30.         for (String user : allUsers.keySet()) {
  31.             System.out.print(user + ": ");
  32.             int allMinutes = 0;
  33.  
  34.             for (String ip : allUsers.get(user).keySet()) {
  35.                 allMinutes += allUsers.get(user).get(ip);
  36.             }
  37.             System.out.print(allMinutes + " ");
  38.             System.out.print(allUsers.get(user).keySet());
  39.             System.out.println();
  40.         }
  41.         input.close();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment