-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.java
More file actions
149 lines (114 loc) · 4.42 KB
/
DataManager.java
File metadata and controls
149 lines (114 loc) · 4.42 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class DataManager{
private static String ensureCSV(String fileName){
if(!fileName.endsWith(".csv")){
fileName += ".csv";
}
return "data/" + fileName;
}
// going to save each students information to .csv file
public static void saveStudentsToFile(HashMap<String,Student> students, String fileName){
fileName = DataManager.ensureCSV(fileName);
try(PrintWriter fileS=new PrintWriter(fileName)){
fileS.println("name,id,tuition");
for(Student s: students.values()){
fileS.printf("%s,%s,%.2f\n",s.getName(),s.getID(),s.getTuition());
}
}
catch(FileNotFoundException e){
System.out.println("Error: "+ e.getMessage());
}
}
public static void saveProfessorsToFile(HashMap<String,Professor> professors, String fileName){
fileName = DataManager.ensureCSV(fileName);
try(PrintWriter fileS=new PrintWriter(fileName)){
fileS.println("name,id");
for(Professor p: professors.values()){
fileS.printf("%s,%s\n", p.getName(), p.getID());
}
}catch(FileNotFoundException e){
System.out.println("There was an error:" + e.getMessage());
}
}
public static void saveCoursesToFile(HashMap<String, Course> courses,String fileName){
fileName = DataManager.ensureCSV(fileName);
try(PrintWriter fileS = new PrintWriter(fileName)){
fileS.println("title,credits");
for(Course p: courses.values()){
fileS.printf("%s,%d\n",p.getCourseTitle(), p.getCourseCredits());
}
}catch(FileNotFoundException e){
System.out.println("There was an error: " + e.getMessage());
}
}
public static ArrayList<Student> loadStudentsFromFile(String filename) {
ArrayList<Student> students = new ArrayList<>();
File file = new File(DataManager.ensureCSV(filename));
try (Scanner sc = new Scanner(file)) {
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty() || line.startsWith("<<<<<<<") || line.startsWith("=======") || line.startsWith(">>>>>>>")) continue;
if (line.equalsIgnoreCase("name,id,tuition")) continue;
String[] parts = line.split(",");
if (parts.length != 3) continue;
String name = parts[0].trim();
String id = parts[1].trim();
String tuitionText = parts[2].trim();
if (name.equalsIgnoreCase("name") && id.equalsIgnoreCase("id") && tuitionText.equalsIgnoreCase("tuition")) continue;
double tuition;
try {
tuition = Double.parseDouble(tuitionText);
} catch (NumberFormatException e) {
continue;
}
students.add(new Student(name, id, tuition));
}
} catch (Exception e) {
System.out.println("Error loading file: " + e.getMessage());
}
return students;
}
public static ArrayList<Professor> loadProfessorFromFile(String fileName){
ArrayList<Professor> professors = new ArrayList<>();
File file = new File(DataManager.ensureCSV(fileName));
try(Scanner sc = new Scanner(file)){
while(sc.hasNextLine()){
String line = sc.nextLine().trim();
if(line.isEmpty() || line.startsWith("<<<<<<<") || line.startsWith("=======") || line.startsWith(">>>>>>>")) continue;
if(line.equalsIgnoreCase("name,id")) continue;
String[] parts = line.split(",");
if(parts.length!=2) continue;
String name = parts[0].trim();
String id = parts[1].trim();
if(name.equalsIgnoreCase("name") && id.equalsIgnoreCase("id")) continue;
professors.add(new Professor(name, id));
}
}catch (FileNotFoundException e){
System.out.println("Error loading file: " + fileName);
}
return professors;
}
public static ArrayList<Course> loadCourseFromFile(String fileName){
ArrayList<Course> loadedCourses = new ArrayList<>();
File file = new File(DataManager.ensureCSV(fileName));
try(Scanner sc = new Scanner(file)){
while(sc.hasNextLine()){
String line = sc.nextLine().trim();
if(line.isEmpty() || line.equalsIgnoreCase("title,credits")) continue;
String[] parts = line.split(",");
if(parts.length != 2) continue;
String name = parts[0].trim();
int credits=Integer.parseInt(parts[1].trim());
loadedCourses.add(new Course(name, credits));
}
}catch(Exception e){
System.out.println("Error loading file: " + fileName);
}
return loadedCourses;
}
}