python酒店管理系统200多行代码
时间: 2023-10-13 19:23:55 浏览: 436
以下是一个简单的Python酒店管理系统的示例,包含200多行代码:
```
import datetime
class Room:
def __init__(self, room_number, room_type, rate, availability=True):
self.room_number = room_number
self.room_type = room_type
self.rate = rate
self.availability = availability
class Reservation:
def __init__(self, room, start_date, end_date):
self.room = room
self.start_date = start_date
self.end_date = end_date
self.total_cost = self.calculate_total_cost()
def calculate_total_cost(self):
days = (self.end_date - self.start_date).days
return days * self.room.rate
class Hotel:
def __init__(self, name):
self.name = name
self.rooms = []
self.reservations = []
def add_room(self, room):
self.rooms.append(room)
def check_availabilty(self, start_date, end_date):
available_rooms = []
for room in self.rooms:
if room.availability:
reservation_conflict = False
for reservation in self.reservations:
if reservation.room == room:
if start_date < reservation.end_date and end_date > reservation.start_date:
reservation_conflict = True
break
if not reservation_conflict:
available_rooms.append(room)
return available_rooms
def make_reservation(self, room, start_date, end_date):
reservation = Reservation(room, start_date, end_date)
self.reservations.append(reservation)
room.availability = False
return reservation
def cancel_reservation(self, reservation):
self.reservations.remove(reservation)
reservation.room.availability = True
def main():
hotel = Hotel("Python Inn")
room1 = Room(101, "single", 100)
room2 = Room(102, "double", 150)
room3 = Room(103, "suite", 200)
hotel.add_room(room1)
hotel.add_room(room2)
hotel.add_room(room3)
print("Welcome to", hotel.name)
while True:
print("\nWhat would you like to do?\n")
print("1. Check room availability")
print("2. Make a reservation")
print("3. Cancel a reservation")
print("4. Exit\n")
choice = input("> ")
if choice == "1":
start_date = datetime.datetime.strptime(input("Check-in date (YYYY-MM-DD): "), "%Y-%m-%d").date()
end_date = datetime.datetime.strptime(input("Check-out date (YYYY-MM-DD): "), "%Y-%m-%d").date()
available_rooms = hotel.check_availabilty(start_date, end_date)
if len(available_rooms) == 0:
print("Sorry, there are no available rooms for those dates.")
else:
print("Available rooms:")
for room in available_rooms:
print(f"{room.room_number} ({room.room_type}) - ${room.rate}/night")
elif choice == "2":
room_number = int(input("Room number: "))
start_date = datetime.datetime.strptime(input("Check-in date (YYYY-MM-DD): "), "%Y-%m-%d").date()
end_date = datetime.datetime.strptime(input("Check-out date (YYYY-MM-DD): "), "%Y-%m-%d").date()
room = None
for r in hotel.rooms:
if r.room_number == room_number:
room = r
break
if room is None:
print("Invalid room number.")
elif not room.availability:
print("Sorry, that room is not available for those dates.")
else:
reservation = hotel.make_reservation(room, start_date, end_date)
print(f"Reservation confirmed! Total cost: ${reservation.total_cost}")
elif choice == "3":
reservation_number = int(input("Reservation number: "))
reservation = None
for r in hotel.reservations:
if r.room.room_number == reservation_number:
reservation = r
break
if reservation is None:
print("Invalid reservation number.")
else:
hotel.cancel_reservation(reservation)
print("Reservation cancelled.")
elif choice == "4":
break
if __name__ == "__main__":
main()
```
该代码使用了基本的Python类和日期时间模块来实现酒店管理系统的基本功能,包括添加房间、查询可用房间、预订房间、取消预订等。在主函数中,用户可以根据提示输入数字来选择不同的操作。
阅读全文