1 import java.awt.BorderLayout;
2 import java.awt.Frame;
3 import java.awt.GridLayout;
4 import java.awt.Label;
5 import java.awt.TextArea;
6 import java.awt.TextField;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.BufferedReader;
10 import java.io.BufferedWriter;
11 import java.io.FileReader;
12 import java.io.FileWriter;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Scanner;
16
17 import javax.swing.JButton;
18 import javax.swing.JFrame;
19 import javax.swing.JOptionPane;
20 import javax.swing.JPanel;
21
22 public class Main {
23 public static TextField ta1=new TextField();
24 public static TextField ta2=new TextField();
25 public static TextField ta3=new TextField();
26 public static TextField ta4=new TextField();
27 public static JPanel panelTwo=new JPanel();
28 public static String fileName="student.txt";
29 Main(){
30 JFrame f=new JFrame("管理系统");
31 f.setSize(468,120);
32 f.setLocation(800,400);
33 f.setVisible(true);
34 panelTwo.setLayout(new GridLayout(3, 4));
35 ActionListener al=new Listener();
36 Label la1=new Label("学号");
37 Label la2=new Label("姓名");
38 Label la3=new Label("年龄");
39 Label la4=new Label("地址");
40 panelTwo.add(la1);
41 panelTwo.add(la2);
42 panelTwo.add(la3);
43 panelTwo.add(la4);
44 panelTwo.add(ta1);
45 panelTwo.add(ta2);
46 panelTwo.add(ta3);
47 panelTwo.add(ta4);
48 JButton bInsert=new JButton("增加");
49 JButton bDelete=new JButton("删除");
50 JButton bAlter=new JButton("修改");
51 JButton bSearch=new JButton("查询");
52 bInsert.addActionListener(al);
53 bDelete.addActionListener(al);
54 bAlter.addActionListener(al);
55 bSearch.addActionListener(al);
56 panelTwo.add(bInsert);
57 panelTwo.add(bDelete);
58 panelTwo.add(bAlter);
59 panelTwo.add(bSearch);
60 f.add(panelTwo);
61 }
62 public static void main(String args[]) {
63 new Main();
64 }
65 public static void readData(String fileName, ArrayList<Student> array) throws IOException{
66 //创建输入缓冲流对象
67 BufferedReader br=new BufferedReader(new FileReader(fileName));
68
69 String line;
70 //获得每一行的字符串,赋值给line
71 while((line=br.readLine())!=null){
72 //截取字符数组
73 String [] str=line.split(",");
74 //创建学生对象接收数据
75 Student s=new Student();
76 s.setId(str[0]);
77 s.setName(str[1]);
78 s.setAge(str[2]);
79 s.setAdress(str[3]);
80 //将获取到的数据存储到集合中
81 array.add(s);
82 }
83 br.close();
84 }
85
86 //把集合中的数据存储到文件中
87 public static void writeData(String fileName, ArrayList<Student> array) throws IOException{
88 //创建输出缓冲流对象
89 BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
90 //遍历集合,获取到数据
91 for(int x=0;x<array.size();x++){
92 //将集合中的每一行数据赋值给学生对象
93 Student s=array.get(x);
94
95 //创建StringBuilder对象用来拼接字符串
96 StringBuilder sb= new StringBuilder();
97 sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAdress());
98 //写入文件中
99 bw.write(sb.toString());
100 bw.newLine();
101 bw.flush();
102 }
103 bw.close();
104 }
105
106 //查看所有学生的代码编写
107 public static void findAllStudent(String fileName) throws IOException{
108 //首先创建集合对象
109 ArrayList<Student> array=new ArrayList<>();
110 //获取到学生信息
111 readData(fileName,array);
112
113 //判断是否有数据
114 if(array.size()==0){
115 // System.out.println("当前没有学生信息,请重新您的选择");
116 JOptionPane.showMessageDialog(panelTwo, "当前没有学生信息,请重新您的选择");
117 return;
118 }
119 // System.out.println("学号\t\t\t姓名\t年龄\t地址");
120 //遍历集合获取数据
121 JFrame frame=new JFrame("查询结果");
122 frame.setVisible(true);
123 frame.setSize(468,200);
124 frame.setLocation(800,150);
125 TextArea ta5=new TextArea();
126 frame.add(ta5);
127 for(int x=0;x<array.size();x++){
128 //定义学生对象,用来存储学生信息
129 Student s=array.get(x);
130 // System.out.println(s.getId()+"\t\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAdress());
131 ta5.appendText(s.getId()+"\t\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAdress()+"\n");
132 }
133
134
135 }
136
137 //添加学生代码编码
138 public static void addStudent(String fileName) throws IOException{
139 //创建集合对象
140 ArrayList<Student> array=new ArrayList<>();
141 //从文件中读取数据
142 readData(fileName, array);
143 //创建键盘录入对象
144 Scanner sc= new Scanner(System.in);
145 //定义字符串类型的id,后面也需要使用,所以定义到循环外面
146 String id;
147 while(true){
148 // System.out.println("请输入新的学生学号:");
149 // id=sc.nextLine();
150 id=ta1.getText();
151 //定义布尔那类型的变量,用来判断
152 boolean flag=false;
153
154 //判断学号是否存在
155 //遍历集合获取元素
156 for(int x=0;x<array.size();x++){
157 //定义学生对象,获取到每一个学生
158 Student s=array.get(x);
159 //判断这个输入的id在学生对象中是否存在
160 if(s.getId().equals(id)){
161 flag=true;
162 break;//跳出for循环
163 }
164 }
165 if(flag){
166 // System.out.println("学号已存在,请重新您的选择:");
167 JOptionPane.showMessageDialog(panelTwo, "学号已存在,请重新您的选择:");
168 break;
169 }else{
170 break;
171 }
172 }
173 //System.out.println("请输入新的姓名:");
174 String name=ta2.getText();
175 //System.out.println("请输入新的年龄:");
176 String age=ta3.getText();
177 //System.out.println("请输入新的地址:");
178 String address=ta4.getText();
179 //创建学生对象存储输入的信息
180 Student s=new Student();
181 s.setId(id);
182 s.setName(name);
183 s.setAge(age);
184 s.setAdress(address);
185
186 //将学生对象存储到集合
187 array.add(s);
188 //将集合中的重新存储到文件中
189 writeData(fileName, array);
190 //System.out.println("添加成功!");
191 ta1.setText("");
192 ta2.setText("");
193 ta3.setText("");
194 ta4.setText("");
195 JOptionPane.showMessageDialog(panelTwo, "添加成功!");
196 }
197
198 //删除学生代码的编写
199 public static void deleteStudent(String fileName) throws IOException{
200 //首先定义一个集合
201 ArrayList<Student> array=new ArrayList<>();
202 //读取出学生信息
203 readData(fileName, array);
204
205 while(true){
206 //创建键盘录入对象
207 // Scanner sc=new Scanner(System.in);
208 // System.out.println("请输入您要删除的学号:");
209 //JOptionPane.showMessageDialog(panelTwo, "请输入您要删除的学号:");
210 // String id=sc.nextLine();
211 String id=ta1.getText();
212 //定义一个索引
213 int index=-1;
214
215 //遍历集合获取到元素
216 for(int x=0;x<array.size();x++){
217 Student s=array.get(x);
218 if(s.getId().equals(id)){
219 index=x;
220 break;
221 }
222 }
223 if(index==-1){
224 //System.out.println("您所需要删除的学员信息不存在,请重新您的选择!");
225 JOptionPane.showMessageDialog(panelTwo, "您所需要删除的学员信息不存在,请重新您的选择!");
226 break;
227 }else{
228 array.remove(index);
229 //把集合中的文件重新写回到文件
230 writeData(fileName, array);
231 //System.out.println("删除学生成功!");
232 ta1.setText("");
233 ta2.setText("");
234 ta3.setText("");
235 ta4.setText("");
236 JOptionPane.showMessageDialog(panelTwo, "删除学生成功!");
237 break;
238 }
239
240 }
241
242 }
243 //修改学生代码的编写
244 public static void updateStudent(String fileName) throws IOException{
245 //创建集合对象
246 ArrayList<Student> array=new ArrayList<>();
247 //读取文件
248 readData(fileName, array);
249 //定义索引
250 int index=-1;
251 //创建键盘输入对象
252 // Scanner sc=new Scanner(System.in);
253 String id;
254 // JFrame frame=new JFrame("输入待修改学生学号");
255 // frame.setVisible(true);
256 // frame.setSize(468,200);
257 // frame.setLocation(800,150);
258 // TextField ta5=new TextField();
259 // frame.add(ta5);
260 while(true){
261 // System.out.println("请输入您要修改的学生ID:");
262 //JOptionPane.showMessageDialog(panelTwo, "请输入您要修改的学生ID:");
263 //id=sc.nextLine();
264 id=ta1.getText();
265 //遍历集合获取元素
266 for(int x=0;x<array.size();x++){
267 Student s=array.get(x);
268 if(s.getId().equals(id)){
269 index=x;
270 break;
271 }
272 }
273 if(index==-1){
274 //System.out.println("您输入的不存在,请重新您的选择!");
275 JOptionPane.showMessageDialog(ta1, "输入不合法");
276 break;
277 }
278 else{
279 // System.out.println("请输入新的姓名:");
280 // String name=sc.nextLine();
281 // System.out.println("请输入新的年龄:");
282 // String age=sc.nextLine();
283 // System.out.println("请输入新的地址:");
284 // String address=sc.nextLine();
285
286 //创建学生对象
287 String name=ta2.getText();
288 //System.out.println("请输入新的年龄:");
289 String age=ta3.getText();
290 //System.out.println("请输入新的地址:");
291 String address=ta4.getText();
292 Student s=new Student();
293 s.setId(id);
294 s.setName(name);
295 s.setAge(age);
296 s.setAdress(address);
297
298 //修改集合中的学生对象
299 array.set(index, s);
300 //将集合写入到文件中
301 writeData(fileName, array);
302 //给出提示
303 // System.out.println("修改成功!");
304 ta1.setText("");
305 ta2.setText("");
306 ta3.setText("");
307 ta4.setText("");
308 JOptionPane.showMessageDialog(panelTwo, "修改成功!");
309 break;
310 }
311 }
312 }
313 private class Listener implements ActionListener{
314 @Override
315 public void actionPerformed(ActionEvent e) {
316 // TODO Auto-generated method stub
317 String input = e.getActionCommand();
318 if(input.equals("增加")){
319 try {
320 addStudent(fileName);
321 } catch (IOException e1) {
322 // TODO Auto-generated catch block
323 e1.printStackTrace();
324 }
325 }
326 if(input.equals("删除")){
327 try {
328 deleteStudent(fileName);
329 } catch (IOException e1) {
330 // TODO Auto-generated catch block
331 e1.printStackTrace();
332 }
333 }
334 if(input.equals("修改")){
335 try {
336 updateStudent(fileName);
337 } catch (IOException e1) {
338 // TODO Auto-generated catch block
339 e1.printStackTrace();
340 }
341 }
342 if(input.equals("查询")){
343 try {
344 findAllStudent(fileName);
345 } catch (IOException e1) {
346 // TODO Auto-generated catch block
347 e1.printStackTrace();
348 }
349 }
350 }
351 }
352 }
353
354 class Student{
355 String id;
356 String name;
357 String age;
358 String address;
359 String getId(){
360 return this.id;
361 }
362 String getName(){
363 return this.name;
364 }
365 String getAge(){
366 return this.age;
367 }
368 String getAdress(){
369 return this.address;
370 }
371 void setId(String id){
372 this.id=id;
373 }
374 void setName(String name){
375 this.name=name;
376 }
377 void setAge(String age){
378 this.age=age;
379 }
380 void setAdress(String address){
381 this.address=address;
382 }
383
384 }