用java写一个控制台表现的简易宿舍管理系统
时间: 2024-03-20 07:42:01 浏览: 60
好的,下面是一个简单的宿舍管理系统的Java代码,它可以实现添加、修改、删除和查询学生信息的功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class DormitoryManagementSystem {
static ArrayList<Student> studentList = new ArrayList<>();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("欢迎使用宿舍管理系统");
System.out.println("1. 添加学生信息");
System.out.println("2. 修改学生信息");
System.out.println("3. 删除学生信息");
System.out.println("4. 查询学生信息");
System.out.println("5. 退出");
System.out.print("请选择操作:");
int option = input.nextInt();
switch (option) {
case 1:
addStudent();
break;
case 2:
updateStudent();
break;
case 3:
deleteStudent();
break;
case 4:
queryStudent();
break;
case 5:
System.out.println("谢谢使用!");
return;
default:
System.out.println("无效的选项,请重新选择!");
}
}
}
public static void addStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入学号:");
String id = input.next();
System.out.print("请输入姓名:");
String name = input.next();
System.out.print("请输入性别(男/女):");
String gender = input.next();
System.out.print("请输入年龄:");
int age = input.nextInt();
System.out.print("请输入宿舍号:");
String dormitory = input.next();
Student student = new Student(id, name, gender, age, dormitory);
studentList.add(student);
System.out.println("添加成功!");
}
public static void updateStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
String id = input.next();
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
System.out.print("请输入新的姓名(原姓名为 " + student.getName() + "):");
String name = input.next();
System.out.print("请输入新的性别(原性别为 " + student.getGender() + "):");
String gender = input.next();
System.out.print("请输入新的年龄(原年龄为 " + student.getAge() + "):");
int age = input.nextInt();
System.out.print("请输入新的宿舍号(原宿舍号为 " + student.getDormitory() + "):");
String dormitory = input.next();
student.setName(name);
student.setGender(gender);
student.setAge(age);
student.setDormitory(dormitory);
System.out.println("修改成功!");
return;
}
}
System.out.println("该学生不存在!");
}
public static void deleteStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
String id = input.next();
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
studentList.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("该学生不存在!");
}
public static void queryStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要查询的学生学号:");
String id = input.next();
for (Student student : studentList) {
if (student.getId().equals(id)) {
System.out.println("学号\t姓名\t性别\t年龄\t宿舍号");
System.out.println(student);
return;
}
}
System.out.println("该学生不存在!");
}
}
class Student {
private String id;
private String name;
private String gender;
private int age;
private String dormitory;
public Student(String id, String name, String gender, int age, String dormitory) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.dormitory = dormitory;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDormitory() {
return dormitory;
}
public void setDormitory(String dormitory) {
this.dormitory = dormitory;
}
@Override
public String toString() {
return id + "\t" + name + "\t" + gender + "\t" + age + "\t" + dormitory;
}
}
```
以上就是一个简易的宿舍管理系统的Java代码,可以用控制台进行交互操作。
阅读全文