From e28face0b626c336c30fee50fef75913c454a36b Mon Sep 17 00:00:00 2001 From: wxy-00771100 Date: Fri, 22 May 2026 14:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=E3=80=8C/=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- house-main3.0优化版.py | 259 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 house-main3.0优化版.py diff --git a/house-main3.0优化版.py b/house-main3.0优化版.py new file mode 100644 index 0000000..8689c2c --- /dev/null +++ b/house-main3.0优化版.py @@ -0,0 +1,259 @@ +from house_service import ( + add_house, delete_house, update_house, query_house, + push_operation_stack, push_browse_stack, + pop_operation_stack, pop_browse_stack, + get_recent_records, clear_stack, get_next_house_id, + query_by_area, add_community_distance, show_adj_matrix +) +# 引入上一轮优化后的数据保存方法 +from house.house_operation import save_all_data + + +def get_valid_input(prompt, cast_type=str): + """通用输入工具:自动处理类型转换异常,输入为空时返回 None""" + value = input(prompt).strip() + if not value: + return None + try: + return cast_type(value) + except ValueError: + print(f"错误:输入内容必须是有效的{cast_type.__name__}!") + return False + + +def print_house(house): + """统一房源信息的打印格式""" + print( + f"ID:{house['id']} | 地址:{house['address']} | 租金:{house['price']}元/月 | 面积:{house['area']}㎡ | 户型:{house['house_type']}") + + +def print_house_list(result, is_browse_history=False): + """统一打印房源列表(支持普通列表和浏览历史记录)""" + if not result: + print("未找到相关记录!") + return + for i, item in enumerate(result, 1): + house = item if not is_browse_history else query_house("id", item)[0] if query_house("id", item) else None + if house: + prefix = f"{i}. " if not is_browse_history else "" + print(f"{prefix}ID:{house['id']} | 地址:{house['address']} | 租金:{house['price']}元/月") + elif is_browse_history: + print(f"{i}. ID:{item}(房源已删除)") + + +# ==================== 业务功能模块拆分 ==================== + +def handle_add_house(): + """处理新增房源""" + print("\n----- 新增房源 -----") + address = input("请输入房源地址:").strip() + price = get_valid_input("请输入租金(元/月):", float) + if price is False: return + area = get_valid_input("请输入面积(㎡):", float) + if area is False: return + house_type = input("请输入户型(如:一居室、两居室):").strip() + + if add_house(address, price, area, house_type): + new_id = get_next_house_id() - 1 + push_operation_stack(f"新增房源ID:{new_id},地址:{address}") + print("房源新增成功!") + + +def handle_delete_house(): + """处理删除房源""" + print("\n----- 删除房源 -----") + house_id = get_valid_input("请输入要删除的房源ID:", int) + if house_id is False or house_id is None: return + + house = query_house("id", house_id) + house_addr = house[0]["address"] if house else "未知地址" + if delete_house(house_id): + push_operation_stack(f"删除房源ID:{house_id},地址:{house_addr}") + print("房源删除成功!") + + +def handle_update_house(): + """处理修改房源""" + print("\n----- 修改房源 -----") + house_id = get_valid_input("请输入要修改的房源ID:", int) + if house_id is False or house_id is None: return + + new_info = {} + address = input("请输入新地址(不修改按回车):").strip() + if address: new_info["address"] = address + + price = get_valid_input("请输入新租金(不修改按回车):", float) + if price is False: return + if price: new_info["price"] = price + + area = get_valid_input("请输入新面积(不修改按回车):", float) + if area is False: return + if area: new_info["area"] = area + + house_type = input("请输入新户型(不修改按回车):").strip() + if house_type: new_info["house_type"] = house_type + + if new_info: + if update_house(house_id, new_info): + push_operation_stack(f"修改房源ID:{house_id},修改内容:{new_info}") + print("房源修改成功!") + else: + print("未输入任何修改内容!") + + +def handle_query_house(): + """处理查询房源""" + print("\n----- 查询房源 -----") + cond_type_map = {"1": "id", "2": "address", "3": "price", "4": "house_type"} + print("查询条件类型:1-ID 2-地址 3-租金 4-户型") + cond_choice = input("请输入条件类型编号(1-4):").strip() + + if cond_choice not in cond_type_map: + print("错误:无效的条件类型!") + return + + condition_type = cond_type_map[cond_choice] + condition_value = input(f"请输入{condition_type}查询值:").strip() + result = query_house(condition_type, condition_value) + + if result: + print("\n查询结果:") + for house in result: + print_house(house) + if condition_type == "id": + push_browse_stack(house["id"]) + else: + print("未找到符合条件的房源!") + + +def handle_operation_records(): + """处理操作记录管理""" + while True: + print("\n----- 操作记录管理 -----") + print("1. 查看最新10条操作记录\n2. 获取最新1条操作记录(并删除)\n3. 清空所有操作记录\n4. 返回主菜单") + c = input("请输入操作编号:").strip() + if c == "1": + records = get_recent_records("operation", 10) + if records: + print("\n最新10条操作记录(从新到旧):") + for i, r in enumerate(records, 1): print(f"{i}. {r}") + else: + print("暂无操作记录!") + elif c == "2": + latest = pop_operation_stack() + print(latest if latest else "暂无操作记录!") + elif c == "3": + clear_stack("operation") + print("操作记录已清空!") + elif c == "4": + break + + +def handle_browse_history(): + """处理浏览历史管理""" + while True: + print("\n----- 浏览历史管理 -----") + print("1. 查看最新10条浏览历史\n2. 获取最新1条浏览房源(并删除)\n3. 清空所有浏览历史\n4. 返回主菜单") + c = input("请输入操作编号:").strip() + if c == "1": + ids = get_recent_records("browse", 10) + print_house_list(ids, is_browse_history=True) + elif c == "2": + hid = pop_browse_stack() + if hid: + h = query_house("id", hid) + if h: + print(f"最新浏览:ID:{hid} | 地址:{h[0]['address']}") + else: + print(f"最新浏览ID:{hid}(已删除)") + else: + print("暂无浏览历史!") + elif c == "3": + clear_stack("browse") + print("浏览历史已清空!") + elif c == "4": + break + + +def handle_address_area(): + """V3.0 处理地址解析与区域查询""" + while True: + print("\n----- 地址解析与区域查询 -----") + print("1. 按区域关键词查询房源\n2. 返回主菜单") + c = input("请输入操作编号:").strip() + if c == "1": + keyword = input("请输入区域关键词(如:朝阳、花园):").strip() + res = query_by_area(keyword) + print_house_list(res) + elif c == "2": + break + + +def handle_adj_matrix(): + """V3.0 处理小区邻接矩阵管理""" + while True: + print("\n----- 小区邻接矩阵管理 -----") + print("1. 设置小区间距离\n2. 查看邻接矩阵\n3. 返回主菜单") + c = input("请输入操作编号:").strip() + if c == "1": + c1 = input("请输入小区1名称:").strip() + c2 = input("请输入小区2名称:").strip() + dis = get_valid_input("请输入距离(米):", int) + if dis and dis is not False: + if add_community_distance(c1, c2, dis): + print("距离设置成功!") + else: + print("设置失败,请检查小区名称是否正确!") + elif c == "2": + show_adj_matrix() + elif c == "3": + break + + +# ==================== 主程序 ==================== + +def print_menu(): + print("\n===== 房屋出租系统 V3.0 =====") + print("1. 新增房源") + print("2. 删除房源") + print("3. 修改房源") + print("4. 查询房源") + print("5. 查看/管理操作记录") + print("6. 查看/管理浏览历史") + print("7. 地址解析与区域查询") + print("8. 小区邻接矩阵管理") + print("9. 退出系统") + print("==============================") + + +def main(): + action_map = { + "1": handle_add_house, + "2": handle_delete_house, + "3": handle_update_house, + "4": handle_query_house, + "5": handle_operation_records, + "6": handle_browse_history, + "7": handle_address_area, + "8": handle_adj_matrix, + } + + while True: + print_menu() + choice = input("请输入操作编号(1-9):").strip() + + if choice == "9": + print("正在保存数据...") + save_all_data() # 退出前保存内存中的数据到磁盘 + print("感谢使用房屋出租系统 V3.0,再见!") + break + + action = action_map.get(choice) + if action: + action() + else: + print("错误:请输入1-9之间的有效编号!") + + +if __name__ == "__main__": + main()