目的
实现学生登录-选课-课程添加等操作,以下代码分三部分来实现:学生系统部分,课程系统部分与主方法选课部分
学生系统部分
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();// 查找成功返回该学生,查找失败返回nullif("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; //学生idprivate 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));}// 需要构造一个输出学生选课信息的方法
}
课程管理部分
package CourseSelectionSystem;import java.util.HashMap;
import java.util.Scanner;public class coursesSystem{private static HashMap<String,courses> map; // 课程名为key,课程信息为valScanner 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 + "》";}
}
主方法
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("退出成功!!!");}}
end
以上代码分别使用了三个类来实现要求,最后启动的入口在主方法部分。实现的比较粗糙,如有错误,欢迎指正。