LardaX

ActivityTrackerWithDate

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