Advertisement
desislava_topuzakova

09. User Logs

Jan 18th, 2022
1,570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. package SetsAndMapAdvanced_Exercise;
  2.  
  3. import java.util.*;
  4.  
  5. public class UserLogs_09 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         //user -> {ip -> count}
  9.         TreeMap<String, LinkedHashMap<String, Integer>> data = new TreeMap<>();
  10.         String input = scanner.nextLine();
  11.         //"IP={IP.Address} message={A&sample&message} user={username}"
  12.         while(!input.equals("end")) {
  13.             String[] inputParts = input.split("\\s+"); //["IP={IP.Address}", "message={A&sample&message}", "user={username}"]
  14.             String ip = inputParts[0].split("=")[1]; //"IP={IP.Address}".split("=") -> ["IP","{ip addres}"]
  15.             //String message = inputParts[1].split("=")[1]; // "message={A&sample&message}".split("=") -> ["message", "{A&sample&message}"]
  16.              String username = inputParts[2].split("=")[1]; //"user={username}".split("=") -> ["user", "{username}"]
  17.  
  18.             //username да не съществува
  19.             if (!data.containsKey(username)) {
  20.                 data.put(username, new LinkedHashMap<>(){
  21.                     {
  22.                         put(ip, 1);
  23.                     }
  24.                 });
  25.             }
  26.             // username да съществува
  27.             else {
  28.                 //текущия списък с ip
  29.                 Map<String, Integer> currentIps = data.get(username); //списък с ip-та (ip -> бр срещанията)
  30.                 //ip да е посетено (има в списъка)
  31.                 if (currentIps.containsKey(ip)) {
  32.                     int currentTimes = currentIps.get(ip);
  33.                     currentIps.put(ip, currentTimes + 1);
  34.                 }
  35.                 //ip не е посещавано (няма в списъка)
  36.                 else {
  37.                     currentIps.put(ip, 1);
  38.                 }
  39.             }
  40.  
  41.             input = scanner.nextLine();
  42.         }
  43.  
  44.         //"username:
  45.         //{IP} => {count}, {IP} => {count}."
  46.         data.keySet().forEach(username -> {
  47.             System.out.println(username + ":");
  48.             Map<String, Integer> ips = data.get(username);
  49.             //запис: ip -> бр. срещанията
  50.             ips.entrySet().forEach(entry -> {
  51.                 //if не е последното ip
  52.                 System.out.println(entry.getKey() + " => " + entry.getValue() + ", ");
  53.                 //TODO: последното ip да се отпчатва с точка накрая
  54.                 //if е последното ip в map-a
  55.                 //System.out.println(entry.getKey() + " => " + entry.getValue() + ". ");
  56.             });
  57.         });
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement