-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path更新的学生类
560 lines (503 loc) · 16.9 KB
/
更新的学生类
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package student;
import java.awt.event.*;
import java.util.LinkedList;
import java.awt.*;
public class StudentClass {
public static void main(String args[])
{
new StudentFrame();
}
}
class StudentFrame extends Frame implements ActionListener,ItemListener{
Label prompt1, prompt2, prompt3, prompt4, prompt5, prompt6, prompt7, prompt8, prompt9,prompt10;
TextField input1,input2,input3,input4,input5,input6,input7;
TextArea output;
CheckboxGroup style1,style2;
Checkbox stu,tea,man,woman;
Button btn,btn1,btn2,btn3,btn4,btn5,btn6;
String name,identity,sex,mailbox,professions,courses,id,tel,salary;//姓名 身份 性别 邮箱 专业 课程
//学号 电话 薪水
Boolean stuOrtea,manOrwoman;
LinkedList<Student> students =new LinkedList<Student>();
LinkedList<Student> teachers =new LinkedList<Student>();
StudentFrame(){
super("学生教师application");
prompt1 = new Label("姓 名:");
prompt2 = new Label("学号/工号:");
prompt3 = new Label("身 份:");
prompt4 = new Label("性 别:");
prompt5 = new Label("专 业:");
prompt6 = new Label("所 修 课 程:");
prompt7 = new Label("联 系 电 话:");
prompt8 = new Label("邮 箱:");
prompt9 = new Label("工 资:");
prompt10 = new Label("信 息 输 出 文 本 框 :");
input1 = new TextField(20);
input2 = new TextField(20);
input3 = new TextField(20);
input4 = new TextField(20);
input5= new TextField(20);
input6 = new TextField(20);
input7 = new TextField("无",20);
output = new TextArea("在修改信息前请先通过学号查寻到需要"+"\n"+"修改信息的个人,然后在相应的文本框"
+"\n"+ "中修改信息,本程序只支持修改"+"\n"+"非(姓名,性别,身份,学号)"+"\n只支持通过学号或工号查找",10,30);
style1 = new CheckboxGroup();
style2 = new CheckboxGroup();
stu = new Checkbox("学生 ", false,style1);
tea = new Checkbox("教师 ", false,style1);
man = new Checkbox("男 ", false,style2);
woman = new Checkbox("女 ", false,style2);
btn = new Button("加入信息");
btn1 = new Button("修改信息");
btn2 = new Button("学号查询");
btn3 = new Button("输出所有");
btn4 = new Button("删除信息");
btn5 = new Button("专业查询");
btn6 = new Button("姓名查询");
output.setBackground(Color.lightGray);
output.setForeground(Color.blue);
setLayout(new FlowLayout());//少不得,否则add添加的东西会一一覆盖,最后只有最后一个add能显示
setSize(300,550);
setBackground(Color.lightGray);
setVisible(true);
add(prompt1); add(input1);
add(prompt2); add(input2);
add(prompt3); add(stu);add(tea);
add(prompt4); add(man);add(woman);
add(prompt5); add(input3);
add(prompt6); add(input4);
add(prompt7); add(input5);
add(prompt8); add(input6);
add(prompt9); add(input7);
add(prompt10); add(output);
add(btn);
add(btn4);
add(btn1);
add(btn6);
add(btn2);
add(btn5);
add(btn3);
addWindowListener(new windowsClose());
btn.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn.setBackground(Color.lightGray);
btn1.setBackground(Color.lightGray);
btn2.setBackground(Color.lightGray);
btn3.setBackground(Color.lightGray);
btn4.setBackground(Color.lightGray);
btn5.setBackground(Color.lightGray);
btn6.setBackground(Color.lightGray);
stu.addItemListener(this);
tea.addItemListener(this);
man.addItemListener(this);
woman.addItemListener(this);
input1.addFocusListener(new HandleFocus());
input2.addFocusListener(new HandleFocus());
input3.addFocusListener(new HandleFocus());
input4.addFocusListener(new HandleFocus());
input5.addFocusListener(new HandleFocus());
input6.addFocusListener(new HandleFocus());
input7.addFocusListener(new HandleFocus());
}//构造函数
private void setBackground(String string) {
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent e) {
name = input1.getText();
id = input2.getText();
professions = input3.getText();
courses = input4.getText();
tel = input5.getText();
mailbox = input6.getText();
salary = input7.getText();
if(e.getSource()==btn) {//如果是加入按钮触发
try{
if(stuOrtea) {//如果是学生
Student stude = new Student(name, identity, sex, mailbox, professions, courses, id, tel);
if( StudentList.addStudent(students, stude))//像链表students中加入一个Student类的引用
output.setText(StudentList.getStudent(students, id).getsAll());//在文本框中输出刚加入的信息
else
output.setText("该学号已存在!");
}else {//如果是教师
Student stude = new Student(name, identity, sex, mailbox, professions, courses, id, tel,salary);
if(StudentList.addStudent(teachers, stude))
output.setText(StudentList.getStudent(teachers, id).gettAll());
else
output.setText("该工号已存在!");
}
}catch(Exception e1){
output.setText("未输入信息。。。");
}
}else
if(e.getSource()==btn1) {//如果触发修改按钮
if(StudentList.seekIdAll(students, id)) {
Student p = StudentList.getStudent(students, id);
p.setsall( mailbox, professions, courses, tel);
output.setText(p.getsAll());
}
else if(StudentList.seekIdAll(teachers, id)){
Student p = StudentList.getStudent(teachers, id);
p.settall(mailbox, professions, courses, tel,salary);
output.setText(p.gettAll());
}
}else
if(e.getSource()==btn2) {//如果触发学号查询按钮
if(StudentList.seekIdAll(students, id)) {
Student p = StudentList.getStudent(students, id);
input1.setText(p.getName());
input3.setText(p.getProfessions());
input4.setText(p.getCourses());
input5.setText(p.getTel());
input6.setText(p.getMailbox());
output.setText(p.getsAll());
}
else
if(StudentList.seekIdAll(teachers, id)){
Student p = StudentList.getStudent(teachers, id);
input1.setText(p.getName());
input3.setText(p.getProfessions());
input4.setText(p.getCourses());
input5.setText(p.getTel());
input6.setText(p.getMailbox());
input7.setText(p.getSalary());
output.setText(p.gettAll());
}else {
output.setText("查无此人!");
}
}else
if(e.getSource()==btn3) {//如果触发输出所有按钮
if(StudentList.isExit(students)){
output.setText("以下是学生的全部信息:"+"\n");
for (Student st: students)
output.append(st.getsAll());
}else{
output.setText("未查找到任何学生信息。。。"+"\n");
}
if(StudentList.isExit(teachers)){
output.append("以下是教师的全部信息:"+"\n");
for (Student st: teachers)
output.append(st.gettAll());
}else{
output.append("未查找到任何教师信息。。。"+"\n");
}
}else
if(e.getSource()==btn4) {//如果触发删除信息按钮
if(StudentList.seekIdAll(students, id)) {
StudentList.deleteStudent(students, id);
}else
if(StudentList.seekIdAll(teachers, id)) {
StudentList.deleteStudent(teachers, id);
}else {
output.setText("查无此人!");
}
}else
if(e.getSource()==btn5) {//如果触发专业查询按钮
output.setText("");
if(StudentList.seekProfessionsAll(students, professions)){
for(Student st: students)
if(st.getProfessions().equals(professions))
output.append(st.getsAll());
}else{
output.append("此专业无学生信息。。。\n");
}
if(StudentList.seekProfessionsAll(teachers, professions)){
for(Student st: teachers)
if(st.getProfessions().equals(professions))
output.append(st.gettAll());
}else{
output.append("此专业无教师信息。。。\n");
}
}else
if(e.getSource()==btn6) {//如果触发姓名查询按钮
if(StudentList.seekNameAll(students, name)) {
Student p = StudentList.getStudentName(students, name);
input2.setText(p.getId());
input3.setText(p.getProfessions());
input4.setText(p.getCourses());
input5.setText(p.getTel());
input6.setText(p.getMailbox());
output.setText(p.getsAll());
}
else
if(StudentList.seekNameAll(teachers, name)){
Student p = StudentList.getStudentName(teachers, name);
input2.setText(p.getId());
input3.setText(p.getProfessions());
input4.setText(p.getCourses());
input5.setText(p.getTel());
input6.setText(p.getMailbox());
input7.setText(p.getSalary());
output.setText(p.gettAll());
}else {
output.setText("查无此人!");
}
}
}
public void itemStateChanged(ItemEvent e) {
Checkbox temp = (Checkbox)(e.getSource());
if(temp.getLabel()=="学生 ") {
stuOrtea = true;
identity = "学生";
}else
if(temp.getLabel()=="教师 "){
stuOrtea = false;
identity = "教师";
}else
if(temp.getLabel()=="男 "){
manOrwoman = true;
sex = "男";
}else
if(temp.getLabel()=="女 "){
manOrwoman = false;
sex = "女";
}
}
class HandleFocus implements FocusListener{
public void focusGained(FocusEvent e) {
TextField q = (TextField )(e.getSource());//将引发文本事件的事件源强制转换为文本类型
q.setText("");//将引发文本事件的文本设置为空
q.setBackground(Color.blue);
}
@Override
public void focusLost(FocusEvent e) {
TextField q = (TextField )(e.getSource());//将引发文本事件的事件源强制转换为文本类型
q.setForeground(Color.green);//将引发文本事件的文本设置为空
q.setBackground(Color.white);
}
}
class windowsClose extends WindowAdapter{//window监视执行函数
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}//end of winC
}//end of StufentFrame
class Student{
String name,identity,sex,mailbox,professions,courses,id,tel,salary;//姓名 身份 性别 邮箱 专业 课程
//学号 电话 薪水
Student(String a,String b,String c,String d,String f, String g,String h,String j,String k){
name = a;
identity = b;
sex = c;
mailbox = d;
professions = f;
courses = g;
id = h;
tel = j;
salary = k;
}
Student(String a,String b,String c,String d,String f, String g,String h,String j){
name = a;
identity = b;
sex = c;
mailbox = d;
professions = f;
courses = g;
id = h;
tel = j;
}
//用于输出全部信息
String getsAll() {
String s = new String("姓名:"+name+"\n"+"学号:"+id+"\n"+"身份:"+identity+"\n"+"性别:"+sex+"\n"+"专业:"+professions+"\n"+"所修课程:"+courses+"\n"+"联系电话:"+tel+"\n"+"邮箱:"+mailbox+"\n"+"\n");
return s;
}
String gettAll() {
String s = new String("姓名:"+name+"\n"+"工号:"+id+"\n"+"身份:"+identity+"\n"+"性别:"+sex+"\n"+"专业:"+professions+"\n"+"所授课程:"+courses+"\n"+"联系电话:"+tel+"\n"+"邮箱:"+mailbox+"\n"+"薪水:"+salary+"\n"+"\n");
return s;
}
//用于修改信息
/* String setsAll(String a,String b,String c,String d,String f, String g,String h,String j) {
name = a;
identity = b;
sex = c;
mailbox = d;
professions = f;
courses = g;
id = h;
tel = j;
String s = new String("姓名:"+name+"\n"+"学号:"+id+"\n"+"身份:"+identity+"\n"+"性别:"+sex+"\n"+"专业:"+professions+"\n"+"所修课程:"+courses+"\n"+"联系电话:"+tel+"\n"+"邮箱:"+mailbox+"\n"+"\n");
return s;
}
String settAll(String a,String b,String c,String d,String f, String g,String h,String j,String k) {
name = a;
identity = b;
sex = c;
mailbox = d;
professions = f;
courses = g;
id = h;
tel = j;
salary = k;
String s = new String("姓名:"+name+"\n"+"工号:"+id+"\n"+"身份:"+identity+"\n"+"性别:"+sex+"\n"+"专业:"+professions+"\n"+"所授课程:"+courses+"\n"+"联系电话:"+tel+"\n"+"邮箱:"+mailbox+"\n"+"薪水:"+salary+"\n"+"\n");
return s;
}*/
String setsall(String d,String f, String g,String j) {
mailbox = d;
professions = f;
courses = g;
tel = j;
String s = new String("联系方式:"+tel+"\n"+"邮箱:"+mailbox+"\n"+"专业:"+professions+"\n"+"所修课程:"+courses+"\n"+"\n");
return s;
}
String settall(String d,String f, String g,String j,String k) {
mailbox = d;
professions = f;
courses = g;
tel = j;
salary = k;
String s = new String("联系方式:"+tel+"\n"+"邮箱:"+mailbox+"\n"+"专业:"+professions+"\n"+"所授课程:"+courses+"\n"+"薪水:"+salary+"\n"+"\n");
return s;
}
String getName() {//获取名字
return name;
}
String setName(String a) {//修改名字
name = a;
return name;
}
String getIdentity() {//获取身份
return identity;
}
String setIdentity(String b) {//修改身份
identity = b;
return identity;
}
String getSex() {//获取性别
return sex;
}
String setSex(String c) {//修改性别
sex = c;
return sex;
}
String getProfessions() {//获取专业
return professions;
}
String setProfessionsx(String f) {//修改专业
professions = f;
return professions;
}
String getMailbox() {//获取邮箱
return mailbox;
}
String setMailbox(String d) {//修改邮箱
mailbox = d;
return mailbox;
}
String getCourses() {//获取课程
return courses;
}
String setCourses(String g) {//修改课程
courses = g;
return courses;
}
String getId() {//获取学号
return id;
}
String setId(String h) {//修改学号
id = h;
return id;
}
String getTel() {//获取电话号码
return tel;
}
String setTel(String j) {//修改号码
tel = j;
return tel;
}
String getSalary() {//获取薪水
return salary;
}
String setSalary(String k) {//修改薪水
salary = k;
return salary;
}
}//end of
class StudentList {
public static Boolean addStudent(LinkedList<Student> stu, Student s) {
for(Student st:stu){
if(st.getId().equals(s.getId()))
return false;
}
stu.add(s);
return true;
}
public static void deleteStudent(LinkedList<Student> stu, String no) {
Student student = null;
for (Student s: stu) { //遍历链表stu里的每个元素
if (no != null && s.getId().equals(no)) {
student = s;
break;
}
}
if (student != null) {
stu.remove(student);
}
}
public static Student getStudent(LinkedList<Student> stu, String id) {
Student student = null;
for (Student st: stu) { //遍历链表stu里的每个元素
if(id != null && st.getId().equals(id))//通过学号查找
{
student = st;
break;
}
}
return student;
}
public static Student getStudentName(LinkedList<Student> stu, String name) {
Student student = null;
for (Student st: stu) { //遍历链表stu里的每个元素
if(name != null && st.getName().equals(name))//通过学号查找
{
student = st;
break;
}
}
return student;
}
public static Boolean seekIdAll(LinkedList<Student> stu, String id) {//查询链表中是否有id
Boolean correct = false;
for (Student st: stu) { //遍历链表stu里的每个元素
if(id != null && st.getId().equals(id))//通过学号查找
{
correct = true;
break;
}
}
return correct;
}
public static Boolean seekProfessionsAll(LinkedList<Student> stu, String professions) {//查询链表中是否有professions
Boolean correct = false;
for (Student st: stu) { //遍历链表stu里的每个元素
if(professions != null && st.getProfessions().equals(professions))//通过专业查找
{
correct = true;
break;
}
}
return correct;
}
public static Boolean seekNameAll(LinkedList<Student> stu, String name) {//查询链表中是否有name
Boolean correct = false;
for (Student st: stu) { //遍历链表stu里的每个元素
if(name != null && st.getName().equals(name))//通过专业查找
{
correct = true;
break;
}
}
return correct;
}
public static Boolean isExit(LinkedList<Student> stu){//判断链表是否为空,如果为空返回false;不为空返回true;
if(stu.size()==0)
return false;
else
return true;
}
}//end of StudentList