先放个题目哈:java2021programming.pdf

首先要吐槽两个点:一个是机房的eclipse太难用了(为什么会有这么鸡肋的 Code Assistant),还有一个就是都1202年了还有人在学swing。

怎么说呢,期末第一门考试就来了个下马威(X)

直到11点机房里还是一片敲击键盘的声音…… 总体来说比去年要的工作量大一点,两个小时紧打紧敲完了,不过有不少同学都没写完。

凑巧的是文本域和下拉列表之前大作业里面用过(没错,大作业也是写swing),开卷考试准备的代码模板有个大概,算是没啥阻碍地写了过来~~,除了时间有点不够用~~。

还有是10分钟做完选择题之后一直没发题目,坐那干等;最后我都提交了才通知延长15分钟,有点搞人心态(

最后附上将近俩小时的成果:

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
// Main.java
import java.io.File;

// 主类
public class Main {
// 文件路径
public static final String inFile = System.getProperty("user.dir") + File.separator + "product.txt";
public static final String outFile = System.getProperty("user.dir") + File.separator + "result.txt";

public static void main(String[] args) {
// 创建文件对象
File in = new File(inFile);
File out = new File(outFile);
new FrmProduct(in, out); // 启动GUI
}

}

// FrmProduct.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import java.util.*;

import javax.swing.*;

public class FrmProduct extends JFrame {

private JRadioButton pidBtn; // 产品编号
private JRadioButton numBtn; // 产品库存
private ButtonGroup select; // 产品编号 or 产品库存
private JComboBox<String> typeBox; // 类型
private JTextArea jta; // 文本域
private JButton saveBtn; // 保存

private ArrayList<Product> list, show; // 数据
private File saveTo;

public FrmProduct(File from, File to) {
pidBtn = new JRadioButton("产品编号");
numBtn = new JRadioButton("产品库存");
select = new ButtonGroup();
select.add(pidBtn);
select.add(numBtn);
typeBox = new JComboBox<>(Product.TYPES);
jta = new JTextArea();
saveBtn = new JButton("保存");

saveTo = to;
list = new ArrayList<>();
show = new ArrayList<>();

Container content = getContentPane();

// 顶部
Box topLine = Box.createHorizontalBox();
topLine.add(Box.createHorizontalStrut(10));
JLabel label1 = new JLabel("排序方式:");
topLine.add(label1);
topLine.add(Box.createHorizontalStrut(10));
topLine.add(pidBtn);
topLine.add(Box.createHorizontalStrut(10));
topLine.add(numBtn);
topLine.add(Box.createHorizontalGlue());
JLabel label2 = new JLabel("选择类型:");
topLine.add(label2);
topLine.add(Box.createHorizontalStrut(10));
topLine.add(typeBox);
topLine.add(Box.createHorizontalStrut(10));
content.add(topLine, BorderLayout.NORTH);

// 显示滚动条的文本域
JScrollPane jsp = new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
content.add(jsp, BorderLayout.CENTER);

// 确认按钮
Box buttomLine = Box.createHorizontalBox();
buttomLine.add(Box.createHorizontalGlue());
buttomLine.add(saveBtn);
buttomLine.add(Box.createHorizontalGlue());
content.add(buttomLine, BorderLayout.SOUTH);

setSize(500, 400);
setTitle("查看产品");
setDefaultCloseOperation(EXIT_ON_CLOSE);

loadData(from); // 加载数据
initListener(); // 初始化监听器

pidBtn.setSelected(true);
typeBox.setSelectedIndex(0);

// 显示数据
sortShown();
setToShow();

setVisible(true);
}

// 加载数据
private void loadData(File file) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String aline = null;
while (null != (aline = br.readLine())) {
if (aline.trim().isEmpty()) {
continue;
}
Product p = new Product(aline);
list.add(p);
show.add(p);
}
br.close();
} catch(IOException e) {
System.err.println(e.getMessage());
}
}

// 初始化监听器
private void initListener() {
ItemListener litener1 = new SortListener();
pidBtn.addItemListener(litener1);
numBtn.addItemListener(litener1);
ItemListener listener2 = new FilterListener();
typeBox.addItemListener(listener2);
saveBtn.addActionListener(new ClickListener());
}

// 显示
private void setToShow() {
StringBuilder str = new StringBuilder();
for (Product p : show) {
str.append(p.toString());
str.append("\n");
}
jta.setText(str.toString());
}

// 显示数据
private void sortShown() {
if (pidBtn.isSelected()) {
Collections.sort(show, new ProductIdComparator());// 按照id排序的比较器
} else if (numBtn.isSelected()) {
Collections.sort(show, new ProductNumberComparator());// 按照库存排序的比较器
}
}

// 排序选项监听器
class SortListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == pidBtn) {
sortShown();
} else if (e.getSource() == numBtn) {
sortShown();
}
setToShow();
}
}

// 类型监听
class FilterListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
String selected = (String) typeBox.getSelectedItem();
show.clear();
for (Product p : list) {
if (selected.equals("全部") || p.getType().equals(selected)) {
show.add(p);
}
}
sortShown();
setToShow();
}
}

// 点击监听
class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveTo), "UTF-8"));
for (Product p : show) {
br.write(p.toString());
br.newLine();
}
br.close();
} catch(IOException e) {
System.err.println(e.getMessage());
}
JOptionPane.showMessageDialog(null, "文件成功保存");
}
}


}

// Product.java
public class Product {
public static String[] TYPES = { "全部", "电脑", "手机", "书籍" }; // 所有类型

private String id; //产品编号
private String name; // 名称
private String type; // 类型
private double price; // 价格
private int number; // 库存

public Product(String line) {
String[] args = line.split(";");
id = args[0];
name = args[1];
type = args[2];
price = Double.parseDouble(args[3]);
number = Integer.parseInt(args[4]);
}

public String toString() {
return id + ";" + name + ";" + type + ";" + price + ";" + number;
}

public String getType() {
return type;
}

public String getId() {
return id;
}

public int getNumber() {
return number;
}

}

// ProductIdComparator.java
import java.util.Comparator;

// 按照id排序的比较器
public class ProductIdComparator implements Comparator<Product> {
@Override
public int compare(Product arg0, Product arg1) {
return arg0.getId().compareTo(arg1.getId());
}
}

// ProductNumberComparator.java
import java.util.Comparator;

//按照库存排序的比较器
public class ProductNumberComparator implements Comparator<Product> {
@Override
public int compare(Product arg0, Product arg1) {
return arg0.getNumber() - arg1.getNumber();
}
}

运行结果和题目要求差不多,就不放出来了。

最后一条建议,Java这门课的重点可以调整一下,以适应当前主流的开发。