-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentInformation.java
More file actions
149 lines (129 loc) · 5.1 KB
/
Copy pathStudentInformation.java
File metadata and controls
149 lines (129 loc) · 5.1 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
package exp3;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class StudentInformationException extends Exception {
StudentInformationException(String message) {
super(message);
}
}
class EmptyBuildIsNotAllowedException extends StudentInformationException {
EmptyBuildIsNotAllowedException() {
super("Empty build is not allowed.");
}
}
public class StudentInformation {
public record Detail(String name, String stuid, Gender gender, int[] birthdate) {
}
private static List<Detail> StudentInfo = new ArrayList<>(
List.of(
new Detail("郑秀梅", "65385", Gender.FEMALE, new int[]{2004, 8, 11}),
new Detail("段莹", "76679", Gender.FEMALE, new int[]{2005, 5, 28}),
new Detail("毛桂香", "98372", Gender.FEMALE, new int[]{2003, 7, 15}),
new Detail("姜文", "45617", Gender.MALE, new int[]{2006, 6, 23}),
new Detail("许平", "78703", Gender.MALE, new int[]{2003, 8, 7}),
new Detail("贾桂荣", "14469", Gender.FEMALE, new int[]{2007, 3, 3}),
new Detail("王凤兰", "86713", Gender.FEMALE, new int[]{2004, 3, 4}),
new Detail("方亮", "85327", Gender.FEMALE, new int[]{2008, 3, 10}),
new Detail("杨瑞", "95331", Gender.MALE, new int[]{2007, 2, 24}),
new Detail("张霞", "36881", Gender.FEMALE, new int[]{2006, 5, 28})));
public static void main(String[] args) {
Student[] students = new Student[10];
for (int i = 0; i < 10; i++) {
try {
students[i] = new Student();
} catch (EmptyBuildIsNotAllowedException e) {
students[i] = new Student(
StudentInformation.StudentInfo.get(i).name(),
StudentInformation.StudentInfo.get(i).stuid(),
StudentInformation.StudentInfo.get(i).gender(),
StudentInformation.StudentInfo.get(i).birthdate());
}
}
System.out.println("当前共有 " + students.length + " 个学生,信息如下:");
FileWriter f = null;
try {
f = new FileWriter("./StudentInformation.txt");
} catch (IOException e) {
System.out.println("无法写出到文件");
System.exit(1);
}
for (Student student : students) {
System.out.println("姓名:" + student.getName());
System.out.println("学号:" + student.getStuid());
System.out.println("性别:" + student.getGender());
System.out.println("出生日期:" + String.format("%d-%02d-%02d", student.getBirthdate()[0],
student.getBirthdate()[1], student.getBirthdate()[2]));
System.out.println();
if (f != null) {
try {
f.write("姓名:" + student.getName() + "\n");
f.write("学号:" + student.getStuid() + "\n");
f.write("性别:" + student.getGender() + "\n");
f.write("出生日期:" + String.format("%d-%02d-%02d", student.getBirthdate()[0],
student.getBirthdate()[1], student.getBirthdate()[2]) + "\n");
f.write("\n");
} catch (IOException e) {
System.out.println("无法写出到文件");
System.exit(1);
}
}
}
if (f != null) {
try {
f.close();
} catch (IOException e) {
System.out.println("无法关闭文件");
System.exit(1);
}
}
// 从文件中读出学生信息
try {
FileReader fr = new FileReader("./StudentInformation.txt");
BufferedReader br = new BufferedReader(fr);
String line;
System.out.println("从文件中读取的学生信息如下:");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
fr.close();
} catch (IOException e) {
System.out.println("无法读取文件");
System.exit(1);
}
}
}
enum Gender {
MALE, FEMALE;
}
class Student {
private String name;
private String stuid;
private Gender gender;
private int[] birthdate; // [year, month, day]
Student() throws EmptyBuildIsNotAllowedException {
throw new EmptyBuildIsNotAllowedException(); // 不允许空构造
}
Student(String name, String stuid, Gender gender, int[] birthdate) {
this.name = name;
this.stuid = stuid;
this.gender = gender;
this.birthdate = birthdate;
}
String getName() {
return name;
}
String getStuid() {
return stuid;
}
Gender getGender() {
return gender;
}
int[] getBirthdate() {
return birthdate;
}
}