JavaSE基础-学生管理系统案例

ArrayList集合                 【重点】
    学生管理系统                   【重点】

ArrayList集合                 【重点】
    ArrayList集合的概述
        什么是ArrayList集合
            集合,是一种容器,可以存储多个数据,集合有很多种,每种集合都会有一个对应的类
            这些形成了一个体系结构,ArrayList集合只是众多集合中的一种。

        ArrayList集合和数组区别对比
            相同点:
                1、都是容器,可以存储多个相同类型的数据
                2、都有索引,范围:0~长度-1
            不同点:
                1、数组是长度固定的,集合长度是可变的
                2、数组只能通过“数组名[索引]”这样格式进行修改和获取(查询)操作
                    ArrayList集合是一个类,可以通过方法进行操作,而且操作增删改查都行
                3、数组既可以存储基本类型的数据,也可以存储引用类型的数据,即可以存储任意类型的数据
                   集合只能存储引用类型的数据,不能直接存储基本类型的数据。

    ArrayList类
        构造方法
            public ArrayList()
        成员方法
            增
                boolean add(E e)
                void add(int index, E element)
            删
                E remove(int index)
                void clear()
            改
                E set(int index, E element)
            查
                E get(int index)
                int size()
            判断
                boolean isEmpty()
                boolean contains(Object o)

学生管理系统                   【重点】
    步骤:
        1、定义Student类
        2、完成主界面功能(退出)
        3、完成添加功能
        4、完成查看功能
        5、完成删除功能
        6、完成修改功能
        7、完成优化

/*
标准类:学生类
 */
public class Student {
    private String id;
    private String name;
    private int age;
    private String birthday;

    public Student() {
    }

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

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
}
import java.util.ArrayList;
import java.util.Scanner;

/*
测试类
    主界面:
        输出语句
        Scanner
        switch
        死循环
        循环跳转语句
        循环标号
 */
public class StudentMangerTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 创建集合,用于管理学生对象
        ArrayList<Student> list = new ArrayList<>();

        loop:while (true) {// ctrl+alt+t
            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("请输入您的选择:");
            int choice = sc.nextInt();

            // 根据选择完成不同的操作
            switch (choice) {
                case 1:
//                    System.out.println("添加学生");
                    addStudent(list);
                    break;
                case 2:
//                    System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case 3:
//                    System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case 4:
//                    System.out.println("查看所有学生");
                    selectAllStudents(list);
                    break;
                case 5:
                    System.out.println("谢谢使用!");
                    break loop;
//                    return;
//                    System.exit(0);// 退出JVM
                default:
                    System.out.println("您的输入有误,请重新输入!");
                    break;
            }
        }
    }

    /**
     * 修改学生
     * @param list ArrayList集合
     */
    public static void updateStudent(ArrayList<Student> list) {
        // 获取键盘录入的学号
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String id = sc.next();
        // 判断是否存在该学号的学生
        int index = getIndex(id, list);
        // 不存在,提示修改失败
        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 newStu = new Student(id,name,age,birthday);
            list.set(index,newStu);

//            Student stu = list.get(index);
//            stu.setName(name);
//            stu.setAge(age);
//            stu.setBirthday(birthday);

            System.out.println("修改成功!");
        }
    }

    /**
     * 删除学生
     * @param list ArrayList集合
     */
    public static void deleteStudent(ArrayList<Student> list) {
        // 键盘录入学生学号
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String id = sc.next();
        // 根据学号判断学生是否存在
        int index = getIndex(id, list);
        // 如果不存在,提示删除是吧
        if (index == -1) {
            System.out.println("该信息不存在,请重新输入!");
        } else {
            // 如果存在,根据学号获取到索引,然后根据索引删除
            list.remove(index);
            System.out.println("删除成功!");
        }
    }

    /**
     * 查看所有学生
     * @param list ArrayList集合
     */
    public static void selectAllStudents(ArrayList<Student> list) {
//        if(list.size() == 0){
        if(list.isEmpty()){
            System.out.println("无数据,请添加后再查询!");
            return;
        }

        // 遍历ArrayList集合
        System.out.println("学号\t姓名\t年龄\t\t生日");
        for (int i = 0; i < list.size(); i++) {
            // 获取每一个学生对象
            Student stu = list.get(i);
            // 打印每一个学生信息
            System.out.println(stu.getId() + "\t\t" + stu.getName()
                    + "\t" + stu.getAge() + "\t" + stu.getBirthday());
        }

    }

    /**
     * 添加学生
     * @param list  ArrayList集合
     */
    public static void addStudent(ArrayList<Student> list) {
        // 键盘录入学生信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String id = null;
        while (true) {
            id = sc.next();
            int index = getIndex(id, list);
            // 说明学号不存在
            if (index == -1) {
                break;
            } else {
                System.out.println("学号已存在,请重新输入:");
            }
        }

        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        // 将学生信息封装到Student对象中
        Student stu = new Student(id,name,age,birthday);
        // 将Student对象添加到集合中
        list.add(stu);
        // 提示添加成功
        System.out.println("添加成功!");
    }

    /**
     * 根据学号获取学生的索引
     * @param id     学生学号
     * @param list   ArrayList集合
     * @return       索引
     */
    public static int getIndex(String id,ArrayList<Student> list){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                // 存在相同学号的学生,则修改为该学生的学号
                index = i;

                // 提高效率,学号肯定不会有重复的,找到了一个和输入的学号相同的学生,
                // 后面再遍历,永远不可能出现还重复的,后面的遍历就没有必要
                break;
            }
        }
        return index;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值