-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
287 lines (272 loc) · 9.31 KB
/
Student.java
File metadata and controls
287 lines (272 loc) · 9.31 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
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
package finalProject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
public class Student{
String account; //學號兼帳號
String password;
String identity;
String name;
String year;
ArrayList<ArrayList<String>> lesson = new ArrayList<ArrayList<String>>();
public Student(String account, String password, String identity, String name, String year, ArrayList<ArrayList<String>> lesson) {
this.account = account;
this.password = password;
this.identity = identity;
this.name = name;
this.year = year;
this.lesson = lesson;
} //OK
public boolean buildFile() { //建檔_OK
try {
String fileName = this.account;
String path = "C:\\StudentGradeSystem\\AccountFile\\" + fileName +".txt";
File newFile = new File(path);
if (!newFile.exists()) {
newFile.createNewFile();
}
String word = this.account + "\n" +
this.password + "\n" +
this.identity + "\n" +
this.name + "\n" +
this.year + "\n";
String arrayWord = "";
for (int i = 0; i < this.lesson.size(); i++) {
arrayWord += this.lesson.get(i).get(0) + " "; //取學期
for (int j = 1; j < this.lesson.get(i).size(); j++) { //取課程
arrayWord += this.lesson.get(i).get(j) + " ";
}
arrayWord += "\n";
}
FileOutputStream fws = new FileOutputStream(path);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fws, "UTF-8"));
bw.write(word + arrayWord);
bw.close();
return true;
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean deleteFile() { //刪除檔案_OK
String fileName = this.account;
String path = "C:\\StudentGradeSystem\\AccountFile\\" + fileName +".txt";
File newFile = new File(path);
for (int i = 0; i < this.lesson.size(); i++) {
for (int j = 1; j < this.lesson.get(i).size(); j++) {
Lesson lessonDelete = this.findLessonData(this.lesson.get(i).get(0), this.lesson.get(i).get(j));
lessonDelete.deleteStudentData(this.name);
}
}
return newFile.delete();
}
public boolean changeFile() { //修改密碼_OK
String fileName = this.account;
String path = "C:\\StudentGradeSystem\\AccountFile\\" + fileName +".txt";
String word = this.account + "\n" +
this.password + "\n" +
this.identity + "\n" +
this.name + "\n" +
this.year + "\n";
String arrayWord = "";
for (int i = 0; i < this.lesson.size(); i++) {
arrayWord += this.lesson.get(i).get(0) + " "; //取學期
for (int j = 1; j < this.lesson.get(i).size(); j++) { //取課程
arrayWord += this.lesson.get(i).get(j) + " ";
}
arrayWord += "\n";
}
try {
FileOutputStream fws = new FileOutputStream(path);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fws, "UTF-8"));
bw.write(word + arrayWord);
bw.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public void printLesson(String year) { //列出課程_OK
ArrayList<String> place = new ArrayList<String>();
for (int i = 0; i < this.lesson.size(); i++) {
ArrayList<String> getData = this.lesson.get(i);
if (getData.get(0).equals(year)) {
place = getData;
break;
}
}
String arrayWord = "";
for (int i = 1; i < place.size(); i++) {
arrayWord += place.get(i) + "\n";
}
System.out.println(arrayWord);
}
public String[] showAllLesson(String year) { //依學期列出所有課程_待依GUI修改
String folderPath = "C:\\StudentGradeSystem\\LessonFile\\" + year + "\\";
try{
ArrayList<String> lessonList = new ArrayList<String>();
File folder = new File(folderPath);
File[] list = folder.listFiles();
for(int i = 0; i < list.length; i++){
lessonList.add(list[i].getName());
}
String[] allLesson = new String[lessonList.size() + 1];
allLesson[0] = " ";
for(int j = 0; j < list.length; j++){
String name = lessonList.get(j);
FileInputStream fis = new FileInputStream(list[j].getAbsoluteFile());
BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
ArrayList<StudentGrade> arraylist = new ArrayList<StudentGrade>();
Lesson getData = new Lesson(name, "", year, "", "", "", "", arraylist);
getData.lessonName = br.readLine();
allLesson[j + 1] = getData.lessonName;
br.close();
}
return allLesson;
}catch(Exception e){
return null;
}
}
public Lesson findLessonData(String year, String name) { //查詢課程_OK
String fileName = name;
String folderName = year;
String path = "C:\\StudentGradeSystem\\LessonFile\\" + folderName + "\\" + fileName + ".txt";
File newFile = new File(path);
if (!newFile.exists()) {
System.out.println("You need to create a new one.");
}
else {
try {
FileInputStream fis = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
ArrayList<StudentGrade> list = new ArrayList<StudentGrade>();
Lesson getData = new Lesson(name, "", year, "", "", "", "", list);
getData.lessonName = br.readLine();
getData.professorName = br.readLine();
getData.year = br.readLine();
getData.num = br.readLine();
getData.word = br.readLine();
getData.type = br.readLine();
getData.point = br.readLine();
br.readLine();
String line;
while ((line = br.readLine()) != null) {
String[] stuGra = new String[2];
stuGra = line.split(" ");
StudentGrade data = new StudentGrade(stuGra[0], stuGra[1]);
getData.list.add(data);
}
br.close();
return getData;
}
catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void chooseLesson(Lesson lesson) { //選課
lesson.list.add(new StudentGrade(this.name, "00"));
if (this.lesson.isEmpty()) {
ArrayList<String> newlesson = new ArrayList<String>();
newlesson.add(lesson.year);
newlesson.add(lesson.lessonName);
this.lesson.add(newlesson);
lesson.changeLessonFile();
this.changeFile();
return;
}
ArrayList<String> place = new ArrayList<String>();
for (int i = 0; i < this.lesson.size(); i++) {
ArrayList<String> getData = this.lesson.get(i);
if (getData.get(0).equals(lesson.year)) {
place = getData;
break;
}
}
int x = this.lesson.indexOf(place);
if (this.lesson.get(x).indexOf(lesson.lessonName) != -1) {
System.out.println("You have chose it.");
return;
}
this.lesson.get(x).add(lesson.lessonName);
lesson.changeLessonFile();
this.changeFile();
}
public String studentAverage(String year) { //總平均計算(依人)_OK
int sum = 0;
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < this.lesson.size(); i++) {
ArrayList<String> getData = this.lesson.get(i);
if (getData.get(0).equals(year)) {
list = getData;
break;
}
}
for (int j = 1; j < list.size(); j++) {
Lesson subject = this.findLessonData(year, list.get(j));
String myGrade = subject.findStudentGrade(this.name);
String[] grade = myGrade.split(" ");
sum += Integer.parseInt(grade[1]);
}
String average = "" + ((float)(sum/(list.size()-1)));
return average;
}
public ArrayList<String> findStudentGrade(String year){ //成績整理(依人)_OK
ArrayList<String> myLessonGrade = new ArrayList<String>();
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < this.lesson.size(); i++) {
ArrayList<String> getData = this.lesson.get(i);
if (getData.get(0).equals(year)) {
list = getData;
break;
}
}
String word = "";
for (int j = 1; j < list.size(); j++) {
Lesson lessonName = this.findLessonData(year, list.get(j));
String mygrade = lessonName.findStudentGrade(this.name);
String[] grade = mygrade.split(" ");
word += lessonName.lessonName + " " + grade[1];
myLessonGrade.add(word);
word = "";
}
myLessonGrade.add("總平均:" + this.studentAverage("1091"));
return myLessonGrade;
}
public void buildTranscript() { //成績單全學期產生_OK
String allWord = "";
for (int a = 0; a < this.lesson.size(); a++) {
String year = this.lesson.get(a).get(0);
String word = "\t\t" + year + this.name + "的成績單\n";
ArrayList<String> list = this.lesson.get(a);
for (int j = 1; j < list.size(); j++) {
Lesson lesson = this.findLessonData(year, list.get(j));
String[] grade = lesson.findStudentGrade(this.name).split(" ");
word += "\t" + lesson.lessonName + "\t" + grade[1] + "分\n";
}
word += "\t\t總平均:" + this.studentAverage(year);
allWord += word + "\n\n";
}
try {
String fileName = this.name + "的成績單";
File newFile = new File("C:\\StudentGradeSystem\\GradeFile\\" + fileName +".txt");
if (!newFile.exists()) {
newFile.createNewFile();
}
FileOutputStream fws = new FileOutputStream(newFile);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fws, "UTF-8"));
bw.write(allWord);
bw.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}