项目练习之利用Arraylist 实现学生管理系统(查询,添加,删除,修改)

package XianMu;


import java.util.ArrayList;
import java.util.Scanner;

public class StudentManger {
    //

    public static void main(String[] args) {

        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 sh = sc.nextLine();
            switch (sh) {
                case "1":
                    showStudent(array);
                    break;
                case "2":
                    addStudent(array);
                    break;
                case "3":
                    deleteStudent(array);
                    break;
                case "4":
                    updateStudent(array);
                    break;
                case "5":
                default:
                    System.out.println("谢谢使用");
                    System.exit(0);
                    break;
            }
        }

    }
    static ArrayList array=new ArrayList <>();
    //查看学生
    public static void showStudent(ArrayList array){
        //首先判断这个集合是否有数据,并且让方法停止
        if (array.size()==0){
            System.out.println("不好意思目前木有学生");
            return;
        }
        System.out.println("名字\t学号\t年龄\t地址");
        for (int i = 0; i < array.size(); i++) {
            Student student=array.get(i);
            System.out.println(student.getName()+"\t\t"+student.getId()+"\t\t"+student.getAge()+"\t\t"+student.getAddress());
        }
    }
    public static void addStudent(ArrayList array){
       Scanner sc=new Scanner(System.in);
       Student stu=new Student();
       String id;
       while(true) {
           System.out.println("学号");
           id = sc.nextLine();
//        stu.setId(sc.nextLine());

           //判断学号是否被占用
           boolean flag = false;
           for (int i = 0; i < array.size(); i++) {
               if (array.get(i).getId().equals(id)) {
                   flag = true;   //说明学号被占用
               }
           }
           if (flag == true) {
               System.out.println("已经有学号了请重新输入");
           }
           else {
               break;
           }
       }
        stu.setId(id);
        System.out.println("名字");
        stu.setName(sc.nextLine());
        System.out.println("年龄");
        stu.setAge(sc.nextLine());
        System.out.println("地址");
        stu.setAddress(sc.nextLine());
        array.add(stu);
        System.out.println("添加成功");
    }
    public static void deleteStudent(ArrayList array){
        Scanner sc=new Scanner(System.in);

         while (true) {
             System.out.println("请输入要删除的学生的学号");
             String id = sc.nextLine();
             boolean flag = false;
             for (int i = 0; i < array.size(); i++) {
                 if (array.get(i).getId().equals(id)) {
                     array.remove(i);
                     flag = true;
                     System.out.println("删除成功");
                     break;
                 }
             }
             if (flag==false) {
                 System.out.println("系统中木有这个学生请重写输入");
             }
             else {break;}
         }

    }
    public static void updateStudent(ArrayList array){
        Scanner sc=new Scanner(System.in);
        while (true) {
            System.out.println("请输入要修改的学生的学号");
            String id = sc.nextLine();
            boolean flag = false;
            for (int i = 0; i < array.size(); i++) {
                if (array.get(i).getId().equals(id)) {
                    array.remove(i);
                    addStudent(array);
                    flag = true;
                    System.out.println("修改成功");
                    break;
                }
            }
            if (flag==false) {
                System.out.println("系统中木有这个学生请重写输入");
            }
            else {break;}
        }
    }

}

学生对象类

package XianMu;

public class Student {
    private String name;
    private String id;
    private String age;
    private String address;

    public Student() {
    }

    public Student(String name, String id, String age, String address) {
        this.name = name;
        this.id = id;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

你可能感兴趣的:(项目练习之利用Arraylist 实现学生管理系统(查询,添加,删除,修改))