学生管理系统Java版(老活新整)

以下是系统代码

package studentUserBao.managerSystem;

import studentUserBao.domain.Student;

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

public class Demo {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        //创建集合对象
        ArrayList<Student> list = new ArrayList<Student>();

        lo:
        while (true) {
            // 1. 搭建主界面菜单
            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 choice = sc.next();

            switch (choice) {
                case "1":
                    //封装一个添加方法
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("2 删除学生");
                    //封装删除学生方法
                    deleteStudent(list);
                    break;
                case "3":
                    //System.out.println("3 修改学生");
                    //封装修改学生信息方法
                    modifyStudent(list);
                    break;
                case "4":
                    //封装一个查看学生的方法
                    viewStudent(list);
                    break;
                case "5":
                    System.out.println("欢迎再次使用!");
                    break lo;//break只能退出最近的一个循环,加入lo指明,则输入5时退出整个while循环
                default:
                    System.out.println("请重新输入:");
                    break;
            }
        }
    }

    //根据学号修改学生信息,学号不变
    public static void modifyStudent(ArrayList<Student> list) {
        System.out.println("请输入要修改的学号:");
        Scanner sc = new Scanner(System.in);
        String modifySid = sc.next();
        //调用判断学号是否存在的方法并且接收返回值
        int index = isExientSid(list, modifySid);
        if (index == -1) {
            System.out.println("抱歉,你所输入的学号不存在,请重新输入!");
        } else {
            //说明输入的学号存在,调用集合的set方法修改
            System.out.println("请输入新的姓名:");
            String newName = sc.next();
            System.out.println("请输入新的年龄:");
            int newAge = sc.nextInt();
            System.out.println("请输入新的生日:");
            String newBirthday = sc.next();
            //封装到学生对象
            Student stu = new Student(modifySid, newName, newAge, newBirthday);
            //把新的学生对象封装到集合容器,根据索引修改
            list.set(index, stu);
            System.out.println("修改成功!");
        }
    }

    //创建删除学生方法
    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入要删除的学号:");
        Scanner sc = new Scanner(System.in);
        String deleteSid = sc.next();
        //调用判断学号是否存在的方法并且接收返回值
        int index = isExientSid(list, deleteSid);
        if (index == -1) {
            System.out.println("抱歉,你所输入的学号不存在,请重新输入!");
        } else {
            //说明输入的学号存在,调用集合的remove方法删除
            list.remove(index);
            System.out.println("删除成功!");
        }
    }

    //封装一个方法,判断学号是否存在,并且返回查找的学号在集合中的索引,如果不存在则返回-1
    public static int isExientSid(ArrayList<Student> list, String sid) {
        //1假设学号不存在,定义index为-1
        int index = -1;
        //2存在,判断学号对应的对象在集合中的索引
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            //注意:if括号中的值是true时才会执行大括号的语句,false就不执行
            if (id.equals(sid)) {
                //让index记录此时的索引
                index = i;
            }
        }

        //返回值
        return index;
    }

    //查看学生方法
    public static void viewStudent(ArrayList<Student> list) {
        //1首先判断集合中是否有数据
        if (list.size() == 0) {
            System.out.println("没有数据,请录入学生信息!");
            return;//结束当前方法
        }
        //2有数据,先输入表头信息
        System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "生日");
        //3遍历集合,获取集合数据
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
        }

    }

    //添加学生方法
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        //给出录入的提示信息
        //给出一个无限循环,当学号不重复时才能结束循环
        String sid;

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

            //调用判断学号是否存在的方法,不存在返回-1,存在就返回学号对应的对象在集合中的索引
            int index = isExientSid(list, sid);
            //判断返回值是否为-1
            if(index == -1){
                //-1就说明学号不存在,也就是输入的学号并没有重复,此时break结束while死循环
                break;
            }
        }

        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        //2将键盘录入的信息封装成学生对象
        //带参数构造方法
        Student stu = new Student(sid, name, age, birthday);
        //3将封装好的学生对象添加到集合容器中
        list.add(stu);
        //4给出添加成功的提示
        System.out.println("添加成功!");
    }
}

以下是学生类代码

package studentUserBao.domain;

public class Student {

    private String sid;
    private String name;
    private int age;
    private String birthday;

    public Student() {
    }

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值