Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.util.*;
- import java.util.stream.IntStream;
- class WrongDateException extends Exception {
- public WrongDateException(String message) {
- super(message);
- }
- }
- public class EventCalendarTest {
- public static void main(String[] args) throws ParseException {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- scanner.nextLine();
- int year = scanner.nextInt();
- scanner.nextLine();
- EventCalendar eventCalendar = new EventCalendar(year);
- DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
- for (int i = 0; i < n; ++i) {
- String line = scanner.nextLine();
- String[] parts = line.split(";");
- String name = parts[0];
- String location = parts[1];
- Date date = df.parse(parts[2]);
- try {
- eventCalendar.addEvent(name, location, date);
- } catch (WrongDateException e) {
- System.out.println(e.getMessage());
- }
- }
- Date date = df.parse(scanner.nextLine());
- eventCalendar.listEvents(date);
- eventCalendar.listByMonth();
- }
- }
- // vashiot kod ovde
- class EventCalendar {
- class Event implements Comparable<Event> {
- String name;
- String location;
- Date date;
- public Event(String name, String location, Date date) {
- this.name = name;
- this.location = location;
- this.date = date;
- }
- public Date getDate() {
- return date;
- }
- @Override
- public int compareTo(Event event) {
- Comparator<Event> date = Comparator.comparing(Event::getDate);
- Comparator<Event> name = date.thenComparing(Event::getName);
- return name.compare(this, event);
- }
- public String getName() {
- return name;
- }
- @Override
- public String toString() {
- SimpleDateFormat df = new SimpleDateFormat("dd MMM, YYY HH:mm");
- String d = df.format(date);
- return String.format("%s at %s, %s", d, location, name);
- }
- }
- int year;
- HashMap<Integer, TreeSet<Event>> events;
- int[] eventsInMonth;
- public EventCalendar(int year) {
- this.year = year;
- events = new HashMap<>();
- eventsInMonth = new int[12];
- }
- public void addEvent(String name, String location, Date date) throws WrongDateException {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- if (cal.get(Calendar.YEAR) != year)
- throw new WrongDateException("Wrong date: " + date.toString()
- .replaceAll("GMT", "UTC"));
- eventsInMonth[cal.get(Calendar.MONTH)]++;
- events.putIfAbsent(cal.get(Calendar.DAY_OF_YEAR), new TreeSet<>());
- events.get(cal.get(Calendar.DAY_OF_YEAR)).add(new Event(name, location, date));
- }
- public void listEvents(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- if (events.containsKey(cal.get(Calendar.DAY_OF_YEAR)))
- events.get(cal.get(Calendar.DAY_OF_YEAR))
- .forEach(System.out::println);
- else
- System.out.println("No events on this day!");
- }
- public void listByMonth() {
- for (int i = 0; i < 12; i++) {
- System.out.println(i + 1 + " : " + eventsInMonth[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement