Catalog
  1. 1. 题目
  2. 2. 项目简介
  3. 3. 功能描述
  4. 4. 代码清单
数据结构课程设计实验一

题目

考试报名管理系统

项目简介

​ 考试报名给各高校报名工作带来了新的挑战,给教务管理部门增加了很大的工作量,报名数据手工录入既费时又会不可避免地出现错误。本项目是对考试报名管理的简单模拟,请用菜单选择方式完成下列功能:输入考生信息;输出考生信息;查询考生信息;添加考生信息;修改考生信息;删除考生信息。

功能描述

  1. 打开功能:可在用户界面从.txt文件中的信息显示到表格中。

  2. 显示功能:完成全部记录的显示。

  3. 查找功能:完成按姓名查找学生信息功能并显示,可分为精确查询和模糊查询。

  4. 排序功能:按录入的准考证号依次排序显示。

  5. 编辑功能:对指定的学生的信息进行编辑,更改ArrayList中的值。

  6. 删除功能:选中学生,将其从ArrayList中删除。

  7. 保存功能:将查询到的学生信息保存在选择目录的.txt文件下。

代码清单

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
//(1)	GUI
package data_structure;

import java.awt.*;
import java.util.Vector;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.io.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

public class GUI extends JFrame implements ActionListener{
private JTable jtb;
private DefaultComboBoxModel<String> combo;
private JTextField jtf;
private DefaultTableModel table;
private ArrayList<Management> stu;
private JFileChooser jfc;
private Comparator<Management> cmp;

public GUI(){
super("考试报名管理系统");
this.setBounds(350, 150, 1200, 700);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
this.jfc = new JFileChooser();
this.jfc.setFileFilter(new Filter("文本文件(*.txt)","txt"));

//顶部菜单栏
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("文件");
JMenuItem jm1 = new JMenuItem("保存");
jm1.addActionListener(this);
JMenuItem jm2 = new JMenuItem("打开");
jm2.addActionListener(this);
jm.add(jm1);
jm.add(jm2);
jmb.add(jm);
jmb.setBounds(0,0,1200,30);
this.add(jmb);

//按钮面板
JPanel jp = new JPanel();
//下拉列表
combo = new DefaultComboBoxModel<String>();
JComboBox<String> jcb = new JComboBox<String>(combo);
combo.addElement("模糊查询");
combo.addElement("精确查询");
jcb.setFont(new Font("宋体",Font.PLAIN,20));
jp.add(jcb);
//标签
JLabel jl = new JLabel("输入学生姓名");
jl.setFont(new Font("宋体",Font.PLAIN,20));
jp.add(jl);
//输入框
jtf = new JTextField("",20);
jtf.setFont(new Font("宋体",Font.PLAIN,20));
jp.add(jtf);
//按钮
String[] buttonNames = {"查询", "编辑", "添加", "删除"};
for(int i = 0; i < buttonNames.length; i++) {
JButton jb = new JButton(buttonNames[i]);
jb.setFont(new Font("宋体",Font.PLAIN,20));
jb.addActionListener(this);
jp.add(jb);
}
jp.setBounds(0,30,1200,50);
this.add(jp);

//显示信息的表格
Vector<String> column = new Vector<String>();
String[] columnNames = {"准考证号", "姓名", "性别", "年龄", "成绩"};
for(int i = 0; i < columnNames.length; i++)
column.add(columnNames[i]);

table = new DefaultTableModel(column, 0);
jtb = new JTable(table);
JScrollPane jsp = new JScrollPane(jtb);
jtb.setRowHeight(30);//设置表格行宽
jtb.setFont(new Font("宋体",Font.PLAIN,25));//设置表格字体
JTableHeader head = jtb.getTableHeader();
head.setPreferredSize(new Dimension(head.getWidth(),40));//设置表头大小
head.setFont(new Font("微软雅黑",Font.PLAIN,25));//设置表头字体

jtb.setBounds(10,120,1160,500);
jsp.setBounds(10,100,1160,500);
this.add(jsp);

//排序
cmp = Comparator.comparingInt(m -> Integer.valueOf(m.examno));

stu = new ArrayList<Management>();
this.setVisible(true);//设为可见

}

//把stu中的信息显示到表格里
public static void showStu(DefaultTableModel table, ArrayList<Management> stu) {
for(Management m:stu) {
String[] info = new String[]{m.examno, m.name, m.sex, String.valueOf(m.age), String.valueOf(m.grade)};
table.addRow(info);
}
}

public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "查询":
switch(combo.getSelectedItem().toString()) {
case "模糊查询":
table.setRowCount(0);
String inputF = jtf.getText().trim();
for(Management m: stu) {
if(m.name.contains(inputF)) {
String[]info = new String[]{m.examno, m.name, m.sex, String.valueOf(m.age), String.valueOf(m.grade)};
table.addRow(info);
}
}
break;
case "精确查询":
table.setRowCount(0);
String inputA = jtf.getText().trim();
for(Management m: stu) {
if(m.name.equals(inputA)) {
String[]info = new String[]{m.examno, m.name, m.sex, String.valueOf(m.age),
String.valueOf(m.grade)};
table.addRow(info);
}
}
break;
}
break;

case "编辑":
new EditFrame(table, stu, jtb);
break;

case "添加":
new AddFrame(table, stu);
break;

case "删除":
int row = jtb.getSelectedRow();//获取要删除的行号
stu.remove(row);//删除所选行
// refresh(table, stu);
table.setRowCount(0);
Collections.sort(stu, cmp);
showStu(table,stu);//重新打开
break;

case "保存":
if(jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
Filter filter = (Filter) jfc.getFileFilter();
String ends = filter.getExt();
File newFile= new File(jfc.getSelectedFile().getAbsolutePath() + '.' + ends);
if (!newFile.exists()) {
try {
newFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
IO.writeTo(newFile, stu);
}
break;

case "打开":
if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
stu.clear();
IO.readFrom(file, stu);
table.setRowCount(0);
showStu(table, stu);
}
break;
}
}

public static void main(String[] args) {
new GUI();
}
}

//(2) EditFrame
package data_structure;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class EditFrame extends JFrame implements ActionListener {
private JTextField[] jtf;
private ArrayList<Management> stu;
private DefaultTableModel table;
private int notNull = 1;
private Comparator<Management> cmp;
private int row;

EditFrame(DefaultTableModel table, ArrayList<Management> stu, JTable jtb) {
super("Edit Student");
this.stu = stu;
this.table = table;
this.setBounds(700, 260, 500, 480);
this.setLayout(new GridLayout(6, 1));

cmp = Comparator.comparingInt(s -> Integer.parseInt(s.examno));

JPanel[] jp = new JPanel[6];
for (int i = 0; i < jp.length; i++) {
jp[i] = new JPanel();
}

jtf = new JTextField[5];
for (int i = 0; i < jtf.length; i++) {
jtf[i] = new JTextField("", 10);
jtf[i].setFont(new Font("宋体",Font.PLAIN,20));
String[] columnNames = {"准考证号", "姓名", "性别", "年龄", "成绩"};
JLabel jlb = new JLabel(columnNames[i]);
jlb.setFont(new Font("宋体",Font.PLAIN,20));
jp[i].add(jlb);
jp[i].add(jtf[i]);
}
row = jtb.getSelectedRow();
jtf[0].setText(stu.get(row).examno);
jtf[1].setText(stu.get(row).name);
jtf[2].setText(stu.get(row).sex);
jtf[3].setText(String.valueOf(stu.get(row).age));
jtf[4].setText(String.valueOf(stu.get(row).grade));


String[] buttonNames = {"确认", "取消"};
for (String buttonName : buttonNames) {
JButton button = new JButton(buttonName);
button.setFont(new Font("宋体", Font.PLAIN, 20));
button.addActionListener(this);
jp[5].add(button);
}

for (JPanel jPanel : jp) this.add(jPanel);

this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "确认":
stu.remove(row);
for (JTextField jTextField : jtf)
if (jTextField.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "添加学生信息不能为空");
notNull = 0;
this.setVisible(false);
break;
}
if (notNull == 1) {
Management newstu = new Management(jtf[0].getText(), jtf[1].getText(), jtf[2].getText(),
Integer.parseInt(jtf[3].getText().trim()), Double.parseDouble(jtf[4].getText().trim()));
stu.add(newstu);
stu.sort(cmp);
table.setRowCount(0);
GUI.showStu(table, stu);
this.setVisible(false);
}
break;

case "取消":
this.setVisible(false);
break;
}
}
}

//(3) AddFrame
package data_structure;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.Comparator;

public class AddFrame extends JFrame implements ActionListener{
private JTextField[] jtf;
private ArrayList<Management> stu;
private DefaultTableModel table;
private int notNull = 1;
private Comparator<Management> cmp;

AddFrame(DefaultTableModel table, ArrayList<Management> stu) {
super("添加学生信息");
this.stu = stu;
this.table = table;
this.setBounds(700, 260, 500, 480);
this.setLayout(new GridLayout(6,1));

cmp = Comparator.comparingInt(s -> Integer.parseInt(s.examno));

JPanel[] jp = new JPanel[6];
for(int i = 0; i < jp.length; i++) {
jp[i] = new JPanel();
}

jtf = new JTextField[5];
for(int i = 0; i < jtf.length; i++) {
JTextField jtfAdd = new JTextField("", 10);
jtfAdd.setFont(new Font("宋体",Font.PLAIN,20));
jtf[i] = jtfAdd;
String[] columnNames = {"准考证号", "姓名", "性别", "年龄", "成绩"};
JLabel jl = new JLabel(columnNames[i]);
jl.setFont(new Font("宋体",Font.PLAIN,20));
jp[i].add(jl);
jp[i].add(jtf[i]);
}

String[] buttonNames = {"确认", "取消"};
for (String buttonName : buttonNames) {
JButton button = new JButton(buttonName);
button.setFont(new Font("宋体", Font.PLAIN, 20));
button.addActionListener(this);
jp[5].add(button);
}

for (JPanel jPanel : jp) this.add(jPanel);

this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "确认":
for (JTextField jTextField : jtf)
if (jTextField.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "添加学生信息不能为空");
notNull = 0;
this.setVisible(false);
break;
}
if(notNull == 1) {
Management newstu = new Management(jtf[0].getText(), jtf[1].getText(), jtf[2].getText(),
Integer.parseInt(jtf[3].getText().trim()),Double.parseDouble(jtf[4].getText().trim()));
stu.add(newstu);
stu.sort(cmp);
table.setRowCount(0);
GUI.showStu(table, stu);
this.setVisible(false);
}
break;
case "取消":
dispose();
break;
}
}
}

//(4) Filter
package data_structure;

import java.io.File;

public class Filter extends javax.swing.filechooser.FileFilter {
private String desc, ext;

Filter(String description, String extension) {
this.desc = description;
this.ext = extension.toLowerCase();
}

public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(this.ext);
}

public String getDescription() {
return this.desc;
}

String getExt() {
return this.ext;
}
}

//(5) IO
package data_structure;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import javax.swing.JOptionPane;

class IO {
static void readFrom(File file, ArrayList<Management> arr) {
try {
BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
String line;
while((line=bfr.readLine())!=null) {
String[]keys = line.split(";");
Management a = new Management(keys[0],keys[1],keys[2],Integer.parseInt(keys[3]),Double.parseDouble(keys[4]));
arr.add(a);
}
bfr.close();
}

catch(FileNotFoundException ex) {
if(!file.getName().equals(""))
JOptionPane.showMessageDialog(null, "\""+file.getName()+"\"文件不存在。");
}
catch(IOException ex) {
JOptionPane.showMessageDialog(null, "读取文件时数据错误");
}
}

static void writeTo(File file, ArrayList<Management> arr) {
try {
BufferedWriter bufrd = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
String line = new String("");
for(Management s:arr) {
line = s.toString();
bufrd.write(line + "\r\n");
}
bufrd.close();
}
catch(FileNotFoundException ex) {
if (!file.getName().equals(""))
JOptionPane.showMessageDialog(null, "\"" + file.getName() + "\"文件不存在。");
}
catch(IOException ex) {
JOptionPane.showMessageDialog(null, "写入文件时数据错误");
}
}
}

//(6) Management
package data_structure;

public class Management {

String examno; //准考证号
String name; //姓名
String sex; //性别
int age; //年龄
double grade; //成绩

//无参构造方法
public Management() {
super();
}

//有参构造方法
Management(String examno, String name, String sex, int age, double grade) {
super();
this.examno = examno;
this.name = name;
this.sex = sex;
this.age = age;
this.grade = grade;
}

public String toString(){
return this.examno + ";" + this.name + ";" + this.sex + ";" + String.valueOf(this.age) + ";"
+ String.valueOf(this.grade) ;
}
}
Author: Christopher Shen
Link: https://www.pasxsenger.com/2020/02/25/数据结构课程设计实验一/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶