一个中介有若干房源,信息包括房屋编号、房屋地址、业主名称、联系方式、月租金;租赁信息包括房屋编号、承租人、承租开始时间、结束时间;请设计相关类和程序实现房源信息的添加、修改和查询功能以及房屋租赁的管理。
时间: 2024-01-31 07:04:43 浏览: 114
根据题目需求,可以设计以下类:
1. House:房源类,包括属性编号、地址、业主名称、联系方式和月租金,以及方法用于设置和获取这些属性值;
2. Lease:租赁类,包括属性房屋编号、承租人、承租开始时间和结束时间,以及方法用于设置和获取这些属性值;
3. Agency:中介类,包括属性房源列表和租赁列表,以及方法用于添加、修改和查询房源信息,以及管理房屋租赁。
下面是一个示例程序实现:
```python
class House:
def __init__(self, number, address, owner_name, contact, rent):
self.number = number
self.address = address
self.owner_name = owner_name
self.contact = contact
self.rent = rent
def set_number(self, number):
self.number = number
def set_address(self, address):
self.address = address
def set_owner_name(self, owner_name):
self.owner_name = owner_name
def set_contact(self, contact):
self.contact = contact
def set_rent(self, rent):
self.rent = rent
def get_number(self):
return self.number
def get_address(self):
return self.address
def get_owner_name(self):
return self.owner_name
def get_contact(self):
return self.contact
def get_rent(self):
return self.rent
class Lease:
def __init__(self, house_number, tenant, start_date, end_date):
self.house_number = house_number
self.tenant = tenant
self.start_date = start_date
self.end_date = end_date
def set_house_number(self, house_number):
self.house_number = house_number
def set_tenant(self, tenant):
self.tenant = tenant
def set_start_date(self, start_date):
self.start_date = start_date
def set_end_date(self, end_date):
self.end_date = end_date
def get_house_number(self):
return self.house_number
def get_tenant(self):
return self.tenant
def get_start_date(self):
return self.start_date
def get_end_date(self):
return self.end_date
class Agency:
def __init__(self):
self.house_list = []
self.lease_list = []
def add_house(self, house):
self.house_list.append(house)
def modify_house(self, number, address=None, owner_name=None, contact=None, rent=None):
for house in self.house_list:
if house.get_number() == number:
if address is not None:
house.set_address(address)
if owner_name is not None:
house.set_owner_name(owner_name)
if contact is not None:
house.set_contact(contact)
if rent is not None:
house.set_rent(rent)
return True
return False
def search_house(self, number=None, address=None, owner_name=None, contact=None, rent=None):
result = []
for house in self.house_list:
if (number is None or house.get_number() == number) and \
(address is None or house.get_address() == address) and \
(owner_name is None or house.get_owner_name() == owner_name) and \
(contact is None or house.get_contact() == contact) and \
(rent is None or house.get_rent() == rent):
result.append(house)
return result
def lease_house(self, house_number, tenant, start_date, end_date):
lease = Lease(house_number, tenant, start_date, end_date)
self.lease_list.append(lease)
def terminate_lease(self, house_number, tenant):
for lease in self.lease_list:
if lease.get_house_number() == house_number and lease.get_tenant() == tenant:
self.lease_list.remove(lease)
return True
return False
def search_lease(self, house_number=None, tenant=None):
result = []
for lease in self.lease_list:
if (house_number is None or lease.get_house_number() == house_number) and \
(tenant is None or lease.get_tenant() == tenant):
result.append(lease)
return result
```
使用示例:
```python
# 创建中介对象
agency = Agency()
# 添加房源
house1 = House(1, "北京市海淀区", "张三", "13812345678", 2000)
house2 = House(2, "北京市朝阳区", "李四", "13987654321", 2500)
agency.add_house(house1)
agency.add_house(house2)
# 修改房源信息
agency.modify_house(1, address="北京市西城区", rent=2200)
# 查询房源信息
result = agency.search_house(owner_name="李四")
for house in result:
print("编号:{},地址:{},业主:{},联系方式:{},月租金:{}".format(
house.get_number(), house.get_address(), house.get_owner_name(), house.get_contact(), house.get_rent()))
# 租赁房屋
agency.lease_house(2, "王五", "2021-10-01", "2022-09-30")
# 终止租赁
agency.terminate_lease(2, "王五")
# 查询租赁信息
result = agency.search_lease(house_number=1)
for lease in result:
print("房屋编号:{},承租人:{},开始时间:{},结束时间:{}".format(
lease.get_house_number(), lease.get_tenant(), lease.get_start_date(), lease.get_end_date()))
```
阅读全文