用python编写一个学生宿舍信息管理系统
时间: 2023-07-20 16:35:29 浏览: 111
好的,这是一个比较大的项目,我会给您提供一个基本的框架,您可以根据自己的需求和实际情况进行修改和完善。
首先,我们可以考虑创建一个Student类,用于存储学生的信息,例如学号、姓名、性别、年级、宿舍等。
```python
class Student:
def __init__(self, stu_id, name, gender, grade, dorm):
self.stu_id = stu_id
self.name = name
self.gender = gender
self.grade = grade
self.dorm = dorm
```
接下来,我们可以创建一个Dormitory类,用于存储宿舍的信息,例如宿舍号、可容纳人数、当前住户等。
```python
class Dormitory:
def __init__(self, dorm_num, capacity):
self.dorm_num = dorm_num
self.capacity = capacity
self.current_num = 0
self.students = []
def add_student(self, student):
if self.current_num < self.capacity:
self.students.append(student)
self.current_num += 1
return True
else:
return False
def remove_student(self, student):
if student in self.students:
self.students.remove(student)
self.current_num -= 1
return True
else:
return False
```
然后,我们可以创建一个DormitoryManager类,用于管理宿舍信息,例如添加宿舍、删除宿舍、修改宿舍容纳人数、添加学生到宿舍等。
```python
class DormitoryManager:
def __init__(self):
self.dormitories = []
def add_dormitory(self, dormitory):
self.dormitories.append(dormitory)
def remove_dormitory(self, dorm_num):
for dorm in self.dormitories:
if dorm.dorm_num == dorm_num:
self.dormitories.remove(dorm)
return True
return False
def modify_capacity(self, dorm_num, capacity):
for dorm in self.dormitories:
if dorm.dorm_num == dorm_num:
dorm.capacity = capacity
return True
return False
def add_student_to_dorm(self, dorm_num, student):
for dorm in self.dormitories:
if dorm.dorm_num == dorm_num:
if dorm.add_student(student):
return True
else:
return False
return False
def remove_student_from_dorm(self, dorm_num, student):
for dorm in self.dormitories:
if dorm.dorm_num == dorm_num:
if dorm.remove_student(student):
return True
else:
return False
return False
```
最后,我们可以创建一个StudentManager类,用于管理学生信息,例如添加学生、删除学生、修改学生信息、查询学生宿舍等。
```python
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, stu_id):
for student in self.students:
if student.stu_id == stu_id:
self.students.remove(student)
return True
return False
def modify_info(self, stu_id, name=None, gender=None, grade=None):
for student in self.students:
if student.stu_id == stu_id:
if name:
student.name = name
if gender:
student.gender = gender
if grade:
student.grade = grade
return True
return False
def query_dorm(self, stu_id):
for student in self.students:
if student.stu_id == stu_id:
return student.dorm
return None
```
以上是一个简单的学生宿舍信息管理系统的框架,您可以根据需求进行修改和完善。
阅读全文