写一份学生宿舍管理系统代码
时间: 2023-12-01 13:57:31 浏览: 39
以下是一个简单的学生宿舍管理系统代码示例,使用Python语言编写。
```python
class Student:
def __init__(self, name, gender, room_number):
self.name = name
self.gender = gender
self.room_number = room_number
class Dormitory:
def __init__(self, name):
self.name = name
self.rooms = {}
def add_room(self, room_number, capacity):
self.rooms[room_number] = [None] * capacity
def add_student(self, student):
for room_number, students in self.rooms.items():
if None in students:
index = students.index(None)
students[index] = student
student.room_number = room_number
return True
return False
def remove_student(self, student_name):
for students in self.rooms.values():
for i, student in enumerate(students):
if student and student.name == student_name:
students[i] = None
return True
return False
def get_students_by_gender(self, gender):
students = []
for students_in_room in self.rooms.values():
for student in students_in_room:
if student and student.gender == gender:
students.append(student)
return students
```
使用示例:
```python
# 创建一个宿舍
dorm = Dormitory("A")
# 添加几个房间
dorm.add_room(101, 2)
dorm.add_room(102, 3)
# 添加学生
dorm.add_student(Student("张三", "男", None))
dorm.add_student(Student("李四", "女", None))
dorm.add_student(Student("王五", "男", None))
dorm.add_student(Student("赵六", "女", None))
dorm.add_student(Student("钱七", "男", None))
# 查看每个房间的学生
print(dorm.rooms)
# 根据学生姓名删除学生
dorm.remove_student("张三")
print(dorm.rooms)
# 根据性别获取学生列表
male_students = dorm.get_students_by_gender("男")
female_students = dorm.get_students_by_gender("女")
print("男生列表:", [s.name for s in male_students])
print("女生列表:", [s.name for s in female_students])
```
输出结果:
```
{101: [<__main__.Student object at 0x7f4aae534dc0>, <__main__.Student object at 0x7f4aae534f70>], 102: [<__main__.Student object at 0x7f4aae534910>, <__main__.Student object at 0x7f4aae534be0>, <__main__.Student object at 0x7f4aae534c40>]}
{101: [None, <__main__.Student object at 0x7f4aae534f70>], 102: [<__main__.Student object at 0x7f4aae534910>, <__main__.Student object at 0x7f4aae534be0>, <__main__.Student object at 0x7f4aae534c40>]}
男生列表: ['王五', '钱七']
女生列表: ['李四', '赵六']
```
相关推荐
![](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)