学生宿舍管理系统的基础代码
时间: 2023-09-02 14:08:49 浏览: 43
以下是一个简单的学生宿舍管理系统的基础代码,你可以根据自己的需求进行修改和完善:
```python
class Student:
def __init__(self, name, age, gender, room):
self.name = name
self.age = age
self.gender = gender
self.room = room
class Dorm:
def __init__(self, name, address, capacity):
self.name = name
self.address = address
self.capacity = capacity
self.students = []
def add_student(self, student):
if len(self.students) < self.capacity:
self.students.append(student)
print(f"{student.name} has been added to {self.name}")
else:
print(f"{self.name} is already full")
def remove_student(self, student):
if student in self.students:
self.students.remove(student)
print(f"{student.name} has been removed from {self.name}")
else:
print(f"{student.name} is not in {self.name}")
def get_students(self):
return self.students
# Example usage:
# Create students
s1 = Student("Alice", 20, "Female", "101")
s2 = Student("Bob", 19, "Male", "102")
s3 = Student("Carol", 21, "Female", "103")
# Create dorms
d1 = Dorm("Dorm A", "123 Main St.", 3)
d2 = Dorm("Dorm B", "456 Oak St.", 2)
# Add students to dorms
d1.add_student(s1)
d1.add_student(s2)
d1.add_student(s3)
d2.add_student(s1)
d2.add_student(s2)
# Remove a student from a dorm
d1.remove_student(s3)
# Get all students in a dorm
print(d1.get_students())
```
以上代码实现了学生宿舍管理系统的基本功能,包括添加、删除学生以及获取宿舍中所有学生的信息。你可以根据需要进行修改和完善,例如添加更多的属性和方法,实现更复杂的功能,如查询宿舍中某个年龄段的学生等。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)