学生宿舍管理系统,功能拥有姓名查询,学号查询,寝室号查询Java代码
时间: 2024-05-13 18:16:29 浏览: 74
以下是一个简单的学生宿舍管理系统的 Java 代码。其中包括姓名查询、学号查询和寝室号查询功能。
```
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDormitoryManagementSystem {
static Scanner input = new Scanner(System.in);
static ArrayList<Student> students = new ArrayList<Student>();
public static void main(String[] args) {
// 添加一些测试数据
students.add(new Student("张三", "1701010101", "101"));
students.add(new Student("李四", "1701010102", "101"));
students.add(new Student("王五", "1701010103", "102"));
students.add(new Student("赵六", "1701010104", "102"));
while (true) {
System.out.println("请选择要进行的操作:");
System.out.println("1. 姓名查询");
System.out.println("2. 学号查询");
System.out.println("3. 寝室号查询");
System.out.println("4. 退出程序");
int choice = input.nextInt();
switch (choice) {
case 1:
searchByName();
break;
case 2:
searchByNumber();
break;
case 3:
searchByDormitory();
break;
case 4:
System.exit(0);
default:
System.out.println("无效的选择,请重新选择。");
break;
}
}
}
public static void searchByName() {
System.out.print("请输入学生姓名:");
String name = input.next();
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student.toString());
found = true;
}
}
if (!found) {
System.out.println("未找到该学生。");
}
}
public static void searchByNumber() {
System.out.print("请输入学生学号:");
String number = input.next();
boolean found = false;
for (Student student : students) {
if (student.getNumber().equals(number)) {
System.out.println(student.toString());
found = true;
}
}
if (!found) {
System.out.println("未找到该学生。");
}
}
public static void searchByDormitory() {
System.out.print("请输入寝室号:");
String dormitory = input.next();
boolean found = false;
for (Student student : students) {
if (student.getDormitory().equals(dormitory)) {
System.out.println(student.toString());
found = true;
}
}
if (!found) {
System.out.println("未找到该寝室的学生。");
}
}
}
class Student {
private String name;
private String number;
private String dormitory;
public Student(String name, String number, String dormitory) {
this.name = name;
this.number = number;
this.dormitory = dormitory;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
public String getDormitory() {
return dormitory;
}
public String toString() {
return "姓名:" + name + " 学号:" + number + " 寝室号:" + dormitory;
}
}
```
这个代码使用了 ArrayList 存储学生信息,并提供了三个查询功能。在 main 方法中,通过一个死循环来让用户不断选择要进行的操作。选择后,通过一个 switch 语句来调用相应的查询方法。每个查询方法都会依次遍历学生信息列表,并找到符合条件的学生信息后输出。如果找不到符合条件的学生,会输出相应的提示信息。
阅读全文