java:设计一个学生宿舍管理
时间: 2024-09-11 10:12:55 浏览: 38
Java毕业设计:学生宿舍管理系统源码.zip
5星 · 资源好评率100%
在Java中设计一个学生宿舍管理系统,可以分为以下几个步骤:
1. **实体类**(如StudentRoom.java):
- 宿舍楼(Building)
- 学生房间(Room)
- 房间编号(roomNumber)
- 学生列表(studentList)
- 学生(Student)
- 姓名(name)
- 房间号(roomId)
```java
public class Building {
private String name;
List<Room> rooms;
// getters and setters
}
public class Room {
private int roomNumber;
private List<Student> students;
public Room(int roomNumber) {
this.roomNumber = roomNumber;
}
// getters and setters
public void addStudent(Student student) {
students.add(student);
}
}
public class Student {
private String name;
private int roomId;
public Student(String name, int roomId) {
this.name = name;
this.roomId = roomId;
}
// getters and setters
}
```
2. **管理类**(如ResidenceManager.java):
- 添加/删除学生房间、查看信息等操作
```java
public class ResidenceManager {
private Building building;
public ResidenceManager() {
building = new Building();
}
public void addStudentToRoom(Student student, int roomId) {
Room room = getRoom(roomId);
room.addStudent(student);
}
private Room getRoom(int roomId) {
return building.getRooms().stream()
.filter(Room::getRoomNumber).findAny()
.orElseThrow(() -> new IllegalArgumentException("Invalid room number"));
}
// 其他管理方法...
}
```
3. **主函数**(main method):
- 创建实例并进行操作展示系统功能
```java
public static void main(String[] args) {
ResidenceManager manager = new ResidenceManager();
Student student1 = new Student("张三", 101);
// ...其他学生
manager.addStudentToRoom(student1, 101);
// 查看宿舍信息或进行其他管理操作
}
```
阅读全文