svetlozar_kirkov

Acivity Tracker

Feb 5th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.time.LocalDate;
  3. import java.time.format.DateTimeFormatter;
  4. import java.util.ArrayList;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.TreeMap;
  8.  
  9.  
  10. public class ActivityTracker {
  11.  
  12.    
  13.     public static void main(String[] args) throws ParseException {
  14.         Scanner input = new Scanner(System.in);
  15.         int n = input.nextInt();
  16.         input.nextLine();
  17.         ArrayList<String[]> inputInfos = new ArrayList<>();
  18.         for (int i = 0; i < n; i++){
  19.             inputInfos.add(input.nextLine().split("[\\s]+"));
  20.         }
  21.         Map<String,String> tracker = new TreeMap<>();
  22.         for (int i = 0; i < inputInfos.size(); i++){
  23.             String[] tempInfo = inputInfos.get(i);
  24.             String dateString = tempInfo[0];
  25.             DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
  26.             LocalDate date = LocalDate.parse(dateString, formatter);
  27.             String month = Integer.toString(date.getMonthValue());
  28.             String name = tempInfo[1];
  29.             String distance = tempInfo[2];
  30.             if (tracker.containsKey(month)){
  31.                 String old = tracker.get(month);
  32.                 tracker.put(month, old+name+" "+distance+", ");
  33.             }
  34.             else {
  35.                 tracker.put(month, name+" "+distance+", ");
  36.             }
  37.         }
  38.         for(Map.Entry<String,String> entry : tracker.entrySet()) {
  39.             String month = entry.getKey();
  40.             String namesDistances = entry.getValue();
  41.             String[] temporary = namesDistances.split("[\\s\\', ']+");
  42.             Map<String,Integer> monthValues = new TreeMap<>();
  43.             for (int i = 0; i < temporary.length; i+=2){
  44.                 if (monthValues.containsKey(temporary[i])){
  45.                     int oldValue = monthValues.get(temporary[i]);
  46.                     monthValues.put(temporary[i], oldValue+Integer.parseInt(temporary[i+1]));
  47.                 }
  48.                 else{
  49.                     monthValues.put(temporary[i], Integer.parseInt(temporary[i+1]));
  50.                 }
  51.             }
  52.             System.out.printf("%s: ",month);
  53.             int count = 0;
  54.             int size = monthValues.size();
  55.             for(Map.Entry<String,Integer> entra : monthValues.entrySet()) {
  56.                 count++;
  57.                 String key = entra.getKey();
  58.                 Integer value = entra.getValue();
  59.                 if (count == size){
  60.                     System.out.printf("%s(%d)",key,value);
  61.                 }
  62.                 else{
  63.                     System.out.printf("%s(%d), ",key,value);
  64.                 }
  65.             }
  66.             System.out.println();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment