-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileLoader.java
228 lines (193 loc) · 5.85 KB
/
FileLoader.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
package ws.thurn.dossier;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* The FileLoader class is passed a specific class to locate in the program's
* save file. It loads this class into the program.
*
* @author Derek Thurn
*/
public class FileLoader
{
private File theFile;
// The program's save file.
private RandomAccessFile io;
// A RandomAccessFile to access the save file.
long klassLocation;
// The location, by pointer, of the class to be overwritten.
private boolean time = false;
// If true, the program will time how long it takes to load data from the
// file.
/**
* Creates a new FileLoader for a specific class.
*
* @param f
* @param k
*/
public FileLoader( File f, Klass k )
{
long start = 0;
if( time )
{
start = System.currentTimeMillis();
}
try
{
klassLocation = GradeBook.fileFindClass( k );
theFile = f;
io = new RandomAccessFile( theFile, "rw" );
loadClass();
}
catch( IOException e )
{
GradeBook.error( "IO Exception!" );
}
if( time )
{
long stop = System.currentTimeMillis();
System.out.println( stop - start );
}
}
/**
* Loads the specified class.
*
* @throws IOException
* @pre the specified class location is in the savefile
* @post the specified class has been loaded into the program.
*/
public void loadClass() throws IOException
{
io.seek( klassLocation );
// read basic class data
String className = readString();
String teacherName = readString();
String schoolName = readString();
if( GradeBook.isDuplicate( new Klass( className ) ) )
{
GradeBook.deleteClass( className );
}
Klass theClass = new Klass( className, teacherName, schoolName );
GradeBook.addClass( theClass );
// read student info
readStudents( theClass );
// read category info
readCategories( theClass );
// read assignment info
readAssignments( theClass );
// read grade info
io.seek( klassLocation + 21100 );
for( int i = 0; i < GradeBook.PROGRAM_LIMIT; i++ )
{
for( int j = 1; j < GradeBook.PROGRAM_LIMIT; j++ )
{
int grade = io.readInt();
if( grade != -1 )
theClass.setGradeData( i, j, grade );
}
}
}
/**
* Reads the class' students.
*
* @param k
* @throws IOException
* @pre the class is correctly saved to the file
* @post the class' students have been read into the program
*/
public void readStudents( Klass k ) throws IOException
{
String name;
int id;
io.seek( klassLocation + 300 );
for( int i = 0; i < GradeBook.PROGRAM_LIMIT; i++ )
{
name = readString();
if( name.equals( "" ) || name == null )
return;
id = io.readInt();
k.addStudent( new Student( name, id, k ) );
}
}
/**
* Read the class' categories.
*
* @param k
* @throws IOException
* @pre the class is correctly saved to the file
* @post the class' categories have been read into the program
*/
public void readCategories( Klass k ) throws IOException
{
String name;
int weight;
io.seek( klassLocation + 5500 );
for( int i = 0; i < GradeBook.PROGRAM_LIMIT; i++ )
{
name = readString();
if( name.equals( "" ) || name == null )
return;
weight = io.readInt();
Category temp = new Category( name, weight );
k.addCategory( temp );
}
}
/**
* Reads the class' assignments
*
* @param k
* @throws IOException
* @pre the class is correctly saved to the file
* @post the class' assignments have been read into the program
*/
public void readAssignments( Klass k ) throws IOException
{
String name;
int tm;
int weight;
String cat;
io.seek( klassLocation + 10700 );
for( int i = 0; i < GradeBook.PROGRAM_LIMIT; i++ )
{
name = readString();
if( name.equals( "" ) || name == null )
return;
tm = io.readInt();
weight = io.readInt();
cat = readString();
if( cat.equals( "Assignment Has No Category X" ) )
{
k.addAssignment( new Assignment( name, tm, weight ) );
}
else
{
Category c = k.findCategory( new Category( cat ) );
k.addAssignment( new Assignment( name, tm, weight, c ) );
}
}
}
/**
* Reads in a string from the file.
*
* @return
* @throws IOException
* @pre the pointer is in the place where a read is desired, the string is
* properly formatted
* @post the string has been read in and "unpadded".
*/
public String readString() throws IOException
{
String temp = "";
for( int i = 0; i < GradeBook.PROGRAM_LIMIT; i++ )
{
temp = temp + io.readChar();
}
for( int i = GradeBook.PROGRAM_LIMIT; i > 0; i-- )
{
if( temp.charAt( i - 1 ) != ' ' )
break;
temp = temp.substring( 0, i - 1 );
}
return temp;
}
}