Advertisement
DNNdrago

4, Logs Aggregator

Jun 2nd, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.Scanner;
  3. import java.util.TreeMap;
  4. import java.util.TreeSet;
  5.  
  6.  
  7. public class _04_Problem4 {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.        
  12.         int number = Integer.parseInt(sc.nextLine());
  13.        
  14.         Map<String, User> users = new TreeMap<>();
  15.        
  16.         for (int i = 0; i < number; i++) {
  17.             String[]  line = sc.nextLine().split(" ");
  18.            
  19.            
  20.             if(users.containsKey(line[1])) {
  21.                 users.get(line[1]).addIP(line[0]);
  22.                 users.get(line[1]).addDuration(Long.parseLong(line[2]));
  23.                 users.put(line[1], users.get(line[1]));
  24.             }
  25.             else {
  26.                 users.put(line[1], new User(line[1], line[0], Long.parseLong(line[2])));
  27.             }
  28.            
  29.            
  30.         }
  31.        
  32.         for (Map.Entry<String, User> user : users.entrySet()) {
  33.             System.out.println(user.getValue().toString());
  34.         }
  35.        
  36.        
  37.     }
  38.  
  39. }
  40.  
  41.  
  42. class User {
  43.     private String name;
  44.     private TreeSet<String> IP;
  45.     private long duration;
  46.    
  47.     public User() {
  48.         IP = new TreeSet<>();
  49.     }
  50.    
  51.     public User(String name, String ip, long duration) {
  52.         this.IP = new TreeSet<>();
  53.         this.name = name;
  54.         this.IP.add(ip);
  55.         this.duration = duration;
  56.     }
  57.    
  58.     public void addIP(String ip) {
  59.         this.IP.add(ip);
  60.     }
  61.     public void addDuration(long dur) {
  62.         this.duration += dur;
  63.     }
  64.    
  65.     public String toString() {
  66.         String allIP = "";
  67.        
  68.             allIP = this.IP.toString();
  69.  
  70.        
  71.         return this.name + ": " + this.duration + " " + allIP;//+ this.duration + " [" + allIP + " ]";
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement