学生管理系统(包含登录)
无删减
建立对象类
创建student1(可改为student)
public class Student1 {
private String id;
private String name;
private int age;
private String address;
public Student1() {
}
public Student1(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
建立Array集合 和学生管理界面的选择
public class StudentSystem {
public static void startStudentSystem() {
ArrayList<Student1> list = new ArrayList<>();
loop:
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.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1" -> addStudent(list);
case "2" -> deleteStudent(list);
case "3" -> updateStudent(list);
case "4" -> queryStudent(list);
case "5" -> {
System.out.println("退出");
System.exit(0);
}
default -> System.out.println("没有这个选项");
}
}
}
添加学生方法addStudent(都放在StudentSystem里)
//添加学生
public static void addStudent(ArrayList<Student1> list) {
//利用空参构造先创建学生对象
Student1 s = new Student1();
Scanner sc = new Scanner(System.in);
String id = null;
while (true) {
System.out.println("请输入学生的id");
id = sc.next();
boolean flag = contains(list, id);
if (flag) {
//表示id已存在,需要重新录入
System.out.println("id已经存在,请重新录入");
} else {
//表示id不存在,表示可以使用
s.setId(id);
break;
}
}
System.out.println("请输入学生的姓名");
String name = sc.next();
s.setName(name);
System.out.println("请输入学生的年龄");
int age = sc.nextInt();
s.setAge(age);
System.out.println("请输入学生的家庭住址");
String address = sc.next();
s.setAddress(address);
//把学生对象添加到集合当中
list.add(s);
//提示用户
System.out.println("学生信息添加成功!");
}
删除方法deleteStudent(都放在StudentSystem里)
//删除学生
public static void deleteStudent(ArrayList<Student1> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的id");
String id = sc.next();
//查询id在集合中的索引
int index = getIndex(list, id);
//对index进行判断
//如果-1,就表示不存在,结束方法,回到初始菜单
if (index >= 0) {
//如果大于等于0的,表示存在,直接删除
list.remove(index);
System.out.println("id为: " + id + "的学生删除成功");
} else {
System.out.println("id不存在就,删除失败