-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.java
More file actions
241 lines (231 loc) · 8.45 KB
/
Copy pathDataManager.java
File metadata and controls
241 lines (231 loc) · 8.45 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author GOThreads
*/
public class DataManager {
//this constructor to be used during integration with ATSManager
/*ATSManager mgr;
public DataManager(ATSManager mgr) {
this.mgr = mgr;
}*/
public Course[] readCourses(String filename) {
BufferedReader br = null;
FileReader fr = null;
Course[] courses=new Course[50];
int i=0;
try{
HashMap<String,String> tempAssociation = new HashMap<>();
String str, courseName;
Course partner;
fr = new FileReader(filename);
br = new BufferedReader(fr);
br.readLine();
while((str=br.readLine())!=null){
//String[] result = str.split("[,]",-1);
StringTokenizer st = new StringTokenizer(str,",");
courseName = st.nextToken();
courses[i]=new Course(courseName,st.nextToken(),Integer.parseInt(st.nextToken()),st.nextToken(),st.nextToken());
//to check for and store the association course
if(st.hasMoreTokens()){
tempAssociation.put(courseName,st.nextToken());
}
i++;
}
for(Course c : courses){
if((c!=null) && (tempAssociation.containsKey(c.getCode()))){
partner = this.getCourseByCode(courses, tempAssociation.get(c.getCode()));
c.setAssociation(partner);
}
}
for(Course c : courses){
if(c!=null)
c.printAsString();
}
}catch(FileNotFoundException ex){
System.out.println("File not found for courses. Invalid filename: "+ filename);
} catch (IOException ex) {
System.out.println("IOException for courses file with filename: "+ filename);
}finally{
try {
if(br!=null)
br.close();
if(fr!=null)
fr.close();
} catch (IOException ex) {
System.out.println("Problem closing file with filename: "+ filename);
}
}
return courses;
}
public Instructor[] readInstructors(String filename, Course[] courses) {
BufferedReader br = null;
FileReader fr = null;
Instructor[] instructors=new Instructor[50];
Course[] expertise = new Course[4];
String name;
int i=0;
try{
String str;
fr = new FileReader(filename);
br = new BufferedReader(fr);
br.readLine();
while((str=br.readLine())!=null){
StringTokenizer st = new StringTokenizer(str,",");
int k=0;
name=st.nextToken();
while(st.hasMoreTokens()){
expertise[k] = getCourseByCode(courses,st.nextToken());
k++;
}
instructors[i]=new Instructor(name,expertise);
i++;
}
for(Instructor ins : instructors){
if(ins!=null){
ins.printAsString();
}
}
}catch(FileNotFoundException ex){
System.out.println("File not found for instructors. Invalid filename: "+ filename);
} catch (IOException ex) {
System.out.println("IOException for instructors file with filename: "+ filename);
}finally{
try {
if(br!=null)
br.close();
if(fr!=null)
fr.close();
} catch (IOException ex) {
System.out.println("Problem closing file with filename: "+ filename);
}
}
return instructors;
}
public Timeslot[] readTimeslots(String filename) {
BufferedReader br = null;
FileReader fr = null;
Timeslot[] timeslots=new Timeslot[50];
String dayOfWeek;
int slot,startTime,endTime;
int i=0;
try{
String str;
fr = new FileReader(filename);
br = new BufferedReader(fr);
br.readLine();
while((str=br.readLine())!=null){
StringTokenizer st = new StringTokenizer(str,",");
while(st.hasMoreTokens()){
dayOfWeek=st.nextToken();
slot=Integer.parseInt(st.nextToken());
startTime=Integer.parseInt(st.nextToken());
endTime=Integer.parseInt(st.nextToken());
timeslots[i]=new Timeslot(dayOfWeek,slot,startTime,endTime);
}
i++;
}
for(Timeslot t: timeslots){
if(t!=null){
t.printAsString();
}
}
}catch(FileNotFoundException ex){
System.out.println("File not found for timeslots. Invalid filename: "+ filename);
} catch (IOException ex) {
System.out.println("IOException for timeslots file with filename: "+ filename);
}finally{
try {
if(br!=null)
br.close();
if(fr!=null)
fr.close();
} catch (IOException ex) {
System.out.println("Problem closing file with filename: "+ filename);
}
}
return timeslots;
}
/*public Section[] readSections(String filename) {
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(filename));
}catch(FileNotFoundException ex){
System.out.println("File not found for sections. invalid filename: "+ filename);
}
return new Section[10];
}*/
public Room[] readRooms(String filename) {
BufferedReader br = null;
FileReader fr = null;
Room[] rooms=new Room[50];
String resource,type;
int capacity;
int i=0;
try{
String str;
fr = new FileReader(filename);
br = new BufferedReader(fr);
br.readLine();
while((str=br.readLine())!=null){
StringTokenizer st = new StringTokenizer(str,",");
int k=0;
while(st.hasMoreTokens()){
resource=st.nextToken();
type=st.nextToken();
capacity=Integer.parseInt(st.nextToken());
rooms[i]=new Room(resource,type,capacity);
}
i++;
}
for(Room r : rooms){
if(r!=null){
r.printAsString();
}
}
}catch(FileNotFoundException ex){
System.out.println("File not found for timeslots. Invalid filename: "+ filename);
} catch (IOException ex) {
System.out.println("IOException for timeslots file with filename: "+ filename);
}finally{
try {
if(br!=null)
br.close();
if(fr!=null)
fr.close();
} catch (IOException ex) {
System.out.println("Problem closing file with filename: "+ filename);
}
}
return rooms;
}
public Course getCourseByCode(Course[] courses, String courseCode){
for(Course c : courses){
if(c.getCode().equals(courseCode)){
return c;
}
}
return null;
}
//test all methods using main
public static void main(String args[]){
DataManager dataMgr = new DataManager();
Course[] courses = dataMgr.readCourses("courses.csv");
System.out.println("\n=========================================================");
dataMgr.readInstructors("instructors.csv", courses);
System.out.println("\n=========================================================");
dataMgr.readTimeslots("timeslots.csv");
dataMgr.readRooms("rooms.csv");
System.out.println("\n=========================================================");
}
}