-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutor.java
More file actions
491 lines (386 loc) · 17.5 KB
/
Copy pathTutor.java
File metadata and controls
491 lines (386 loc) · 17.5 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Η κλάση Tutor παριστά ένα καθηγητή στο σύστημα του φοιτητολογίου.Η κλάση κληρονομεί την κλαση {Person}.
*/
public class Tutor extends Person
{
private String tutorId;
private String specialty;
public Tutor (String tutorId,String PersonName, String PersonSurname, String PersonEmail, String PersonContactno,String specialty)
{
super(PersonName, PersonSurname, PersonEmail, PersonContactno);
this.tutorId=tutorId;
this.specialty=specialty;
}
//Getters-Setters (not in use)
public String gettutorId ()
{
return tutorId;
}
public void settutorId (String tutorId)
{
this.tutorId = tutorId;
}
public String getspecialty ()
{
return specialty;
}
public void setspecialty (String specialty)
{
this.specialty = specialty;
}
String info;
/**
* Αρχείο καταγραφής των στοιχείων των Καθηγητών του Φοιτητολογίου
*/
public static Path pTutor = Paths.get("C:\\Users\\Anna\\IdeaProjects\\UniSystemManager\\src\\tutor.txt");
public static Path pTemp = Paths.get("C:\\Users\\Anna\\IdeaProjects\\UniSystemManager\\src\\tmp.txt");
File tutorFile = new File(String.valueOf(pTutor));
File temp = new File(String.valueOf(pTemp));
Scanner input = new Scanner(System.in);
Tutor(){}
Tutor(String tutorName,String tutorSurname, String tutorId, String contactNum, String email, String specialty,String course){
this.tutorId = tutorId;
this.PersonName = tutorName;
this.PersonSurname = tutorSurname;
this.PersonContactno = contactNum;
this.PersonEmail = email;
this.specialty = specialty;
}
public void viewAllTutor(){
try(Scanner in = new Scanner(tutorFile)){
while(in.hasNextLine()){
String id = in.next();
String name = in.next().trim();
String surname = in.next().trim();
String cnum = in.next().trim();
String mail = in.next().trim();
String special = in.next().trim();
String courses = in.nextLine();
System.out.println("Tutor ID: " + id);
System.out.println("Tutor Name: " + name);
System.out.println("Tutor Surname: " + surname);
System.out.println("Tutor Contact Number: " + cnum);
System.out.println("Tutor Email Adrress: " + mail);
System.out.println("Tutor Specialty: " + special);
System.out.println("Tutor Courses: " + courses);
System.out.println("\n");
}
} catch(FileNotFoundException e){
System.out.println("File Not Found!");
}
}
/**
* Μέθοδος που τροποποιεί την εγγραφή ενός καθηγητή με κριτήριο τον κωδικό του που δίνεται ως είσοδος
* αντλώντας την πληροφορία απευθείας από το αρχείο καταγραφής tutor.txt
* Επίσης:
* γίνεται έλεγχος για σωστή εισαγωγή του κωδικού καθώς και για την ύπαρξη του αρχείου και
* "άδειασμα" του προσωρινού αρχείου tmp από τις εγγραφές προκειμένου να χρησιμοποιηθεί σε άλλη διεργασία
* και εμφάνιση της τροποποιημένης εγγραφής
*/
void modifyTutorInfo(){
System.out.print("Enter Tutor ID to modify his/her Information: ");
this.info = input.nextLine();
try {
Scanner in = new Scanner(tutorFile);
PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter(temp, false)));
int flag = 0;
while(in.hasNextLine()){
String id = in.next();
String name = in.next().trim();
String surname = in.next().trim();
String cnum = in.next().trim();
String mail = in.next().trim();
String special = in.nextLine();
if(id.compareToIgnoreCase(info) == 0){
System.out.println("Enter Tutor ID: ");
tutorId = input.nextLine();
System.out.println("Enter Tutor Name: ");
PersonName = input.nextLine();
System.out.println("Enter Tutor SurName: ");
PersonSurname = input.nextLine();
System.out.println("Enter Tutor Contact Number: ");
PersonContactno = input.nextLine();
System.out.println("Enter Tutor Email Address: ");
PersonEmail = input.nextLine();
System.out.println("Enter Tutor Specialty: ");
specialty = input.nextLine();
write.println(tutorId + " " + PersonName + " " + PersonSurname + " " + PersonContactno + " " + PersonEmail + " " + specialty);
flag = 1;
System.out.println("Tutor Record Modification completed.");
} else{
write.println(id + " " + name + " " + surname + " " + cnum + " " + mail + " " + special);
}
}
if (flag == 0){
System.out.println("The Tutor record of the entered ID isn't available.");
}
write.close();
in.close();
} catch (IOException e1) {
System.out.println("File Not Found!");
}
try {
Scanner in = new Scanner(temp);
PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter(tutorFile, false)));
while(in.hasNextLine()){
write.println(in.nextLine());
}
write.close();
in.close();
} catch (IOException e1) {
System.out.println("File Not Found!");
}
}
/**
* Μέθοδος που διαγράφει έναν καθηγητη με κριτήριο τον κωδικό του που δίνεται ως είσοδος
* απευθείας από το αρχείο καταγραφής tutor.txt
* Ταυτόχρονα γίνεται έλεγχος για σωστή εισαγωγή του κωδικού καθώς και για την ύπαρξη του αρχείου και
* "άδειασμα" του προσωρινού αρχείου tmp από τις εγγραφές προκειμένου να χρησιμοποιηθεί σε άλλη διεργασία.
*/
void deleteTutor(){
System.out.print("Enter Tutor ID to delete his/her Information: ");
this.info = input.nextLine();
try {
Scanner in = new Scanner(tutorFile);
PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter(temp, false)));
int flag = 0;
while(in.hasNextLine()){
String id = in.next();
String name = in.next().trim();
String surname = in.next().trim();
String cnum = in.next().trim();
String mail = in.next().trim();
String special = in.nextLine();
if(id.compareToIgnoreCase(info) == 0){
flag = 1;
System.out.println("Tutor Deleted successfully.");
} else{
write.println(id + " " + name + " " + surname +" " + cnum + " " + mail + " " + special);
}
}
if (flag == 0){
System.out.println("The Tutor record of the entered ID isn't available.");
}
write.close();
in.close();
} catch (IOException e1) {
System.out.println("File Not Found!");
}
try {
Scanner in = new Scanner(temp);
PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter(tutorFile, false)));
while(in.hasNextLine()){
write.println(in.nextLine());
}
write.close();
in.close();
} catch (IOException e1) {
System.out.println("File Not Found!");
}
}
/**
* Μέθοδος εισαγωγής στοιχείων ενός καθηγητή
* Η εγγραφή γίνεται απευθείας στο αρχείο καταγραφής
*
*/
void createTutor(){
System.out.println("Enter Tutor ID: ");
tutorId = input.nextLine();
System.out.println("Enter Tutor Name: ");
PersonName = input.nextLine();
System.out.println("Enter Tutor SurName: ");
PersonSurname = input.nextLine();
System.out.println("Enter Tutor Contact Number: ");
PersonContactno = input.nextLine();
System.out.println("Enter Tutor Email Address: ");
PersonEmail = input.nextLine();
System.out.println("Enter Tutor Specialty: ");
specialty = input.nextLine();
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tutorFile, true)))){
out.println("\n"+ tutorId + " " + PersonName + " " + PersonSurname + " " + PersonContactno + " " + PersonEmail + " " + specialty);
} catch(IOException e){
System.out.println("File Not Found!");
}
System.out.println("Tutor Record Created.");
}
/**
* Μέθοδος ανάθεσης μαθήματος σε φοιτητή με βάση τον κωδικό του.
* Γίνεται αναζήτηση στο αρχείο καταγραφής βάσει του κωδικού του,η εγγραφή καταχωρείται σε Arraylist στην οποία προστίθεται
* το νέο μάθημα και στη συνέχεια επαναγράφεται στο αρχείο.
*
*/
void addCourseToTutor() throws IOException {
String splitBy = " ";
BufferedReader bufReader = new BufferedReader(new FileReader(String.valueOf(pTutor)));
ArrayList<String> listOfLines = new ArrayList<>();
System.out.println("Input Tutor ID");
Scanner sc = new Scanner(System.in);
String studid = sc.next();
System.out.println("Input Course");
Scanner sc1 = new Scanner(System.in);
String course = sc1.next();
String line = null;
try {
line = bufReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
String oldline = null;
while (line != null) {
String[] b = line.split(splitBy);
if (b[0].equals(studid)) {
oldline = line;
listOfLines.add(line + " " + course);
}
try {
line = bufReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bufReader.close();
} catch (IOException e) {
e.printStackTrace();
}
String newline = null;
for (String s : listOfLines) {
String p = System.lineSeparator();
newline = s + p;
System.out.println(newline);
}
replacefin(oldline, newline);
replacefiles();
erasetmp();
EmptyLines();
ReverseFiles();
erasetmp();
}
/**
* Μέθοδος ανάθεσης για το άδεισμα του προσωρινού αρχείου εγγραφών
*/
static void erasetmp() throws FileNotFoundException {
try (PrintWriter writer = new PrintWriter(String.valueOf(pTemp))) {
writer.print(" ");
//writer.close();
}
}
/**
* Μέθοδος που αντικαθιστά τη νέα εγγραφή στο προσωρινό αρχείο
*/
static void replacefin(String stringToReplace, String replaceWith) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(String.valueOf(pTutor)));
BufferedWriter tmp = new BufferedWriter(new FileWriter(String.valueOf(pTemp)));
String line;
while ((line = in.readLine()) != null) {
if (line.contains(stringToReplace))
line = line.replace(stringToReplace, replaceWith);
tmp.write(line);
tmp.newLine();
}
in.close();
tmp.close();
}
/**
* Μέθοδος με την οποία γίνεται εγγραφή του προσωρινού αρχείου στο αρχείο καταγραφής
* δηλ του διαμορφωμένου tmp.txt στο tutor.txt
*/
public static void replacefiles() {
File source = new File(String.valueOf(pTemp));
File dest = new File(String.valueOf(pTutor));
try {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
} catch (Exception e) {
System.out.println("replace failure");
}
}
/**
* Μέθοδος με την οποία διαγράφονται οι κενές γραμμές που πιθανόν να έχουν δημιουργηθεί
* στο αρχείο καταγραφής
*/
public static void EmptyLines() throws FileNotFoundException {
Scanner file;
PrintWriter writer;
file = new Scanner(new File(String.valueOf(pTutor)));
writer = new PrintWriter(String.valueOf(pTemp));
while (file.hasNext()) {
String line = file.nextLine();
if (!line.isEmpty()) {
writer.write(line);
writer.write("\n");
}
}
file.close();
writer.close();
}
/**
* Μέθοδος με την οποία αντικαθίσταται το διαμορφωμένο αρχείο στο οποίο έχουν διαγραφεί
* οι κενές γραμμές με το αρχικό αρχείο καταγραφής.
*/
public static void ReverseFiles(){
File source = new File(String.valueOf(pTemp));
File dest = new File(String.valueOf(pTutor));
try {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
} catch (Exception e) {
System.out.println("Reverse failure" );
}
}
/**
* Μέθοδος που αναζητά ένα καθηγητή με κριτήριο τον κωδικό του ο οποίος δίνεται ως είσοδος
* αντλώντας την πληροφορία απευθείας από το αρχείο καταγραφής tutor.txt
* Ταυτόχρονα γίνεται έλεγχος για σωστή εισαγωγή του ΑΜ καθώς και για την ύπαρξη του αρχείου.
*/
void viewTutorInfo(){
System.out.print("Enter Tutor ID to view his/her Student Information: ");
this.info = input.nextLine();
try {
Scanner in = new Scanner(pTutor);
PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter(temp, false)));
int flag = 0;
while(in.hasNextLine()){
String id = in.next();
String name = in.next().trim();
String surn = in.next().trim();
String cnum = in.next().trim();
String mail = in.next().trim();
String special = in.next().trim();
String courses = in.nextLine();
if(id.compareToIgnoreCase(info) == 0){
flag = 1;
System.out.println("Tutor ID: " + id);
System.out.println("Tutor Name: " + name);
System.out.println("Tutor Surname: " + surn);
System.out.println("Tutor Contact Number: " + cnum);
System.out.println("Tutor Email Adrress: " + mail);
System.out.println("Tutor Specialty: " + special);
System.out.println("Tutor Courses: " + courses);
}
}
if (flag == 0){
System.out.println("The Student record of the entered ID isn't available.");
}
write.close();
in.close();
} catch (IOException e1) {
System.out.println("File Not Found!");
}
}
}