importjava.util.ArrayList;importjava.util.Scanner;publicclassStudentManager{//1.主界面publicstaticvoidmain(String[] args){//1.1创建ArrayList集合,存储Student对象ArrayList<Student> list =newArrayList<>();Scanner sc =newScanner(System.in);//1.2主界面可以一直重复使用,while死循环while(true){System.out.println("========欢迎使用学生管理系统========");System.out.println("请选择以下功能:\r\n1.添加学生信息\t2.删除学生信息\t3.修改学生信息\t4.查询学生信息\t5.退出学生管理系统");String choice = sc.next();//1.3根据choice执行不同的分支功能,使用switchswitch(choice){case"1":addStudent(list);break;case"2":deleteStudent(list);break;case"3":updateStudent(list);break;case"4":findStudent(list);break;case"5":System.out.println("感谢使用本系统,欢迎下次使用!");return;default:System.out.println("您的选择有误,请检查后重新输入!");}}}
3、添加学生的代码编写思路:
1、用键盘录入选择添加学生
2、定义一个方法,用于添加学生:
1、显示提示信息,提示要输入何种信息
2、键盘录入学生对象所需的数据
3、创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
4、将学生对象添加到集合中
5、给出添加成功的提示
publicstaticvoidaddStudent(ArrayList<Student> list){System.out.println("=========添加学生信息=========");Scanner sc =newScanner(System.in);String id;//2.开始录入学生信息,解决学号重复的问题,利用死循环不断让用户输入学号while(true){System.out.println("请输入学生学号:");
id = sc.next();//查询输入的id的索引int index =getIndex(list, id);//判断index是否是-1if(index ==-1){//说明这个id不存在,可以添加!结束循环break;}else{//说明这个id已经存在,得重新输入System.out.println("抱歉,您输入的id已被占用,请重新输入!");}}System.out.println("请输入学生的姓名:");String name = sc.next();System.out.println("请输入学生的年龄:");int age = sc.nextInt();System.out.println("请输入学生的生日:");String birthday = sc.next();//创建学生对象,封装录入学生信息Student stu =newStudent(id, name, age, birthday);//存入集合
list.add(stu);System.out.println("添加成功");}
3.13.查询指定学号在集合中的索引:将此方法的返回值调用于其他方法
publicstaticintgetIndex(ArrayList<Student> list,String id){//3.查询指定学号在集合中的索引/*
明确参数:ArrayList<stdunt>list,String id
明确返回值:int
*/for(int i =0; i < list.size(); i++){//取出i索引位置的学生对象Student stu = list.get(i);//获取stu对象的id和方法传入的id比较内容是否相同if(id.equals(stu.getId())){//成立说明找到了,返回索引ireturn i;}}//如果循环结束还没找到,方法还没结束,没有找到返回-1return-1;}
publicstaticvoidfindStudent(ArrayList<Student> list){//4.查询学生添加的信息/**
* 明确参数:ArrayList<Student>list
* 无返回值
*/System.out.println("=========查询所有学生信息========");//首先判断集合是否为空if(list.size()==0){System.out.println("该系统还未录入学生信息,请添加后重试!");return;}//打印表头System.out.println("学号\t\t姓名\t\t年龄\t\t生日");//遍历数组,获取集合索引i位置的学生对象for(int i =0; i < list.size(); i++){Student stu = list.get(i);//获取学生对象的各项信息String id = stu.getId();String name = stu.getName();int age = stu.getAge();String birthday = stu.getBirthday();//安装表头的格式打印数据System.out.println(id +"\t\t"+ name +"\t"+ age +"\t\t"+ birthday);}}
publicstaticvoiddeleteStudent(ArrayList<Student>list){System.out.println("=========删除学生系统=========");Scanner sc =newScanner(System.in);System.out.println("请输入学生的学号:");String id = sc.next();int index =findStudent(list, id);if(index==-1){System.out.println("该学号还未录入,请添加后再来操作!");return;}list.remove(index);System.out.println("删除成功!");}
6、修改学生信息的代码思路:
1、键盘录入想要修改的学生信息2、调用方法集合索引返回值,判断该学号是否存在3,进行学生信息修改
publicstaticvoidupdateStudent(ArrayList<Student> list){System.out.println("==========修改学生信息===========");Scanner sc =newScanner(System.in);System.out.println("请输入学生的学号:");String id = sc.next();int index =findStudent(list, id);if(index ==-1){System.out.println("该学号还未录入,请添加后再来操作!");return;}System.out.println("请输入学生的姓名:");String name = sc.next();System.out.println("请输入学生的年龄:");int age = sc.nextInt();System.out.println("请输入学生的生日:");String birthday = sc.next();Student stu =newStudent(id, name, age, birthday);
list.set(index, stu);System.out.println("修改成功!");}}