请用python编写一个共享雨伞管理系统
时间: 2023-09-19 07:08:28 浏览: 59
好的,下面是一个简单的共享雨伞管理系统的Python代码示例:
```python
class Umbrella:
def __init__(self, id):
self.id = id
self.is_available = True
class User:
def __init__(self, name):
self.name = name
self.borrowed_umbrella = None
class UmbrellaSharingSystem:
def __init__(self, num_umbrellas):
self.umbrellas = [Umbrella(i) for i in range(1, num_umbrellas+1)]
self.users = []
def add_user(self, user):
self.users.append(user)
def borrow_umbrella(self, user, umbrella_id):
umbrella = self.get_umbrella_by_id(umbrella_id)
if not umbrella.is_available:
print("Sorry, umbrella is not available.")
return
umbrella.is_available = False
user.borrowed_umbrella = umbrella
def return_umbrella(self, user):
umbrella = user.borrowed_umbrella
if umbrella:
umbrella.is_available = True
user.borrowed_umbrella = None
def get_umbrella_by_id(self, id):
for umbrella in self.umbrellas:
if umbrella.id == id:
return umbrella
return None
# Usage:
system = UmbrellaSharingSystem(10)
user1 = User("Alice")
system.add_user(user1)
user2 = User("Bob")
system.add_user(user2)
system.borrow_umbrella(user1, 1)
system.borrow_umbrella(user2, 2)
system.return_umbrella(user1)
```
这个示例代码实现了一个简单的共享雨伞管理系统,包括Umbrella、User和UmbrellaSharingSystem三个类。其中,Umbrella类表示雨伞,包含一个id和一个is_available属性表示该雨伞是否可用;User类表示用户,包含一个name和一个borrowed_umbrella属性表示该用户借用的雨伞;UmbrellaSharingSystem类表示整个系统,包含一个umbrellas列表表示所有的雨伞,一个users列表表示所有的用户,以及一些操作雨伞的方法。
在使用这个系统时,我们首先需要创建一个UmbrellaSharingSystem对象,然后添加一些用户到该系统中。接着,我们可以使用borrow_umbrella方法让用户借用雨伞,使用return_umbrella方法让用户归还雨伞。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)