python
定义一个Room类,表示房间
class Room:
def init(self, number, price, occupied):
self.number = number
self.price = price
self.occupied = occupied
定义一个Hotel类,表示酒店
class Hotel:
def init(self, name, room_count):
self.name = name
self.rooms = [] # 存储所有房间的列表
self.room_count = room_count # 酒店的房间数量
# 添加房间到酒店中
def add_room(self, number, price, occupied):
room = Room(number, price, occupied)
self.rooms.append(room)
print("添加成功!")
# 查找房间并返回房间对象
def find_room(self, number):
for room in self.rooms:
if room.number == number:
return room
return None
# 显示所有房间信息
def show_all_rooms(self):
for room in self.rooms:
print(f"房间号:{room.number},价格:{room.price}元,是否已占用:{room.occupied}")
# 预订房间并返回预订信息,如果预订失败则返回None
def book_room(self, number):
room = self.find_room(number)
if room is None or room.occupied:
return None
room.occupied = True
print("预订成功!")
return f"房间号:{number},价格:{room.price}元"
这个酒店管理系统实现了添加房间、查找房间、显示所有房间信息、预订房间的基本功能。可以通过实例化Hotel类来使用这些方法。例如:
python
实例化一个酒店对象,假设酒店名为"MyHotel",房间数量为100个
hotel = Hotel("MyHotel", 100)
添加房间到酒店中,假设添加一个价格为100元的空闲房间(未被占用)
hotel.add_room(1, 100, False)
显示所有房间信息
hotel.show_all_rooms()
预订房间并输出预订信息,假设预订房间号为1的房间,该房间已经被占用,因此预订失败并输出None到控制台
print(hotel.book_room(1)) # None