• Java学生管理系统(纯练手)


    Java学生管理系统(纯练手)

    该系统主要功能如下:

    • ​添加学生:通过键盘录入学生信息,添加到集合中
    • 删除学生:通过键盘录入要删除学生的学号,将该学生对象从集合中删除
    • 修改学生:通过键盘录入要修改学生的学号,将该学生对象其他信息进行修改
    • 查看学生:将集合中的学生对象信息进行展示
    • 退出系统:结束程序
      学生类的定义
    package com.studentsystem;
    
    public class Student {
        private  String  id;
        private  String name;
        private int age;
        private  String address;
    
    
        public Student(){
        }
        public Student(String  id, String name, int age, String address) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.address = address;
        }
    
        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 getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    测试类的定义

    package com.studentsystem;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            ArrayList<Student> list = new ArrayList<>();
    
            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("请输入您的选择:");
                Scanner sc = new Scanner(System.in);
                String   choose = sc.next();
                switch(choose){
                    case "1"->addStudent(list);
                    case "2"->deleteStudent(list);
                    case "3"->updateStudent(list);
                    case "4"-> queryStudent(list);
                    case "5"->{
                        System.out.println("退出");
                        System.exit(0);
                    }
                    default->System.out.println("没有这个选项!");
                }
            }
    
        }
        public static  void addStudent(ArrayList<Student> list){
            Student student = new Student();
            Scanner sc = new Scanner(System.in);
            String id = null;
            while(true){
                System.out.println("请输入学生id:");
                id = sc.next();
                boolean flag = contains(list,id);
                if(flag){
                    System.out.println("id已经存在,请重新输入!");
                }else{
                    student.setId(id);
                    break;
                }
            }
            System.out.println("请输入学生的姓名:");
            String name = sc.next();
            student.setName(name);
    
            System.out.println("请输入学生的年龄:");
            int age = sc.nextInt();
            student.setAge(age);
    
            System.out.println("请输入学生的家庭住址:");
            String address = sc.next();
            student.setAddress(address);
    
            list.add(student);
            System.out.println("学生信息添加成功!");
        }
        public static boolean contains(ArrayList<Student> list,String id){
            return  getIndex(list,id)>=0;
        }
        public static int getIndex(ArrayList<Student> list, String id){
            //遍历集合
            for (int i = 0; i < list.size(); i++) {
                //得到每一个学生对象
                Student stu = list.get(i);
                //得到每一个学生对象的id
                String sid = stu.getId();
                //拿着集合中的学生id跟要查询的id进行比较
                if(sid.equals(id)){
                    //如果一样,那么就返回索引
                    return i;
                }
            }
            //当循环结束之后还没有找到,就表示不存在,返回-1.
            return -1;
        }
        public static  void deleteStudent(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){
                list.remove(index);
                System.out.println("id为:"+id+"的学生删除成功!");
            }else{
                System.out.println("id不存在,删除失败!");
            }
        }
        public static  void updateStudent(ArrayList<Student> list){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要修改的学生的id:");
            String id = sc.next();
            int index = getIndex(list,id);
            if (index ==-1) {
                System.out.println("所删除id为:"+id+"的学生不存在!");
            }
            //获取要修改的学生对象
            Student stu = list.get(index);
    
            //输入其他的信息并修改
            System.out.println("请输入要修改的学生姓名");
            String newName = sc.next();
            stu.setName(newName);
    
            System.out.println("请输入要修改的学生年龄:");
            int newAge = sc.nextInt();
            stu.setAge(newAge);
    
            System.out.println("请输入要修改的学生家庭住址:");
            String newAddress = sc.next();
            stu.setAddress(newAddress);
    
            System.out.println("学生信息修改成功!");
        }
        public static  void queryStudent(ArrayList<Student> list){
            if (list.size() == 0) {
                System.out.println("当前无学生信息,请添加后再查询!");
            }
            System.out.println("id\t\t姓名\t年龄\t家庭住址");
            //当代码执行到这里,表示集合中是有数据的
            for (Student stu : list) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    今天就记录到这里了!
    在这里插入图片描述

  • 相关阅读:
    【计算机导论调研报告】AI技术的发展
    Redis-带你深入学习数据类型zset
    three.js学习笔记
    Java基础:Java基本概念
    为什么阿里不推荐使用 keySet() 遍历HashMap?
    AI杀疯!2023上半年至今有趣的AI算法(内附视频)
    漏洞修复-SSH版本信息可被获取漏洞
    文件批量从gbk转成utf8的工具
    Linux进程控制(一)
    vue3+vite+ts配置多个代理并解决报404问题
  • 原文地址:https://blog.csdn.net/weixin_51293134/article/details/133801098