服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现学生管理系统(IO版)

Java实现学生管理系统(IO版)

2022-07-28 11:56_Rick Java教程

这篇文章主要为大家详细介绍了Java实现学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现学生管理系统的具体代码,供大家参考,具体内容如下

图解: 

Java实现学生管理系统(IO版)

cade: 

student.java

?
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
/*
 * 这是我的学生类
 */
public class Student {
    //学号
    private String id;
    //姓名
    private String name;
    //年龄
    private String age;
    //居住地
    private String address;
    
    public Student() {
        
    }
 
    public Student(String id, String name, String age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
 
    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 String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
    
}

studentmangager类

?
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
 
/*
 * 这是我的学生管理系统的主类
 
 * 步骤如下:
 * A:定义学生类
 * B:学生管理系统的主界面的代码编写
 * C:学生管理系统的查看所有学生的代码编写
 * D:学生管理系统的添加学生的代码编写
 * E:学生管理系统的删除学生的代码编写
 * F:学生管理系统的修改学生的代码编写
 */
public class StudentManagerTest {
    public static void main(String[] args) throws IOException{
        //定义文件路径
        String fileName = "students.txt";
        
        //为了让程序能够回到这里来,我们使用循环
        while(true) {
            //这是学生管理系统的主界面
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 查看所有学生");
            System.out.println("2 添加学生");
            System.out.println("3 删除学生");
            System.out.println("4 修改学生");
            System.out.println("5 退出");
            System.out.println("请输入你的选择:");
            //创建键盘录入对象
            Scanner sc = new Scanner(System.in);
            String choiceString = sc.nextLine();
            //用switch语句实现选择
            switch(choiceString) {
            case "1":
                //查看所有学生
                findAllStudent(fileName);
                break;
            case "2":
                //添加学生
                addStudent(fileName);
                break;
            case "3":
                //删除学生
                deleteStudent(fileName);
                break;
            case "4":
                //修改学生
                updateStudent(fileName);
                break;
            case "5":
            default:
                System.out.println("谢谢你的使用");
                System.exit(0); //JVM退出
                break;
            }
        }
    }
    
    //从文件中读数据到集合
    public static void readData(String fileName,ArrayList<Student> array) throws IOException {
        //创建输入缓冲流对象
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        
        String line;
        while((line=br.readLine())!=null) {
            String[] datas = line.split(",");
            Student s = new Student();
            s.setId(datas[0]);
            s.setName(datas[1]);
            s.setAge(datas[2]);
            s.setAddress(datas[3]);
            array.add(s);
        }
        
        br.close();
    }
    
    //把集合中的数据写入文件
    public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
        //创建输出缓冲流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
        
        for(int x=0; x<array.size(); x++) {
            Student s = array.get(x);
            StringBuilder sb = new StringBuilder();
            sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
            
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        
        bw.close();
    }
    
    //修改学生
    public static void updateStudent(String fileName) throws IOException {
        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();
        //从文件中把数据读取到集合中
        readData(fileName, array);
        
        //修改学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就修改该学生
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生的学号:");
        String id = sc.nextLine();
        
        //定义一个索引
        int index = -1;
        
        //遍历集合
        for(int x=0; x<array.size(); x++) {
            //获取每一个学生对象
            Student s = array.get(x);
            //拿学生对象的学号和键盘录入的学号进行比较
            if(s.getId().equals(id)) {
                index = x;
                break;
            }
        }
        
        if(index == -1) {
            System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新你的选择");
        }else {
            System.out.println("请输入学生新姓名:");
            String name = sc.nextLine();
            System.out.println("请输入学生新年龄:");
            String age = sc.nextLine();
            System.out.println("请输入学生新居住地:");
            String address = sc.nextLine();
            
            //创建学生对象
            Student s = new Student();
            s.setId(id);
            s.setName(name);
            s.setAge(age);
            s.setAddress(address);
            
            //修改集合中的学生对象
            array.set(index, s);
            //把集合中的数据重新写回到文件
            writeData(fileName, array);
            //给出提示
            System.out.println("修改学生成功");
        }
    }
    
    //删除学生
    public static void deleteStudent(String fileName) throws IOException {
        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();
        //从文件中把数据读取到集合中
        readData(fileName, array);
        
        //删除学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就删除该学生
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号:");
        String id = sc.nextLine();
        
        //我们必须给出学号不存在的时候的提示
        
        //定义一个索引
        int index = -1;
        
        //遍历集合
        for(int x=0; x<array.size(); x++) {
            //获取到每一个学生对象
            Student s = array.get(x);
            //拿这个学生对象的学号和键盘录入的学号进行比较
            if(s.getId().equals(id)) {
                index = x;
                break;
            }
        }
        
        if(index == -1) {
            System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新你的选择");
        }else {
            array.remove(index);
            //把集合中的数据重新写回到文件
            writeData(fileName, array);
            System.out.println("删除学生成功");
        }
        
    }
    
    //添加学生
    public static void addStudent(String fileName) throws IOException {
        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();
        //从文件中把数据读取到集合中
        readData(fileName, array);
                
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        
        //为了让id能够被访问到,我们就把id定义在了循环的外面
        String id;
        
        //为了让代码能够回到这里,用循环
        while(true) {
            System.out.println("请输入学生学号:");
            //String id = sc.nextLine();
            id = sc.nextLine();
            
            //判断学号有没有被人占用
            //定义标记
            boolean flag = false;
            //遍历集合,得到每一个学生
            for(int x=0; x<array.size(); x++) {
                Student s = array.get(x);
                //获取该学生的学号,和键盘录入的学号进行比较
                if(s.getId().equals(id)) {
                    flag = true; //说明学号被占用了
                    break;
                }
            }
            
            if(flag) {
                System.out.println("你输入的学号已经被占用,请重新输入");
            }else {
                break; //结束循环
            }
        }
        
        
        System.out.println("请输入学生姓名:");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄:");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地:");
        String address = sc.nextLine();
        
        //创建学生对象
        Student s = new Student();
        s.setId(id);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        
        //把学生对象作为元素添加到集合
        array.add(s);
        //把集合中的数据重新写回到文件
        writeData(fileName, array);
        
        //给出提示
        System.out.println("添加学生成功");
    }
    
    //查看所有学生
    public static void findAllStudent(String fileName) throws IOException {
        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();
        //从文件中把数据读取到集合中
        readData(fileName, array);
        
        //首先来判断集合中是否有数据,如果没有数据,就给出提示,并让该方法不继续往下执行
        if(array.size() == 0) {
            System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作");
            return;
        }
        
        //\t 其实就是一个tab键的位置
        System.out.println("学号\t\t姓名\t年龄\t居住地");
        for(int x=0; x<array.size(); x++) {
            Student s = array.get(x);
            System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_44718300/article/details/88585106

延伸 · 阅读

精彩推荐