• Java-学生选课系统


    目的

    实现学生登录-选课-课程添加等操作,以下代码分三部分来实现:学生系统部分,课程系统部分与主方法选课部分


    学生系统部分

    package CourseSelectionSystem;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Scanner;
    
    public class studentSystem{
        Scanner in=new Scanner(System.in);
        private ArrayList<student> studentList;
    
        public boolean isEmpty(){
            return studentList.isEmpty();
        }
    
        public student getStudent(int index){
            if(index < 0 || index >= studentList.size()){
                System.out.println("下标错误,获取失败!");
                return null;
            }
            return studentList.get(index);
        }
    
        // 输出所有学生信息
        public void out_print(){
            System.out.println("\n---------输出所有学生信息-----------");
            studentList.forEach(s -> System.out.println(s.toString()));
        }
    
        public studentSystem(){
            studentList=new ArrayList<>();
        }
    
        // 查找学生
        public student lookUpStudent(){
            if(studentList.size() == 0) {
                System.out.println("暂无学生信息,无法进行查找操作");
                return null;
            }
            System.out.println("\n-------------查找学生------------");
            System.out.println("1、根据学生姓名进行查找 \t\t\t\t 2、根据学生学号进行查找");
            System.out.println("3、退出操作");
            System.out.print("请输入需要使用的功能序号:");
            String choose=in.next();
    
            // 查找成功返回该学生,查找失败返回null
            if("2".equals(choose)){
                System.out.print("\n请输入需要查找的学生学号:");
                int ans = judgmentId(in.next());
                if(-1 == ans) {
                    System.out.println("查找失败,该学生学号不存在!!!");
                    return null;
                }
                System.out.println("\n查找成功!!!");
                System.out.println(studentList.get(ans).toString());
                return studentList.get(ans);
            }
            // 按姓名开始查找
            if("1".equals(choose)){
                System.out.print("请输入需要查找学生的姓名:");
                String targetName=in.next();
                List<student> ans = lookUpTargetName(targetName);
                if(0 == ans.size()){
                    System.out.println("查找失败,无此姓名学生");
                    return null;
                }
    
                System.out.println("查找成功!以下是姓名为"+targetName+"的学生信息");
                ans.forEach(s -> System.out.println(s.toString()));
                return null;
            }
            System.out.println("退出成功!");
            return null;
        }
    
        // 按姓名查找学生
        private List<student> lookUpTargetName(String targetName){
            List<student> res=new ArrayList<>();
            for(int i=0,end=studentList.size(); i<end; ++i){
                if(studentList.get(i).getName().equals(targetName)){
                    res.add(studentList.get(i));
                }
            }
            return res;
        }
    
        // 修改
        public boolean amend(){
            if(studentList.size() == 0) {
                System.out.println("暂无学生信息,无法进行修改操作");
                return false;
            }
            System.out.println("\n--------修改学生信息--------------");
            System.out.print("请输入需要修改学生的学号:");
            String targetId=in.next();
            int ans = judgmentId(targetId);
            if(-1 == ans){
                System.out.println("该学号不存在,修改失败,请重新操作");
                return false;
            }
    
            student target = studentList.get(ans);
            System.out.println("学生信息为:"+target.toString());
            System.out.print("请输入需要修改后的学生姓名:");
            target.setName(in.next());
            System.out.print("请输入需要修改后的学生年龄:");
            target.setAge(in.nextInt());
    
            System.out.println("-----修改成功-------");
            System.out.println(target.toString());
            return true;
        }
    
        // 删除
        public boolean delect(){
            if(studentList.size() == 0) {
                System.out.println("暂无学生信息,无法进行删除操作");
                return false;
            }
            System.out.println("\n--------删除学生-------");
            System.out.print("请输入需要删除学生的学号:");
            String targetId=in.next();
            int ans = judgmentId(targetId);
            if(-1 == ans){
                System.out.println("该学号不存在,删除失败,请重新操作");
                return false;
            }
            System.out.println("删除成功!被删除学生的信息为:"+studentList.get(ans).toString());
            studentList.remove(ans);    //删除
            return true;
        }
        // 添加学生
        public boolean add(){
            System.out.println("\n-----------添加学生-----------");
            student newStudent=new student();
    
            System.out.print("请输入添加学生的id:");
            String id=in.next();
            int index = judgmentId(id);
            if(index != -1){
                System.out.print(studentList.get(index).toString()+"\t");
                System.out.println("该学号已被占用,请重新操作!");
                return false;
            }
            newStudent.setId(id);
    
            System.out.print("请输入姓名:");
            newStudent.setName(in.next());
            System.out.print("请输入年龄:");
            newStudent.setAge(in.nextInt());
    
            studentList.add(newStudent);
            System.out.println("添加成功");
            return true;
        }
        // 判断是否id存在
        public int judgmentId(String targetId){
            for(int i=0,end=studentList.size(); i< end; ++i){
                if(studentList.get(i).getId().equals(targetId)){
                    return i;
                }
            }
            return -1;
        }
    }
    
    class student {
        private String id;              //学生id
        private String name;            //姓名
        private int age;                //年龄
        private HashSet<courses> courseNumber; // 课程号
    
        //空参构造
        public student(){
            this.courseNumber=new HashSet<>();
        }
        public student(String id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.courseNumber=new HashSet<>();
        }
    
        /**
         * 获取
         * @return id
         */
        public String getId() {
            return id;
        }
    
        /**
         * 设置
         * @param id
         */
        public void setId(String id) {
            this.id = id;
        }
    
        /**
         * 获取
         * @return name
         */
        public String getName() {
            return name;
        }
    
        /**
         * 设置
         * @param name
         */
        public void setName(String name) {
            this.name = name;
        }
    
        /**
         * 获取
         * @return age
         */
        public int getAge() {
            return age;
        }
    
        /**
         * 设置
         * @param age
         */
        public void setAge(int age) {
            this.age = age;
        }
    
        /**
         * 获取
         * @return courseNumber
         */
        public HashSet<courses> getCourseNumber() {
            return courseNumber;
        }
    
    
        // 重载
        public boolean setCourseNumber(courses curCourses){
            return this.courseNumber.add(curCourses);
        }
    
        public String toString() {
            return "学号 = " + id + ", 姓名 = " + name + ", 年龄 = " + age;
        }
        public void out_Courses(){
            if(0 == courseNumber.size()){
                System.out.println("暂无已选课程");
                return;
            }
    
            System.out.println("学号:"+this.id+",姓名:"+this.name+" 已选的课程有:");
            this.courseNumber.forEach(s -> System.out.println(s));
        }
        // 需要构造一个输出学生选课信息的方法
    }
    
    
    • 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
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260

    课程管理部分

    package CourseSelectionSystem;
    
    import java.util.HashMap;
    import java.util.Scanner;
    
    public class coursesSystem{
        private static HashMap<String,courses> map; // 课程名为key,课程信息为val
        Scanner in=new Scanner(System.in);
        public coursesSystem() {
            map=new HashMap<>();
        }
    
        public boolean isEmpty(){
            return map.isEmpty();
        }
    
        public courses getCourses(String target){
            return map.get(target);
        }
    
        public void out_data(){
            map.forEach((String e1,courses e2) -> System.out.println(e2));
        }
    
        //修改指定课程信息
        public boolean amendCoursesMessage(){
            System.out.print("请输入需要修改的课程名称:");
            String target=in.next();
    
            if(!map.containsKey(target)){
                System.out.println("修改失败,该课程不存在");
                return false;
            }
    
            courses res = map.get(target);
            System.out.print("请输入需要修改的课程号:");
            res.setId(in.nextInt());
    
            System.out.println("修改成功!!!");
            System.out.println("修改后的信息为:"+res.toString());
            return true;
        }
        // 删除
        public courses deleteCourse(){
            System.out.println("\n---------课程删除---------");
            System.out.print("请输入需要删除的课程名:");
            String target=in.next();
    
            courses res = map.remove(target);
            if(null == res){
                System.out.println("删除失败,该课程不存在");
                return null;
            }
            System.out.println("删除成功!!!");
            return res;
        }
        // 查找
        public boolean seekTargetCourse(){
            System.out.println("\n-----------课程查找------------");
            System.out.print("请输入需要查找的课程名:");
            String target=in.next();
            courses res = map.get(target);
            if(null == res){
                System.out.println("查找失败,该课程不存在");
                return false;
            }
            System.out.println("查找成功!!!");
            System.out.println(res.toString());
            return true;
        }
        // 添加
        public boolean add(){
            System.out.println("\n---------------课程添加-------------");
            System.out.print("请输入需要添加的课程名称:");
            String courseName=in.next();
            if(map.containsKey(courseName)){
                System.out.println("添加失败,该课程已存在");
                return false;
            }
            System.out.print("请输入需要添加的课程id:");
            int id=in.nextInt();
            map.put(courseName,new courses(id,courseName));
            System.out.println("添加成功!!!");
            return true;
        }
    
    }
    
     class courses {
        private int id; //课程序号
        private String CourseName;  // 课程名
    
    
        public courses() {
        }
    
        public courses(int id, String CourseName) {
            this.id = id;
            this.CourseName = CourseName;
        }
    
        /**
         * 获取
         * @return id
         */
        public int getId() {
            return id;
        }
    
        /**
         * 设置
         * @param id
         */
        public void setId(int id) {
            this.id = id;
        }
    
        /**
         * 获取
         * @return CourseName
         */
        public String getCourseName() {
            return CourseName;
        }
    
        /**
         * 设置
         * @param CourseName
         */
        public void setCourseName(String CourseName) {
            this.CourseName = CourseName;
        }
    
        public String toString() {
            return "课程号:" + id + ", 课程名:《" + CourseName + "》";
        }
    }
    
    
    • 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
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138


    主方法

    package CourseSelectionSystem;
    
    
    // 学生选课系统
    
    import java.util.*;
    
    public class Main {
        static Scanner in=new Scanner(System.in);
        static studentSystem studentSystemList;   // 学生系统
        static coursesSystem coursesSystemList;   // 选课系统
        public static void main(String[] args) {
            studentSystemList=new studentSystem();
            coursesSystemList=new coursesSystem();
    
            HomePage(); // 主界面
        }
        public static void HomePage(){
            boolean flag=true;
            while(flag){
                System.out.println("\n--------------欢迎来到学生选课系统----------------");
                System.out.println("1、学生管理 \t\t\t\t 2、课程管理");
                System.out.println("3、学生登录 \t\t\t\t 4、退出");
    
                switch (in.next()){
                    case "1" : studentInitialMenu(); break; //学生管理系统
                    case "2" : coursesInitialMenu(); break; // 课程管理系统
                    case "3" : studentLonIn(); break;
                    default: flag=false; break;
                }
                System.out.println("\n\n\n\n\n");
            }
            System.out.println("退出成功!!!");
        }
    
        // 学生登录界面
        public static void studentLonIn(){
    
            if(studentSystemList.isEmpty()){
                System.out.println("暂无学生信息,请添加后进行登录");
                return;
            }
    
            System.out.println("\n----------学生登录-----------");
            System.out.print("请输入学号:");
            String id=in.next();
            student targetStudent = studentSystemList.getStudent(studentSystemList.judgmentId(id));
            if(null == targetStudent){
                System.out.println("查无此学号,登录失败!请确认再进行操作");
                return;
            }
            boolean flag=true;
            while(flag){
                System.out.println("\n-----------登录成功------------");
                System.out.println("1、输出已选课程 \t\t\t\t 2、选课");
                System.out.println("其他输入:退出");
    
                switch (in.next()){
                    case "1" : targetStudent.out_Courses(); break;
                    case "2" : studnetCourseSelection(targetStudent); break;
                    default: flag=false; break;
                }
            }
            System.out.println("退出成功!");
            return;
        }
    
        // 学生选课
        private static void studnetCourseSelection(student targetStudent){
            if(coursesSystemList.isEmpty()){
                System.out.println("暂无课程信息,请等候公布");
                return;
            }
            do{
                System.out.println("课程列表:");
                coursesSystemList.out_data();   // 输出课程界面
                System.out.print("请输入需要选的课程名称:");
                courses course = coursesSystemList.getCourses(in.next());
                if(null == course){
                    System.out.println("无此课程信息,请重新输入");
                    continue;
                }
                System.out.println(course.toString());
                if(!targetStudent.setCourseNumber(course)){
                    System.out.println("添加失败!你已选过该课程。");
                }
                else{
                    System.out.println("添加成功");
                }
                System.out.println("是否继续选课?(yes/no)");
            }while("yes".equals(in.next()));
            System.out.println("------------选课结束-------------");
        }
    
        // 课程系统界面
        public static void coursesInitialMenu(){
            boolean flag=true;
            while(flag){
                System.out.println("\n-------------欢迎来到课程管理系统----------------");
                System.out.println("1、添加课程");
                System.out.println("2、删除课程");
                System.out.println("3、修改课程");
                System.out.println("4、查询课程");
                System.out.println("5、输出所有课程信息");
                System.out.println("6、退出");
                System.out.println("请输入您的选择:");
                switch (in.next()){
                    case "1" : coursesSystemList.add(); break;
                    case "2" : coursesSystemList.deleteCourse(); break;
                    case "3" : coursesSystemList.amendCoursesMessage(); break;
                    case "4" : coursesSystemList.seekTargetCourse(); break;
                    case "5" : coursesSystemList.out_data(); break;
                    case "6" : flag=false; break;
                    default:
                        System.out.println("输入错误,请重新输入");break;
                }
            }
            System.out.println("退出成功!!!");
    
        }
    
        // 学生管理管理界面
        public static void studentInitialMenu(){
            boolean flag=true;
            while(flag){
                System.out.println("\n-------------欢迎来到学生管理系统----------------");
                System.out.println("1、添加学生");
                System.out.println("2、删除学生");
                System.out.println("3、修改学生");
                System.out.println("4、查询学生");
                System.out.println("5、输出所有学生信息");
                System.out.println("6、退出");
                System.out.println("请输入您的选择:");
                switch (in.next()){
                    case "1" : studentSystemList.add(); break;
                    case "2" : studentSystemList.delect(); break;
                    case "3" : studentSystemList.amend(); break;
                    case "4" : studentSystemList.lookUpStudent(); break;
                    case "5" : studentSystemList.out_print(); break;
                    case "6" : flag=false; break;
                    default:
                        System.out.println("输入错误,请重新输入");break;
                }
            }
            System.out.println("退出成功!!!");
        }
    
    }
    
    
    • 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
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149


    end

    以上代码分别使用了三个类来实现要求,最后启动的入口在主方法部分。实现的比较粗糙,如有错误,欢迎指正。

  • 相关阅读:
    ATtiny88初体验(五):ADC
    E 网络编程
    C# 实现xlsx文件导入
    运用工具Postman快速导出python接口测试脚本
    vue项目使用高德地图时报错:AMap is not defined
    FLStudio2024汉化破解版在哪可以下载?
    Docker使用总结
    技术团队:给代码评审发起者的4个建议
    本地服务启动慢问题及dubbo测试方法记录
    VisualSVN 8.1 Release Notes Date: November 3, 2022
  • 原文地址:https://blog.csdn.net/Lion__king/article/details/133320526