C:\Documents and Settings\Gijs\Desktop\src\client\Client.java dinsdag 3 november 2009 10:50 package client; import hotel.bookingconstraints; import hotel.bookingexception; import hotel.costumersessionremote; import hotel.hotel; import hotel.managersessionremote; import hotel.room; import hotel.ihotel; import java.rmi.remoteexception; import java.rmi.server.unicastremoteobject; import java.util.calendar; import java.util.date; public class Client { public static void main(string[] args) throws RemoteException { TravelAgency agency = new TravelAgency(); CostumerSessionRemote session = agency.getcostumersession(0); Calendar c = Calendar.getInstance(); c.set(2009, Calendar.OCTOBER, 11, 0, 0, 0); Date day1 = c.gettime(); c.set(2009, Calendar.OCTOBER, 12, 0, 0, 0); Date day2 = c.gettime(); c.set(2009, Calendar.OCTOBER, 13, 0, 0, 0); Date day3 = c.gettime(); c.set(2009, Calendar.OCTOBER, 6, 0, 0, 0); Date day4 = c.gettime(); BookingConstraints constraints1 = new BookingConstraints(day4, day3, 3, 150.0, false); BookingConstraints constraints2 = new BookingConstraints(day1, day2, 1, 150.0, false); try { session.createbooking(constraints1, "Frans Guelinckx", "Fawlty_Towers"); session.createbooking(constraints2, "Gijs Vanderhoydonk", "Fawlty_Towers"); session.finalizebookings(); session.createbooking(constraints1, "Frans Guelinckx", "Fawlty_Towers"); session.createbooking(constraints2, "Gijs Vanderhoydonk", "Fawlty_Towers"); session.finalizebookings(); session.createbooking(constraints1, "Frans Guelinckx", "Rilton"); session.createbooking(constraints2, "Gijs Vanderhoydonk", "Rilton"); session.finalizebookings(); session.createbooking(constraints1, "Frans Guelinckx", "Rilton"); session.createbooking(constraints2, "Gijs Vanderhoydonk", "Rilton"); session.finalizebookings(); // session = agency.getcostumersession(1); System.out.println(); catch (BookingException e) { System.out.println("One or more of the tentative bookings could not be finalized."); -1-
C:\Documents and Settings\Gijs\Desktop\src\client\Client.java dinsdag 3 november 2009 10:50 ManagerSessionRemote managersession = agency.getmanagersession(1); System.out.println(managerSession.getRooms("Fawlty_Towers")); System.out.println(managerSession.getHotels()); Hotel hotel = new Hotel("Den houten lepel", new Room[0]); ihotel ihotel = (ihotel) UnicastRemoteObject.exportObject(hotel, 0); managersession.registerhotel("den houten lepel", ihotel); System.out.println(managerSession.getHotels()); -2-
C:\Documents and Settings\Gijs\Desktop\src\client\TravelAgency.java dinsdag 3 november 2009 10:51 package client; import hotel.costumersession; import hotel.costumersessionremote; import hotel.managersession; import hotel.managersessionremote; import java.rmi.remote; import java.util.hashmap; public class TravelAgency implements Remote { private HashMap<Integer, CostumerSessionRemote> costumersessions = new HashMap<Integer, CostumerSessionRemote>(); private HashMap<Integer, ManagerSessionRemote> managersessions = new HashMap<Integer, ManagerSessionRemote>(); public CostumerSessionRemote getcostumersession(int id) { if (costumersessions.get(id) == null) { return this.createcostumersession(id); else { return this.costumersessions.get(id); private CostumerSessionRemote createcostumersession(int id) { CostumerSession session = new CostumerSession(); this.costumersessions.put(id, session); return session; public ManagerSessionRemote getmanagersession(int id) { if (managersessions.get(id) == null) { return this.createmanagersession(id); else { return this.managersessions.get(id); private ManagerSessionRemote createmanagersession(int id) { ManagerSession session = new ManagerSession(); this.managersessions.put(id, session); return session; -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\BookingConstraints.java dinsdag 3 november 2009 10:54 package hotel; import java.io.serializable; import java.util.date; * A class representing booking constraints. It containt all sorts of * requirements to which a real booking should adhere. public class BookingConstraints implements Serializable { private static final long serialversionuid = -5102822030107653932L; * De starting date of the tentative booking. private Date from; * The maximum cost per night a booking should have. private double maxcostpernight; * The number of beds requested. private int nbofbeds; * A boolean indicating whether smoking should be allowed or not. private boolean smoking; * The end date of the tentative booking. private Date to; * A constructor setting all the desired requirements. * * @param from * The date on which the reservation should start. * @param to * The date on which the reservation should end. * @param nbofbeds * The desired number of beds. * @param maxcost * The maximum cost per night. * @param smoking * A boolean indicating whether smoking should be allowed or not. public BookingConstraints(Date from, Date to, int nbofbeds, double maxcost, boolean smoking) { setbegin(from); setend(to); setmaxcostpernight(maxcost); setnbofbeds(nbofbeds); -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\BookingConstraints.java dinsdag 3 november 2009 10:54 setsmoking(smoking); public Date getbegin() { return from; public Date getend() { return to; public double getmaxcostpernight() { return maxcostpernight; public int getnbofbeds() { return nbofbeds; public void setbegin(date from) { this.from = from; public void setend(date to) { this.to = to; public void setmaxcostpernight(double cost) { this.maxcostpernight = cost; public void setnbofbeds(int nbofbeds) { this.nbofbeds = nbofbeds; public void setsmoking(boolean smoking) { this.smoking = smoking; public boolean smokingallowed() { return smoking; @Override public String tostring() { return "From: " + this.getbegin() + " To: " + this.getend() + " Max cost per night: " + this.getmaxcostpernight() + " Nb of beds: " + this.getnbofbeds() + " Smoking: " + this.smokingallowed(); -2-
C:\Documents and Settings\Gijs\Desktop\src\hotel\BookingDetails.java dinsdag 3 november 2009 10:54 package hotel; import java.io.serializable; import java.util.date; * A class representing an actual booking. public class BookingDetails implements Serializable { private static final long serialversionuid = -9137030824176999042L; * The name of the person who made the reservation. private String booker; * The total cost of the booking. private double cost; * The date on which the reservation starts. private Date from; * The name of the hotel in which the booking is made.. private String hotel; * The number of the booked room. private int roomnb; * The date on which the reservation ends. private Date to; * Returns true if a given instance of BookingDetails is equal to this * instance. This means that all attributes (requirements) are the same. @Override public boolean equals(object o) { if (this == o) return true; if (o instanceof BookingDetails) { BookingDetails other = (BookingDetails) o; if (other.getbegin().equals(getbegin()) && other.getend().equals(getend()) && other.getnameofbooker().equals(getnameofbooker()) && other.gethotelname().equals(gethotelname()) && (other.gettotalcost() == gettotalcost()) && (other.getroomnb() == getroomnb())) -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\BookingDetails.java dinsdag 3 november 2009 10:54 return true; return false; public Date getbegin() { return from; public Date getend() { return to; public String gethotelname() { return hotel; public String getnameofbooker() { return booker; public int getroomnb() { return roomnb; public double gettotalcost() { return cost; @Override public int hashcode() { int hash = 7; hash = 43 * hash + (this.from!= null? this.from.hashcode() : 0); hash = 43 * hash + (this.to!= null? this.to.hashcode() : 0); hash = 43 * hash + (this.booker!= null? this.booker.hashcode() : 0); hash = 43 * hash + (this.hotel!= null? this.hotel.hashcode() : 0); hash = 43 * hash + this.roomnb; hash = 43 * hash + (int) (Double.doubleToLongBits(this.cost) ^ (Double.doubleToLongBits(this.cost) >>> 32)); return hash; void setbegin(date from) { this.from = from; void setbooker(string booker) { this.booker = booker; void setend(date to) { this.to = to; void sethotelname(string hotel) { this.hotel = hotel; -2-
C:\Documents and Settings\Gijs\Desktop\src\hotel\BookingDetails.java dinsdag 3 november 2009 10:54 void setroomnb(int roomnb) { this.roomnb = roomnb; void settotalcost(double cost) { this.cost = cost; @Override public String tostring() { return "Booking for " + getnameofbooker() + " from " + getbegin() + " to " + getend() + "\n" + "Room: " + getroomnb() + "\ttotal cost: " + gettotalcost(); -3-
C:\Documents and Settings\Gijs\Desktop\src\hotel\BookingException.java dinsdag 3 november 2009 10:52 package hotel; public class BookingException extends Exception { private static final long serialversionuid = 6159168236279397523L; public BookingException(String string) { super(string); -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\CostumerSession.java dinsdag 3 november 2009 10:52 package hotel; import java.rmi.remoteexception; import java.util.arraylist; * A costumer session, used by costumers for creating, finalizing en reading the * current bookings. public class CostumerSession extends Session implements CostumerSessionRemote { * An {@link ArrayList containing all tentative bookings. ArrayList<BookingDetails> bookings = new ArrayList<BookingDetails>(); public CostumerSession() { super(); * Creates a tantative booking in the given hotel and for a given person, * which satisfies the given booking constraints. @Override public BookingDetails createbooking(bookingconstraints constraints, String bookername, String hotelname) throws BookingException, RemoteException { BookingDetails result = this.gethotelinstance(hotelname).createbooking( constraints, bookername); bookings.add(result); return result; * Finalizes the list of tentative bookings. Of one or more bookings fail, * all bookings are cancelled and removed. @Override public void finalizebookings() throws BookingException, RemoteException { try { for (BookingDetails details : this.bookings) { ihotel hotel = this.gethotelinstance(details.gethotelname()); hotel.finalizebooking(details); this.bookings.clear(); catch (RuntimeException e) { for (BookingDetails details : this.bookings) { this.gethotelinstance(details.gethotelname()).cancelbooking( details); this.bookings.clear(); throw new BookingException( "One or more of the reservations cannot be finalized."); -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\CostumerSession.java dinsdag 3 november 2009 10:52 * Returns the list of tentative bookings. @Override public ArrayList<BookingDetails> getcurrentbookings() throws RemoteException { return bookings; -2-
C:\Documents and Settings\Gijs\Desktop\src\hotel\CostumerSessionRemote.java dinsdag 3 november 2009 10:52 package hotel; import java.rmi.remote; import java.rmi.remoteexception; import java.util.arraylist; * An interface for a costumer session, extending the remote interface for * distributed use. public interface CostumerSessionRemote extends Remote { * Creates a tantative booking in the given hotel and for a given person, * which satisfies the given booking constraints. * * @param constraints * The constraints that have to be satistied. * @param bookername * The name of the person that makes the reservation. * @param hotelname * The name of the hotel in which the reservation should be made. * @return An instance of {@link BookingDetails, containing all the info of * the tentative booking. * @throws BookingException * Thrown when one or more of the constraints cannot be * satisfied. public BookingDetails createbooking(bookingconstraints constraints, String bookername, String hotelname) throws BookingException, RemoteException; * Finalizes all tentative bookings. * * @throws BookingException * Thrown when one or more of the bookings cannot be finalized. * All tentative bookings are also cancelled and deleted. public void finalizebookings() throws BookingException, RemoteException; * Returns a list of all tentative bookings. public ArrayList<BookingDetails> getcurrentbookings() throws RemoteException; -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\Hotel.java dinsdag 3 november 2009 10:52 package hotel; import java.util.arraylist; import java.util.calendar; import java.util.date; * A class representing a hotel. public class Hotel implements ihotel { * private static final long serialversionuid = -319960495458592073L; * The name of the hotel. private String name; * The rooms in this hotel. private Room[] rooms; * Constructs a hotel with a given name and rooms. public Hotel(String name, Room[] rooms) { System.out.println("<" + name + "> Hotel " + name + " starting up..."); setname(name); this.rooms = rooms; * Cancels a given booking. public void cancelbooking(bookingdetails booking) { System.out.println("<" + name + "> Cancelling booking " + booking.tostring()); for (Room r : this.rooms) { if (r.getdetails().getroomnb() == booking.getroomnb()) { r.removebooking(booking); * Creates an instance of BookingDetails that satisfies the given * constraints for the given person. If the constraints cannot be met, no * tentative reservation is made. public BookingDetails createbooking(bookingconstraints constraints, String person) throws BookingException { System.out.println("<" + name + "> Creating bookingdetails for " + person + " with constraints " + constraints.tostring()); for (RoomDetails r : this.getfreerooms(constraints.getbegin(), -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\Hotel.java dinsdag 3 november 2009 10:52 constraints.getend())) { if (constraints.getnbofbeds() <= r.getnbofbeds() && constraints.smokingallowed() == r.smokingallowed() && constraints.getmaxcostpernight() >= r.getcostpernight()) { return createbooking(r.getroomnb(), constraints.getbegin(), constraints.getend(), person); throw new BookingException("<" + name + "> No rooms left to satisfy constraints."); * Creates an actual tentative reservation. * * @param roomnb * The number of rooms needed. * @param begin * The start date for the reservation. * @param end * The end date. * @param person * The name of the person that makes the booking. * @return An instance of BookingDetails, containing all the details of the * tentative booking. * @throws BookingException * Thrown when the room is not free during the given period. private BookingDetails createbooking(int roomnb, Date begin, Date end, String person) throws BookingException { if (rooms[roomnb].isfree(begin, end)) { BookingDetails b = new BookingDetails(); b.setbooker(person); b.setbegin(begin); b.setend(end); b.setroomnb(roomnb); b.sethotelname(getname()); b.settotalcost(rooms[roomnb].getdetails().getcostpernight() * (Math.ceil(end.getTime() - begin.gettime()) / (1000 * 60 * 60 * 24))); return b; else { throw new BookingException("<" + name + "> Room is not free during that period."); * Finalizes a given tentative booking. public void finalizebooking(bookingdetails b) { System.out.println("<" + name + "> Booking " + b.tostring()); getroom(b.getroomnb()).book(b); * Returns a list of all bookings of all the rooms in this hotel. -2-
C:\Documents and Settings\Gijs\Desktop\src\hotel\Hotel.java dinsdag 3 november 2009 10:52 public ArrayList<BookingDetails> getbookings() { ArrayList<BookingDetails> result = new ArrayList<BookingDetails>(); for (Room room : rooms) { result.addall(room.getbookings()); return result; * Returns a list of rooms that are free during a given period. public ArrayList<RoomDetails> getfreerooms(date begin, Date end) { ArrayList<RoomDetails> freerooms = new ArrayList<RoomDetails>(); for (int i = 0; i < rooms.length; i++) { Room room = rooms[i]; if (room.isfree(begin, end)) { freerooms.add(room.getdetails()); return freerooms; public String getname() { return name; public Room getroom(int roomnb) { for (Room r : rooms) { if (r.getdetails().getroomnb() == roomnb) return r; throw new IllegalArgumentException("<" + name + "> No such room, " + roomnb); * Returns a list of {@link RoomDetails of all the rooms in this hotel. public ArrayList<RoomDetails> getrooms() { ArrayList<RoomDetails> result = new ArrayList<RoomDetails>(); for (Room room : rooms) { result.add(room.getdetails()); return result; * Returns a list of all the rooms that are booked today. * * @return public ArrayList<Room> getroomsbookedtoday() { ArrayList<Room> result = new ArrayList<Room>(); Date date = Calendar.getInstance().getTime(); for (Room room : rooms) { if (!room.isbooked(date)) result.add(room); -3-
C:\Documents and Settings\Gijs\Desktop\src\hotel\Hotel.java dinsdag 3 november 2009 10:52 return result; * Indicates whether a room is free during a given period. * * @param roomid * The number of the room that has to be checked. * @param begin * The beginning of the desired period. * @param end * The end of the desired period. * @return True if the room is free, false otherwise. public boolean isfree(int roomid, Date begin, Date end) { System.out.println("<" + name + "> Checking availability for room " + roomid); return this.rooms[roomid].isfree(begin, end); private void setname(string name) { this.name = name; -4-
C:\Documents and Settings\Gijs\Desktop\src\hotel\HotelServer1.java dinsdag 3 november 2009 10:53 package hotel; import java.rmi.notboundexception; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.rmi.server.unicastremoteobject; import java.util.arraylist; import naming.namingserviceremote; public class HotelServer1 { public static void main(string[] args) { System.setSecurityManager(null); ArrayList<Hotel> hotels = createhotels(); try { ihotel stub; Registry registry = LocateRegistry.getRegistry(); NamingServiceRemote service = (NamingServiceRemote) registry.lookup("namingservice"); for (Hotel hotel : hotels) { stub = (ihotel) UnicastRemoteObject.exportObject(hotel, 0); service.register(hotel.getname(), stub); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); public static ArrayList<Hotel> createhotels() { ArrayList<Hotel> result = new ArrayList<Hotel>(); Room[] rooms1 = new Room[5]; rooms1[0] = new Room(new RoomDetails(0, 2, 50.0, false)); rooms1[1] = new Room(new RoomDetails(1, 2, 50.0, false)); rooms1[2] = new Room(new RoomDetails(2, 4, 90.0, false)); rooms1[3] = new Room(new RoomDetails(3, 4, 95.0, true)); rooms1[4] = new Room(new RoomDetails(4, 5, 150.0, false)); result.add(new Hotel("Fawlty_Towers", rooms1)); return result; -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\HotelServer2.java dinsdag 3 november 2009 10:53 package hotel; import java.rmi.notboundexception; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.rmi.server.unicastremoteobject; import java.util.arraylist; import naming.namingserviceremote; public class HotelServer2 { * @param args public static void main(string[] args) { System.setSecurityManager(null); ArrayList<Hotel> hotels = createhotels(); try { ihotel stub; Registry registry = LocateRegistry.getRegistry(); NamingServiceRemote service = (NamingServiceRemote) registry.lookup("namingservice"); for (Hotel hotel : hotels) { stub = (ihotel) UnicastRemoteObject.exportObject(hotel, 0); service.register(hotel.getname(), stub); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); public static ArrayList<Hotel> createhotels() { ArrayList<Hotel> result = new ArrayList<Hotel>(); Room[] rooms2 = new Room[4]; rooms2[0] = new Room(new RoomDetails(1, 2, 90.0, false)); rooms2[1] = new Room(new RoomDetails(2, 1, 100.0, false)); rooms2[2] = new Room(new RoomDetails(3, 4, 150.0, false)); rooms2[3] = new Room(new RoomDetails(4, 3, 120.0, true)); result.add(new Hotel("Rilton", rooms2)); return result; -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\iHotel.java dinsdag 3 november 2009 10:53 package hotel; import java.rmi.remote; import java.rmi.remoteexception; import java.util.arraylist; import java.util.date; public interface ihotel extends Remote { public void cancelbooking(bookingdetails details) throws RemoteException; public BookingDetails createbooking(bookingconstraints constraints, String person) throws BookingException, RemoteException; public void finalizebooking(bookingdetails b) throws RemoteException; public ArrayList<BookingDetails> getbookings() throws RemoteException; public ArrayList<RoomDetails> getfreerooms(date begin, Date end) throws RemoteException; public ArrayList<RoomDetails> getrooms() throws RemoteException; -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\ManagerSession.java dinsdag 3 november 2009 10:53 package hotel; import java.rmi.notboundexception; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.util.arraylist; import java.util.date; import naming.namingserviceremote; public class ManagerSession extends Session implements ManagerSessionRemote { * Returns a list of rooms that are free in a given period. @Override public ArrayList<RoomDetails> getfreerooms(date begin, Date end, String hotelname) throws RemoteException { return this.gethotelinstance(hotelname).getfreerooms(begin, end); * Returns a list of names of all the hotels. @Override public ArrayList<String> gethotels() throws RemoteException { ArrayList<String> result = null; System.setSecurityManager(null); Registry registry; try { registry = LocateRegistry.getRegistry("localhost", 1099); NamingServiceRemote namingservice; namingservice = (NamingServiceRemote) registry.lookup("namingservice"); result = namingservice.gethotels(); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); return result; * Returns a list of rooms of a given hotels. @Override public ArrayList<RoomDetails> getrooms(string hotelname) throws RemoteException { return this.gethotelinstance(hotelname).getrooms(); * Registers a new hotel in the naming service. @Override public void registerhotel(string name, ihotel hotel) throws RemoteException { -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\ManagerSession.java dinsdag 3 november 2009 10:53 System.setSecurityManager(null); Registry registry; try { registry = LocateRegistry.getRegistry("localhost", 1099); NamingServiceRemote namingservice; namingservice = (NamingServiceRemote) registry.lookup("namingservice"); namingservice.register(name, hotel); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); * Unregisters a hotel at the naming service. @Override public void unregisterhotel(string name) throws RemoteException { System.setSecurityManager(null); Registry registry; try { registry = LocateRegistry.getRegistry("localhost", 1099); NamingServiceRemote namingservice; namingservice = (NamingServiceRemote) registry.lookup("namingservice"); namingservice.remove(name); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); -2-
C:\Documents and Settings\Gijs\Desktop\src\hotel\ManagerSessionRemote.java dinsdag 3 november 2009 10:53 package hotel; import java.rmi.remote; import java.rmi.remoteexception; import java.util.arraylist; import java.util.date; public interface ManagerSessionRemote extends Remote { public ArrayList<RoomDetails> getfreerooms(date begin, Date end, String hotelname) throws RemoteException; public ArrayList<String> gethotels() throws RemoteException; public ArrayList<RoomDetails> getrooms(string hotelname) throws RemoteException; public void registerhotel(string name, ihotel stub) throws RemoteException; public void unregisterhotel(string name) throws RemoteException; -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\Room.java dinsdag 3 november 2009 10:53 package hotel; import java.util.arraylist; import java.util.date; public class Room { public ArrayList<BookingDetails> bookings = new ArrayList<BookingDetails>(); private RoomDetails details; public Room(RoomDetails details) { this.setdetails(details); public void book(bookingdetails b) { synchronized (this) { if (isfree(b.getbegin(), b.getend())) { bookings.add(b); System.out.println(); else throw new RuntimeException("Booking failed, room " + b.getroomnb() + " is unavailable from " + b.getbegin() + " to " + b.getend()); public ArrayList<BookingDetails> getbookings() { return this.bookings; public RoomDetails getdetails() { return details; public boolean isbooked(date date) { boolean result = false; for (BookingDetails booking : this.bookings) { if (date.after(booking.getbegin()) && date.before(booking.getend())) result = true; return result; public boolean isfree(date begin, Date end) { if (begin.compareto(end) == 0) { throw new RuntimeException("illegal period"); boolean free = true; for (BookingDetails booking : bookings) { if (booking.getbegin().equals(begin) booking.getend().equals(end)) free = false; else if (!booking.getbegin().after(end) &&!booking.getend().before(begin)) free = false; // for (int i = 0; i < bookings.size(); i++) { // BookingDetails booking = bookings.get(i); -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\Room.java dinsdag 3 november 2009 10:53 // // if (!booking.getbegin().before(begin) // &&!booking.getbegin().before(end)) { // continue; // // if (!booking.getend().after(begin) &&!booking.getend().after(end)) { // continue; // // return false; // // return true; return free; public void removebooking(bookingdetails booking) { synchronized (this) { for (BookingDetails b : this.bookings) { if (b.getbegin().equals(booking.getbegin()) && b.getend().equals(booking.getend()) && b.getnameofbooker().equals(booking.getnameofbooker())) { bookings.remove(b); return; private void setdetails(roomdetails details) { this.details = details; @Override public String tostring() { return "Room: " + this.getdetails().getroomnb(); -2-
C:\Documents and Settings\Gijs\Desktop\src\hotel\RoomDetails.java dinsdag 3 november 2009 10:54 package hotel; import java.io.serializable; public class RoomDetails implements Serializable { private static final long serialversionuid = 7131439091511998524L; private double costpernight; private int nbofbeds; private int roomnb; private boolean smoking; public RoomDetails(int roomnb, int nbofbeds, double costpernight, boolean smoking) { this.setcostpernight(costpernight); this.setnbofbeds(nbofbeds); this.setroomnb(roomnb); this.setsmokingallowed(smoking); public double getcostpernight() { return costpernight; public int getnbofbeds() { return nbofbeds; public int getroomnb() { return roomnb; public void setcostpernight(double costpernight) { this.costpernight = costpernight; public void setnbofbeds(int nbofbeds) { this.nbofbeds = nbofbeds; public void setroomnb(int roomnb) { this.roomnb = roomnb; public void setsmokingallowed(boolean smoking) { this.smoking = smoking; public boolean smokingallowed() { return smoking; @Override public String tostring() { String r = "Room " + getroomnb() + ", " + getnbofbeds() + " beds, " + getcostpernight() + " euro per night, "; if (smokingallowed()) r += "smoking allowed."; -1-
C:\Documents and Settings\Gijs\Desktop\src\hotel\RoomDetails.java dinsdag 3 november 2009 10:54 else r += "non-smoking."; return r; -2-
C:\Documents and Settings\Gijs\Desktop\src\hotel\Session.java dinsdag 3 november 2009 10:54 package hotel; import java.rmi.notboundexception; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.util.arraylist; import naming.namingserviceremote; public class Session { * Returns a hotel instance of a given hotel. protected ihotel gethotelinstance(string name) { Registry registry; ihotel result = null; try { registry = LocateRegistry.getRegistry(); NamingServiceRemote service = (NamingServiceRemote) registry.lookup("namingservice"); result = (ihotel) service.gethotelinstance(name); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); return result; * Returns a list of the names of all the hotels. protected ArrayList<String> gethotelnames() { ArrayList<String> result = new ArrayList<String>(); System.setSecurityManager(null); Registry registry; try { registry = LocateRegistry.getRegistry("localhost", 1099); NamingServiceRemote namingservice; namingservice = (NamingServiceRemote) registry.lookup("namingservice"); result = namingservice.gethotels(); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); return result; -1-
C:\Documents and Settings\Gijs\Desktop\src\naming\NamingServer.java dinsdag 3 november 2009 10:54 package naming; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.rmi.server.unicastremoteobject; public class NamingServer { * Creates a naming service and registers it. public static void main(string[] args) { System.setSecurityManager(null); NamingService service = new NamingService(); try { NamingServiceRemote stub; Registry registry = LocateRegistry.getRegistry(); stub = (NamingServiceRemote) UnicastRemoteObject.exportObject( service, 0); registry.rebind("namingservice", stub); catch (RemoteException e) { e.printstacktrace(); System.out.println("Naming Service Running!"); -1-
C:\Documents and Settings\Gijs\Desktop\src\naming\NamingService.java dinsdag 3 november 2009 10:54 package naming; import hotel.ihotel; import java.rmi.notboundexception; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.util.arraylist; import java.util.hashmap; public class NamingService implements NamingServiceRemote { HashMap<String, ihotel> hotels = new HashMap<String, ihotel>(); @Override public ArrayList<String> gethotels() throws RemoteException { return new ArrayList<String>(hotels.keySet()); * Registers a hotel stub with a given name. @Override public void register(string name, ihotel stub) throws RemoteException { System.setSecurityManager(null); Registry registry; try { registry = LocateRegistry.getRegistry(); registry.rebind(name, stub); catch (RemoteException e) { e.printstacktrace(); hotels.put(name, stub); * Removes a given hotel from the naming service. @Override public void remove(string name) throws RemoteException { System.setSecurityManager(null); Registry registry; try { registry = LocateRegistry.getRegistry(); registry.unbind(name); catch (RemoteException e) { e.printstacktrace(); catch (NotBoundException e) { e.printstacktrace(); hotels.remove(name); * Returns the hotel instance of a given hotel. @Override -1-
C:\Documents and Settings\Gijs\Desktop\src\naming\NamingService.java dinsdag 3 november 2009 10:54 public ihotel gethotelinstance(string name) throws RemoteException { return this.hotels.get(name); -2-
C:\Documents and Settings\Gijs\Desktop\src\naming\NamingServiceRemote.java dinsdag 3 november 2009 10:51 package naming; import hotel.ihotel; import java.rmi.remote; import java.rmi.remoteexception; import java.util.arraylist; public interface NamingServiceRemote extends Remote { void register(string name, ihotel stub) throws RemoteException; void remove(string name) throws RemoteException; ArrayList<String> gethotels() throws RemoteException; ihotel gethotelinstance(string name) throws RemoteException; -1-
!"#$%&'$()""'#"*%+%&","-. /"'%012*3141*567*81(9*:*;*<!""#$%&'($)*+,-#."/,"+0$1$2#*+($%3-4&+5067$89$"0:";-#$<99=! "#$%&'($'))#!*! +,-..(/0&-1#-2!"#$%&#'()**%"+,)-.)/#0,1"#2#3%.*4$,((%"+%#)55(,4)6%*#&%#0,%"7#8.#,*#%%"#!"#$%&9#+%0%#')"#*%**,%*# ))"3.)-%"#:,1#+%#&;%%+%#)55(,4)6%9#+%#'()*$"+,$%!-9#%"#<="46%*#>5#+%0%#*%.3%.#.%/>&%#=,&3>%.%"7#?%#4(,%"&#')"#&;%%#*>>.&%"#*%**,%*#))"3.)-%"9#%%"#@(,%"&A%**,>"#%"#%%"#B)")-%.A%**,>"7#?%#*%.3%.# 0)(#+%0%#*%**,%#))"/)'%"#>5#))"3.))-#3)"#+%#4(,%"&#%"#3%.3>(-%"*#%%"#=",%'%#,+%"6C%.#&%.=-*&=.%"7#?%#4(,%"&#')"#3,)#+%0%#,+%"6C%.#0,1"#%,-%"#*%**,%#>5#+%#*%.3%.#:%$%.%"7?%#*%.3%.#/))'&#-%:.=,'#3)"#%%"#.)/#%,0$(*$(7#?%0%#*%.3%.#+,%"&#)((%#3%.*4$,((%"+%#$>&%(*#&%# :%3)D%"9#+%0%#'=""%"#3,)#%%"#B)")-%.A%**,>"#3)"#+%#E.)3%(F-%"4G#-%.%-,*&.%%.+#;>.+%"7#?%# H)/,"-A%.3%.#')"#%%"#$>&%("))/#'.,1-%"#%"#-%%I#3%.3>(-%"*#$%=,*&%#)+.%*#&%.=-7 J,)#%%"#*%**,%#')"#$%&$"9#;)).3)"#$%&#)+.%*#3,)#+%#H)/,"-A%.3%.#3%.'.%-%"#;>.+&9#3)">5#)<*&)"+# :%$%%.+#;>.+%"9#3,)#%%"#,K>&%(#,"&%.<)4%7#K%&#$>&%(#:%3)%.*4$,((%"+%#,"*&)"6%*#3)"#$%&#&G5%#L>>/9# ;%('%#%('#%%"#*%.,)(,*%%.:).%#,"*&)"6%#L>>/?%&),(*#:%3)&7#J,)#+%#*%.,)(,*%%.:).%#'()**%#3)"#$%&#&G5%# M>>',"-@>"*&.),"&*#')"#%%"#4(,%"&#%%"#-%*4$,'&%#')/%.#0>%'%"#,"#$%&#-%'>0%"#$>&%(7 N/#&%#0>.-%"#+)&#%.#-%%"#4>"O,4&%"#&=**%"#+%#3%.*4$,((%"+%#-%:.=,'%.*#'=""%"#3>>.'>/%"#:,1#$%&# &%-%(,1'#:>%'%"#3)"#%%"#')/%.9#0,1"#%.#3%.*4$,((%"+%#<="46%*#*G"4$.>",0%+#-%/))'&7#K%&#&%)/#$%%I# %.3>>.#-%'>0%"#>/#+%#*G"4$.>",*)6%#>5#$%&#())-*&%#",3%)=#&>%#&%#5)**%"7#?,&#$>=+&#,"#+)&#+%#<="46%*# :>>'PQ#%"#.%/>3%M>>',"-PQ#,"#+%#'()**%#L>>/#-%*G"4$.>",*%%.+#0,1"7!*3 4(#.$%&,,(/52('567#&1(56(#.&( K%&#&%)/#,*#%.,"#-%*())-+#$%&#>>.*5.>"'%(,1'%#+%*,-"#&%#:%$>=+%"7#8.#/>%*&%"#-%%"#-.>&%# 3%.)"+%.,"-%"#-%:%=.%"7#K%&#&>%3>%-%"#3)"#,"&%.<)4%*#>/#.%/>&%#)44%**#&%#-).)"+%.%"#;>.+&# -%0,%"#)(*#%%"#(>-,*4$%#&>%3>%-,"-9#-%%"#3%.)"+%.,"-#3)"#$%&#>>.*5.>"'%(,1'%#'()**%"+,)-.)/7 E,1+%"*#+%#,/5(%/%"&)6%#';)/%"#%.#;%(#%"'%(%#5.>:(%/%"#))"#$%&#(,4$&#+,%#",%>>.0,%"#;).%"# 61+%"*#+%#+%*,-"<)*%7#8R"#3)"#+%0%#5.>:(%/%"#$%%I#&%#/)'%"#/%&#+%#H)/,"-A%.3%.#+,%#>3%.)(# :%*4$,':)).#+,%"&#&%#0,1"7#?,&#;%.+#>5-%(>*&#+>>.#+%#H)/,"-A%.3%.#&%#.%-,*&%.%"#,"#$%&#LB!#.%-,*&.G9# ;%('%#>3%.)(#>5-%3.))-+#')"#;>.+%"7
8(9)(/'&(0&-1#-22-:. # =$-10$>"*?99@$-2%!"&*%"%%$9-
5900A(1B@
C01%%"#$12'1,*D*E0$-@%*(94"-F
C01%%"#$12'1,*D*E,$##"-*(94"-F
C01%%"#$12'1,*D*E'"BG&%*(94"-F C01%%"#$12'1,*:
H"I09+","-&#$12'1,