本文為大家分享了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