Untitled

mail@pastecode.io avatar
unknown
java
a month ago
1.9 kB
0
Indexable
Never
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

class Event {
    LocalDateTime startTime;
    LocalDateTime endTime;
    String name;

    public Event(LocalDateTime startTime, LocalDateTime endTime, String name) {
        this.startTime = startTime;
        this.endTime = endTime;
        this.name = name;
    }
}

public class EventSchedule {
    private TreeMap<LocalDateTime, Event> schedule;

    public EventSchedule(List<Event> events) {
        schedule = new TreeMap<>();
        for (Event event : events) {
            schedule.put(event.startTime, event);
        }
    }

    public String getEventAt(LocalDateTime time) {
        Map.Entry<LocalDateTime, Event> entry = schedule.floorEntry(time);
        if (entry != null && entry.getValue().endTime.isAfter(time)) {
            return entry.getValue().name;
        }
        return "No event";
    }

    public static void main(String[] args) {
        List<Event> events = new ArrayList<>();
        events.add(new Event(LocalDateTime.of(2024, 8, 6, 9, 0), LocalDateTime.of(2024, 8, 6, 10, 0), "Meeting"));
        events.add(new Event(LocalDateTime.of(2024, 8, 6, 10, 30), LocalDateTime.of(2024, 8, 6, 11, 30), "Lunch"));
        events.add(new Event(LocalDateTime.of(2024, 8, 6, 13, 0), LocalDateTime.of(2024, 8, 6, 15, 0), "Workshop"));

        EventSchedule schedule = new EventSchedule(events);

        List<LocalDateTime> timesToCheck = new ArrayList<>();
        timesToCheck.add(LocalDateTime.of(2024, 8, 6, 9, 30));
        timesToCheck.add(LocalDateTime.of(2024, 8, 6, 10, 15));
        timesToCheck.add(LocalDateTime.of(2024, 8, 6, 11, 0));
        timesToCheck.add(LocalDateTime.of(2024, 8, 6, 14, 0));

        for (LocalDateTime time : timesToCheck) {
            System.out.println("Event at " + time + ": " + schedule.getEventAt(time));
        }
    }
}
Leave a Comment