Java assignment: Classroom Scheduler Description Design a room reservation syste
ID: 3575972 • Letter: J
Question
Java assignment:
Classroom Scheduler
Description
Design a room reservation system (RRS) that will be used for scheduling a set of classes taught by a set of instructors in a set of rooms. Four of the rooms will be used for holding classes. Classes will be held in these rooms from 8 am to 10 pm Monday through Saturday. A room can be reserved for any half hour time slot during those times.
RRS will display to the user each room as a grid called the Room Grid with days along the columns and time along the rows. Each room grid can be implemented as a set of JPanels and all the room grids can be included within a single JFrame. The top row of a room grid will be used for column headings (such as M Tu W Th F S), the bottom row for room name (such as ATC-109, ATC-113 etc.) and the left-most column for row headings (such as 8:00, 8:30, 9:00, all in half hour intervals). The RRS will display to the user the four room grids with their room numbers showing underneath in a single row one after the other as shown in the picture below.
Above the room grids, the RRS will display an uncheck check box, a group of two radio buttons, several drop-down combo boxes and a text field with their headings on the top. The check box will be used to activate the Display Matches functionality described later, the group of two radio buttons will be used for selecting one of the two modes of interaction with the RRS, Reserve mode or Un-reserve mode to reserve or un-reserve a half-hour time slot in a room grid. The text field, labeled Status, will be used for displaying the status of the ongoing user action. Each of the combo boxes will be used for displaying a list of choices to select from. One combo box will display courses or course abbreviations (such as 110,165 or 210), another course sections (such as 1, 2, or 3), another class types (such as lec, lab, hybrid), another professors’ names or initials (such as cp, cm, or rb) and another colors (such as red, blue, or green) for highlighting the selected time slot in the room grid.
At a time, the user can reserve or un-reserve only a one half-hour time slot in a room grid. For making a reservation, the user will select the Reserve radio button and select from the combo boxes the course, the section, the class type, the class professor and the display color and then click the desired half hour time slot in the desired room grid, the time slot will be reserved and will be highlighted in the selected color. If the time slot clicked is already taken, the program will display a message in the Status text field “Already Taken” and the reservation attempt would fail. For un-reserving a room, the user will select the Un-reserve radio button and click on the target half-hour time slot, the time slot will be un-reserved, it will be un-highlighted and become available for a future reservation. In the above scheme, the user can reserve or un-reserve only a one half-hour time slot at a time. For reserving or un-reserving a room for a two hour class, the user will have to click on the four half-hour time slots one after the other.
Below the room grids, there will be two buttons, Store and Restore, and a text field for specifying a file name. The user may save the contents of the room grids any time by entering the file name and pressing the Store button and the content of the room grids will be saved in the file. Similarly, the user may restore the contents of the room grids from a file by entering the name of the file and pressing the Restore button. Below the room grids, there will also be an Output button, a text field for specifying the output file name and a group of two radio buttons for selecting the type of output. For outputting the contents of room grids to a text file in a readable format, the user will select the type of output (output sorted by professors or by classes), enter the output file name and press the Output button and the contents of the room grids will be outputted to the file. Then the user will be able to open the file in a text editor and examine the contents.
RRS will also provide the Display Matches functionality. To activate this functionality, the user will check the Display Matches check box. From then onwards, when the user clicks on a time slot, RRS will display in a dialog box or console window the list of all the time slots which has the same value for the course and the section as the time slot clicked. The display will include the course, section, type and professor for each matching time slot. This way the user will be able to check the reservation for any section of a course.
RRS would read the names of the rooms and the contents of combo boxes from a configuration file named config.txt. For storing/restoring the contents of room grids, RSS will use serialization/de-serialization.
Implement the Checking and Output functionality the last.
Sample Design 1
In this sample design, there are three classes namely Scheduler, Room and Timeslot which are described below;
Scheduler class is an extended JFrame. The program creates a single Scheduler object. This object is responsible for creating most of the GUI and handling configuration. It creates four Room objects and adds them to one of its panels for displaying it to the user. It also passes each Room object the name of the room that it will manage. Additionally, it passes each Room object its own reference to allow it access to the user’s GUI selections. It also provides buttons such as Store and Restore for initiating user requests and invokes methods in Room objects for completing those requests.
Room class is an extended JPanel that manages a room grid consisting of 28 by 6 Timeslot object. Each Room object creates 168 (28*6) Timeslot objects and populates its room grid. It registers itself as the MouseListener with each of them and handles their mouse clicks. During handling a click, it acquires the required information (the class, section, type, professor, display color) from GUI selections in the Scheduler object and stores it in the Timeslot object as its state at the time of the click. It is also responsible for storing the states of Timeslot objects to a file and restoring it from the file any time at user’s request.
Timeslot class is an extend JPanel which manages a single one half hour time slot. A Timeslot object maintains the state data relating to the timeslot. The state data indicates whether the time slot is occupied or not and if occupied the occupying class, section, type, professor and display color. It also over-rides the paintComponent method in which it paints the time slot in the display color when occupied and default background color when not occupied. It also displays in the time slot the class, section, type and professor occupying the time slot.
Problem with Serializing/Deserializing JPanels or JButtons & Workaround
A JPanel (or JButton) object do not serialize/deserialize correctly after you have called its registration (addMouseListener, addActionListener etc.) method to listen for events. In the sample design, we call the addMouseListener of each Timeslot (extended JPanel) object to listen for mouse clicks. So they do not correctly serialize/deserialize.
The code below is a way around this problem. Note that in store method below, we first deregister (removeMouseListener) from Timeslot object, then we serialize it (store its serialized copy) and then re-register (addMouseListener) with it to put it in its original state. Similarly in restore method below, first we deserialize it i.e. create a new copy of the object based on stored serialized copy. Then we replace the current object with the new copy. Then we register (addMouseListener) with the new copy because it has the registration information missing,
//Sample Serialization Code
ObjectOuputStream out = new ObjectOutputStream
(new FileOutputStream("myFile"));
public synchronized void store (ObjectOutputStream out) throws Exception {
for (int i=0;i<6*28;i++){
//deregister
timeslot[i].removeMouseListener(this);
//serialize
out.writeObject(timeslot[i]);
out.flush();
//register
timeslot[i].addMouseListener(this);
}
}
// Sample Deserialization Code
ObjectInputStream in = new ObjectInputStream (
new FileInputStream("abc"));
public synchronized void restore (ObjectInputStream in) throws Exception {
int i=0;
try{
for (i=0;i<6*28;i++){
//deserialize
timeslot[i] = (Tmeslot) in.readObject();
//replace the old Timeslot panel with the new Timeslot panel
jpc.remove(i);
jpc.add(timeslot[i],i);
//register listener
timeslot[i].addMouseListener(this);
//display
timeslot[i].repaint();
}
}catch (Exception e){
e.printStackTrace();
System.out.println(""+e.getMessage());
}
}
Configuration
Populating a Combo Box from Configuration File
//The code below populates a combo box with a list of courses in the configuration file config.txt.
BufferedReader config = new BufferedReader (new FileReader ("config.txt"));
String line="";
JComboBox cboCourse = new JComboBox();
while ((line=config.readLine())!=null){
if (line.equalsIgnoreCase("courses"))
break;
}
while (!((line=config.readLine()).equals("///"))){
cboCourse.addItem(line.trim());
}
Contents of the configuration file (config.txt)
COURSES
110
165
200
210
255
256
257
260
275
276
277
///
SECTIONS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
///
TYPES
lec
lab
hybrid
online
///
PROFS
cp
cm
gk
hc
rb
///
COLORS
red
blue
green
yellow
cyan
magenta
///
ROOMS
ATC-109
ATC-113
ATC-115
LA-142
///
Picture:
IIIIIIIIIIIIIIRRRRRRIIIIIIII IIIIIIRRRRRRIIIIIIIIIIIIIIIIExplanation / Answer
Hope this will help you out in solving above assignment. I have tried dummy scheduler task. Code is below:
Scheduler Task
For this functionality,
2. Run Scheduler Task
A class to run above scheduler task.
Thanks,
Shweta Sharma
Scheduler Task
For this functionality,
- You should create a class extending TimerTask(available in java.util package). TimerTask is a abstract class.
- Write your code in public void run() method that you want to execute periodically.
- Insert below code in your Main class.
import java.util.TimerTask;
import java.util.Date;
/**
*
* @author Dhinakaran P.
*/
// Create a class extends with TimerTask
public class ScheduledTask extends TimerTask { Date now; // to display current time
// Add your task here
public void run() { now = new Date(); // initialize date
System.out.println("Time is :" + now); // Display current time }
}
2. Run Scheduler Task
A class to run above scheduler task.
- Instantiate Timer Object Timer time = new Timer();
- Instantiate Scheduled Task class Object ScheduledTask st = new ScheduledTask();
- Assign scheduled task through Timer.shedule() method.
import java.util.Timer;
/**
*/
//Main class
public class SchedulerMain { public static void main(String args[]) throws InterruptedException { Timer time = new Timer(); // Instantiate Timer Object
ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class
time.schedule(st, 0, 1000); // Create Repetitively task for every 1 secs
//for demo only.
for (int i = 0; i <= 5; i++) { System.out.println("Execution in Main Thread...." + i); Thread.sleep(2000);
if (i == 5) { System.out.println("Application Terminates"); System.exit(0);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.