import json
import warnings
import matplotlib.pyplot as plt
import numpy as np
warnings.filterwarnings("ignore")
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams["axes.unicode_minus"] = False
class HotelManagementSystem:
def __init__(self):
self.rooms = []
def input_data(self):
room_number = input("请输入房间号: ")
occupancy_status = input("请输入房间入住状态 (已入住/空闲): ")
price_per_night = input("请输入房间每晚价格: ")
room = {
'number': room_number,
'status': occupancy_status,
'price_per_night': price_per_night
}
self.rooms.append(room)
print("房间信息录入成功!")
def save_data(self, filename='hotel_inventory.json'):
with open(filename, 'w') as file:
json.dump(self.rooms, file)
print("房间信息已保存到文件:", filename)
def add_data(self):
print("添加房间信息:")
self.input_data()
def delete_data(self):
room_to_delete = input("请输入要删除的房间号: ")
self.rooms = [room for room in self.rooms if room['number'] != room_to_delete]
print(f"已删除房间号为 {room_to_delete} 的房间信息")
def modify_data(self):
room_to_modify = input("请输入要修改的房间号: ")
for room in self.rooms:
if room['number'] == room_to_modify:
room['status'] = input("请输入新的房间入住状态 (已入住/空闲): ")
room['price_per_night'] = input("请输入新的房间每晚价格: ")
print(f"{room_to_modify} 房间的信息已更新")
break
else:
print(f"未找到房间号为 {room_to_modify} 的房间信息")
def query_data(self):
room_to_query = input("请输入要查询的房间号: ")
for room in self.rooms:
if room['number'] == room_to_query:
print("房间信息如下:")
print(f"房间号: {room['number']}")
print(f"入住状态: {room['status']}")
print(f"每晚价格: {room['price_per_night']}")
break
else:
print(f"未找到房间号为 {room_to_query} 的房间信息")
def data_analysis(self):
total_rooms = len(self.rooms)
print(f"当前系统中共有 {total_rooms} 间房间")
if total_rooms > 0:
occupied_rooms = [room for room in self.rooms if room['status'] == '已入住']
occupied_count = len(occupied_rooms)
print(f"已入住房间数: {occupied_count}")
prices = [float(room['price_per_night']) for room in self.rooms]
average_price = np.mean(prices)
print(f"所有房间的平均每晚价格为: {average_price:.2f}")
plt.bar(range(total_rooms), prices, tick_label=[room['number'] for room in self.rooms])
plt.title("房间价格分布")
plt.xlabel("房间号")
plt.ylabel("每晚价格")
plt.xticks(rotation=45, ha="right")
plt.show()
else:
print("当前系统中没有房间信息,无法进行数据统计分析")
def main():
hms = HotelManagementSystem()
while True:
print("\n酒店管理系统")
print("1. 添加房间信息")
print("2. 删除房间信息")
print("3. 修改房间信息")
print("4. 查询房间信息")
print("5. 数据统计分析")
print("6. 保存并退出")
choice = input("请选择操作 (1-6): ")
if choice == '1':
hms.add_data()
elif choice == '2':
hms.delete_data()
elif choice == '3':
hms.modify_data()
elif choice == '4':
hms.query_data()
elif choice == '5':
hms.data_analysis()
elif choice == '6':
hms.save_data()
break
else:
print("无效的选择,请重新输入")
if __name__ == "__main__":
main()