-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfessor.java
More file actions
194 lines (181 loc) · 6.05 KB
/
Professor.java
File metadata and controls
194 lines (181 loc) · 6.05 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
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 Professor {
String account;
String password;
String identity;
String name;
ArrayList<ArrayList<String>> lesson = new ArrayList<ArrayList<String>>();
public Professor(String account, String password, String identity, String name, ArrayList<ArrayList<String>> lesson) {
this.account = account;
this.password = password;
this.identity = identity;
this.name = name;
this.lesson = lesson;
}
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";
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.deleteLessonFile();
}
}
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";
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 buildLessonFile(String lessonName, String year) { //建立課程_OK(GUI→建立之後直接進修改)
ArrayList<StudentGrade> list = new ArrayList<StudentGrade>();
Lesson lesson = new Lesson(lessonName, this.name, year, "", "", "", "", list);
lesson.buildLessonFile();
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(year)) {
place = getData;
break;
}
}
int lessonIndex = this.lesson.indexOf(place);
this.lesson.get(lessonIndex).add(lessonName);
this.changeFile();
}
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 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 String findLessonGrade(String year, String name){ //成績整理(依課程)_OK
Lesson lesson = this.findLessonData(year, name);
String lessonGrade = lesson.findGrade();
return lessonGrade;
}
public void setGrade(Lesson lesson, String name, String grade) { //成績登錄_OK
ArrayList<StudentGrade> list = lesson.list;
int place = list.indexOf(new StudentGrade(name, ""));
list.get(place).grade = grade;
lesson.changeLessonFile();
}
}