练习ArrayList的使用
学生管理系统(练习)
package com.itheima_02.studentsystem;

public class Student {
    private String sid;//学号
    private String name;//姓名
    private String age;//年龄
    private String address;//居住地

    public Student() {
    }

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

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

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

    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;
    }
}

package com.itheima_02.studentsystem;

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

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean flag = false;
        ArrayList<Student> list = new ArrayList<Student>();

        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("请输入你的选择:");
            String x = sc.nextLine();
            switch (x) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    findAllStudent(list);
                    break;
                case "5":
                    System.out.println("谢谢使用!欢迎下次光临!");
                    System.exit(0);
                    // flag = true;
                    break;
                default:
                    System.out.println("请输入正确指令!");
                    break;

            }
//            if (flag){
//                System.out.println("谢谢使用!欢迎下次光临!");
//                break;
//            }
        }
        //   sc.close();
    }

    //添加学生信息
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        String sid;
        while (true) {
            System.out.println("请输入学生学号:");
            sid = sc.nextLine();

            if (compare(list, sid)) {
                System.out.println("你输入的学号已被占用,请重新输入!");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄:");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地:");
        String address = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        list.add(s);
        System.out.println("添加学生成功!");


    }

    //查看学生信息
    public static void findAllStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无信息!请添加信息再查询!");
            return;
        }
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t居住地\t\t");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getSid() + "\t"
                    + s.getName() + "\t\t"
                    + s.getAge() + "岁\t"
                    + s.getAddress());
        }
    }

    //删除学生信息
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生学号:");
        String sid = sc.nextLine();

        boolean index = true;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                list.remove(i);
                index = false;
                break;
            }
        }
        if (index) {
            System.out.println("该信息不存在,请重新输入!");
        } else {
            System.out.println("删除学生成功!");
        }
    }

    //修改学生信息
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        boolean index = true;
        System.out.println("请输入你要修改的学生学号:");
        String sid = sc.nextLine();
        int i;
        for (i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                index = false;
                break;
            }
        }
        if (index) {
            System.out.println("该信息不存在,请重新输入!");
            return;
        }

        System.out.println("请输入学生新姓名:");
        String name = sc.nextLine();
        System.out.println("请输入学生新年龄:");
        String age = sc.nextLine();
        System.out.println("请输入学生新居住地:");
        String address = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        list.set(i, s);
        System.out.println("修改学生成功!");

    }

    public static boolean compare(ArrayList<Student> list, String sid) {
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                flag = true;
                break;
            }
        }

        return flag;
    }

}