-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
108 lines (104 loc) · 4.17 KB
/
Student.java
File metadata and controls
108 lines (104 loc) · 4.17 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
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Student extends User{
/*
Represents each student's username and password.
It also writes the username corresponding to their names and passwords to a CSV file.
*/
ArrayList<String> registeredCourses;
static List<String> pastCourses;
String fullName;
static String fileName;
static int studentCount = 0;
ArrayList<String> prefferedCourses;
Student(String netId, String fullName, float preGPA, List<String> pastCourses) throws IOException {
super(netId, fullName, preGPA, pastCourses);
this.pastCourses = pastCourses;
fileName = "Student.csv";
writeCSV();
}
void writeCSV() throws IOException {
boolean fileExists = new File(fileName).exists(); //checks if the file already exists
//set the append mode to true so that it won't override things
try(FileWriter fw = new FileWriter(fileName, true)) {
if (!fileExists) {
fw.append("NetID,Full Name,GPA,Courses Taken\n");
}
if (!studentExists(this.netId)) {
System.out.println("Writing Student.csv");
fw.append(this.netId);
fw.append(",");
fw.append(this.name);
fw.append(",");
fw.append(Float.toString(this.prevGPA));
fw.append(",");
fw.append(String.join("|", this.pastCourses));
fw.append("\n");
System.out.println("Student Added Successfully");
studentCount ++;
}
}
catch (RuntimeException e) {
throw new RuntimeException(e);
}
catch(IOException e){
System.out.println("Problem writing to file");
}
}
void studentCoursePrefWriter(ArrayList<String> prefferedCourses) throws IOException {
this.prefferedCourses = prefferedCourses;
fileName = "StudentPref.csv";
try(FileWriter fw = new FileWriter(fileName, true)){
fw.append(this.netId);
fw.append(",");
fw.append(String.join("|", this.pastCourses));
fw.append("\n");
}
}
public static ArrayList<String> getPreferredCourses(String netId) throws IOException {
ArrayList<String> preferredCourses = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader("Student.csv"))) {
String line;
while((line=br.readLine())!=null){
line = line.trim();
if(line.contains(netId.trim())){
String courses = line.substring(line.lastIndexOf(",")+1).trim();
preferredCourses.addAll(Arrays.asList(courses.split("\\|")));
}
}
return preferredCourses;
}
}
public static ArrayList<String> getPastCourses(String netId) throws IOException {
ArrayList<String> pastCourses = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader("Courses.csv"))) {
String line;
while((line=br.readLine())!=null){
line = line.trim();
if(line.contains(netId.trim())){
String courses = line.substring(line.indexOf(",", 15),line.lastIndexOf(",")).trim();//TODO: NEEDS CORRECTION
pastCourses.addAll(Arrays.asList(courses.split("\\|")));
}
}
return pastCourses;
}
}
public static boolean studentExists(String netId) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
line = line.split(",")[0].trim();
if (line.equalsIgnoreCase(netId.trim())) {
System.out.println("NetId is found");
return true;
}
}
}
return false;
}
static int getStudentCount() {
return studentCount;
}
}