123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package J20250715.studentManager;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * @author WanJl
- * @version 1.0
- * @title StudentManager
- * @description
- * @create 2025/7/15
- */
- public class StudentManagerSystem {
- //添加学生到系统
- /**
- * 向list集合中添加学生对象
- * @param list
- */
- public static void addStudent(ArrayList<Student> list){
- System.out.println("添加学生信息...");
- Scanner sc=new Scanner(System.in);
- //判断学号是否重复,如果不重复才可以输入
- System.out.println("请输入学号:");
- String sid=sc.nextLine();
- System.out.println("请输入姓名:");
- String name=sc.nextLine();
- System.out.println("请输入年龄:");
- int age=sc.nextInt();
- System.out.println("请输入生日:");
- String birthday=sc.next();
- //创建学生对象
- Student student=new Student(sid,name,age,birthday);
- //把学生对象存入ArrayList集合对象
- list.add(student);
- System.out.println("添加学生成功");
- }
- //根据学号修改学生信息
- public static void updateStudent(ArrayList<Student> list){
- //获取键盘输入
- Scanner sc=new Scanner(System.in);
- System.out.println("请输入要修改学生的学号:");
- //先根据学号,找到这个学生对象元素在集合的位置索引值下标。
- String sid = sc.next();
- int index = getIndex(sid, list);//找到索引值
- //判断 index如果是-1,说明没有这个学生
- if (index==-1){
- System.out.println("查无此人,请重新输入学号");
- }else {
- System.out.println("请输入新的学生姓名:");
- String name = sc.next();
- System.out.println("请输入新的学生年龄:");
- int age = sc.nextInt();
- System.out.println("请输入新的学生生日:");
- String birthday = sc.next();
- Student s=new Student(sid,name,age,birthday);
- //调用list的修改元素的方法
- //list.set(集合的索引值,学生对象);
- list.set(index,s); //找到集合中index位置,将该元素替换为s
- System.out.println("修改学生成功");
- }
- }
- //根据学号删除学生信息
- //查询学生列表信息
- public static void queryStudentList(ArrayList<Student> list){
- //判空
- if(list.size()==0){
- System.out.println("没有学生信息,请先添加,后查询");
- return;
- }
- System.out.println("学号\t\t姓名\t年龄\t生日");
- //遍历集合
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- System.out.println(student);
- }
- }
- //根据id获取学生对象在集合中的索引值的方法
- public static int getIndex(String sid,ArrayList<Student> list){
- //索引值初始化为-1
- int index=-1;
- //遍历集合
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- String studentSid = student.getSid();
- if (sid.equals(studentSid)){ //id比较相等
- //说明存在这个学生,把索引值赋值给index
- index=i;
- }
- }
- return index;
- }
- }
|