国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java實現選課系統

java實現選課系統

2021-07-16 15:56wrxingkong Java教程

這篇文章主要為大家詳細介紹了java實現選課系統,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現選課系統的具體代碼,供大家參考,具體內容如下

這個程序主要是練習io(文件讀寫,序列化),集合框架的使用

學生端可以實現,查找課程,增加選課,刪除選課的功能

管理端可以實現對備選課程,學生信息的增刪查改

缺點:登陸操作沒有實現密碼驗證和多態。           

另外map對象明明put了,可是get的時候竟然會取到null,而且嘗試多次,有時候成功,有時候取到null,并不確定。據說這是由多線程引起的map取值為null,因為多線程部分還沒開始學習,所以也沒做修改。

?
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
//課程信息
package selectcourse;
 
import java.io.serializable;
 
public class course implements serializable{
 private string id;
 private string name;
 
 public course(string id, string name) {
 super();
 this.id = id;
 this.name = name;
 }
 
 public course() {
 super();
 }
 
 public string getid() {
 return id;
 }
 
 public void setid(string id) {
 this.id = id;
 }
 
 public string getname() {
 return name;
 }
 
 public void setname(string name) {
 this.name = name;
 }
 
 public int hashcode() {
 final int prime = 31;
 int result = 1;
 result = prime * result + ((id == null) ? 0 : id.hashcode());
 result = prime * result + ((name == null) ? 0 : name.hashcode());
 return result;
 }
 public boolean equals(object obj) {
 if (this == obj)
  return true;
 if (obj == null)
  return false;
 if (getclass() != obj.getclass())
  return false;
 course other = (course) obj;
 if (id == null) {
  if (other.id != null)
  return false;
 } else if (!id.equals(other.id))
  return false;
 if (name == null) {
  if (other.name != null)
  return false;
 } else if (!name.equals(other.name))
  return false;
 return true;
 }
 
 public string tostring() {
 return "課程號:" + id + " " + "課程名:" + name;
 }
 
}
?
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
//學生信息
package selectcourse;
 
import java.io.serializable;
import java.util.hashset;
import java.util.set;
 
public class student implements serializable,comparable<student>{
 
 private int id;
 private string name;
 private set<course> courses;
 
 public student(int id, string name) {
 super();
 this.id = id;
 this.name = name;
 this.courses = new hashset<course>();
 }
 
 public student() {
 super();
 this.id = 0;
 this.name = null;
 this.courses = new hashset<course>();
 }
 
 public int getid() {
 return id;
 }
 
 public void setid(int id) {
 this.id = id;
 }
 
 public string getname() {
 return name;
 }
 
 public void setname(string name) {
 this.name = name;
 }
 
 public set<course> getcourses() {
 return courses;
 }
 
 public void setcourses(set<course> courses) {
 this.courses = courses;
 }
 
 public int hashcode() {
 final int prime = 31;
 int result = 1;
 result = prime * result + id;
 return result;
 }
 
 public boolean equals(object obj) {
 if (this == obj)
  return true;
 if (obj == null)
  return false;
 if (getclass() != obj.getclass())
  return false;
 student other = (student) obj;
 if (id != other.id)
  return false;
 return true;
 }
 
 
 public string tostring() {
 return "學號:"+id+" " +"姓名:"+name;
 }
 //遍歷輸出所選課程
 public void travese()
 {
 if(courses.size()>0)
 {
 for (course course : courses) {
  system.out.println(course);
 }
 }
 else
 {
  system.out.println("還沒有選課");
 }
 }
 
 public int compareto(student s) {
 
  int result=this.id-s.id;
  return result;
 }
 
 
}
?
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
261
262
263
264
265
266
267
268
269
270
271
//管理端
package selectcourse;
 
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream;
import java.util.arraylist;
import java.util.collections;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.scanner;
 
public class administratorop {
//管理端,用來管理學生信息和備選課程
 list<student> students = new arraylist<student>();
 map<integer, student> map1 = new hashmap<integer, student>();
 list<course> courses = new arraylist<course>();
 map<string, course> map2 = new hashmap<string, course>();
 scanner in = new scanner(system.in);
 
 public administratorop() {
 }
 
//~~~~~~~~~~~~~~~~~從文件讀入list~~~~~~~~~~~~~~~~~~~~~
 public void load1() {
 file file = new file("students.txt");
 if (!file.exists()) {
  try {
  file.createnewfile();
  } catch (ioexception e) {
  e.printstacktrace();
  }
 }
 fileinputstream fis;
 try {
  fis = new fileinputstream(file);
  objectinputstream ois = new objectinputstream(fis);
  students = (list<student>) ois.readobject();
  ois.close();
  fis.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 } catch (classnotfoundexception e) {
  e.printstacktrace();
 }
 
 }
 
 public void load2() {
 file file = new file("courses.txt");
 if (!file.exists()) {
  try {
  file.createnewfile();
  } catch (ioexception e) {
  e.printstacktrace();
  }
 }
 fileinputstream fis;
 try {
  fis = new fileinputstream(file);
  objectinputstream ois = new objectinputstream(fis);
  courses = (list<course>) ois.readobject();
  ois.close();
  fis.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 } catch (classnotfoundexception e) {
  e.printstacktrace();
 }
 
 }
 
//將信息寫回文件
 public void save1() {
 file file = new file("students.txt");
 fileoutputstream fos;
 try {
  fos = new fileoutputstream(file);
  objectoutputstream oos = new objectoutputstream(fos);
  oos.writeobject(students);
  oos.close();
  fos.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  // todo 自動生成的 catch 塊
  e.printstacktrace();
 }
 
 }
 
 public void save2() {
 file file = new file("courses.txt");
 fileoutputstream fos;
 try {
  fos = new fileoutputstream(file);
  objectoutputstream oos = new objectoutputstream(fos);
  oos.writeobject(courses);
  oos.close();
  fos.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  // todo 自動生成的 catch 塊
  e.printstacktrace();
 }
 
 }
 
//~~~~~~~~~~~~~~~~~~~~~~~~~ 根據list來創建map~~~~~~~~~~~~~~~~~~~~~~~
 public void createmap1() {
 for (int i = 0; i < students.size(); i++) {
  map1.put(students.get(i).getid(), students.get(i));
 }
 }
 
 public void createmap2() {
 for (int i = 0; i < courses.size(); i++) {
  map2.put(courses.get(i).getid(), courses.get(i));
 }
 }
 
 // ~~~~~~~~~~~~~~~~~~~~~~ 增刪查改~~~~~~~~~~~~~~~~~~~~~~~
 // 增加學生基本信息
 public void add() {
 system.out.println("輸入學生信息,輸入0結束");
 while (true) {
  int id = in.nextint();
  if(id!=0) {
  string name = in.next();
  student s = new student(id, name);
  students.add(s);
  collections.sort(students);
  map1.put(id, s);
  system.out.println("添加成功");
  }
  if (id == 0) {
  break;
  }
 }
 }
 
 // 刪除學生信息
 public void del() {
 while(true) {
  int id = in.nextint();
  student s = map1.get(id);
  students.remove(s);
  map1.remove(id);
  system.out.println("移除成功");
  if (id == 0) {
  break;
  }
 }
 }
 // 增加課程基本信息
 public void add2() {
 system.out.println("輸入課程信息,輸入end結束");
 while (true) {
  string id = in.nextline();
  if(!id.equals("end"))
  {
  string name = in.nextline();
  course cr = new course(id, name);
  courses.add(cr);
  map2.put(id, cr);
  system.out.println("添加成功");
  }
  else{
  //system.out.println("添加結束");
  break;
  }
 }
 }
 
 // 刪除課程信息
 public void del2() {
 while(true) {
  string id = in.next();
  if(!id.equals("end")) {
  course cr = map2.get(id);
  courses.remove(cr);
  map2.remove(id);
  system.out.println("移除成功");
  }
  else
  {
  break;
  }
 }
 }
 
 // 根據學號查找學生
 public void query1() {
 system.out.println("請輸入要查詢的學生學號:");
 if (in.hasnext()) {
  int id = in.nextint();
  system.out.println(map1.get(id));
  map1.get(id).travese();
 }
 }
 
 // 根據課程號查找課程
 public void query2() {
 system.out.println("請輸入要查詢的課程號:");
 if (in.hasnext()) {
  string id = in.nextline();
  system.out.println(map2.get(id));
 }
 }
 
 // 修改學生基本信息
 public void modify1() {
 system.out.println("請輸入要修改的學生的學號:");
 int id = in.nextint();
 student s = map1.get(id);
 system.out.println("輸入修改后的學生信息:");
 int no = in.nextint();
 string name = in.next();
 int i = students.indexof(s);
 students.set(i, new student(no, name));
 collections.sort(students);
 map1.remove(id);
 map1.put(no, new student(no, name));
 system.out.println("修改成功");
 }
 
 // 修改課程信息
 public void modify2() {
 system.out.println("請輸入要修改的課程的課程號:");
 string id = in.nextline();
 course cr = map2.get(id);
 system.out.println("輸入修改后的課程信息:");
 string no = in.nextline();
 string name = in.nextline();
 int i = courses.indexof(cr);
 courses.set(i, new course(no, name));
 map2.remove(id);
 map2.put(no, new course(no, name));
 system.out.println("修改成功");
 }
 
// ~~~~~~~~~~~~~~~~~~~~~~ 遍歷list~~~~~~~~~~~~~~~~~~~~~~~
 void display1() {
 system.out.println("所有的學生信息:");
 for (student s : students) {
  system.out.println(s.tostring());
  s.travese();
 }
 }
 
 void display2() {
 system.out.println("所有的備選課程信息:");
 for (course course : courses) {
  system.out.println(course.tostring());
 }
 }
public void close()
{
 in.close();
}
}
?
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
//學生操作端
package selectcourse;
 
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.scanner;
 
public class studentop {
 scanner in = new scanner(system.in);
 student st;
 list<student> students = new arraylist<student>();
 list<course> courses = new arraylist<course>();
 map<string, course> map = new hashmap<string, course>();
 
 public studentop(int no) {
 load3(no);
 load4();
 
 }
 
 // ~~~~~~~~~~~~~~~~~從文件讀入信息~~~~~~~~~~~~~~~~~~~~~
 public void load3(int n) {
 file file = new file("students.txt");
 fileinputstream fis;
 try {
  fis = new fileinputstream(file);
  objectinputstream ois = new objectinputstream(fis);
  students = (list<student>) ois.readobject();
  ois.close();
  fis.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 } catch (classnotfoundexception e) {
  e.printstacktrace();
 }
 for (int i = 0; i < students.size(); i++) {
  if (n == students.get(i).getid()) {
  st = students.get(i);
  break;
  }
 }
 
 }
 
 public void load4() {
 file file = new file("courses.txt");
 if (!file.exists()) {
  try {
  file.createnewfile();
  } catch (ioexception e) {
  e.printstacktrace();
  }
 }
 fileinputstream fis;
 try {
  fis = new fileinputstream(file);
  objectinputstream ois = new objectinputstream(fis);
  courses = (list<course>) ois.readobject();
  ois.close();
  fis.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 } catch (classnotfoundexception e) {
  e.printstacktrace();
 }
 
 }
 
// 將信息寫回文件
 public void save3() {
 file file = new file("students.txt");
 fileoutputstream fos;
 try {
  fos = new fileoutputstream(file);
  objectoutputstream oos = new objectoutputstream(fos);
  oos.writeobject(students);
  oos.close();
  fos.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  // todo 自動生成的 catch 塊
  e.printstacktrace();
 }
 
 }
 
 public void save4() {
 file file = new file("courses.txt");
 fileoutputstream fos;
 try {
  fos = new fileoutputstream(file);
  objectoutputstream oos = new objectoutputstream(fos);
  oos.writeobject(courses);
  oos.close();
  fos.close();
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  // todo 自動生成的 catch 塊
  e.printstacktrace();
 }
 
 }
 
//~~~~~~~~~~~~~~~~~~~~~~~~~ 根據list來創建map~~~~~~~~~~~~~~~~~~~~~~~
 public void createmap() {
 for (int i = 0; i < courses.size(); i++) {
  map.put(courses.get(i).getid(), courses.get(i));
 }
 
 //遍歷map
  /*set<string> set = map.keyset();
 iterator<string> iterator = set.iterator();
 while (iterator.hasnext()) {
  string key = iterator.next();
  system.out.println(key + " " + map.get(key));
 } */
 }
 
//遍歷顯示備選課程
 public void displayallcourse() {
 system.out.println("所有的備選課程信息:");
 for (course course : courses) {
  system.out.println(course.tostring());
 }
 }
 
//根據課程號查詢備選課程
 public void querycourse() {
 system.out.println("請輸入要查詢的課程號:");
 string str = in.next();
 system.out.println(str);
 system.out.println((map.containskey(str) ? "yes" : "no"));
 system.out.println(map.get(str));
 }
 
//顯示所選課程
 public void display() {
 system.out.println("所選課程:");
 st.travese();
 }
 
//增加所選課程
 public void addselect() {
 system.out.println("輸入所選課程的課程號,輸入end結束");
 while (true) {
  string id = in.nextline();
  if (!id.equals("end")) {
  course cr = map.get(id);
  st.getcourses().add(cr);
  system.out.println("選課成功");
  } else {
  // system.out.println("添加結束");
  break;
  }
 }
 }
 
//減少所選課程
 public void deleteselect() {
 system.out.println("要刪除課程的課程號,輸入end結束");
 while (true) {
  string id = in.nextline();
  if (!id.equals("end")) {
  course cr = map.get(id);
  st.getcourses().remove(cr);
  system.out.println("刪除成功");
  } else {
  // system.out.println("添加結束");
  break;
  }
 }
 }
 
 public void close() {
 in.close();
 }
}
?
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
//測試類
package selectcourse;
 
import java.util.scanner;
 
public class test {
 
 public static void main(string[] args) {
//~~~~~~~~~~~~~測試管理端~~~~~~~~~~~~~~~~~~~~~~~~~~
   /*添加學生
 administratorop a1=new administratorop();
 a1.add();
 //a1.display1();
 // a1.close();
 a1.save1();*/
 /*添加課程
 administratorop a2=new administratorop();
 a2.add2();
 //a2.display2();
 a2.close();   
   a2.save2();*/
/* // 測試刪除,查找,修改
   administratorop a3=new administratorop();
 a3.load1();
 a3.createmap1();
 a3.load2();
 a3.createmap2();
// a3.display1();
// a3.display2();
// a3.del();
// a3.display1();
// a3.del2();
// a3.display2();
// a3.query1();
// a3.query2();
// a3.modify1();
// a3.display1();
// a3.modify2();
// a3.display2();
    a3.close();
    // a3.save1();
    // a3.save2();*/
//~~~~~~~~~~~~~~~~測試學生端~~~~~~~~~~~~~~~~~~~~~~~~~
  /*scanner in=new scanner(system.in);
  system.out.println("請輸入學號:");
  int id=in.nextint();
 studentop sto=new studentop(id);
 sto.createmap();
 //sto.displayallcourse();
 //sto.querycourse();
 
// sto.addselect();
// sto.deleteselect();
 sto.display();
 sto.close();
 in.close();
// sto.save3();
// sto.save4();
*/ }
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/wrxingkong/article/details/81486148

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品福利在线观看 | 国产精品观看 | 在线a视频 | 91av视频在线 | 91在线视频观看 | 99久久99久久精品 | 免费成人av网站 | 国产精品一区二区三区在线播放 | 在线观看日韩av | 亚洲伦理一区二区 | 国产一级黄色av | 国产一区二区三区撒尿在线 | 成人在线免费观看 | 亚洲午夜在线 | 2020国产在线| 九九视频在线 | 国产高清在线精品一区二区三区 | 99re6在线视频精品免费 | 91久久精品一区二区二区 | 亚洲精品国偷拍自产在线观看 | 精品视频久久 | 日韩影音 | 一区二区三区 在线 | 国产精品国产三级国产aⅴ 成人在线免费看 | 成人国产在线 | 淫语视频 | 狠狠干美女 | 精品久久久久久 | 亚洲乱码国产乱码精品精的特点 | 欧洲精品视频在线观看 | 欧美一区二区在线刺激视频 | www.久久久久久久 | 成人av免费 | 欧美一区二区三 | 国产一区 欧美 | 日韩成人精品 | 久草在线 | 欧美亚洲91 | 精品久久久久一区二区国产 | 一级片黄 | www.中文字幕.com|