-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson.java
More file actions
314 lines (293 loc) · 10.1 KB
/
Lesson.java
File metadata and controls
314 lines (293 loc) · 10.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package finalProject;
import java.io.*;
import java.util.ArrayList;
import finalProject.StudentGrade;
public class Lesson {
String lessonName;
String professorName;
String year;
String num;
String word;
String type;
String point;
ArrayList<StudentGrade> list = new ArrayList<StudentGrade>();
public Lesson(String lessonName, String professorName, String year, String num, String word, String type, String point, ArrayList<StudentGrade> list) {
this.lessonName = lessonName;
this.professorName = professorName;
this.year = year;
this.num = num;
this.word = word;
this.type = type;
this.point = point;
this.list = list;
}
public boolean buildLessonFile() { //建檔_OK
try {
String folderName = this.year;
File newFolder = new File("C:\\StudentGradeSystem\\LessonFile\\" + folderName);
newFolder.mkdir();
String fileName = this.lessonName;
String path = "C:\\StudentGradeSystem\\LessonFile\\" + folderName + "\\" + fileName + ".txt";
File newFile = new File(path);
if (newFile.exists()) {
return false;
}
if (!newFile.exists()) {
newFile.createNewFile();
}
String word = this.lessonName + "\n" +
this.professorName + "\n" +
this.year + "\n" +
this.num + "\n" +
this.word + "\n" +
this.type + "\n" +
this.point + "\n";
String arrayWord = "";
for (int i = 0; i < this.list.size(); i++) {
arrayWord += this.list.get(i).name + " ";
arrayWord += this.list.get(i).grade + "\n";
}
FileOutputStream fws = new FileOutputStream(path);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fws, "UTF-8"));
bw.write(word + "\n" + arrayWord);
bw.close();
return true;
}catch (IOException e) {
e.printStackTrace();
return false;
}
}
public boolean changeLessonFile() { //修改資料_不含課程名稱
String fileName = this.lessonName;
String folderName = this.year;
String path = "C:\\StudentGradeSystem\\LessonFile\\" + folderName + "\\" + fileName + ".txt";
String word = this.lessonName + "\n" +
this.professorName + "\n" +
this.year + "\n" +
this.num + "\n" +
this.word + "\n" +
this.type + "\n" +
this.point + "\n";
String arrayWord = "\n";
for (int i = 0; i < this.list.size(); i++) {
arrayWord += this.list.get(i).name + " ";
arrayWord += this.list.get(i).grade + "\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 boolean changeLessonProName(String newName) { //修改課程教授名稱
Professor Newpro = new Manager(" "," ", " ").readProFile(newName);
if (Newpro == null) {
return false;
}
Professor Oldpro = new Manager(" "," ", " ").readProFile(this.professorName);
ArrayList<String> place = new ArrayList<String>();
for (int i = 0; i < Oldpro.lesson.size(); i++) {
ArrayList<String> getData = Oldpro.lesson.get(i);
if (getData.get(0).equals(this.year)) {
place = getData;
break;
}
}
int lessonIndex = Oldpro.lesson.indexOf(place);
Oldpro.lesson.get(lessonIndex).remove(this.lessonName);
Oldpro.changeFile();
this.professorName = newName;
this.changeLessonFile();
ArrayList<StudentGrade> list = new ArrayList<StudentGrade>();
Lesson lesson = new Lesson(this.lessonName, newName, this.year, this.num, this.word, this.type, this.point, this.list);
lesson.buildLessonFile();
if (Newpro.lesson.isEmpty()) {
ArrayList<String> newlesson = new ArrayList<String>();
newlesson.add(lesson.year);
newlesson.add(lesson.lessonName);
Newpro.lesson.add(newlesson);
lesson.changeLessonFile();
Newpro.changeFile();
return true;
}
ArrayList<String> placeNew = new ArrayList<String>();
for (int i = 0; i < Newpro.lesson.size(); i++) {
ArrayList<String> getDataNew = Newpro.lesson.get(i);
if (getDataNew.get(0).equals(lesson.year)) {
placeNew = getDataNew;
break;
}
}
int lessonIndexNew = Newpro.lesson.indexOf(placeNew);
if (lessonIndexNew == -1) {
placeNew.add(lesson.year);
placeNew.add(lesson.lessonName);
Newpro.lesson.add(placeNew);
return Newpro.changeFile();
}
else {
Newpro.lesson.get(lessonIndexNew).add(lesson.lessonName);
return Newpro.changeFile();
}
}
public boolean changeLessonName(String newName) { //修改課程名稱_OK
String pathNew = "C:\\StudentGradeSystem\\LessonFile\\" + this.year + "\\" + newName + ".txt";
File ready = new File (pathNew);
if (ready.exists()) {
return false;
}
Professor pro = new Manager(" "," ", " ").readProFile(this.professorName);
ArrayList<String> place = new ArrayList<String>();
for (int i = 0; i < pro.lesson.size(); i++) {
ArrayList<String> getData = pro.lesson.get(i);
if (getData.get(0).equals(year)) {
place = getData;
break;
}
}
int lessonIndex = pro.lesson.indexOf(place);
pro.lesson.get(lessonIndex).remove(this.lessonName);
pro.lesson.get(lessonIndex).add(newName);
pro.changeFile();
for (int j = 0; j < this.list.size(); j++) {
Student stu = new Manager(" "," ", " ").readStuFile(this.list.get(j).name);
ArrayList<String> placeStu = new ArrayList<String>();
for (int i = 0; i < stu.lesson.size(); i++) {
ArrayList<String> getDataStu = stu.lesson.get(i);
if (getDataStu.get(0).equals(year)) {
placeStu = getDataStu;
break;
}
}
int lessonIndexStu = stu.lesson.indexOf(placeStu);
stu.lesson.get(lessonIndexStu).remove(this.lessonName);
stu.lesson.get(lessonIndexStu).add(newName);
stu.changeFile();
}
String fileName = this.lessonName;
String folderName = this.year;
String path = "C:\\StudentGradeSystem\\LessonFile\\" + folderName + "\\" + fileName + ".txt";
File newFile = new File(path);
newFile.delete();
this.lessonName = newName;
return this.buildLessonFile();
}
public boolean deleteLessonFile() { //刪除課程_未加入移除教授/學生的部分
String fileName = this.lessonName;
String folderName = this.year;
String path = "C:\\StudentGradeSystem\\LessonFile\\" + folderName + "\\" + fileName + ".txt";
File newFile = new File(path);
Professor pro = new Manager("","","").readProFile(this.professorName);
ArrayList<String> place = new ArrayList<String>();
for (int i = 0; i < pro.lesson.size(); i++) {
ArrayList<String> getData = pro.lesson.get(i);
if (getData.get(0).equals(year)) {
place = getData;
break;
}
}
int lessonIndex = pro.lesson.indexOf(place);
pro.lesson.get(lessonIndex).remove(this.lessonName);
pro.changeFile();
for(int j = 0; j < this.list.size(); j++) {
Student stu = new Manager("","","").readStuFile(this.list.get(j).name);
ArrayList<String> placeStu = new ArrayList<String>();
for (int i = 0; i < stu.lesson.size(); i++) {
ArrayList<String> getDataStu = stu.lesson.get(i);
if (getDataStu.get(0).equals(year)) {
placeStu = getDataStu;
break;
}
}
int lessonIndexStu = stu.lesson.indexOf(placeStu);
stu.lesson.get(lessonIndexStu).remove(this.lessonName);
stu.changeFile();
}
return newFile.delete();
}
public void deleteStudentData(String name) { //移除學生資料
ArrayList<StudentGrade> list = this.list;
int place = list.indexOf(new StudentGrade(name, ""));
list.remove(list.get(place));
this.list = list;
this.changeLessonFile();
}
public String findStudentGrade(String name) { //查看成績(依人)_待依GUI修改輸出
ArrayList<StudentGrade> list = this.list;
int place = list.indexOf(new StudentGrade(name, ""));
String grade = list.get(place).name + " " + list.get(place).grade;
return grade;
}
public String gradeAverage() { //計算課程總平均_OK
int sum = 0;
for (int i = 0; i < this.list.size(); i++) {
int add = Integer.parseInt(this.list.get(i).grade);
sum += add;
}
float average = sum / this.list.size();
String grade = "" + average;
return grade;
}
public String findGrade() { //查看成績(依課程)_待依GUI修改輸出
String grade = this.lessonName + "名單成績\n";
for (int i = 0; i < this.list.size(); i++) {
String add = this.findStudentGrade(this.list.get(i).name);
grade += add + "\n";
}
if (this.list.size() != 0) {
grade += "總平均:" + this.gradeAverage();
}
else {
grade += "總平均: --";
}
return grade;
}
public String printDate() { //查看課程資訊_待依GUI修改
String word = "\t課程名稱:" +
this.lessonName + "\n\t授課教授:" +
this.professorName + "\n\t開課學期:" +
this.year + "\n\t課程代碼:" +
this.num + "\n\t課程大綱:" +
this.word + "\n\t選課類型:" +
this.type + "\n\t 學分數:" +
this.point + "\n";
String arrayWord = "\n\t選課名單:\n\t";
for (int i = 0; i < this.list.size(); i++) {
Student stu = new Manager(" ", " ", " ").readStuFile(this.list.get(i).name);
arrayWord += stu.account + " ";
arrayWord += stu.name + "\n\t";
}
String print = word + arrayWord + "\n";
return print;
}
public void readFile(String year, String name) { //導入資料_OK
String fileName = name;
String folderName = year;
String path = "C:\\StudentGradeSystem\\LessonFile\\" + folderName + "\\" + fileName + ".txt";
try {
FileInputStream fis = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
this.lessonName = br.readLine();
this.professorName = br.readLine();
this.year = br.readLine();
this.num = br.readLine();
this.word = br.readLine();
this.type = br.readLine();
this.point = br.readLine();
br.readLine();
String line;
while ((line = br.readLine()) != null) {
String[] studentGrade= line.split(" ");
this.list.add(new StudentGrade(studentGrade[0],studentGrade[1]));
}
br.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}