学生管理系统(基础版),基于集合实现



package org.example;
import java.util.*;


//藏有学生的信息以及可以交出学生的信息
class Student {
    private String stuNum;//学号
    private String stuName;//名字
    private Integer stuAge;//建议使引用类型  Integer要是使用int类型默认值就是0 要是Integer 默认值是null
    private String stuAddress;//地址

    public Student(){

    }
    public Student(String ID, String name, Integer age, String address) {
        this.stuNum = ID;
        this.stuName = name;
        this.stuAge = age;
        this.stuAddress = address;
    }

    public String getStuID() {
        return stuNum;
    }

    public void setStuID(String stuID) {
        this.stuNum = stuID;
    }

    public String getName() {
        return stuName;
    }

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

    public Integer getAge() {
        return stuAge;
    }

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

    public String getAddress() {
        return stuAddress;
    }

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

    //重写toString
    @Override
    public String toString() {
        return  "学生信息:"+"学号:"+stuNum+" "+"姓名:"+stuName+" "+"年龄:"+stuAge+" "+"住址:"+stuAddress+"\n";
    }
}


public class Main {
    private static ArrayList<Student> stuarr = new ArrayList<Student>();//存储所有信息的的集合.本系统中仅此一个
    private static Scanner scanner = new Scanner(System.in);

    //main方法执行主要操作
    public static void main(String[] args) {

        /*以下开始正式进入系统--------------------------------------------------------------------------------*/
        SetOpera();

    }//main方法到此结束

    public static void SetOpera() {
        boolean setquit = true;
        while (setquit == true) {
            System.out.println("----------------------------------------------------------------------------------");
            System.out.println("*               欢迎使用Set系统服务(1.增加 2.修改 3.删除 4.查询  5.退出)                 *");
            System.out.println("----------------------------------------------------------------------------------");
            System.out.print("请选择你需要的服务:");
            //进行判断
            int setchoice = scanner.nextInt();

            while (setchoice < 1 || setchoice > 6) {
                System.out.print("请重新输入正确的选项:");
                scanner = new Scanner(System.in);
                setchoice = scanner.nextInt();
            }

            switch (setchoice) {
                case 1:
                    SetAdd();
                    break;
                case 2:
                    SetUpdate();
                    break;
                case 3:
                    SetDelete();
                    break;
                case 4:
                    SetQuery();
                    break;
                case 5:
                    setquit = false;//该循环就不会继续执行
                    System.out.println("退出系统!");
                    break;
            }
        }
    }

    //增加学生,根据学号判断是否可以放入集合
    public static void SetAdd() {
        Scanner scanner = new Scanner(System.in);//开

        System.out.print("请输入学生的学号:");
        String num = scanner.next();

        //根据我们的学号从集合中查询,如果存在就不允许------学号不能重复
        Student student = getByIdStudent(num, false);
        if (student != null) {
            System.out.println("这个学号已经被" + student.getName());
            return;//程序就不会向下执行
        }
        System.out.print("请输入学生的姓名:");
        String name = scanner.next();

        System.out.print("请输入学生的地址:");
        String address = scanner.next();

        System.out.print("请输入学生的年龄:");
        Integer age = scanner.nextInt();

        Student tempstuobj = new Student(num, name, age, address);
        //将学生的信息存到集合中
        stuarr.add(tempstuobj);
        System.out.println("第" + stuarr.size() + "位学生信息新增完成");

        //将学生信息展示
        String tempinfo = tempstuobj.toString();
        System.out.println(tempinfo);
        System.out.println("");
    }

    //查询函数
    public static void SetQuery() {
        System.out.print("你需要的查询所有人的信息还是个别学生(所有学生按1,个别学生按2):");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        while (choice != 1 && choice != 2) {
            System.out.print("请重新输入正确选择:");
            choice = scanner.nextInt();
        }

        if (choice == 1) {
            SetShow();
        } else {
            System.out.print("请输入待查询学生的学号:");
            Scanner sscanner = new Scanner(System.in);
            String queryId = sscanner.next();
            Student tempstu = getByIdStudent(queryId, false);
            while (tempstu == null) {
                System.out.print("请重新输入正确学号:");
                queryId = sscanner.next();
                tempstu = getByIdStudent(queryId, false);
            }

            System.out.println("查询到的" + tempstu.toString());
            System.out.println("");

        }


        return;
    }

    //展示所有学生信息
    public static void SetShow() {
        //判断要是集合中没有任何数据就提示
        if (stuarr.size() == 0) {
            System.out.println("Set系统内信息暂无");
            return;//程序就不会执行了
        }
        //直接遍历就行
        for (int i = 0; i < stuarr.size(); i++) {
            System.out.print("第" + (i + 1) + "位学生信息如下:" + stuarr.get(i));
        }
        System.out.println("");

    }

    //通过学号进行查找的方法
    public static Student getByIdStudent(String num, boolean isDelete) {
        //遍历集合
        for (int i = 0; i < stuarr.size(); i++) {
            //取出每一个学生的对象
            Student tempstudent = stuarr.get(i);
            if (tempstudent.getStuID().equals(num)) {//判断传入的id与正在遍历的学生的id是否一致
                if (isDelete == true) {//被查找的学生需要删除
                    //根据学号查询我们的信息,如果isDelete=true  就会删除我们的该学生
                    stuarr.remove(i);
                    return tempstudent;
                } else
                    return tempstudent;//只是需要查找该学生的信息
            }
        }

        return null;//没找到该学生
    }

    //通过学号,删除学生
    public static void SetDelete() {
        //学号是唯一的,学号不能重复,根据学号进行删除
        System.out.print("请输入待删除学生的学号:");
        Scanner scanner = new Scanner(System.in);
        String num = scanner.next();

        Student student = getByIdStudent(num, true);
        if (student == null) {
            System.out.println("该生信息不存在!");
            return;
        }

        System.out.println("删除学生" + student.getName() + "信息成功!");
        System.out.println("");

    }//删除函数

    //改
    public static void SetUpdate() {
        //根据学号进行修改
        System.out.print("请输入要修改信息的学生的学号:");
        Scanner scanner = new Scanner(System.in);
        String num = scanner.next();
        //根据学号查找到学生的学号
        Student student = getByIdStudent(num, false);

        if (student == null) {
            System.out.println("从集合中没有查找到对应的学生对象!");
            return;
        } else {
            System.out.print("请选择你需要更改的信息(1.姓名 2.地址 3.年龄 4.全改):");
            int choice = scanner.nextInt();

            while (choice < 1 || choice > 5) {
                System.out.print("请重新输入正确选择:");
                choice = scanner.nextInt();
            }

            if (choice == 1) {
                System.out.print("请输入学生的新姓名:");
                String newName = scanner.next();
                student.setName(newName);
            } else if (choice == 2) {
                System.out.print("请输入学生的新地址:");
                String newAddress = scanner.next();
                student.setAddress(newAddress);
            } else if (choice == 3) {
                System.out.print("请输入学生的新的年龄:");
                Integer newAge = scanner.nextInt();
                student.setAge(newAge);
            } else {
                System.out.println("请输入学生的新姓名:");
                String newName = scanner.next();
                System.out.println("请输入学生的新地址:");
                String newAddress = scanner.next();
                System.out.println("请输入学生的新的年龄:");
                Integer newAge = scanner.nextInt();

                student.setName(newName);
                student.setAddress(newAddress);
                student.setAge(newAge);
            }
            System.out.println("该生修改后的信息" + student.toString());

        }
    }//修改方法
}
这是本人java课程期末作业,目前仅发出了最基础的集合操作信息系统,我已按照要求完成了一个使用到集合,数据库,文件的信息系统,目前发表集合部分,后续会发布与数据库,文件相关的操作,实现集合与数据库之间的交互,有兴趣的可以关注我

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值