Files
house/main3.0.py
T
2026-05-21 21:24:46 +08:00

229 lines
10 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
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, get_next_house_id
# V3.0 导入新增业务
from house_service import query_by_area, add_community_distance, show_adj_matrix
def print_menu():
print("\n===== 房屋出租系统 V3.0 =====")
print("1. 新增房源")
print("2. 删除房源")
print("3. 修改房源")
print("4. 查询房源")
print("5. 查看/管理操作记录")
print("6. 查看/管理浏览历史")
# V3.0 新增菜单
print("7. 地址解析与区域查询")
print("8. 小区邻接矩阵管理")
print("9. 退出系统")
print("==============================")
def main():
while True:
print_menu()
choice = input("请输入操作编号(1-9):")
# 1-6 功能完全保留 V2.0 代码
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):
new_id = get_next_house_id() - 1
push_operation_stack(f"新增房源ID:{new_id},地址:{address}")
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):
push_operation_stack(f"删除房源ID:{house_id},地址:{house_addr}")
except ValueError:
print("错误:房源ID必须是整数!")
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):
push_operation_stack(f"修改房源ID:{house_id},修改内容:{new_info}")
else:
print("未输入任何修改内容!")
except ValueError:
print("错误:房源ID必须是整数!")
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']}")
if condition_type == "id":
push_browse_stack(house["id"])
else:
print("未找到符合条件的房源!")
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("无效编号!")
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("无效编号!")
# ==================== V3.0 新增:地址解析与区域查询 ====================
elif choice == "7":
while True:
print("\n----- 地址解析与区域查询 -----")
print("1. 按区域关键词查询房源")
print("2. 返回主菜单")
c = input("请输入操作编号:")
if c == "1":
keyword = input("请输入区域关键词(如:朝阳、花园):")
res = query_by_area(keyword)
if res:
print("\n查询结果:")
for h in res:
print(f"ID:{h['id']} | 地址:{h['address']} | 租金:{h['price']}")
else:
print("未找到匹配房源!")
elif c == "2":
break
else:
print("无效编号!")
# ==================== V3.0 新增:邻接矩阵管理 ====================
elif choice == "8":
while True:
print("\n----- 小区邻接矩阵管理 -----")
print("1. 设置小区间距离")
print("2. 查看邻接矩阵")
print("3. 返回主菜单")
c = input("请输入操作编号:")
if c == "1":
c1 = input("请输入小区1名称:")
c2 = input("请输入小区2名称:")
try:
dis = int(input("请输入距离(米):"))
if add_community_distance(c1, c2, dis):
print("距离设置成功!")
else:
print("小区不存在,请先添加房源!")
except ValueError:
print("错误:距离必须是数字!")
elif c == "2":
show_adj_matrix()
elif c == "3":
break
else:
print("无效编号!")
elif choice == "9":
print("感谢使用房屋出租系统 V3.0,再见!")
break
else:
print("错误:请输入1-9之间的有效编号!")
if __name__ == "__main__":
main()