Student.java
public class Student {
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
StudentSystem.java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
loop:while (true){
Student stu=new Student();
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.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1":
addStudent(list);//添加学生
break;
case "2":
deleteStudent(list);//删除学生
break;
case "3":
updateStudent(list);//修改学生
break;
case "4":
queryStudent(list);//查询学生
break;
case "5":
System.out.println("退出");
break loop;
default:
System.out.println("没有这个选项");
}
}
}
//添加学生
public static void addStudent(ArrayList<Student> list){
Student stu=new Student();
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
stu.setId(sc.next());
boolean bo=true;
for (int i = 0; i < list.size(); i++) {
if (stu.getId().equals(list.get(i).getId())){
System.out.println("该学生id已存在,添加失败");
bo=false;
}
}
if (bo){
System.out.println("请输入学生姓名:");
stu.setName(sc.next());
System.out.println("请输入学生年龄:");
stu.setAge(sc.nextInt());
System.out.println("请输入学生地址:");
stu.setAddress(sc.next());
list.add(stu);
System.out.println("添加成功!");
}
}
//删除学生
public static void deleteStudent(ArrayList<Student> list){
System.out.println("请输入要删除的学生学号:");
Scanner sc=new Scanner(System.in);
String sid=sc.next();
boolean bo=true;
for (int i = 0; i < list.size(); i++) {
if (sid.equals(list.get(i).getId())){
list.remove(i);
System.out.println("删除成功!");
bo=false;
}
}
if (bo){
System.out.println("该学生不存在,删除失败!");
}
}
//修改学生
public static void updateStudent(ArrayList<Student> list){
System.out.println("请输入要修改的学生学号:");
Scanner sc=new Scanner(System.in);
String sid=sc.next();
boolean bo=true;
for (int i = 0; i < list.size(); i++) {
if (sid.equals(list.get(i).getId())){
System.out.println("请输入修改后的学号:");
list.get(i).setId(sc.next());
System.out.println("请输入修改后的姓名:");
list.get(i).setName(sc.next());
System.out.println("请输入修改后的年龄:");
list.get(i).setAge(sc.nextInt());
System.out.println("请输入修改后的地址:");
list.get(i).setAddress(sc.next());
System.out.println("修改成功!");
bo=false;
}
}
if (bo){
System.out.println("该学生不存在,修改失败!");
}
}
//查询学生
public static void queryStudent(ArrayList<Student> list){
Scanner sc=new Scanner(System.in);
System.out.println("请输入要查询的学生学号:");
String sid=sc.next();
boolean bo=true;
for (int i = 0; i < list.size(); i++) {
if (sid.equals(list.get(i).getId())){
System.out.println(
"学号:"+list.get(i).getId()+"\n"+
"姓名:"+list.get(i).getName()+"\n"+
"年龄:"+list.get(i).getAge()+"\n"+
"地址:"+list.get(i).getAddress()
);
bo=false;
}
}
if (bo){
System.out.println("该学生不存在,查询失败!");
}
}
}