In: Computer Science
Project Outcomes:
Develop a python program that uses:
decision constructs
looping constructs
basic operations on an list of objects (find, change, access all
elements)
more than one class and has multiple objects
Project Requirements:
1. Develop a simple Hotel program. We will have two classes, a
Hotel class
representing an individual hotel and a Room class. The Hotel class
will contain several
Room objects and will have several operations. We will also have a
driver program to test
the Hotel class.
2. Build a Hotel class that will store information about a Hotel.
It will include a name and
location. It should also include a list of class Room to hold
information about each
room. It will also have a int called occupiedCnt that keeps track
of how many rooms in
the hotel are occupied.
Specific Requirements for the Hotel Class:
1. The Hotel class has two constructors
1. __init__ function, will read in the hotel name and location from
hard-coded
values in the tester class, such as Beach Marriot Pensacola, it
will also assign
numOfRooms to zero. numOfRooms indicates how many rooms
are in the hotel.
It will create a 10 element array.
2. The Hotel will have an addRoom method that will create each
room with the required
information: room number, bed type, smoking/non-smoking, and the
room rate. Create at
least 5 rooms with different characteristics. Each room will also
have a boolean field
called occupied attribute that will be set to false when the room
is created. Don't forget
to increment the numOfRooms instance variable. Example
values for the rooms are:
101 queen s 100
102 king n 110
103 king n 88
104 twin s 100
105 queen n 99
3. The UML class diagram for the Hotel class will look like
this:
Hotel
theRooms: Array Room[]
name: String
location: String
occupiedCnt: int
numOfRooms: int
def __init(self)__(String,String)
def isFull(self) : boolean
def isEmpty(self) : boolean
def addRoom(self ,roomnumber,bedtype,smoking,price)
def addReservation(self,occupantName ,smoking,
bedtype)
def cancelReservation(self,occupantName)
def findReservation(self,occupantName):
def printReservationList(self)
def getDailySales(self) :
def occupancyPercentage(self) :
Setters and getters methods for name and location.
4. isFull() – returns a boolean that is true if all the rooms in
the hotel are occupied.
5. isEmpty() – returns a boolean that is true if all the rooms in
the hotel are unoccupied.
6. The addReservation() method takes three parameters: the
occupant’s name
(String), smoking or non-smoking request (char), and the requested
bed type (String).
When this method is called, the hotel will search the list of its
rooms for one that matches
the bed type and smoking/non-smoking attributes. If an unoccupied
room with the correct
attributes is found, the renter's name will be set and the
occupied attribute will be set
to true. In either case a message will be printed that will state
whether or not the
reservation was made.
7. When the cancelReservation() method executes, the hotel will
search for the
name of the visitor in each room. If it is found, the occupied
attribute will be set to false.
In either case a message will state whether or not the reservation
was cancelled. This
method calls the private utility method findReservation()to scan
the list of rooms
looking for a guest by name. It will return the index of the room
in the Array of rooms
or NOT_FOUND if the room is not found, which will be declared
as:
NOT_FOUND = -1;
8. findReservation() will take in a String representing the
occupant’s name and
search the occupied rooms for a reservation with that person’s
name. It will return the
index of the room or NOT_FOUND if not found.
9. printReservationList() will scan through all the rooms and
display all details
for only those rooms that are occupied. For example:
Room Number: 102
Occupant name: Pinto
Smoking room: n
Bed Type: king
Rate: 110.0
Room Number: 103
Occupant name: Wilson
Smoking room: n
Bed Type: king
Rate: 88.0
10. getDailySales() will scan the room list, adding up the dollar
amounts of the room
rates of all occupied rooms only.
11. occupancyPercentage() will divide occupiedCnt by the total
number of rooms to
provide an occupancy percentage.
12. __str__ – returns a nicely formatted string giving hotel and
room details (by calling
the __str__ in the Room class) for all the rooms in the hotel. For
example:
Hotel Name : Beach Marriot
Number of Rooms : 5
Number of Occupied Rooms : 1
Room Details are:
Room Number: 101
Occupant name: Not Occupied
Smoking room: s
Bed Type: queen
Rate: 100.0
Room Number: 102
Occupant name: Coffey
Smoking room: n
Bed Type: king
Rate: 110.0
Room Number: 103
Occupant name: Wilson
Smoking room: n
Bed Type: king
Rate: 88.0
Room Number: 104
Occupant name: Not Occupied
Smoking room: s
Bed Type: twin
Rate: 100.0
Room Number: 105
Occupant name: Not Occupied
Smoking room: n
Bed Type: queen
Rate: 99.0
13. The Room class diagram will look like this:
Room
roomNum: int
bedType: String
rate: double
occupantName: String
smoking: char
occupied: boolean
def __init__(int,String,char,double)
def getBedType(): String
def getSmoking():
char
def getRoomNum(): int
def getRoomRate(): double
def getOccupant(): String
def setOccupied(boolean)
def setOccupant(String)
def setRoomNum(int)
def setBedType(String)
def setRate(double)
def setSmoking(char)
def isOccupied(): boolean
1. The __init__() for a Room takes an int (room number), String
(bed type), char (s or n for
smoking or non-smoking)), and a double (room rate).
2. isOccupied() method returns true if the room is occupied, false
otherwise.
3. __str__() provides all the details of a room - room number,
name of guest(if
occupied) , bed type, smoking/non-smoking, rental rate. This should
all be formatted
nicely with one attribute on each line using the
'\n' escape character. See example above.
4. Several accessor and mutator methods for the Room class.
# Use list to store the room details.
You have to store required data in the list/database. You can store
hotel name, address, and
all rooms. Customer data in database tables.
'''
Python version : 2.7
Python program to develop a simple Hotel program
'''
# Room class
class Room:
def __init__(self,roomNum, bedType, smoking, rate):
self.roomNum = roomNum
self.bedType = bedType
self.smoking = smoking
self.rate = rate
self.occupantName = None
self.occupied = False
def getBedType(self):
return self.bedType
def getSmoking(self):
return self.smoking
def getRoomNum(self):
return self.roomNum
def getRoomRate(self):
return self.rate
def getOccupant(self):
return self.occupantName
def setOccupied(self,occupied):
self.occupied = occupied
def setOccupant(self,occupantName):
self.occupantName = occupantName
def setRoomNum(self,roomNum):
self.roomNum = roomNum
def setBedType(self,bedType):
self.bedType = bedType
def setRate(self,rate):
self.rate = rate
def setSmoking(self,smoking):
self.smoking = smoking
def isOccupied(self):
return self.occupied
def __str__(self):
roomStr = '\nRoom Number: '+str(self.roomNum)
if(self.occupied and (self.occupantName != None)):
roomStr += '\nOccupant Name : '+self.occupantName
else:
roomStr += '\nOccupant Name : Not Occupied'
roomStr += '\nSmoking room : '+self.smoking + '\nBed Type : '+self.bedType + '\nRate : '+"{0:.1f}".format(self.rate)+'\n'
return roomStr
# Hotel class
class Hotel:
NOT_FOUND = -1
def __init__(self, name, location):
self.name = name
self.location = location
self.numOfRooms = 0
self.theRooms = [None]*10
self.occupiedCnt = 0
def getName(self):
return self.name
def getLocation(self):
return self.location
def setName(self,name):
self.name = name
def setLocation(self, location):
self.location = location
def isFull(self):
return(self.numOfRooms == self.occupiedCnt)
def isEmpty(self):
return(self.occupiedCnt == 0)
def addRoom(self,roomNumber, bedType, smoking, price):
if self.numOfRooms < len(self.theRooms):
room = Room(roomNumber,bedType,smoking,price)
self.theRooms[self.numOfRooms] = room
self.numOfRooms += 1
else:
print('Rooms list in the Hotel is full')
def addReservation(self, occupantName, smoking, bedType):
roomFound = False
for room in self.theRooms:
if((room != None) and (not room.isOccupied()) and (room.getSmoking().lower() == smoking.lower()) and (room.getBedType().lower() == bedType.lower())):
room.setOccupied(True)
room.setOccupant(occupantName)
self.occupiedCnt += 1
roomFound = True
break
if(roomFound):
print('Reservation finished successfully!')
else:
print('Unable to find room with the given preferences!')
def cancelReservation(self, occupantName):
ind = self.findReservation(occupantName)
if ind != self.NOT_FOUND:
self.theRooms[ind].setOccupied(False)
self.theRooms[ind].setOccupant(None)
self.occupiedCnt -= 1
print('Reservation was cancelled successfully!')
if ind == self.NOT_FOUND:
print("Reservation doesn't exist")
def findReservation(self,occupantName):
for i in range(self.numOfRooms):
if((self.theRooms[i].isOccupied()) and (self.theRooms[i].getOccupant().lower() == occupantName.lower())):
return i
return self.NOT_FOUND
def printReservationList(self):
for room in self.theRooms:
if((room != None) and (room.isOccupied())):
print(room)
def getDailySales(self):
dailySales = 0
for room in self.theRooms:
if((room != None) and (room.isOccupied())):
dailySales += room.getRoomRate()
return dailySales
def occupancyPercentage(self):
if self.numOfRooms > 0:
return (float(self.occupiedCnt*100)/self.numOfRooms)
return 0
def __str__(self):
hotelStr = 'Hotel Name : '+self.name +'\nNumber of Rooms : '+str(self.numOfRooms) +'\nNumber of Occupied Rooms : '+str(self.occupiedCnt)
hotelStr += '\n\nRoom Details are :\n'
for i in range(self.numOfRooms):
hotelStr += str(self.theRooms[i])
return hotelStr
# function to display and get the user choice
def menu():
print('1. Add room')
print('2. Add Reservation')
print('3. Cancel Reservation')
print('4. Print Reservation List')
print('5. Get Daily Sales')
print('6. Get Occupancy Percentage')
print('7. Exit')
choice = int(raw_input('Enter a choice(1-7) : '))
while choice < 1 or choice > 7:
print('Invalid choice')
choice = int(raw_input('Enter a choice(1-7) : '))
return choice
# main function
def main():
hotel = Hotel("Beach Marriot","NNN")
hotel.addRoom(101,"queen","s",100)
hotel.addRoom(102,"king","n",110)
hotel.addRoom(103,"king","n",88)
hotel.addRoom(104,"twin","s",100)
hotel.addRoom(105,"queen","n",99)
print(hotel)
done = False
while not done:
choice = menu()
if choice == 1:
roomNumber = int(raw_input('Room Number : '))
bedType = raw_input('Bed Type : ')
smoking = raw_input('Smoking(s) or Non-Smoking(n) : ')
price = float(raw_input('Price : '))
hotel.addRoom(roomNumber,bedType,smoking,price)
elif choice == 2:
occupantName = raw_input('Occupant Name : ')
smoking = raw_input('Smoking room(s) or Non-Smoking(n) : ')
bedType = raw_input('Bed Type : ')
hotel.addReservation(occupantName, smoking, bedType)
elif choice == 3:
occupantName = raw_input('Occupant Name : ')
hotel.cancelReservation(occupantName)
elif choice == 4:
hotel.printReservationList()
elif choice == 5:
print('Daily Sales : %.2f' %(hotel.getDailySales()))
elif choice == 6:
print('Occupancy Percentage : %.2f' %(hotel.occupancyPercentage()))
else:
done = True
#call the main function
main()
#end of program
Code Screenshot:
Output: