初学java 学生管理系统——v0002版本

续上一篇

上一篇也就简单的把界面打印到了控制台

同时把学生的class写好了

现在开始写功能实现

由于是刚学习java,所以数据的存储暂时只能使用集合变量存储

开始写第一个方法——添加学生信息

 1     // 添加学生的方法
 2     public static void addStudent(ArrayList<Student> list) {
 3         Scanner sc = new Scanner(System.in);
 4         // 1. 给出录入的提示信息
 5         System.out.println("请输入学号:");
 6         String sid = sc.next();
 7 
 8         System.out.println("请输入姓名:");
 9         String name = sc.next();
10 
11         System.out.println("请输入年龄:");
12         int age = sc.nextInt();
13 
14         System.out.println("请输入生日:");
15         String birthday = sc.next();
16 
17         // 2. 将键盘录入的信息封装为学生对象
18         Student stu = new Student(sid, name, age, birthday);
19         // 3. 将封装好的学生对象, 添加到集合容器当中
20         list.add(stu);
21         // 4. 给出添加成功的提示信息
22         System.out.println("添加成功!");
23     }

 

之后是查看学生的方法

 1     // 查看学生的方法
 2     public static void queryStudents(ArrayList<Student> list) {
 3         // 1. 判断集合中是否存在数据, 如果不存在直接给出提示
 4         if (list.size() == 0) {
 5             System.out.println("无信息, 请添加后重新查询");
 6             return;
 7         }
 8         // 2. 存在: 展示表头数据
 9         System.out.println("学号\t\t姓名\t年龄\t生日");
10         // 3. 遍历集合, 获取每一个学生对象的信息, 打印在控制台
11         for (int i = 0; i < list.size(); i++) {
12             Student stu = list.get(i);
13             System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
14         }
15     }

 

接下来是删除学生的方法

删除需要判断将要删除的信息是否在集合中

因此需要额外写一个方法,用于查找元素在集合中出现的位置

 

 1     /**
 2      * getIndex : 接收一个集合对象, 接收一个学生学号
 3      * 查找这个学号, 在集合中出现的索引位置    [stu1,stu2,stu3]
 4      */
 5     public static int getIndex(ArrayList<Student> list, String sid) {
 6         // 1. 假设传入的学号, 在集合中不存在
 7         int index = -1;
 8         // 2. 遍历集合, 获取每一个学生对象, 准备进行查找
 9         for (int i = 0; i < list.size(); i++) {
10             // 获取学生对象
11             Student stu = list.get(i);
12             // 3. 获取每一个学生对象的学号
13             String id = stu.getSid();
14             // 4. 使用获取出的学生学号, 和传入的学号(查找的学号)进行比对
15             if (id.equals(sid)) {
16                 // 存在: 让index变量记录正确的索引位置
17                 index = i;
18             }
19         }
20         return index;
21     }

 

 

 

 

 

接着写删除的方法

 1     // 删除学生的方法
 2     public static void deleteStudent(ArrayList<Student> list) {
 3         // 1. 给出提示信息 (请输入您要删除的学号)
 4         System.out.println("请输入您要删除的学生学号:");
 5         // 2. 键盘接收要删除的学号
 6         Scanner sc = new Scanner(System.in);
 7         String deleteSid = sc.next();
 8         
 9         // 3. 调用getIndex方法, 查找该学号在集合中出现的索引位置
10         int index = getIndex(list, deleteSid);
11         
12         // 4. 根据索引判断, 学号在集合中是否存在
13         if (index == -1) {
14             // 不存在: 给出提示
15             System.out.println("查无信息, 请重新输入");
16         } else {
17             // 存在:删除
18             list.remove(index);
19             System.out.println("删除成功!");
20         }
21     }

 

现在还差个修改学生的方法

 1     // 修改学生的方法
 2     public static void updateStudent(ArrayList<Student> list) {
 3         System.out.println("请输入您要修改的学生学号:");
 4         Scanner sc = new Scanner(System.in);
 5         String updateSid = sc.next();
 6         // 3. 调用getIndex方法, 查找该学号在集合中出现的索引位置
 7         int index = getIndex(list, updateSid);
 8         // 4. 根据索引判断, 学号在集合中是否存在
 9         if (index == -1) {
10             // 不存在: 给出提示
11             System.out.println("查无信息, 请重新输入");
12         } else {
13             // 存在: 接收新的学生信息
14             System.out.println("请输入新的学生姓名:");
15             String name = sc.next();
16             System.out.println("请输入新的学生年龄:");
17             int age = sc.nextInt();
18             System.out.println("请输入新的学生生日:");
19             String birthday = sc.next();
20             // 封装为新的学生对象
21             Student stu = new Student(updateSid, name, age, birthday);
22             // 调用集合的set方法, 完成修改
23             list.set(index, stu);
24             System.out.println("修改成功!");
25         }
26     }

 

到此基本算是写完了

该添加一点点小优化

首先是main方法中的switch语句记得修改下 加上方法的调用

 1             switch (choice) {
 2                 case "1":
 3                     // System.out.println("添加学生");
 4                     addStudent(list);
 5                     break;
 6                 case "2":
 7                     // System.out.println("删除学生");
 8                     deleteStudent(list);
 9                     break;
10                 case "3":
11                     // System.out.println("修改学生");
12                     updateStudent(list);
13                     break;
14                 case "4":
15                     // System.out.println("查看学生");
16                     queryStudent(list);
17                     break;
18                 case "5":
19                     System.out.println("感谢您的使用");
20                     break lo;
21                 default:
22                     System.out.println("您的输入有误");
23                     break;
24             }

 

其次是,在添加学生的信息时

  一般来说 学号是不能重复的

  所以,这里要增加一个限制条件

  修改后的方法如下

 1     // 添加学生的方法
 2     public static void addStudent(ArrayList<Student> list) {
 3         Scanner sc = new Scanner(System.in);
 4         // 1. 给出录入的提示信息
 5         String sid;
 6         while (true) {
 7             System.out.println("请输入学号:");
 8             sid = sc.next();
 9             int index = getIndex(list, sid);
10             if (index == -1) {
11                 // sid不存在, 学号可以使用
12                 break;
13             }
14         }
15 
16         System.out.println("请输入姓名:");
17         String name = sc.next();
18         System.out.println("请输入年龄:");
19         int age = sc.nextInt();
20         System.out.println("请输入生日:");
21         String birthday = sc.next();
22         // 2. 将键盘录入的信息封装为学生对象
23         Student stu = new Student(sid, name, age, birthday);
24         // 3. 将封装好的学生对象, 添加到集合容器当中
25         list.add(stu);
26         // 4. 给出添加成功的提示信息
27         System.out.println("添加成功!");
28     }

 

这个简易的学生管理系统就写完了

 

简单总结下思路

通过ArrayList来存储学生信息

学生的信息通过Student类进行封装

使用while死循环配合switch语句让用户选择功能菜单

每一个独立的功能写一个单独的方法,注意方法的参数都是ArrayList集合,都不需要返回值

 

posted @ 2020-09-29 10:11  Yao_xi  阅读(157)  评论(0编辑  收藏  举报