package com.chn.upgrade; import java.util.ArrayList; import java.util.Scanner; public class ArrayListDemo8 { public static void main(String[] args) { ArrayList<Student> list=new ArrayList<>(); 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.退出"); Scanner sc=new Scanner(System.in); System.out.println("请选择:"); String choose = sc.next(); switch(choose){ case "1": addStudent(list);break; case "2": deleteStudent(list);break; case "3": modifyStudent(list);break; case "4": queryStudent(list);break; case "5": { System.out.println("退出"); System.exit(0); } default: System.out.println("没有此选项"); } } } //增加学生信息 public static void addStudent(ArrayList<Student> list){ Student student=new Student(); Scanner sc=new Scanner(System.in); while (true) { System.out.println("请输入学生id"); String sid = sc.next(); if(contains(list,sid)){ System.out.println("请重新录入"); }else{ //存入学生id student.setId(sid); break; } } System.out.println("请输入学生姓名"); String name = sc.next(); //存入学生姓名 student.setName(name); System.out.println("请输入学生年龄"); int age = sc.nextInt(); //存入学生年龄 student.setAge(age); System.out.println("请输入学生家庭住址"); String address = sc.next(); //存入学生地址 student.setAddress(address); //将学生对象存入学生集合 list.add(student); } //通过id来判断录入学生信息是否已在学生集合中 public static boolean contains(ArrayList<Student> list,String id){ // for (int i = 0; i < list.size(); i++) { // Student student = list.get(i); // String sid = student.getId(); // if(sid.equals(id)){ // return true; // } // } // return false; //此处可以简化代码 return getIndex(list,id)>=0; } //getIndex()方法实质是contains()方法的升级 public static int getIndex(ArrayList<Student> list,String id){ for (int i = 0; i < list.size(); i++) { Student student = list.get(i); String sid = student.getId(); if(sid.equals(id)){ return i; } } return -1; } //查询学生信息 public static void queryStudent(ArrayList<Student> list){ if(list.size()==0){ System.out.println("无信息"); return; } System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址\t"); //遍历输出所有学生信息 for (int i = 0; i < list.size(); i++) { Student student = list.get(i); System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t\t"+student.getAddress()); } } //删除学生信息 public static void deleteStudent(ArrayList<Student> list){ Scanner sc=new Scanner(System.in); //通过id来索引,进而删除 System.out.println("请输入要删除的学生id"); String id = sc.next(); int index = getIndex(list, id); if(index>=0){ list.remove(index); System.out.println("删除成功"); }else { System.out.println("删除失败"); } } //修改学生信息 public static void modifyStudent(ArrayList<Student> list){ Scanner sc=new Scanner(System.in); System.out.println("请输入要修改的学生id"); String newId = sc.next(); int index = getIndex(list, newId); if(index==-1){ System.out.println("学生不存在,请重新输入"); return; } Student student = list.get(index); student.setId(newId); System.out.println("请输入要修改学生的姓名"); String newName = sc.next(); student.setName(newName); System.out.println("请输入要修改学生的年龄"); int newAge = sc.nextInt(); student.setAge(newAge); System.out.println("请输入要修改学生的家庭地址"); String newAddress = sc.next(); student.setAddress(newAddress); System.out.println("学生信息修改成功"); } }
//未完待续