java实现学生选课系统
作者:跌娣 时间:2022-11-19 14:58:24
本文为大家分享了java实现学生选课系统的具体代码,供大家参考,具体内容如下
案例要求:
学生(学号,姓名,专业,所选课程{<3})
老师(工号,姓名,所教课程{<3})
课程(课程号,课程名,学分,教师,已选课学生{<30})
选课系统代码如下:
//teacher
public class Teacher {
private int id;
private String teacherName;
private Course[] courses;
//构造函数
public Teacher() {
super();
courses= new Course[3];
}
public Teacher(int id,String teacherName){
this.id=id;
this.teacherName=teacherName;
courses = new Course[3];
}
//修改或是添加属性
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
}
/**
* 课程
*/
public class Course {
private String courseName;
private int courseId;
private Teacher teacher;
private float credit;
private Student[] students;
//构造函数
public Course(int courseId,String courseName,float credit,Teacher teacher) {
super();
this.courseId=courseId;
this.courseName=courseName;
this.credit=credit;
this.setTeacher(teacher);
students = new Student[30];
}
public Course(int courseId,String courseName,float credit) {
super();
this.courseId=courseId;
this.courseName=courseName;
this.credit=credit;
students = new Student[30];
}
public Course(int courseId,String courseName) {
super();
this.courseId=courseId;
this.courseName=courseName;
students = new Student[30];
}
public Course() {//默认形式,要有以防万一
super();
students = new Student[30];
}
//修改或获取属性值id,name,credit,
public void setId(int id){
this.courseId=id;
}
public int getId(){
return this.courseId;
}
public void setName(String name){
this.courseName=name;
}
public String getName(){
return this.courseName;
}
public void setCredit(float credit ){
this.credit=credit;
}
public float getCredit(){
return this.credit;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
//课加入学生
public boolean addStudent(Student stu){
boolean flag = false;//标志值:是否加入成功
//如果学生没有选过这门课,同时课的学生还没满则执行
if(!isSelectedStudent(stu)&&isNullStudent(stu)){
for(int i=0;i<students.length;i++){
if(students[i]==null){
students[i]=stu;
flag=true;
break;
}
}
}
return flag;
}
//课移除学生
public boolean removeStudent(Student stu){
boolean flag=false;
if(isSelectedStudent(stu)){//选过这门课
for(int i=0;i<students.length;i++){
if(students[i]==stu){
students[i]=null;
flag=true;
break;
}
}
}
return flag;
}
//显示选择课程的学生:
public void displayStudent(){
System.out.println("选择的课程:"+this.courseName+"的学生有:");
for(Student s:students){
if(s!=null){
System.out.print(s.getStuName()+" ");
}
}
System.out.println();
}
//子方法1:学生是否选过这门课
public boolean isSelectedStudent(Student stu){
boolean flag=false;
for(Student s:students){//只能用于检查,不能修改
if(s==stu){
flag=true;
break;
}
}
return flag;
}
//子方法2:学科学生未达到限定人数吗
public boolean isNullStudent(Student stu){
boolean flag=false;
for(Student s:students){
if(s==null){//还有空位
flag=true;
break;
}
}
return flag;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
/**
* 学生代码
* @author Floris0811
*/
public class Student {
private String stuName;
private int stuId;
private String major;
private Course[] courses;
//构造函数
public Student() {//不要忘
super();
courses = new Course[3];
}
public Student(int stuId,String stuName) {
super();
this.stuId=stuId;
this.stuName=stuName;
courses = new Course[3];
}
public Student(int stuId,String stuName,String major) {
super();
this.stuId=stuId;
this.stuName=stuName;
this.major = major;
courses = new Course[3];
}
//修改获取属性name,id,major
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
//学生选课;
public boolean addCourse(Course course){
boolean flag=false;
if(!isSelectedCourse(course)&&isNullCourse(course)){
for(int i=0;i<this.courses.length;i++){
if(courses[i]==null){
courses[i]=course;
course.addStudent(this);//课程也要添加学生
flag=true;
break;
}
}
}
return flag;
}
//学生移除课程
public boolean removeCourse(Course course){
boolean flag=false;
if(isSelectedCourse(course)){
for(int i=0;i<this.courses.length;i++){
if(courses[i]==course){
courses[i]=null;
course.removeStudent(this);//在课程中移除学生
flag=true;
break;
}
}
}
return flag;
}
//显示学生所选的课程
public void displayCourse(){
System.out.println("学生"+this.stuName+"所选课程有:");
for(Course c:courses){
if(c!=null){
System.out.print(c.getName()+" ");
}
}
System.out.println();
}
//子方法1:课是否被选过
public boolean isSelectedCourse(Course course){
boolean flag=false;
for(Course c:courses){
if(c==course){
flag=true;
break;
}
}
return flag;
}
//子方法2:学生是否还有选修课位置
public boolean isNullCourse(Course course){
boolean flag=false;
for(Course c:courses){
if(c==null){
flag=true;
break;
}
}
return flag;
}
}
package test;
public class ChooseCourseByStu {
/**
* 选课管理系统
*/
public static void main(String[] args) {
Student stu0 = new Student(1001,"Lily");
Student stu1 = new Student(1002,"Eilly");
Student stu2 = new Student(1003,"Floris");
Student stu3 = new Student(1004,"HaHa");
Course cour0 = new Course(001,"高数");
Course cour1 = new Course(002,"线代");
Course cour2 = new Course(003,"概率论");
stu0.addCourse(cour0);
stu0.addCourse(cour2);
stu0.addCourse(cour1);
stu1.addCourse(cour2);
stu1.addCourse(cour0);
stu2.addCourse(cour1);
stu3.addCourse(cour0);
stu3.addCourse(cour1);
stu1.removeCourse(cour2);
stu0.displayCourse();
cour0.removeStudent(stu1);
cour1.displayStudent();
}
}
关于管理系统的更多内容请点击《管理系统专题》进行学习
来源:https://blog.csdn.net/Floris_lovelace/article/details/80860470
标签:java,学生选课,选课系统
0
投稿
猜你喜欢
C++报错:Id returned 1exit status的解决办法
2023-08-27 19:47:12
SpringTask实现定时任务方法讲解
2022-06-11 11:43:42
springboot为异步任务规划自定义线程池的实现
2022-12-05 01:13:02
C#中的延时函数sleep
2022-08-05 13:16:49
深入了解c# 匿名类型
2022-04-08 22:38:21
使用springCloud+nacos集成seata1.3.0搭建过程
2022-06-19 02:48:47
c#使用S22.Imap收剑灵激活码邮件代码示例(imap收邮件)
2022-11-27 20:59:37
详解Java的Hibernate框架中的List映射表与Bag映射
2021-09-16 05:35:05
C#利用正则判断输入是否为纯数字、容器类
2022-10-10 03:00:39
c# 可选参数、命名参数
2022-08-06 05:38:41
java服务器的简单实现过程记录
2023-01-18 06:44:34
android 电话状态监听(来电和去电)实现代码
2023-11-21 13:17:26
java日期操作工具类(获取指定日期、日期转换、相隔天数)
2023-11-28 06:42:53
Android实用的代码片段 常用代码总结
2022-02-02 20:29:53
Android开发仿映客送礼物效果
2023-03-24 01:11:12
Springcloud Eureka配置及集群代码实例
2021-09-19 05:54:52
C#实现输入10个数存入到数组中并求max和min及平均数的方法示例
2023-11-30 05:58:54
Spring 中 PageHelper 不生效问题及解决方法
2021-11-25 11:25:04
Android定时器Timer的停止和重启实现代码
2022-10-03 23:25:43
Android中实现GPS定位的简单例子
2021-11-24 21:01:25