Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.ParseException;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class ActivityTracker {
- public static void main(String[] args) throws ParseException {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- input.nextLine();
- ArrayList<String[]> inputInfos = new ArrayList<>();
- for (int i = 0; i < n; i++){
- inputInfos.add(input.nextLine().split("[\\s]+"));
- }
- Map<String,String> tracker = new TreeMap<>();
- for (int i = 0; i < inputInfos.size(); i++){
- String[] tempInfo = inputInfos.get(i);
- String dateString = tempInfo[0];
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
- LocalDate date = LocalDate.parse(dateString, formatter);
- String month = Integer.toString(date.getMonthValue());
- String name = tempInfo[1];
- String distance = tempInfo[2];
- if (tracker.containsKey(month)){
- String old = tracker.get(month);
- tracker.put(month, old+name+" "+distance+", ");
- }
- else {
- tracker.put(month, name+" "+distance+", ");
- }
- }
- for(Map.Entry<String,String> entry : tracker.entrySet()) {
- String month = entry.getKey();
- String namesDistances = entry.getValue();
- String[] temporary = namesDistances.split("[\\s\\', ']+");
- Map<String,Integer> monthValues = new TreeMap<>();
- for (int i = 0; i < temporary.length; i+=2){
- if (monthValues.containsKey(temporary[i])){
- int oldValue = monthValues.get(temporary[i]);
- monthValues.put(temporary[i], oldValue+Integer.parseInt(temporary[i+1]));
- }
- else{
- monthValues.put(temporary[i], Integer.parseInt(temporary[i+1]));
- }
- }
- System.out.printf("%s: ",month);
- int count = 0;
- int size = monthValues.size();
- for(Map.Entry<String,Integer> entra : monthValues.entrySet()) {
- count++;
- String key = entra.getKey();
- Integer value = entra.getValue();
- if (count == size){
- System.out.printf("%s(%d)",key,value);
- }
- else{
- System.out.printf("%s(%d), ",key,value);
- }
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment