Advertisement
filipovv

V-Logger

Oct 23rd, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package ExamJavaAdvanced22Oct2017;
  2.  
  3. import java.util.*;
  4.  
  5. public class VLogger {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         Map<String, followersFollowing> statistics = new HashMap<>();
  9.         while (true) {
  10.             String[] input = scan.nextLine().split(" ");
  11.             if (input[0].equalsIgnoreCase("statistics")) {
  12.                 break;
  13.             }
  14.             if (input.length == 3) {
  15.                 if (statistics.containsKey(input[0]) && statistics.containsKey(input[2])) {
  16.                     if (!input[0].equals(input[2])) {
  17.                         statistics.get(input[0]).getFollowing().add(input[2]);
  18.                         statistics.get(input[2]).getFollowers().add(input[0]);
  19.                     }
  20.                 }
  21.             }
  22.             if (input.length == 4) {
  23.                 if (!statistics.containsKey(input[0])) {
  24.                     statistics.put(input[0], new followersFollowing());
  25.                     statistics.get(input[0]).setFollowers(new HashSet<>());
  26.                     statistics.get(input[0]).setFollowing(new HashSet<>());
  27.                 } else {
  28.                     continue;
  29.                 }
  30.             }
  31.         }
  32.         System.out.println("The V-Logger has a total of " + statistics.size() + " vloggers in its logs.");
  33.         final int[] counter = {1};
  34.         statistics.entrySet()
  35.                 .stream()
  36.                 .sorted((y1, y2) -> Integer.valueOf(y1.getValue().getFollowing().size()).compareTo(Integer.valueOf(y2.getValue().getFollowing().size())))
  37.                 .sorted((x1, x2) -> Integer.valueOf(x2.getValue().getFollowers().size()).compareTo(Integer.valueOf(x1.getValue().getFollowers().size())))
  38.                 .forEach(x -> {
  39.                             if (counter[0] == 1) {
  40.                                 System.out.println(counter[0] + ". " + x.getKey() + " : " + x.getValue().getFollowers().size() + " followers, " + x.getValue().getFollowing().size() + " following");
  41.                                 x.getValue()
  42.                                         .getFollowers()
  43.                                         .stream()
  44.                                         .sorted()
  45.                                         .forEach(f -> System.out.println("*  " + f));
  46.                             } else {
  47.                                 System.out.println(counter[0] + ". " + x.getKey() + " : " + x.getValue().getFollowers().size() + " followers, " + x.getValue().getFollowing().size() + " following");
  48.                             }
  49.                             counter[0]++;
  50.                         }
  51.                 );
  52.     }
  53.  
  54.     static class followersFollowing {
  55.         public HashSet<String> followers;
  56.         public HashSet<String> following;
  57.  
  58.         public HashSet<String> getFollowers() {
  59.             return followers;
  60.         }
  61.  
  62.         public void setFollowers(HashSet<String> followers) {
  63.             this.followers = followers;
  64.         }
  65.  
  66.         public HashSet<String> getFollowing() {
  67.             return following;
  68.         }
  69.  
  70.         public void setFollowing(HashSet<String> following) {
  71.             this.following = following;
  72.         }
  73.  
  74.         public followersFollowing(HashSet<String> followers, HashSet<String> following) {
  75.  
  76.             this.followers = followers;
  77.             this.following = following;
  78.         }
  79.  
  80.         public followersFollowing() {
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement