-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradeBook.java
739 lines (625 loc) · 20.9 KB
/
GradeBook.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package ws.thurn.dossier;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* The GradeBook class launches the program and contains methods which must be
* program-wide by necessity, such as those dealing with multiple classes.
*
* @author Derek Thurn
*/
public class GradeBook
{
private static LinkedList classes = new LinkedList();
// A linked list to contain the classes in the program.
public static final int PROGRAM_LIMIT = 50;
// the program limit constant controls a number of parts of the program,
// including
// the max # of assignmments and students, the max # of grades, and the
// length at which
// a string will be chopped off by the file saver.
private static Klass currentClass = new Klass( "Computer Science" );
// The program's current class to be modified.
private static File saveFile = new File( "gradebook.dat" );
// The program's save file.
/**
* Intatiates the program's Main Menu, may add test data to the program.
*
* @param args
*/
public static void main( String[] args )
{
boolean DEBUG = true;
// If true, add test data.
if( DEBUG )
{
addClass( currentClass );
currentClass
.addStudent( new Student( "Thurn, Derek", currentClass ) );
currentClass.addStudent( new Student( "Kassam, Noorez",
currentClass ) );
currentClass
.addStudent( new Student( "Kelln, Graeme", currentClass ) );
currentClass.addStudent( new Student( "De Yaegher, Michael",
currentClass ) );
currentClass.addStudent( new Student( "Derksen-Hatt, Justin",
currentClass ) );
Category on = new Category( "Odd Numbers", 1 );
Category en = new Category( "Even Numbers", 10 );
currentClass.addCategory( on );
currentClass.addCategory( en );
currentClass.addAssignment( new Assignment( "Assignment 1", 5, 1,
on ) );
currentClass.addAssignment( new Assignment( "Assignment 2", 5, 1,
en ) );
currentClass.addAssignment( new Assignment( "Assignment 3", 5, 1,
on ) );
currentClass.addAssignment( new Assignment( "Assignment 4", 5, 1,
en ) );
currentClass.addAssignment( new Assignment( "Assignment 5", 5, 1,
on ) );
currentClass.setGradeData( 0, 1, 4 );
currentClass.setGradeData( 0, 2, 3 );
currentClass.setGradeData( 0, 3, 4 );
currentClass.setGradeData( 0, 4, 5 );
currentClass.setGradeData( 0, 5, 4 );
currentClass.setGradeData( 1, 1, 3 );
currentClass.setGradeData( 1, 2, 2 );
currentClass.setGradeData( 1, 3, 1 );
currentClass.setGradeData( 1, 4, 2 );
currentClass.setGradeData( 1, 5, 3 );
currentClass.setGradeData( 2, 1, 4 );
currentClass.setGradeData( 2, 2, 5 );
currentClass.setGradeData( 2, 3, 5 );
currentClass.setGradeData( 2, 4, 5 );
currentClass.setGradeData( 2, 5, 4 );
currentClass.setGradeData( 3, 1, 3 );
currentClass.setGradeData( 3, 2, 4 );
currentClass.setGradeData( 3, 3, 5 );
currentClass.setGradeData( 3, 4, 5 );
currentClass.setGradeData( 3, 5, 5 );
currentClass.setGradeData( 4, 1, 1 );
currentClass.setGradeData( 4, 2, 2 );
currentClass.setGradeData( 4, 3, 1 );
currentClass.setGradeData( 4, 4, 3 );
currentClass.setGradeData( 4, 5, 4 );
Klass temp1 = new Klass( "Math", "Ancelin", "SWC" );
Klass temp2 = new Klass( "Social", "Oldfield", "SWC" );
addClass( temp1 );
addClass( temp2 );
temp1.addStudent( new Student( "Thurn, Derek", temp1 ) );
temp1.addStudent( new Student( "Kassam, Noorez", temp1 ) );
on = new Category( "Odd Numbers", 1 );
en = new Category( "Even Numbers", 10 );
temp1.addCategory( on );
temp1.addCategory( en );
temp1.addAssignment( new Assignment( "Assignment 1", 5, 1, on ) );
temp1.addAssignment( new Assignment( "Assignment 2", 5, 1, en ) );
temp1.setGradeData( 0, 1, 4 );
temp1.setGradeData( 0, 2, 3 );
temp1.setGradeData( 0, 3, 4 );
temp1.setGradeData( 0, 4, 5 );
temp1.setGradeData( 0, 5, 4 );
temp1.setGradeData( 1, 1, 3 );
temp1.setGradeData( 1, 2, 2 );
temp1.setGradeData( 1, 3, 1 );
temp1.setGradeData( 1, 4, 2 );
temp1.setGradeData( 1, 5, 3 );
temp2.addStudent( new Student( "Thurn, Derek", temp2 ) );
temp2.addStudent( new Student( "Kassam, Noorez", temp2 ) );
temp2.addStudent( new Student( "Kelln, Graeme", temp2 ) );
temp2.addStudent( new Student( "De Yaegher, Michael", temp2 ) );
on = new Category( "Odd Numbers", 1 );
en = new Category( "Even Numbers", 10 );
temp2.addCategory( on );
temp2.addCategory( en );
temp2.addAssignment( new Assignment( "Assignment 1", 5, 1, on ) );
temp2.addAssignment( new Assignment( "Assignment 2", 5, 1, en ) );
temp2.setGradeData( 0, 1, 4 );
temp2.setGradeData( 0, 2, 3 );
temp2.setGradeData( 0, 3, 4 );
temp2.setGradeData( 0, 4, 5 );
temp2.setGradeData( 0, 5, 4 );
temp2.setGradeData( 1, 1, 3 );
temp2.setGradeData( 1, 2, 2 );
temp2.setGradeData( 1, 3, 1 );
temp2.setGradeData( 1, 4, 2 );
temp2.setGradeData( 1, 5, 3 );
temp2.setGradeData( 2, 1, 4 );
temp2.setGradeData( 2, 2, 5 );
temp2.setGradeData( 2, 3, 5 );
temp2.setGradeData( 2, 4, 5 );
temp2.setGradeData( 2, 5, 4 );
temp2.setGradeData( 3, 1, 3 );
temp2.setGradeData( 3, 2, 4 );
temp2.setGradeData( 3, 3, 5 );
temp2.setGradeData( 3, 4, 5 );
temp2.setGradeData( 3, 5, 5 );
temp2.setGradeData( 4, 1, 1 );
temp2.setGradeData( 4, 2, 2 );
temp2.setGradeData( 4, 3, 1 );
temp2.setGradeData( 4, 4, 3 );
temp2.setGradeData( 4, 5, 4 );
File f = GradeBook.getSaveFile();
try
{
RandomAccessFile io = new RandomAccessFile( f, "rw" );
new FileSaver( io, currentClass );
new FileSaver( io, temp1 );
new FileSaver( io, temp2 );
currentClass.setGradeData( 0, 1, 2 );
new FileSaver( io, currentClass );
new FileLoader( f, currentClass );
int result = currentClass.getGradeValue( 0, 1 );
if( result == 2 )
{
/*
* GradeBook.info("Success!");
*/
}
}
catch( IOException e )
{
GradeBook.error( "Test failed." );
}
}
new MainMenu();
}
/**
* Centers a JFrame.
*
* @pre the frame exists
* @post the frame is centered.
* @param frame
*/
public static void center( JFrame frame )
{
/*
* Andre (2002). How to center a frame from Scratchbook for Java
* Development Kit Retrieved February 2, 2006, from
* http://home.tiscali.nl/~bmc88/java/sbook/0110.html
*/
frame.setVisible( false );
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = frame.getSize();
screenSize.height = screenSize.height / 2;
screenSize.width = screenSize.width / 2;
size.height = size.height / 2;
size.width = size.width / 2;
int y = screenSize.height - size.height;
int x = screenSize.width - size.width;
frame.setLocation( x, y );
frame.setVisible( true );
}
/**
* Sets the current class.
*
* @pre none
* @post the class is set.
* @param currentClass The currentClass to set.
*/
public static void setCurrentClass( Klass cc )
{
currentClass = cc;
}
/**
* Gets the program's save file.
*
* @pre the file exists
* @post it has been returned.
* @return the program's save file.
*/
public static File getSaveFile()
{
return saveFile;
}
/**
* Deletes a specified saved class.
*
* @pre The class to be deleted exists on file
* @post The class has been deleted.
* @param k the class to delete
* @throws IOException
*/
public static void deleteSavedClass( Klass k ) throws IOException
{
long location = fileFindClass( k );
RandomAccessFile io = new RandomAccessFile( saveFile, "rw" );
io.seek( location );
io.writeChars( "This Slot Is Currently Empty" );
while( io.getFilePointer() < location + 30900 )
{
io.writeChar( ' ' );
}
}
/**
* Returns a vector object with the program's saved classes.
*
* @pre none
* @post the saved classes have been returned.
* @return
*/
public static Vector getSavedClasses()
{
Vector<String> classV = new Vector( 5, 1 );
try
{
RandomAccessFile io = new RandomAccessFile( saveFile, "rw" );
if( io.length() == 0 )
{
classV.addElement( "No Classes Have Been Saved" );
return classV;
}
int location = 0;
while( location < io.length() )
{
classV.addElement( readString( location ) );
location = location + 30900;
}
return classV;
}
catch( IOException E )
{
GradeBook.error( "IO Exception" );
return classV;
}
}
/**
* Find the pointer location of a specified class.
*
* @pre the class exists in the save file
* @post the location has been returned.
* @param k the class to find.
* @return
* @throws IOException
*/
public static long fileFindClass( Klass k ) throws IOException
{
String name = k.toString();
RandomAccessFile io = new RandomAccessFile( saveFile, "rw" );
if( io.length() == 0 )
return -1;
if( readString( 0 ).equals( name ) )
return 0;
long location = 0;
while( location < io.length() )
{
io.seek( location );
String theString = readString( location );
if( theString.equals( name ) )
{
return location;
}
location = location + 30900;
}
return -1;
}
/**
* Return the current class
*
* @pre none
* @post none
* @return Returns the currentClass.
*/
public static Klass getCurrentClass()
{
return currentClass;
}
/**
* Gets the linked list with the program's classes.
*
* @pre the list exists
* @post none
* @return Returns the classes linked list.
*/
public static LinkedList getClasses()
{
return classes;
}
/**
* Adds a class to the program.
*
* @pre none
* @post the class has been added.
* @param k the class to add.
*/
public static void addClass( Klass k )
{
classes.addLast( k );
k.initGradeData();
}
/**
* Checks to see if a specific class has the same name as another.
*
* @pre some classes have been added.
* @post none
* @param k the class to check.
* @return true or false
*/
public static boolean isDuplicate( Klass k )
{
String dupe = k.toString();
ListIterator ite = classes.listIterator();
while( ite.hasNext() )
{
if( ite.hasNext() == false )
break;
String c = ite.next().toString();
if( c == null )
{
return true;
}
if( c.equals( dupe ) )
return true;
}
return false;
}
/**
* Get the number of classes in the program
*
* @pre none
* @post none
* @return Returns the numClasses.
*/
public static int getNumClasses()
{
return classes.getLength();
}
/**
* Get the location (in the linked list) of the current class.
*
* @pre the current class has been specified
* @post none
* @return an integer representing the location
*/
public static int getCurrentClassLocation()
{
if( currentClass == null )
return -1;
ListIterator ite = classes.listIterator();
int i = 0;
while( ite.hasNext() )
{
String a = currentClass.toString();
String b = ite.next().toString();
if( a.equals( b ) )
return i;
i++;
}
return -1;
}
/**
* Returns a string array with the program's classes.
*
* @pre a classes linked list exists
* @post a string array with the classes has been returned.
* @return the classes string array.
*/
public static String[] getClassesStringArray()
{
String temp[] = new String[ 100 ];
ListIterator ite = classes.listIterator();
for( int i = 0; i < 100; i++ )
{
temp[ i ] = "EMPTY STRING HERE";
}
for( int i = 0; ite.hasNext(); i++ )
{
temp[ i ] = ite.next().toString();
}
int stopHere = 0;
for( int i = 0; i < 100; i++ )
{
if( temp[ i ].equals( "EMPTY STRING HERE" ) )
{
stopHere = i;
break;
}
}
if( stopHere == 0 )
{
String temp2[] = new String[ 1 ];
temp2[ 0 ] = "";
return temp2;
}
int index = 0;
String ret[] = new String[ stopHere ];
while( index != stopHere )
{
ret[ index ] = temp[ index ];
index = index + 1;
}
return ret;
}
/**
* Deletes a specific class.
*
* @pre The class to be deleted is in the program
* @post the class has been deleted
* @param k the class to delete.
*/
public static void deleteClass( String k )
{
ListIterator lite = classes.listIterator();
lite = classes.listIterator();
lite = classes.listIterator();
while( lite.hasNext() )
{
String compare = lite.next().toString();
if( compare.equals( k ) )
{
lite.remove();
}
}
}
/**
* Returns a class based on its name.
*
* @pre a class by that name already exists
* @post the klass object has been returned.
* @param k The name of the class to find.
* @return The klass object.
*/
public static Klass getClassFromString( String k )
{
return (Klass) ( classes.find( k ) );
}
/**
* Returns a student based on its name.
*
* @pre a student by that name already exists
* @post the student object has been returned.
* @param k The name of the student to find.
* @return The student object.
*/
public static Student getStudentFromString( String studentName )
{
ListIterator ite = classes.listIterator();
while( ite.hasNext() )
{
Klass k = (Klass) ite.next();
Student st = k.findStudent( new Student( studentName ) );
if( st != null )
return st;
}
return null;
}
/**
* Returns an assignment based on its name.
*
* @pre an assignment by that name already exists
* @post the assignment object has been returned.
* @param k The name of the assignment to find.
* @return The assignment object.
*/
public static Assignment getAssignmentFromString( String assignmentName )
{
ListIterator ite = classes.listIterator();
while( ite.hasNext() )
{
Klass k = (Klass) ite.next();
Assignment st = k.findAssignment( new Assignment( assignmentName ) );
if( st != null )
return st;
}
return null;
}
/**
* Returns a category based on its name.
*
* @pre a category by that name already exists
* @post the category object has been returned.
* @param k The name of the category to find.
* @return The category object.
*/
public static Klass getStudentClassFromString( String studentName )
{
ListIterator ite = classes.listIterator();
while( ite.hasNext() )
{
Klass k = (Klass) ite.next();
Student st = k.findStudent( new Student( studentName ) );
if( st != null )
return k;
}
return null;
}
/**
* Given an assignment's name, find which class it is in.
*
* @pre the assignment and class both exist
* @post the correct class has been returned.
* @param assignmentName the assignment to find the class for
* @return the klass object owning the assignment.
*/
public static Klass getAssignmentClassFromString( String assignmentName )
{
ListIterator ite = classes.listIterator();
while( ite.hasNext() )
{
Klass k = (Klass) ite.next();
Assignment b = new Assignment( assignmentName );
Assignment a = k.findAssignment( b );
if( a != null )
return k;
}
return null;
}
/**
* Displays an error message
*
* @pre none
* @post none
* @param e the message to display.
*/
public static void error( String e )
{
JOptionPane.showMessageDialog( null, e, "Error",
JOptionPane.ERROR_MESSAGE );
}
/**
* Checks to see if the program has sufficient data to begin a report
*
* @pre none
* @post the correct response has been returned
* @return true if the program is ready for a report, false otherwise.
*/
public static boolean programReportReady()
{
ListIterator lite = classes.listIterator();
while( lite.hasNext() )
{
if( !( (Klass) lite.next() ).classReportReady() )
{
return false;
}
}
if( classes.getLength() < 1 )
return false;
return true;
}
/**
* Displays an informative message
*
* @pre none
* @post none
* @param s the message to display.
*/
public static void info( String s )
{
JOptionPane.showMessageDialog( null, s, "Grade Book",
JOptionPane.INFORMATION_MESSAGE );
}
/**
* Given an io file pointer location, read in a string
*
* @param location the location to be read from
* @pre the location is within the file, the string is formatted to
* GradeBook.PROGRAM_LIMIT characters.
* @post the string has been correctly parsed.
* @return the string at that location, minus its padding
* @throws IOException
*/
public static String readString( long location ) throws IOException
{
String temp = "";
RandomAccessFile io = new RandomAccessFile( saveFile, "rw" );
io.seek( location );
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;
}
}