ROOM CLIENT JAVA import [Link].*; import [Link].*; import [Link].
*; class RoomBookingClient { /** * This is the Client Class. It takes an input from the user, calls the methods available * to the client from the server class and gives an ouput depending on the operation performed. */ public static boolean validChoice = true; static String [] daysOfWeek = { "Monday |", "Tuesday |", "Wednesday|", "Thursday |" , "Friday |" , "Saturday |" , "Sunday |" }; public static void main (String[] args) { try { [Link] ( new RMISecurityManager ( )); //set up the security manager String name = "rmi://localhost:9999/RoomBookingSystem"; //connect on local host on port 9999 RoomBookingInterface rbi =(RoomBookingInterface) [Link] (name); [Link](); //set up the room booking system
while( validChoice != false ) { //A small command line interface for the user to use the system. [Link](" "); [Link]("*********************Room Booking Service********************"); [Link](""); [Link](" Please select a service"); [Link](""); [Link]("1. List of all rooms."); [Link]("2. Check availability of a room."); [Link]("3. Book a room."); [Link]("4. Display weekly timetable for a room."); [Link](""); //A buffered reader to allow input from the command line from the user. BufferedReader input = new BufferedReader(new InputStreamReader([Link])); [Link](""); [Link]("Select a number between 1 and 4, 0 to exit"); [Link](""); [Link](); String response = [Link]();
int i = [Link](response); RoomList ListOfAllRooms = new RoomList(); stores rooms available. try{ switch (i) { case 0: [Link]("Goodbye"); application. validChoice =false; break;
//RoomList Object which //a list of all the
//User has quit the
case 1: [Link](""); [Link]("The full list of rooms is as follows"); [Link](""); [Link]("Room|Capacity"); [Link]("----|--------"); ListOfAllRooms = [Link](); //Run the allRooms method which //returns the list of all rooms. for(int c = 0; c < 100; c++) //Print the list. { if ([Link][c] ==null) { break; } [Link]([Link][c]); } [Link](""); break; case 2: [Link](""); [Link]("Check a room"); [Link]("Enter the room name"); String check_room = [Link](); [Link]("Enter the day - "); [Link]("0=Mon , 1=Tues, 3=Wed ,4=Thurs , 5=Fri, 6=Sat, 7=Sun"); String check_day = [Link](); int real_day = [Link](check_day); [Link]("Enter the start time - "); [Link]("0=8am , 1=9am , 2=10am , 3=11am , 4=12pm , 5=1pm , 6=2pm , 7=3pm , 8=4pm , 9=5pm , 10=6pm , 11= 7pm"); String check_time = [Link](); int real_time = [Link](check_time);
//This checks whether a room is available given the room name, day and time. String temp = [Link](check_room,real_day,real_time); [Link](temp); [Link](""); break; case 3: [Link]("Room Booking Service - Rooms can be booked from 8am to 8pm"); [Link](""); [Link]("Time slots go from 0 for 8am up to 11 for 7pm - Enter a value in this range"); [Link](""); [Link]("Enter the room name"); String book_room = [Link](); [Link](""); [Link]("Enter the day -"); [Link]("0=Mon , 1=Tues, 3=Wed ,4=Thurs , 5=Fri, 6=Sat, 7=Sun"); String book_day = [Link](); int real_day2 = [Link](book_day); [Link](""); [Link]("Enter the start time -"); [Link]("0=8am , 1=9am , 2=10am , 3=11am , 4=12pm , 5=1pm , 6=2pm , 7=3pm , 8=4pm , 9=5pm , 10=6pm , 11= 7pm"); String book_time = [Link](); int realb_time = [Link](book_time); //This checks whether a room is available, if it is it then reserves the room. String resp = [Link](book_room,real_day2,realb_time); [Link](resp); [Link](""); break; case 4: [Link]("Enter the Room name"); String Room1 = new String(); Room1 = [Link](); //This checks the timetable for a room. A 2D array containing //the timetable is returned from the server. [Link]("TimeSlot | 0 1 2 3 4 5 6 7 8 9 10 11"); int rtt [][]=(int[][])[Link](Room1).clone(); for(int f=0;f<7;f++) { [Link](""); [Link](daysOfWeek[f]); for(int j=0;j<12;j++)
{ [Link](" "); [Link](rtt[f][j]); } } [Link](""); [Link](" "); [Link]("The key to start times is as follows... "); [Link]("0 = 8am , 1 = 9am , 2 = 10am , 3 = 11am , 4 = 12pm , 5 = 1pm , 6 = 2pm , 7 = 3pm , 8 = 4pm , 9 = 5pm , 10 = 6pm , 11 = 7pm"); [Link](""); break; } } catch(Exception e) { [Link]("Sorry but you have entered one of the fields incorrectly, Please try again "); } } } catch (Exception ex) { [Link] (ex); } } }
Room booking server
import [Link].*; import [Link].*; import [Link].*; class RoomBookingServer extends UnicastRemoteObject implements RoomBookingInterface { /** * This is the Server Class. It contains the working methods which can be used by the client. */ protected protected protected protected public for list public public Objects int day; int time; int room; String str = new String(); //Temporary store //Array of Room
String RoomListTemp [] = new String [100]; of rooms String temp = new String(); Room RoomArray[] = new Room[100];
RoomList tempList = new RoomList(); public RoomBookingServer ( ) throws RemoteException { super ( ); } /** * This method is called once by the client when the application starts. It reads * in the input from the text file and creates an Object for each room with the * name and capacity that was specified in the file. */ public void initRooms() throws RemoteException { String record = null; String tempRoom = null; String tempCap = null; int recCount = 0; int num; int capacity; try { //This reads in the text from the file and uses that to create the //Room Objects. The name is specified first in the text file and the //capacity is specified last. This is manipulated in order to take in
//these parameters when creating the Rooms. BufferedReader b = new BufferedReader(new FileReader("[Link]")); while((record = [Link]()) != null) { num = ([Link] (" ", [Link] ())) + 1; tempRoom = [Link] (0,num -1); //Reads in the Room name from file tempCap = [Link] (num,[Link] ()); capacity = [Link](tempCap); the capacity from file RoomArray[recCount] = new Room(tempRoom, capacity); array with the created Objects. recCount ++; } [Link](); //close the input stream. } catch (IOException e) { [Link]("Error!" +[Link]()); } } /** * This method is used to return the list of rooms and there capacity to the client. * It returns a RoomList Object which contains the arrayList of Rooms. The Client * can then retrieve a full list of rooms. */ public RoomList allRooms() throws RemoteException { try { BufferedReader in = new BufferedReader(new FileReader("[Link]")); //read in the text file. if((str = [Link]()) != null) { [Link][0] = str; for (int i = 1; i< 100; i++) { if((str = [Link]()) != null) { [Link][i] = str; } } } [Link](); } catch (IOException e) { } return tempList; } //Reads in //Fills the
/** * This method takes in a string and then compares that string with the name of each Object * in the array of Rooms. If it finds the room it returns the index, -1 otherwise. */ public int compareRoom(String str) { for(int i=0; i< [Link]; i++) { if(RoomArray[i].[Link] (str)) { return i; } } return -1; } /** * This method is used to check whether a room is available or not. Firstly it checks * for the room in the array, if it finds it it then checks whether the requested * time slot on the requested day is available. It returns a string to the client * depending on the value of the timeslot. */ public String checkRoom(String r ,int RemoteException { int i = compareRoom(r); if (RoomArray[i].slotAvailable(day, available to Room Object { String s = "Room is available for return s; } else { String s = "Sorry the room is not return s; } } day , int startTime) throws
startTime) == true) //calls methos booking";
available for booking";
/** * This method is used to book a Room. Again it checks whether the slot is available and depending * on the result it reserves that slot and informs the client or it informs them that * the slot has already been reserved. */
public String bookRoom(String r, int day , int startTime) throws RemoteException { int i = compareRoom(r); if (RoomArray[i].slotAvailable(day, startTime) == true) { RoomArray[i].book(day,startTime); String s = "Room has been successfully booked."; return s; } else { String s = "Sorry but the Room has already been booked."; return s; } } /** * This method is used to calculate the timetable for each room. It returns relevant * the 2D array to the client displaying the weekly timetable for the requested room. */ public int [][] roomTimeTable(String room) throws RemoteException { int i; [Link]("TimeTable" + room); for ( i = 0; i< [Link]; i++) { if(RoomArray[i].[Link](room)) { return RoomArray[i].daySlot; } else { [Link]("Searching for the room"); } } return RoomArray[i].daySlot; } //Main Method public static void main (String[] args) { try { RoomBookingServer server = new RoomBookingServer (); String name = "rmi://localhost:9999/RoomBookingSystem"; [Link] (name, server); [Link] (name + " is running"); } catch (Exception ex) { [Link] (ex);
} } }
Interface
import [Link].*; import [Link].*; /** * This is the interface, it contains the 5 methods which the Client can use. */ public interface RoomBookingInterface extends Remote { public void initRooms() throws RemoteException; public RoomList allRooms () throws RemoteException; public String checkRoom (String r, int day , int startTime) throws RemoteException; public String bookRoom (String r, int day , int startTime) throws RemoteException; public int[][] roomTimeTable (String room) throws RemoteException; }
Room
import import import import [Link].*; [Link].*; [Link].*; [Link];
class Room implements Serializable { /* * This is the Room class. Each Room Object has a name and a capacity. It also * contains a 7 * 12 array which represents the 7 days of the week and the * 12 hours between 8 am and 8pm(The valid hours for booking a room). */ int daySlot[][] = new int[7][12]; //represents days and hours String name; int capacity; public Room(String n , int cap) //constructor that sets all slots to zero - unbooked { [Link] =n; [Link] = cap; for (int i=0; i<7; i++) { for (int j=0; j<11; j++) { [Link][i][j] = 0; } } } /** * This Method is used to check whether a particular timeslot on a particular day * has already been booked. If the slot contains a 1 then it has already been booked. * If it contains a 0 then it is available. The method returns a true or false value. */ public boolean slotAvailable(int day, int slot) { if (daySlot[day][slot] == 1) { return false; } else { return true; } }
/** * This Method is used to book a slot. It sets the relevant slot to a 1. */ public void book(int day , int slot) { [Link][day][slot] = 1; } }
Room list
import import import import [Link].*; [Link].*; [Link].*; [Link];
class RoomList implements Serializable { public String RoomList [] = new String [100]; //contains an array which holds the maximun number of rooms. To allow for //more rooms just increase the size of this array. }