用代码写一个酒店管理系统
时间: 2024-10-11 19:04:21 浏览: 23
基于C#实现的酒店管理系统代码.rar
5星 · 资源好评率100%
创建一个简单的酒店管理系统的代码示例通常会涉及到几个关键模块,如客房管理、预订管理、用户管理和账单计算等。这里提供一个基于Python的简单命令行界面示例,我们先创建一个Hotel类作为基础:
```python
class Hotel:
def __init__(self):
self.rooms = {} # 使用字典存储房间信息
def add_room(self, room_number, room_type, price_per_night):
self.rooms[room_number] = {'type': room_type, 'price': price_per_night}
def list_rooms(self):
for room in self.rooms.values():
print(f"Room {room['room_number']}: Type - {room['type']}, Price - ${room['price']} per night")
def reserve_room(self, room_number, guest_name, start_date, end_date):
if room_number in self.rooms and not self.is_conflict(start_date, end_date, room_number):
self.rooms[room_number]['occupancy'] = (guest_name, start_date, end_date)
print(f"{guest_name} has reserved Room {room_number} from {start_date} to {end_date}.")
else:
print("Room not available or already occupied.")
def is_conflict(self, start_date, end_date, room_number):
# 这里只是一个简化的冲突检查,实际应用中需要考虑日期范围和已有预订情况
return self.rooms.get(room_number, {}).get('occupancy') and end_date < self.rooms[room_number]['occupancy'][1]
def calculate_bill(self, room_number, check_in_date, check_out_date):
if self.rooms.get(room_number) and check_in_date <= check_out_date:
nights_stayed = (check_out_date - check_in_date).days + 1
return f"Billing for Room {room_number}: $${nights_stayed * self.rooms[room_number]['price']}"
else:
print("Invalid room number or dates.")
# 初始化并操作酒店
hotel = Hotel()
hotel.add_room(101, "Standard", 100)
hotel.add_room(102, "Deluxe", 200)
hotel.reserve_room(101, "John Doe", "2023-05-01", "2023-05-05")
hotel.list_rooms()
hotel.calculate_bill(101, "2023-05-01", "2023-05-04") # 结算前四晚的费用
```
这个示例非常基础,实际应用可能需要更复杂的数据库连接、错误处理和其他功能。
阅读全文