Advertisement
dimipan80

Exam 4. Activity Tracker

Sep 16th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.Scanner;
  3. import java.util.TreeMap;
  4.  
  5. public class _4_ActivityTracker {
  6.  
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.         Locale.setDefault(Locale.ROOT);
  10.         Scanner scan = new Scanner(System.in);
  11.         int count = scan.nextInt();
  12.  
  13.         TreeMap<Integer, TreeMap<String, Integer>> monthsActivity = new TreeMap<>();
  14.         for (int i = 0; i < count; i++) {
  15.             String dateStr = scan.next();
  16.             String[] splittedString = dateStr.split("/");
  17.             int month = Integer.parseInt(splittedString[1]);
  18.             String userName = scan.next();
  19.             int distance = scan.nextInt();
  20.             scan.nextLine();
  21.  
  22.             TreeMap<String, Integer> usersActivity;
  23.             if (!monthsActivity.containsKey(month)) {
  24.                 usersActivity = new TreeMap<>();
  25.                 usersActivity.put(userName, distance);
  26.             } else {
  27.                 usersActivity = monthsActivity.get(month);
  28.                 Integer previous = usersActivity.get(userName);
  29.                 if (previous == null) {
  30.                     previous = 0;
  31.                 }
  32.  
  33.                 usersActivity.put(userName, previous + distance);
  34.             }
  35.  
  36.             monthsActivity.put(month, usersActivity);
  37.         }
  38.  
  39.         for (Integer month : monthsActivity.keySet()) {
  40.             System.out.print(month + ": ");
  41.             TreeMap<String, Integer> userActivity = monthsActivity.get(month);
  42.             boolean isFirst = true;
  43.             for (String user : userActivity.keySet()) {
  44.                 if (!isFirst) {
  45.                     System.out.print(", ");
  46.                 }
  47.  
  48.                 isFirst = false;
  49.                 System.out.print(user + "(");
  50.                 System.out.print(userActivity.get(user) + ")");
  51.             }
  52.  
  53.             System.out.println();
  54.         }
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement