Advertisement
dzocesrce

[NP] Event Calendar

Apr 23rd, 2025
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.stream.Collectors;
  3. import java.time.LocalDateTime;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.Scanner;
  6. import java.util.Comparator;
  7. import java.util.*;
  8. public class EventCalendarTest {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         int n = scanner.nextInt();
  12.         scanner.nextLine();
  13.         int year = scanner.nextInt();
  14.         scanner.nextLine();
  15.         EventCalendar eventCalendar = new EventCalendar(year);
  16.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
  17.  
  18.         for (int i = 0; i < n; ++i) {
  19.             String line = scanner.nextLine();
  20.             String[] parts = line.split(";");
  21.             String name = parts[0];
  22.             String location = parts[1];
  23.             LocalDateTime date = LocalDateTime.parse(parts[2], formatter);
  24.             try {
  25.                 eventCalendar.addEvent(name, location, date);
  26.             } catch (WrongDateException e) {
  27.                 System.out.println(e.getMessage());
  28.             }
  29.         }
  30.  
  31.         LocalDateTime date = LocalDateTime.parse(scanner.nextLine(), formatter);
  32.         eventCalendar.listEvents(date);
  33.         eventCalendar.listByMonth();
  34.     }
  35. }
  36.  
  37. class WrongDateException extends Exception{
  38.     public WrongDateException(LocalDateTime date) {
  39.         super(String.format("Wrong date: %s",date));
  40.     }
  41. }
  42.  
  43.  
  44. class EventCalendar {
  45.     List<Event> events;
  46.     int year;
  47.  
  48.     public EventCalendar(int year) {
  49.         events = new ArrayList<>();
  50.         this.year=year;
  51.     }
  52.  
  53.     public void addEvent(String name, String location, LocalDateTime date) throws WrongDateException {
  54.         if(date.getYear()!=year)
  55.             throw new WrongDateException(date);
  56.         events.add(new Event(name,location,date));
  57.     }
  58.  
  59.     public void listEvents(LocalDateTime date) {
  60.         events.stream().sorted().filter(i->i.getDate().getDayOfYear()==date.getDayOfYear())
  61.                 .forEach(System.out::println);
  62.     }
  63.  
  64.     public void listByMonth() {
  65.         Map<Integer,Long> eventsByMonth = events.stream().collect(Collectors.groupingBy(
  66.                 Event::getMonth,
  67.                 TreeMap::new,
  68.                 Collectors.counting()
  69.         ));
  70.         for(int i=1;i<=12;i++){
  71.             eventsByMonth.putIfAbsent(i,0L);
  72.         }
  73.  
  74.         for(Integer i : eventsByMonth.keySet()){
  75.             System.out.println(String.format("%d : %d",i,eventsByMonth.get(i)));
  76.         }
  77.     }
  78. }
  79.  
  80.  
  81. class Event implements Comparable<Event>{
  82.     String name;
  83.     String location;
  84.     LocalDateTime date;
  85.  
  86.     public Event(String name, String location, LocalDateTime date) {
  87.         this.name = name;
  88.         this.location = location;
  89.         this.date = date;
  90.     }
  91.  
  92.     public String getName() {
  93.         return name;
  94.     }
  95.  
  96.     public String getLocation() {
  97.         return location;
  98.     }
  99.  
  100.     public LocalDateTime getDate() {
  101.         return date;
  102.     }
  103.  
  104.     public int getMonth(){
  105.         return getDate().getMonth().getValue();
  106.     }
  107.     public String getThreeChars(String month){
  108.         return month.substring(0,1)+month.substring(1,3).toLowerCase();
  109.     }
  110.  
  111.     @Override
  112.     public String toString() {
  113.         return String.format("%d %s, %d %02d:%02d at %s, %s",getDate().getDayOfMonth(),getThreeChars(getDate().getMonth().toString()),
  114.                 getDate().getYear(),getDate().getHour(),getDate().getMinute(),getLocation(),getName());
  115.     }
  116.  
  117.     @Override
  118.     public int compareTo(Event o) {
  119.         return Comparator.comparing(Event::getDate)
  120.                 .thenComparing(Event::getName)
  121.                 .compare(this,o);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement