Problem Statement
Design a meeting scheduler system that allows users to create meetings, invite participants, and check availability before confirming a meeting. The system should prevent overlapping meetings for any participant and should maintain a schedule for each user.
Functional Requirements
- Users should be able to schedule meetings with a start and end time
- System should prevent overlapping meetings for any participant
- Users can invite multiple participants to a meeting
- Each user should maintain their own meeting calendar
- System should allow cancellation of meetings
- Cancelled meetings should free up participant schedules
Objects Required
- User
- Meeting
- Calendar
- TimeSlot
- MeetingSchedulerService
TimeSlot Class
public class TimeSlot {
private String startTime;
private String endTime;
public TimeSlot(String startTime, String endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public boolean overlaps(TimeSlot other) {
return !(this.endTime.compareTo(other.startTime) <= 0 ||
this.startTime.compareTo(other.endTime) >= 0);
}
}
The constructor initializes a time slot with a start and end time, which represents a scheduled interval for a meeting.
The overlaps() method checks whether two time intervals conflict. It ensures that no two meetings can occupy the same time window for a user, which is the core rule of the scheduler.
Meeting Class
import java.util.*;
public class Meeting {
private String meetingId;
private TimeSlot timeSlot;
private List participants;
public Meeting(String meetingId, TimeSlot timeSlot, List participants) {
this.meetingId = meetingId;
this.timeSlot = timeSlot;
this.participants = participants;
}
public TimeSlot getTimeSlot() {
return timeSlot;
}
public List getParticipants() {
return participants;
}
}
The constructor creates a meeting with a time slot and a list of participants.
The getTimeSlot() method exposes the scheduled time so it can be validated against user calendars.
The getParticipants() method returns all invited users so their calendars can be updated consistently.
Calendar Class
import java.util.*;
public class Calendar {
private List meetings = new ArrayList<>();
public boolean canSchedule(TimeSlot slot) {
for (Meeting meeting : meetings) {
if (meeting.getTimeSlot().overlaps(slot)) {
return false;
}
}
return true;
}
public void addMeeting(Meeting meeting) {
meetings.add(meeting);
}
public void removeMeeting(Meeting meeting) {
meetings.remove(meeting);
}
}
The constructor initializes an empty calendar for each user.
The canSchedule() method ensures that a new meeting does not conflict with existing meetings. This is the core validation step that prevents double-booking.
The addMeeting() method adds a confirmed meeting to the user’s calendar.
The removeMeeting() method removes a meeting when it is cancelled, freeing up the time slot.
User Class
public class User {
private String userId;
private String name;
private Calendar calendar;
public User(String userId, String name) {
this.userId = userId;
this.name = name;
this.calendar = new Calendar();
}
public Calendar getCalendar() {
return calendar;
}
}
The constructor initializes a user along with their personal calendar, which stores all their scheduled meetings.
The getCalendar() method allows the scheduler service to check availability and update meetings for that user.
MeetingSchedulerService Class
import java.util.*;
public class MeetingSchedulerService {
public Meeting scheduleMeeting(String meetingId,
TimeSlot slot,
List users) {
for (User user : users) {
if (!user.getCalendar().canSchedule(slot)) {
System.out.println("User not available: " + user);
return null;
}
}
Meeting meeting = new Meeting(meetingId, slot, users);
for (User user : users) {
user.getCalendar().addMeeting(meeting);
}
return meeting;
}
public void cancelMeeting(Meeting meeting) {
for (User user : meeting.getParticipants()) {
user.getCalendar().removeMeeting(meeting);
}
System.out.println("Meeting cancelled successfully");
}
}
The constructor is implicit since this service does not maintain state and only coordinates scheduling logic.
The scheduleMeeting() method first validates availability for all participants. If even one user is busy, the meeting is rejected to maintain consistency across calendars. If all users are free, the meeting is created and added to every participant’s calendar.
The cancelMeeting() method removes the meeting from all participant calendars, ensuring the time slot becomes available again.
Main Class
import java.util.*;
public class Main {
public static void main(String[] args) {
User u1 = new User("U1", "Alice");
User u2 = new User("U2", "Bob");
TimeSlot slot = new TimeSlot("10:00", "11:00");
MeetingSchedulerService service = new MeetingSchedulerService();
Meeting meeting = service.scheduleMeeting(
"M1",
slot,
Arrays.asList(u1, u2)
);
if (meeting != null) {
System.out.println("Meeting scheduled successfully");
}
service.cancelMeeting(meeting);
}
}
The main method demonstrates a full scheduling flow starting from user creation to meeting scheduling and cancellation.
It shows how the system ensures no overlapping meetings while keeping all participant calendars in sync.
Comments
Post a Comment