Object oriented design for hotel reservation system

Mishra picture Mishra · Jun 16, 2013 · Viewed 8.1k times · Source

I am practicing object oriented design for an upcoming interview. My question is about the design for a hotel reservation system: - The system should be able to return an open room of a specific type or return all the open rooms in the hotel. - There are many types of rooms in hotel like regular, luxury, celebrity and so on.

So far I have come up with following classes:

Class Room{
//Information about room
virtual string getSpecifications(Room *room){};
}

Class regularRoom: public Room{
//get specifications for regular room
}

Class luxuryRoom: public Room{
//get specifications for regular room
}
//Similarly create as many specialized rooms as you want

Class hotel{
vector<Room *>openRooms; //These are all the open rooms (type casted to Room type pointer)

Public:
Room search(Room *aRoom){ //Search room of a specific type
        for(int i=0;i<openRooms.size();i++){
            if(typeid(*aRoom)==typeid(*openRooms[i])) return *openRooms[i];
        }
}

vector<Room> allOpenRooms(){//Return all open rooms
...
}

}

I am confused about the implementation of hotel.search() method where I am checking the type (which I believe should be handled by polymorphism in some way). Is there a better way of designing this system so that the search and allOpenRooms methods can be implemented without explicitly checking the type of the objects?

Answer

Spalteer picture Spalteer · Jun 16, 2013

Going through the sub-class objects asking what type they are isn't really a good illustration of o-o design. You really need something you want to do to all rooms without being aware of what type each one is. For example print out the daily room menu for the room (which might be different for different types). Deliberately looking for the sub-class object's type, while not being wrong, is not great o-o style. If you just want to do that, as the other respondents have said, just have "rooms" with a set of properties.