帮我用python做一个酒店管理系统代码
时间: 2024-02-01 13:04:44 浏览: 92
好的,以下是一个简单的酒店管理系统的Python代码实现,具体实现了房间预定、房间退订、订单查询和财务报表功能。需要注意的是,这只是一个简单的示例代码,实际应用时需要进行更多的优化和完善。
```python
import sqlite3
import datetime
# 连接到数据库
conn = sqlite3.connect('hotel.db')
# 房间预定功能
def book_room(room_type, checkin_date):
# 查询符合条件的房间
cursor = conn.execute("SELECT * FROM rooms WHERE type=? AND status=0", (room_type,))
rooms = cursor.fetchall()
if not rooms:
print("抱歉,该类型房间已满")
return
# 显示可供预定的房间信息
print("可供预定的房间信息:")
print("房间号\t房间类型\t房间价格")
for room in rooms:
print(f"{room[0]}\t{room[1]}\t{room[2]}")
# 用户选择房间
room_id = input("请选择房间号:")
# 更新房间状态为已预定
conn.execute("UPDATE rooms SET status=1 WHERE id=?", (room_id,))
# 插入预定信息到订单表中
checkin_time = datetime.datetime.strptime(checkin_date, '%Y-%m-%d')
checkout_time = checkin_time + datetime.timedelta(days=1)
conn.execute("INSERT INTO orders (room_id, checkin_time, checkout_time, status) VALUES (?, ?, ?, ?)", (room_id, checkin_time, checkout_time, 1))
conn.commit()
print("预定成功")
# 房间退订功能
def cancel_booking(room_id, checkin_date):
# 查询订单信息
cursor = conn.execute("SELECT * FROM orders WHERE room_id=? AND checkin_time=?", (room_id, checkin_date))
order = cursor.fetchone()
if not order:
print("该订单不存在")
return
# 更新订单状态为已取消
conn.execute("UPDATE orders SET status=0 WHERE id=?", (order[0],))
# 更新房间状态为可预定
conn.execute("UPDATE rooms SET status=0 WHERE id=?", (room_id,))
conn.commit()
print("退订成功")
# 订单查询功能
def query_order(order_id=None, room_id=None):
# 查询订单信息
if order_id:
cursor = conn.execute("SELECT * FROM orders WHERE id=?", (order_id,))
elif room_id:
cursor = conn.execute("SELECT * FROM orders WHERE room_id=?", (room_id,))
else:
print("请输入订单号或房间号")
return
order = cursor.fetchone()
if not order:
print("该订单不存在")
return
# 显示订单信息
print("订单号\t房间号\t入住时间\t离店时间\t订单状态")
print(f"{order[0]}\t{order[1]}\t{order[2]}\t{order[3]}\t{'已取消' if order[4]==0 else '已预定'}")
# 财务报表功能
def finance_report():
# 查询订单信息
cursor = conn.execute("SELECT rooms.type, COUNT(orders.id), SUM(rooms.price), (SUM(orders.checkout_time-orders.checkin_time)*rooms.price) FROM orders JOIN rooms ON orders.room_id=rooms.id GROUP BY rooms.type")
orders = cursor.fetchall()
# 显示财务报表
print("房间类型\t订单数\t销售额\t入住率")
for order in orders:
room_type, order_count, sales, duration_sales = order
occupancy_rate = order_count / 365
print(f"{room_type}\t{order_count}\t{sales}\t{occupancy_rate:.2%}")
# 主程序
while True:
print("欢迎使用酒店管理系统")
print("1. 房间预定")
print("2. 房间退订")
print("3. 订单查询")
print("4. 财务报表")
print("5. 退出系统")
choice = input("请输入操作编号:")
if choice == '1':
room_type = input("请输入房间类型:")
checkin_date = input("请输入入住日期(格式:yyyy-mm-dd):")
book_room(room_type, checkin_date)
elif choice == '2':
room_id = input("请输入房间号:")
checkin_date = input("请输入入住日期(格式:yyyy-mm-dd):")
cancel_booking(room_id, checkin_date)
elif choice == '3':
order_id = input("请输入订单号(或者房间号):")
if order_id.isdigit():
query_order(order_id=order_id)
else:
query_order(room_id=order_id)
elif choice == '4':
finance_report()
elif choice == '5':
break
else:
print("无效的操作编号,请重新输入")
```
需要注意的是,这个代码中使用了SQLite数据库,因此需要先安装SQLite库。同时,本代码还存在一些安全问题,比如SQL注入攻击等,实际应用时需要进行更多的安全措施。
阅读全文