- import java.util.ArrayList;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- ArrayList
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" -> LookStudent(list);
- case "5" -> {
- System.out.println("退出!");
- break loop;
- }
- default -> System.out.println("没有这个选项!");
- }
- }
- }
-
- // 添加学生
- public static void AddStudent(ArrayList
list) { - System.out.println("添加学生!");
- Student s = new Student();
- Scanner sc = new Scanner(System.in);
- System.out.println("请录入id:");
- while (true) {
- String id = sc.next();
- boolean soleIds = soleId(list, id);
- if (soleIds) {
- System.out.println("该id已经存在!");
- } else {
- 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);
- }
-
- //删除学生的信息
- public static void DeleteStudent(ArrayList
list) { - System.out.println("请输入要删除的学生id:");
- Scanner sc = new Scanner(System.in);
- String id = sc.next();
- // 查询有没有这个学生
- int indexValue = getIndex(list, id);
- if (indexValue >= 0) {
- list.remove(indexValue);
- System.out.println("删除成功!");
- } else {
- System.out.println("该id不存在!");
- }
- }
-
- //修改学生的信息
- public static void UpdateStudent(ArrayList
list) { - System.out.println("请输入要修改的学生id:");
- Scanner sc = new Scanner(System.in);
- String id = sc.next();
- // 查询有没有这个学生
- Boolean values = soleId(list, id);
- int indexValue = getIndex(list, id);
- if (values) {
- Student stu = list.get(indexValue);
- System.out.println("请输入要修改的姓名:");
- String name = sc.next();
- stu.setName(name);
- System.out.println("请输入要修改的年龄:");
- int age = sc.nextInt();
- stu.setAge(age);
- System.out.println("请输入要修改的家庭住址:");
- String address = sc.next();
- stu.setAddress(address);
- System.out.println("修改成功!");
- } else {
- System.out.println("该id不存在!");
- }
- }
-
- //查询学生的信息
- public static void LookStudent(ArrayList
list) { - //如果没有学生
- if (list.isEmpty()) {
- System.out.println("当前暂无学生,请先添加后再查询!");
- return;
- }
- System.out.println("id\t姓名\t年龄\t家庭住址");
- for (int i = 0; i < list.size(); i++) {
- Student stu = list.get(i);
- System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
- }
- }
-
- //判断id是否唯一
- public static Boolean soleId(ArrayList
list, String id) { - for (int i = 0; i < list.size(); i++) {
- Student stu = list.get(i);
- String ids = stu.getId();
- if (ids.equals(id)) {
- return true;
- }
- }
- return false;
- }
-
- //获取集合中的索引
- public static int getIndex(ArrayList
list, String id) { - for (int i = 0; i < list.size(); i++) {
- Student stu = list.get(i);
- String ids = stu.getId();
- if (ids.equals(id)) {
- return i;
- }
- }
- return -1;
- }
- }
- 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;
- }
-
- /**
- * 获取
- * @return id
- */
- public String getId() {
- return id;
- }
-
- /**
- * 设置
- * @param id
- */
- public void setId(String id) {
- this.id = id;
- }
-
- /**
- * 获取
- * @return name
- */
- public String getName() {
- return name;
- }
-
- /**
- * 设置
- * @param name
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * 获取
- * @return age
- */
- public int getAge() {
- return age;
- }
-
- /**
- * 设置
- * @param age
- */
- public void setAge(int age) {
- this.age = age;
- }
-
- /**
- * 获取
- * @return address
- */
- public String getAddress() {
- return address;
- }
-
- /**
- * 设置
- * @param address
- */
- public void setAddress(String address) {
- this.address = address;
- }
-
- }