好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现学生选课系统

本文为大家分享了java实现学生选课系统的具体代码,供大家参考,具体内容如下

案例要求:

学生(学号,姓名,专业,所选课程{<3})
老师(工号,姓名,所教课程{<3})
课程(课程号,课程名,学分,教师,已选课学生{<30})

选课系统代码如下:

?

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

//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;

  }

 

 

}

?

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

/**

   * 课程

*/

 

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

 

  }

 

 

}

?

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

/**

  * 学生代码

  * @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;

  }

 

 

}

?

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

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实现学生选课系统的详细内容...

  阅读:26次