Files
house/main2.0.py
T
2026-05-08 17:32:18 +08:00

198 lines
8.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 导入原有业务函数,完全保留
from house_service import add_house, delete_house, update_house, query_house
# V2.0新增:导入新增的栈业务函数
from house_service import push_operation_stack, push_browse_stack
from house_service import pop_operation_stack, pop_browse_stack
from house_service import get_recent_records, clear_stack
from house_service import get_next_house_id
# V1.0
def print_menu():
print("\n===== 房屋出租系统 V2.0 =====")
print("1. 新增房源")
print("2. 删除房源")
print("3. 修改房源")
print("4. 查询房源")
# V2.0新增:2个新菜单选项
print("5. 查看/管理操作记录")
print("6. 查看/管理浏览历史")
print("7. 退出系统")
print("============================")
def main():
while True:
print_menu()
choice = input("请输入操作编号(1-7):")
# 1. 新增房源
if choice == "1":
print("\n----- 新增房源 -----")
address = input("请输入房源地址:")
try:
price = float(input("请输入租金(元/月):"))
area = float(input("请输入面积(㎡):"))
except ValueError:
print("错误:租金/面积必须是数字!")
continue
house_type = input("请输入户型(如:一居室、两居室):")
if add_house(address, price, area, house_type):
# V2.0 新增:操作记录埋点
new_id = get_next_house_id() - 1
push_operation_stack(f"新增房源ID:{new_id},地址:{address}")
# 2. 删除房源
elif choice == "2":
print("\n----- 删除房源 -----")
try:
house_id = int(input("请输入要删除的房源ID"))
# 原有删除前的查询,用于埋点
house = query_house("id", house_id)
house_addr = house[0]["address"] if house else "未知地址"
# 原有删除逻辑,完全未改
if delete_house(house_id):
# V2.0 新增:操作记录埋点
push_operation_stack(f"删除房源ID:{house_id},地址:{house_addr}")
except ValueError:
print("错误:房源ID必须是整数!")
# 3. 修改房源)
elif choice == "3":
print("\n----- 修改房源 -----")
try:
house_id = int(input("请输入要修改的房源ID"))
# 原有收集修改字段逻辑,完全未改
new_info = {}
address = input("请输入新地址(不修改按回车):")
if address:
new_info["address"] = address
price_input = input("请输入新租金(不修改按回车):")
if price_input:
try:
new_info["price"] = float(price_input)
except ValueError:
print("错误:租金必须是数字!")
continue
area_input = input("请输入新面积(不修改按回车):")
if area_input:
try:
new_info["area"] = float(area_input)
except ValueError:
print("错误:面积必须是数字!")
continue
house_type = input("请输入新户型(不修改按回车):")
if house_type:
new_info["house_type"] = house_type
# 原有修改逻辑,完全未改
if new_info:
if update_house(house_id, new_info):
# V2.0 新增:操作记录埋点
push_operation_stack(f"修改房源ID:{house_id},修改内容:{new_info}")
else:
print("未输入任何修改内容!")
except ValueError:
print("错误:房源ID必须是整数!")
# 4. 查询房源
elif choice == "4":
print("\n----- 查询房源 -----")
print("查询条件类型:1-ID 2-地址 3-租金 4-户型")
cond_choice = input("请输入条件类型编号(1-4):")
cond_type_map = {"1": "id", "2": "address", "3": "price", "4": "house_type"}
if cond_choice not in cond_type_map:
print("错误:无效的条件类型!")
continue
condition_type = cond_type_map[cond_choice]
condition_value = input(f"请输入{condition_type}查询值:")
# 原有查询逻辑,完全未改
result = query_house(condition_type, condition_value)
# 原有结果展示逻辑,完全未改
if result:
print("\n查询结果:")
for house in result:
print(
f"ID{house['id']} | 地址:{house['address']} | 租金:{house['price']}元/月 | 面积:{house['area']}㎡ | 户型:{house['house_type']}")
# V2.0 新增:浏览历史埋点
if condition_type == "id":
push_browse_stack(house["id"])
else:
print("未找到符合条件的房源!")
# V2.0 新增代码:栈功能交互
# 5. 操作记录管理
elif choice == "5":
while True:
print("\n----- 操作记录管理 -----")
print("1. 查看最新10条操作记录")
print("2. 获取最新1条操作记录(并删除)")
print("3. 清空所有操作记录")
print("4. 返回主菜单")
c = input("请输入操作编号:")
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")
elif c == "4":
break
else:
print("无效编号!")
# 6. 浏览历史管理
elif choice == "6":
while True:
print("\n----- 浏览历史管理 -----")
print("1. 查看最新10条浏览历史")
print("2. 获取最新1条浏览房源(并删除)")
print("3. 清空所有浏览历史")
print("4. 返回主菜单")
c = input("请输入操作编号:")
if c == "1":
ids = get_recent_records("browse", 10)
if ids:
print("\n最新10条浏览历史(从新到旧):")
for i, hid in enumerate(ids, 1):
h = query_house("id", hid)
if h:
h = h[0]
print(f"{i}. ID:{hid} | 地址:{h['address']} | 租金:{h['price']}")
else:
print(f"{i}. ID:{hid}(房源已删除)")
else:
print("暂无浏览历史!")
elif c == "2":
hid = pop_browse_stack()
if hid:
h = query_house("id", hid)
if h:
h = h[0]
print(f"最新浏览:ID:{hid} | 地址:{h['address']}")
else:
print(f"最新浏览ID:{hid}(已删除)")
else:
print("暂无浏览历史!")
elif c == "3":
clear_stack("browse")
elif c == "4":
break
else:
print("无效编号!")
# 7. 退出系统(原有逻辑完全保留)
elif choice == "7":
print("感谢使用房屋出租系统,再见!")
break
# 无效输入(原有逻辑完全保留)
else:
print("错误:请输入1-7之间的有效编号!")
if __name__ == "__main__":
main()