Advertisement
Guest User

Activity Tracker

a guest
Sep 9th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Scanner;
  8. import java.util.SortedMap;
  9. import java.util.TreeMap;
  10.  
  11. public class ActivityTracker {
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         Comparator<Entry<String, Integer>> cValue = new Comparator<Entry<String, Integer>>() {
  16.  
  17.             @Override
  18.             public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
  19.                 return o2.getValue().compareTo(o1.getValue());
  20.             }
  21.         };
  22.  
  23.         Scanner sc = new Scanner(System.in);
  24.  
  25.         int n = sc.nextInt();
  26.         SortedMap<Integer, TreeMap<String, Integer>> activity = new TreeMap<>();
  27.  
  28.         for (int i = 0; i < n; i++) {
  29.             String date = sc.next("[0-9]{2}/[0-9]{2}/[0-9]{4}");
  30.             int month = Integer.parseInt(date.substring(3, 5));
  31.             String name = sc.next();
  32.             int distance = sc.nextInt();
  33.  
  34.             if (!(activity.containsKey(month))) {
  35.                 activity.put(month, new TreeMap<String, Integer>());
  36.             }
  37.             TreeMap<String, Integer> dist = activity.get(month);
  38.             int d = 0;
  39.             if (dist.containsKey(name)) {
  40.                 d = dist.get(name);
  41.             }
  42.             dist.put(name, d + distance);
  43.         }
  44.         sc.close();
  45.  
  46.         for (Integer month : activity.keySet()) {
  47.             List<Map.Entry<String, Integer>> l = new ArrayList<Map.Entry<String, Integer>>(activity.get(month).entrySet());
  48.             Collections.sort(l, cValue);
  49.             System.out.print(month + ": ");
  50.  
  51.             boolean first = true;
  52.             for (Entry<String, Integer> entry : l) {
  53.                 if (!first) {
  54.                     System.out.print(", ");
  55.                 }
  56.                 first = false;
  57.                 System.out.print(entry.getKey() + "(" + entry.getValue() + ")");
  58.             }
  59.             System.out.println();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement