-
Notifications
You must be signed in to change notification settings - Fork 0
/
Course.java
274 lines (258 loc) · 10.8 KB
/
Course.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
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class Course {
public static void createTopic( String getTopicName) throws FileNotFoundException, IOException, ClassNotFoundException {
File file = new File("coursedata.dat");
ArrayList<Topic> oldTopic = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("coursedata.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Course");
Topic a = (Topic)in.readObject();
oldTopic.add(a);
}
in.close();
}
boolean same = false;
ObjectOutputStream writeTopic = new ObjectOutputStream(new FileOutputStream("coursedata.dat"));
for (int i = 0; i < oldTopic.size(); i++) {
System.out.println("Writing Old Course");
writeTopic.writeObject(oldTopic.get(i));
if(getTopicName.equals(oldTopic.get(i).getTopicName())){
same = true;
}
}
if(same == false){
writeTopic.writeObject(new Topic(getTopicName));
}
writeTopic.close();
}
public static void addNewTopic( Subject addSubject, String getTopicName) throws FileNotFoundException, IOException, ClassNotFoundException {
File file = new File("coursedata.dat");
ArrayList<Topic> oldTopic = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("coursedata.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Topic");
Topic a = (Topic)in.readObject();
oldTopic.add(a);
}
in.close();
}
ObjectOutputStream writeTopic = new ObjectOutputStream(new FileOutputStream("coursedata.dat"));
for (int i = 0; i < oldTopic.size(); i++) {
if(getTopicName.equals(oldTopic.get(i).getTopicName())){
oldTopic.get(i).setSubjectArrayList(addSubject);
System.out.println("Finished Add Subject");
writeTopic.writeObject(oldTopic.get(i));
}
else{
System.out.println("Writing Old Topic");
writeTopic.writeObject(oldTopic.get(i));
}
}
writeTopic.close();
}
public static ArrayList<Topic> readoldtopic() throws FileNotFoundException, IOException, ClassNotFoundException{
File file = new File("coursedata.dat");
ArrayList<Topic> oldTopic = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("coursedata.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Topic");
Topic a = (Topic)in.readObject();
oldTopic.add(a);
}
in.close();
}
System.out.println(oldTopic.size());
return oldTopic;
}
public static boolean checkTopic(String topicname) throws IOException, ClassNotFoundException {
boolean same = false;
File file = new File("coursedata.dat");
if(file.exists()){
FileInputStream fin = new FileInputStream("coursedata.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Course");
Topic a = (Topic)in.readObject();
if(topicname.equals(a.getTopicName())){
same = true;
}
}
in.close();
}
if(same == true){
return false;
}
else {
return true;
}
}
public static boolean checkSubject(String subjectname) throws IOException, ClassNotFoundException {
boolean same = false;
File file = new File("coursedata.dat");
if(file.exists()){
FileInputStream fin = new FileInputStream("coursedata.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Course");
Topic a = (Topic)in.readObject();
for(int i = 0; i < a.getSubjectArrayList().size(); i++){
if(subjectname.equals(a.getSubjectArrayList().get(i).getSubjectString())){
same = true;
break;
}
}
}
in.close();
}
if(same == true){
return false;
}
else {
return true;
}
}
public static void updateTopic(Subject subject) throws FileNotFoundException, IOException, ClassNotFoundException {
File file = new File("coursedata.dat");
ArrayList<Topic> oldTopic = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("coursedata.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Topic");
Topic a = (Topic)in.readObject();
oldTopic.add(a);
}
in.close();
}
ObjectOutputStream writeTopic = new ObjectOutputStream(new FileOutputStream("coursedata.dat"));
for (int i = 0; i < oldTopic.size(); i++) {
for(int j = 0; j < oldTopic.get(i).getSubjectArrayList().size(); j++){
if(oldTopic.get(i).getSubjectArrayList().get(j).getSubjectString().equals(subject.getSubjectString())){
oldTopic.get(i).getSubjectArrayList().remove(j);
oldTopic.get(i).getSubjectArrayList().add(subject);
}
}
writeTopic.writeObject(oldTopic.get(i));
}
writeTopic.close();
}
public static void deleteTopic(Topic topic) throws FileNotFoundException, IOException, ClassNotFoundException{
File file = new File("coursedata.dat");
ArrayList<Topic> oldTopic = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("coursedata.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Topic");
Topic a = (Topic)in.readObject();
oldTopic.add(a);
}
in.close();
}
for (int i = 0; i < oldTopic.size(); i++) {
if(oldTopic.get(i).getTopicName().equals(topic.getTopicName())){
oldTopic.remove(i);
}
}
ObjectOutputStream writeTopic = new ObjectOutputStream(new FileOutputStream("coursedata.dat"));
for (int j = 0; j < oldTopic.size(); j++) {
writeTopic.writeObject(oldTopic.get(j));
}
writeTopic.close();
}
public static void addPost(Post post) throws FileNotFoundException, IOException, ClassNotFoundException {
File file = new File("post.dat");
ArrayList<Post> oldPost = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("post.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Post");
Post a = (Post)in.readObject();
oldPost.add(a);
}
in.close();
}
ObjectOutputStream writePost = new ObjectOutputStream(new FileOutputStream("post.dat"));
for(int i = 0; i < oldPost.size(); i++){
writePost.writeObject(oldPost.get(i));
}
writePost.writeObject(post);
System.out.println("Writed Post");
writePost.close();
}
public static ArrayList<Post> getPost() throws IOException, ClassNotFoundException {
File file = new File("post.dat");
ArrayList<Post> oldPost = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("post.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Post");
Post a = (Post)in.readObject();
oldPost.add(a);
}
in.close();
}
return oldPost;
}
public static boolean checkExam() throws ClassNotFoundException, IOException {
boolean check = false;
File file = new File("exam.dat");
ArrayList<Exam> oldExam = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("exam.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old Post");
Exam a = (Exam)in.readObject();
oldExam.add(a);
check = true;
}
in.close();
}
if(check == true){
return true;
}
else return false;
}
public static ArrayList<Exam> readExam() throws IOException, ClassNotFoundException {
File file = new File("exam.dat");
ArrayList<Exam> oldExam = new ArrayList<>();
if(file.exists()){
FileInputStream fin = new FileInputStream("exam.dat");
ObjectInputStream in = new ObjectInputStream(fin);
while(fin.available() != 0){
System.out.println("Reading Old exam");
Exam a = (Exam)in.readObject();
oldExam.add(a);
}
in.close();
}
return oldExam;
}
public static void writeFile(Exam exam) throws FileNotFoundException, IOException, ClassNotFoundException {
ArrayList<Exam> oldExam = new ArrayList<>();
oldExam = readExam();
ObjectOutputStream writePost = new ObjectOutputStream(new FileOutputStream("exam.dat"));
for(int i = 0; i < oldExam.size(); i++){
writePost.writeObject(oldExam.get(i));
}
writePost.writeObject(exam);
System.out.println("Writed Exam");
writePost.close();
}
}