学生管理系统初级

根据题目要求生成大纲

总结:

1.在书写时,考虑到了书写时id可是是abc... 类型是String,但在根据id获取集合中元素时 list.get() ,get()里面是int类型。 2.在书写还有一点功能并不完全,

2.1查找时是打印所有学生信息,并不能根据输入关键信息打印相应学生

2.2修改时,是所有信息都要修改,并没有实现,只修改某一个或几个信息。

代码:

复制代码
public class Studenttext {
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<>();
        Scanner sc=new Scanner(System.in);
        int number;
        boolean flag=true;
        while (flag) {
            System.out.println("请选择你要的功能:1.添加,2.删除,3.修改,4.查找,5.退出");
            number=sc.nextInt();
            switch (number){
                case 1 -> add(list);
                case 2->delete(list);
                case 3->adapt(list);
                case 4-> reaserach(list);
                default -> flag=false;
            }
        }
    }
    //添加
    public static void  add( ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        Student s=new Student();
        System.out.println("请输入学生id");
        String sid=sc.next();
        boolean result = contains(list, sid);
        if (result) {
            System.out.println("该id已经存在");
        }
        s.setId(sid);
        System.out.println("请输入学生姓名");
        String name=sc.next();
        s.setName(name);
        System.out.println("请输入学生年龄");
        int age=sc.nextInt();
        s.setage(age);
        System.out.println("请输入学生家庭住址");
        String adress=sc.next();
        s.setAdress(adress);
        list.add(s);
        System.out.println("已经添加成功");
        //进行id唯一

    }
    //删除
    public static void  delete( ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String sid=sc.next();
        int id=getindex(list,sid);

        if (id>=0) {
            //true就存在
            list.remove(id);
            System.out.println(list.size());
            System.out.println("删除成功");
        }else {
            //不存在
            System.out.println("id不存在");
        }

    }//修改
    public static void  adapt( ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改的id");
        String id=sc.next();
        int index=getindex(list,id);
        if (index>=0) {
            Student s=list.get(index);
            System.out.println("请输入新的名字");
            String Newname=sc.next();
            s.setName(Newname);
            System.out.println("请输入新的年龄");
            int Newage=sc.nextInt();
            s.setage(Newage);
            System.out.println("请输入新的家庭住址");
            String Newadress=sc.next();
            s.setAdress(Newadress);
            System.out.println("修改成功");
        }else {
            System.out.println(id+"不存在 ,更新失败");
        }

    }//查找
    public static void  reaserach( ArrayList<Student> list){
        if (list.size()!=0) {
            //返回true就代表存在
             System.out.println("---------代军的学生管理系统------------");
             System.out.println("id\tname\tage\tadress");
            for (int i = 0; i < list.size(); i++) {
                Student s = list.get(i);
                System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getage()+"\t"+s.getAdress());
            }

        }else {
            //如果不存在,提示当前无学生信息,请添加后在查询
            System.out.println("当前无学生信息,请添加后在查询");

        }


    }
    //进行id唯一,不存在返回flase
    public static boolean contains(ArrayList<Student> list,String id){
        return getindex(list,id)>=0;
    }
    //查找id的学生,根据id查找
    //不存在返回-1
    public static int  getindex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            String sut_id=list.get(i).getId();
            //存在就返回true
            if (sut_id.equals(id)) {
                return i;
            }
        }return -1;
    }

}
相关推荐
非凡ghost13 小时前
猫眼浏览器(Chrome内核增强版浏览器)官方便携版
前端·网络·chrome·windows·软件需求
熊文豪18 小时前
Windows安装RabbitMQ保姆级教程
windows·分布式·rabbitmq·安装rabbitmq
搬砖的小码农_Sky18 小时前
Windows操作系统上`ping`命令的用法详解
运维·网络·windows
Kiri霧1 天前
Rust模式匹配详解
开发语言·windows·rust
程序设计实验室1 天前
使用命令行删除 Windows 网络映射驱动器
windows
用户31187945592181 天前
Windows 电脑安装 XTerminal 1.25.1 x64 版(带安装包下载关键词)
windows
Logintern091 天前
windows如何设置mongodb的副本集
数据库·windows·mongodb
Chandler241 天前
一图掌握 操作系统 核心要点
linux·windows·后端·系统
ajassi20001 天前
开源 C# 快速开发(十七)进程--消息队列MSMQ
windows·开源·c#
Python私教1 天前
5分钟上手 MongoDB:从零安装到第一条数据插入(Windows / macOS / Linux 全平台图解)
windows·mongodb·macos