LardaX

ActivityTracker

Feb 14th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.stream.Collectors;
  7.  
  8. public class ActivityTrackerTests {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         int logsCount = Integer.parseInt(reader.readLine());
  12.  
  13.         // Date thedate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(mystring);
  14.  
  15.         Map<Integer, Map<String, Double>> activityByMonth = new LinkedHashMap<>();
  16.  
  17.         for (int i = 0; i < logsCount; i++) {
  18.             String[] logInput = reader.readLine().split(" ");
  19.             String[] date = logInput[0].split("\\/");
  20.             int month = Integer.parseInt(date[1]);
  21.  
  22.             String user = logInput[1];
  23.             double distance = Double.parseDouble(logInput[2]);
  24.  
  25.             activityByMonth.putIfAbsent(month, new LinkedHashMap<>());
  26.             activityByMonth.get(month).putIfAbsent(user, 0D);
  27.             activityByMonth.get(month).put(user, activityByMonth.get(month).get(user) + distance);
  28.         }
  29.  
  30.         activityByMonth.entrySet().stream()
  31.                 .sorted((m1, m2) -> m1.getKey().compareTo(m2.getKey()))
  32.                 .forEach(month -> System.out.println(month.getKey() + ": " +
  33.                 month.getValue().entrySet().stream()
  34.                         .sorted((u1, u2) -> Double.compare(u2.getValue(), u1.getValue()))
  35.                         .map(u -> String.format("%s(%.0f)",u.getKey(), u.getValue()))
  36.                         .collect(Collectors.joining(", "))
  37.                 ));
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment