users = [ {'username': 'woo1', 'password': '123'}, {'username': 'woo2', 'password': '123'}, {'username': 'woo3', 'password': '123'} ] room_data = [] # 登录函数 def login(): print("===== 酒店管理系统登录 =====") attempts = 3 while attempts > 0: username = input("请输入用户名: ") password = input("请输入密码: ") user = next((user for user in users if user['username'] == username), None) if user is None: print("用户名不存在,请重新输入。") attempts -= 1 elif user['password'] != password: print("密码错误,请重新输入。") attempts -= 1 else: print("登录成功!") return True print("登录失败!程序退出。") return False # 主界面函数 def main_menu(): print("-----------------------------") print("| 酒店管理系统 |") print("-----------------------------") print("| 1. 录入房间信息 |") print("| 2. 查找房间 |") print("| 3. 删除房间信息 |") print("| 4. 修改房间信息 |") print("| 5. 房间信息排序显示 |") print("| 6. 统计房间总个数 |") print("| 7. 显示所有房间信息 |") print("| 0. 退出系统 |") print("-----------------------------") choice = input("请输入选项(0-7): ") return choice def hotel_management_system(): if not login(): return room_data = read_room_data(); while True: choice = main_menu() if choice == "1": add_room(room_data) elif choice == "2": search_room(room_data) elif choice == "3": delete_room(room_data) elif choice == "4": modify_room(room_data) elif choice == "5": sort_room_info(room_data) elif choice == "6": count_rooms(room_data) elif choice == "7": display_all_rooms(room_data) elif choice == "0": exit_system(room_data) else: print("无效的选择。") def read_room_data(): temp_data = [] try: with open("room_data.txt", "r") as file: for line in file: room = line.strip().split(",") room[0] = int(room[0]) temp_data.append(room) print("成功读取房间信息。") except FileNotFoundError: print("未找到房间信息文件。") return temp_data def print_room_info(room): print("房间号:", room[0]) print("房间地址:", room[1]) print("房间类型:", room[2]) print("房间价格:", room[3]) print("房间状态:", room[4]) print("--------------------------") def save_room_data(room_data): # try: # with open("room_data.txt", "w") as file: # for room in room_data: # file.write(','.join(room) + "\n") # print("房间信息已成功保存。") # except: # print("保存房间信息时发生错误。") with open("room_data.txt", "w") as file: for room in room_data: file.write(",".join(str(item) for item in room) + "\n") # 酒店管理系统主程序 def add_room(room_data): room_number = int(input("请输入房间号: ")) room_address = input("请输入房间地址: ") room_type = input("请输入房间类型: ") room_price = float(input("请输入房间价格: ")) room_status = input("请输入房间状态: ") new_room = [room_number,room_address,room_type,room_price,room_status] # new_room = f"{room_number},{room_address},{room_type},{room_price},{room_status}" room_data.append(new_room) print("房间信息已成功录入。") return_last_menu = input("是否继续录入房间信息(1/0)") if return_last_menu =="1": add_room(room_data); return def search_room(room_data): search_choice = input("请选择查找方式 (1.房间号 2.房间类型 3.房间状态): ") if search_choice == "1": room_number = int(input("请输入房间号: ")) for room in room_data: if room[0] == room_number: print("找到匹配的房间信息:") print_room_info(room) return_last_menu = input("是否继续查询房间信息(1/0)") if return_last_menu == "1": search_room(room_data) return print("未找到匹配的房间信息。") elif search_choice == "2": room_type = input("请输入房间类型: ") found_rooms = [room for room in room_data if room[2] == room_type] if found_rooms: print("找到匹配的房间信息:") for room in found_rooms: print_room_info(room) return_last_menu = input("是否继续查询房间信息(1/0)") if return_last_menu == "1": search_room(room_data) return else: print("未找到匹配的房间信息。") elif search_choice == "3": room_status = input("请输入房间状态: ") found_rooms = [room for room in room_data if room[4] == room_status] if found_rooms: print("找到匹配的房间信息:") for room in found_rooms: print_room_info(room) return_last_menu = input("是否继续查询房间信息(1/0)") if return_last_menu == "1": search_room(room_data) return else: print("未找到匹配的房间信息。") else: print("无效的选择。") def delete_room(room_data): delete_choice = input("请选择删除方式 (1.房间号 2.房间地址): ") if delete_choice == "1": room_number = int(input("请输入要删除的房间号: ")) found_rooms = [room for room in room_data if room[0] == room_number] if found_rooms: confirm = input("找到匹配的房间信息,确认要删除吗?(Y/N): ") if confirm.lower() == "y": room_data.remove(found_rooms[0]) print("房间信息已成功删除。") return_last_menu = input("是否继续删除房间信息(1/0)") if return_last_menu == "1": delete_room(room_data) return else: print("已取消删除操作。") else: print("未找到要删除的房间信息。") elif delete_choice == "2": room_address = input("请输入要删除的房间地址: ") found_rooms = [room for room in room_data if room[1] == room_address] if found_rooms: confirm = input("找到匹配的房间信息,确认要删除吗?(Y/N): ") if confirm.lower() == "y": for room in found_rooms: room_data.remove(room) print("房间信息已成功删除。") return_last_menu = input("是否继续删除房间信息(1/0)") if return_last_menu == "1": delete_room(room_data) return else: print("已取消删除操作。") else: print("未找到要删除的房间信息。") else: print("无效的选择。") def modify_room(room_data): room_number = int(input("请输入要修改的房间号: ")) found_rooms = [room for room in room_data if room[0] == room_number] if found_rooms: print("找到匹配的房间信息:") print_room_info(found_rooms[0]) modify_choice = input("请选择要修改的信息 (1.房间状态 2.房间类型 3.房间价格): ") if modify_choice == "1": new_status = input("请输入新的房间状态: ") for room in room_data: if room[0] == room_number: room[4] = new_status print_room_info(room) print("房间信息已成功修改。") return_last_menu = input("是否继续修改房间信息(1/0)") if return_last_menu == "1": modify_room(room_data) elif modify_choice == "2": new_type = input("请输入新的房间类型: ") for room in room_data: if room[0] == room_number: room[2] = new_type print_room_info(room) print("房间信息已成功修改。") return_last_menu = input("是否继续修改房间信息(1/0)") if return_last_menu == "1": modify_room(room_data) elif modify_choice == "3": new_price = float(input("请输入新的房间价格: ")) for room in room_data: if room[0] == room_number: room[3] = new_price print_room_info(room) print("房间信息已成功修改。") return_last_menu = input("是否继续修改房间信息(1/0)") if return_last_menu == "1": modify_room(room_data) else: print("无效的选择。") else: print("未找到要修改的房间信息。") def sort_room_info(room_data): sort_choice = input("请选择排序方式 (1.根据房间号 2.根据房间价格): ") if sort_choice == "1": sorted_data = sorted(room_data, key=lambda x: x[0]) # 根据房间号排序 elif sort_choice == "2": sorted_data = sorted(room_data, key=lambda x: x[3]) # 根据房间价格排序 else: print("无效的选择。") return order_choice = input("请选择排序顺序 (1.升序 2.降序): ") if order_choice == "2": sorted_data.reverse() # 降序 print("排序后的房间信息:") for room in sorted_data: print_room_info(room) return_last_menu = input("是否继续排序房间信息(1/0)") if return_last_menu == "1": sort_room_info(room_data) return def count_rooms(room_data): total_rooms = len(room_data) print("房间总个数:", total_rooms) return_last_menu = input("返回主界面(1):") return def display_all_rooms(room_data): print("所有房间信息:") for room in room_data: print_room_info(room) return_last_menu = input("返回主界面(1):") return def exit_system(room_data): save_choice = input("是否保存房间信息并退出?(Y/N): ") if save_choice.lower() == "y": save_room_data(room_data) print("感谢使用酒店管理系统,再见!") exit() hotel_management_system()