Sample Meeting Room Scheduler Design
Meeting Object
public class Meeting
{
public long startTime;
public long endTime;
public Meeting(long sTime, long eTime)
{
startTime = sTime;
endTime = eTime;
}
}
Room Object
public class Room
{
public List meetings;
public Room()
{
meetings = new ArrayList();
}
public boolean tryScheduleMeeting(Meeting meeting)
{
for (Meeting scheduledMeeting : meetings) {
if ((meeting.startTime < scheduledMeeting.endTime) && (meeting.endTime > scheduledMeeting.startTime))
return false;
}
meetings.add(meeting);
return true;
}
}
Meeting Scheduler
public class Scheduler
{
List rooms;
public Scheduler()
{
rooms = new ArrayList();
}
public List scheduleMeetings(List meetings)
{
for (Meeting meeting : meetings)
{
boolean IsMeetingScheduled = false;
for (Room room : rooms)
{
if (room.tryScheduleMeeting(meeting))
{
IsMeetingScheduled = true;
break;
}
}
if(!IsMeetingScheduled)
{
Room newRoom = new Room();
newRoom.tryScheduleMeeting(meeting);
rooms.add(newRoom);
}
}
return rooms;
}
public static void main(String[] args)
{
List meetings = new ArrayList();
Meeting m1 = new Meeting(8, 10);
Meeting m2 = new Meeting(9, 10);
Meeting m3 = new Meeting(10, 11);
Meeting m4 = new Meeting(8, 12);
meetings.add(m1);
meetings.add(m2);
meetings.add(m3);
meetings.add(m4);
Scheduler s = new Scheduler();
List rooms = s.scheduleMeetings(meetings);
System.out.println("Number of Rooms: "+ rooms.size());
int i = 1;
for (Room room : rooms)
{
System.out.println("Room: "+ i++);
int j = 1;
for(Meeting m : meetings)
{
System.out.println("Meeting: "+ j++);
System.out.println("\tStart: "+ m.startTime);
System.out.println("\t End: "+ m.endTime);
}
System.out.println();
}
}
}
Meeting Object
public class Meeting
{
public long startTime;
public long endTime;
public Meeting(long sTime, long eTime)
{
startTime = sTime;
endTime = eTime;
}
}
Room Object
public class Room
{
public List
public Room()
{
meetings = new ArrayList
}
public boolean tryScheduleMeeting(Meeting meeting)
{
for (Meeting scheduledMeeting : meetings) {
if ((meeting.startTime < scheduledMeeting.endTime) && (meeting.endTime > scheduledMeeting.startTime))
return false;
}
meetings.add(meeting);
return true;
}
}
Meeting Scheduler
public class Scheduler
{
List
public Scheduler()
{
rooms = new ArrayList
}
public List
{
for (Meeting meeting : meetings)
{
boolean IsMeetingScheduled = false;
for (Room room : rooms)
{
if (room.tryScheduleMeeting(meeting))
{
IsMeetingScheduled = true;
break;
}
}
if(!IsMeetingScheduled)
{
Room newRoom = new Room();
newRoom.tryScheduleMeeting(meeting);
rooms.add(newRoom);
}
}
return rooms;
}
public static void main(String[] args)
{
List
Meeting m1 = new Meeting(8, 10);
Meeting m2 = new Meeting(9, 10);
Meeting m3 = new Meeting(10, 11);
Meeting m4 = new Meeting(8, 12);
meetings.add(m1);
meetings.add(m2);
meetings.add(m3);
meetings.add(m4);
Scheduler s = new Scheduler();
List
System.out.println("Number of Rooms: "+ rooms.size());
int i = 1;
for (Room room : rooms)
{
System.out.println("Room: "+ i++);
int j = 1;
for(Meeting m : meetings)
{
System.out.println("Meeting: "+ j++);
System.out.println("\tStart: "+ m.startTime);
System.out.println("\t End: "+ m.endTime);
}
System.out.println();
}
}
}