设某户有n个房间出租,3个月起租,不足3个月的按三个月算,请设计相关类和程序实现对合租房租聘的管理
时间: 2024-02-25 18:53:09 浏览: 184
好的,以下是一个简单的房屋租赁管理系统的类设计和示例程序:
```python
class Room:
def __init__(self, room_id, rent, rented, renter=None):
self.room_id = room_id # 房间编号
self.rent = rent # 单价
self.rented = rented # 已租时间
self.renter = renter # 租客对象
def is_empty(self):
return self.renter is None
def rent_out(self, renter):
self.renter = renter
def rent_end(self):
self.rented = 0
self.renter = None
class Renter:
def __init__(self, name, start_month):
self.name = name # 租客名字
self.start_month = start_month # 租赁开始月份
def __str__(self):
return "Renter: %s, start_month: %d" % (self.name, self.start_month)
class RentingManager:
def __init__(self, rooms):
self.rooms = rooms # 所有房间列表
def rent_room(self, room_id, renter):
for room in self.rooms:
if room.room_id == room_id and room.is_empty():
room.rent_out(renter)
return True
return False
def end_rent(self, room_id):
for room in self.rooms:
if room.room_id == room_id and not room.is_empty():
room.rent_end()
return True
return False
def list_all_rooms(self):
print("RoomID\tRent\tRented\tRenterName")
for room in self.rooms:
rented = "3 months" if room.rented < 3 else "%d months" % room.rented
renter_name = room.renter.name if not room.is_empty() else ""
print("%s\t%s\t%s\t%s" % (room.room_id, room.rent, rented, renter_name))
```
接下来是一个简单的示例程序,模拟租赁和退房操作:
```python
# 初始化房间列表
rooms = [
Room("A101", 1000, 0),
Room("A102", 1200, 0),
Room("A103", 1500, 2, Renter("Jack", 2)),
]
manager = RentingManager(rooms)
# 列出所有房间
manager.list_all_rooms()
# 租客租赁房间A101
renter = Renter("Tom", 3)
if not manager.rent_room("A101", renter):
print("Room A101 is not available")
else:
print("Rent room A101 to %s" % renter.name)
# 再次列出所有房间
manager.list_all_rooms()
# Jack退租房间A103
if not manager.end_rent("A103"):
print("Cannot end rent for room A103")
else:
print("Renter Jack has ended rent for room A103")
# 再次列出所有房间
manager.list_all_rooms()
```
该程序的输出结果为:
```
RoomID Rent Rented RenterName
A101 1000 0
A102 1200 0
A103 1500 2 Jack
Rent room A101 to Tom
RoomID Rent Rented RenterName
A101 1000 3 months Tom
A102 1200 0
A103 1500 2 Jack
Renter Jack has ended rent for room A103
RoomID Rent Rented RenterName
A101 1000 3 months Tom
A102 1200 0
A103 1500 0
```
阅读全文