用java编写一个学生宿舍管理系统
时间: 2023-06-19 16:09:52 浏览: 102
好的,下面是一个简单的学生宿舍管理系统的Java代码示例:
```java
import java.util.Scanner;
public class DormitoryManagementSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Dormitory[] dormitories = new Dormitory[3];
Student[] students = new Student[5];
initializeDormitories(dormitories);
initializeStudents(students);
int choice = 0;
do {
System.out.println("1. 查询宿舍信息");
System.out.println("2. 查询学生信息");
System.out.println("3. 分配宿舍");
System.out.println("4. 退宿");
System.out.println("5. 退出系统");
System.out.print("请选择操作:");
choice = input.nextInt();
switch (choice) {
case 1:
displayDormitoryInformation(dormitories);
break;
case 2:
displayStudentInformation(students);
break;
case 3:
assignDormitory(students, dormitories);
break;
case 4:
checkoutDormitory(students, dormitories);
break;
case 5:
System.out.println("谢谢使用!");
break;
default:
System.out.println("输入无效,请重新输入!");
break;
}
} while (choice != 5);
}
private static void initializeDormitories(Dormitory[] dormitories) {
dormitories[0] = new Dormitory("A001", "男生", 4, 2);
dormitories[1] = new Dormitory("A002", "女生", 4, 2);
dormitories[2] = new Dormitory("A003", "男生", 6, 3);
}
private static void initializeStudents(Student[] students) {
students[0] = new Student("张三", "男", "计算机科学与技术", "A001", 101);
students[1] = new Student("李四", "女", "信息管理", "A001", 102);
students[2] = new Student("王五", "男", "电子工程", "A002", 201);
students[3] = new Student("赵六", "女", "市场营销", "A002", 202);
students[4] = new Student("钱七", "男", "软件工程", null, 0);
}
private static void displayDormitoryInformation(Dormitory[] dormitories) {
System.out.println("宿舍编号\t宿舍类型\t可住人数\t已住人数\t空闲床位");
for (Dormitory dormitory : dormitories) {
System.out.printf("%s\t%s\t%d\t%d\t%d\n",
dormitory.getDormitoryNumber(),
dormitory.getDormitoryType(),
dormitory.getCapacity(),
dormitory.getOccupancy(),
dormitory.getVacancy());
}
}
private static void displayStudentInformation(Student[] students) {
System.out.println("姓名\t性别\t专业\t宿舍编号\t床位号");
for (Student student : students) {
System.out.printf("%s\t%s\t%s\t%s\t%d\n",
student.getName(),
student.getGender(),
student.getMajor(),
student.getDormitoryNumber() != null ? student.getDormitoryNumber() : "",
student.getBedNumber());
}
}
private static void assignDormitory(Student[] students, Dormitory[] dormitories) {
Scanner input = new Scanner(System.in);
System.out.print("请输入学生姓名:");
String name = input.nextLine();
System.out.print("请输入学生性别:");
String gender = input.nextLine();
System.out.print("请输入学生专业:");
String major = input.nextLine();
System.out.print("请输入宿舍类型(男生/女生):");
String dormitoryType = input.nextLine();
Dormitory dormitory = findVacantDormitory(dormitories, dormitoryType);
if (dormitory != null) {
int bedNumber = dormitory.allocateBed();
if (bedNumber > 0) {
Student student = new Student(name, gender, major, dormitory.getDormitoryNumber(), bedNumber);
addStudent(students, student);
System.out.printf("为学生 %s 分配了 %s 宿舍的床位 %d\n", name, dormitoryType, bedNumber);
} else {
System.out.printf("%s 宿舍已无空闲床位!\n", dormitoryType);
}
} else {
System.out.printf("没有空闲的 %s 宿舍!\n", dormitoryType);
}
}
private static Dormitory findVacantDormitory(Dormitory[] dormitories, String dormitoryType) {
for (Dormitory dormitory : dormitories) {
if (dormitory.getDormitoryType().equals(dormitoryType) && dormitory.getVacancy() > 0) {
return dormitory;
}
}
return null;
}
private static void addStudent(Student[] students, Student student) {
for (int i = 0; i < students.length; i++) {
if (students[i] == null) {
students[i] = student;
break;
}
}
}
private static void checkoutDormitory(Student[] students, Dormitory[] dormitories) {
Scanner input = new Scanner(System.in);
System.out.print("请输入学生姓名:");
String name = input.nextLine();
boolean found = false;
for (Student student : students) {
if (student != null && student.getName().equals(name)) {
found = true;
Dormitory dormitory = findDormitory(dormitories, student.getDormitoryNumber());
if (dormitory != null) {
dormitory.deallocateBed(student.getBedNumber());
removeStudent(students, student);
System.out.printf("学生 %s 退宿成功!\n", name);
}
break;
}
}
if (!found) {
System.out.printf("未找到姓名为 %s 的学生!\n", name);
}
}
private static Dormitory findDormitory(Dormitory[] dormitories, String dormitoryNumber) {
for (Dormitory dormitory : dormitories) {
if (dormitory != null && dormitory.getDormitoryNumber().equals(dormitoryNumber)) {
return dormitory;
}
}
return null;
}
private static void removeStudent(Student[] students, Student student) {
for (int i = 0; i < students.length; i++) {
if (students[i] != null && students[i].equals(student)) {
students[i] = null;
break;
}
}
}
}
class Dormitory {
private String dormitoryNumber;
private String dormitoryType;
private int capacity;
private int occupancy;
private int[] beds;
public Dormitory(String dormitoryNumber, String dormitoryType, int capacity, int occupancy) {
this.dormitoryNumber = dormitoryNumber;
this.dormitoryType = dormitoryType;
this.capacity = capacity;
this.occupancy = occupancy;
阅读全文