Having encapsulated so much functionality into the Student, ScheduleOfCourses, CourseCatalog, and Faculty classes allows us to dramatically simplify the Main method for the SRS "driver" class; let's revisit that class to see how it should be changed to accommodate all that we've done in this chapter.
The first part of the method creates CourseCatalog, ScheduleOfClasses, and Faculty objects in a process identical to what was done in the "test scaffolding" Main methods of the individual classes. The order that the objects are created is important, because a ScheduleOfClasses object needs a reference to a CourseCatalog, and a Faculty object needs a reference to a ScheduleOfClasses.
// Create CourseCatalog, ScheduleOfClasses, and Faculty // objects. The order of creation is important because a // ScheduleOfClasses needs a CourseCatalog object to properly
// initialize, and a Faculty object needs a ScheduleOfClasses // object. // Create a CourseCatalog object and read data from input // files. CourseCatalog catalog = new CourseCatalog("CourseCatalog.dat", "Prerequisites.dat"); catalog.ReadCourseCatalogData(); catalog.ReadPrerequisitesData(); // Create a ScheduleOfClasses object and read data from the // input file. ScheduleOfClasses schedule = new ScheduleOfClasses("SoC_SP2009.dat", "SP2009"); schedule.ReadScheduleData(catalog); // Create a Faculty object and read data from input files. Faculty faculty = new Faculty("Faculty.dat", "TeachingAssignments.dat"); faculty.ReadFacultyData(); faculty.ReadAssignmentData(schedule);
We'll handle the students differently: that is, rather than loading them all in at application outset, we'll pull in the data that we need just for one Student when that Student logs on, as we saw when we reviewed the "new and improved" Student class constructor earlier in this chapter. Because we don't yet have a mechanism to allow a user to log on—we'll provide that in Chapter 16—we'll temporarily create a few Student objects by hard-coding calls to the Student constructor, to simulate students logging on to the SRS. This enables us to exercise and test the enhanced Student class constructor. Note that only the first of these Students has preregistered for courses based on the content of their student data file, as discussed earlier.
// Let's temporarily create Students this way as a test, // to simulate Students logging on. Note that only the // first Student has preregistered for courses based // on the content of his/her id.dat file (see Student.cs // for details). Student s1 = new Student("111-11-1111.dat"); s1.ReadStudentData(schedule); Student s2 = new Student("222-22-2222.dat"); s2.ReadStudentData(schedule); Student s3 = new Student("333-33-3333.dat"); s3.ReadStudentData(schedule);
Now, we'll simulate having Student s2 enroll in a Section, so that we may exercise and test the WriteStudentData method. We use the FindSection convenience method of the ScheduleOfClasses class to obtain a reference to a Section object based on a particular course and section number, and we then use the Enroll method of the Section class to bidirectionally enroll Student s2 in that Section.
// Let's have one Student try enrolling in something, so // that we can simulate their logging off and persisting // the enrollment data in the id.dat file (see Student.cs // for details). Section sec = scheduleOfClasses.FindSection("ART101 - 1"); sec.Enroll(s2);
Now, we invoke the WriteStudentData method on Student object s2.
s2.WriteStudentData();
Before running the SRS application, the contents of the 222-22-2222.dat file consisted of a single record as follows:
222-22-2222 Gerson Lopez Information Technology Ph. D.
because this student wasn't enrolled in any sections. After running the SRS application, if we inspect the contents of the 222-22-2222.dat file, we'll find that a record has been added to persist the fact that this student is now enrolled in ART101 section 1:
222-22-2222 Gerson Lopez Information Technology Ph. D. ART101 - 1
and so the WriteStudentData method is indeed working!
To round out our testing, we include a few calls to the Display methods of our various collection objects:
// Let's see if everything got initialized properly // by calling various display methods! Console.WriteLine("===================="); Console.WriteLine("Course Catalog:"); Console.WriteLine("===================="); Console.WriteLine(""); courseCatalog.Display(); Console.WriteLine("===================="); Console.WriteLine("Schedule of Classes:"); Console.WriteLine("===================="); Console.WriteLine(""); scheduleOfClasses.Display(); Console.WriteLine("======================"); Console.WriteLine("Professor Information:");
Console.WriteLine("======================"); Console.WriteLine(""); faculty.Display(); Console.WriteLine("===================="); Console.WriteLine("Student Information:"); Console.WriteLine("===================="); Console.WriteLine(""); s1.Display(); Console.WriteLine(""); s2.Display(); Console.WriteLine(""); s3.Display(); } }
Because several of the classes that make up the SRS have Main methods, we'll have to use the /main option when we compile the SRS application. The following command compiles the SRS and names the executable SRS.exe:
csc /out:SRS.exe *.cs /main:SRS
The output produced by running this program is as follows:
==================== Course Catalog: ==================== Course Catalog: Course Information: Course No.: CMP101 Course Name: Beginning Computer Technology Credits: 3 Prerequisite Courses: Offered As Section(s): 1 2 Course Information: Course No.: OBJ101 Course Name: Object Methods for Software Development Credits: 3 Prerequisite Courses: CMP101: Beginning Computer Technology Offered As Section(s): 1 2 Course Information: Course No.: CMP283 Course Name: Higher Level Languages (C#) Credits: 3
Prerequisite Courses: OBJ101: Object Methods for Software Development Offered As Section(s): 1 Course Information: Course No.: CMP999 Course Name: Living Brain Computers Credits: 3 Prerequisite Courses: CMP283: Higher Level Languages (C#) Offered As Section(s): 1 Course Information: Course No.: ART101 Course Name: Beginning Basketweaving Credits: 3 Prerequisite Courses: Offered As Section(s): 1 ==================== Schedule of Classes: ==================== Schedule of Classes for SP2009 Section Information: Semester: SP2009 Course No.: CMP101 Section No: 1 Offered: M at 8:10 - 10:00 PM In Room: GOVT101 Professor: Jackie Chan Total of 1 students enrolled, as follows: Zachary Palmer Section Information: Semester: SP2009 Course No.: CMP101 Section No: 2 Offered: W at 6:10 - 8:00 PM In Room: GOVT202 Professor: John Carson Total of 0 students enrolled.
Section Information: Semester: SP2009 Course No.: OBJ101 Section No: 1 Offered: R at 4:10 - 6:00 PM In Room: GOVT105 Professor: Jacquie Barker Total of 0 students enrolled. Section Information: Semester: SP2009 Course No.: OBJ101 Section No: 2 Offered: T at 6:10 - 8:00 PM In Room: SCI330 Professor: Jackie Chan Total of 0 students enrolled. Section Information: Semester: SP2009 Course No.: CMP283 Section No: 1 Offered: M at 6:10 - 8:00 PM In Room: GOVT101 Professor: Jacquie Barker Total of 0 students enrolled. Section Information: Semester: SP2009 Course No.: CMP999 Section No: 1 Offered: R at 4:10 - 6:00 PM In Room: SCI241 Professor: John Carson Total of 0 students enrolled. Section Information: Semester: SP2009 Course No.: ART101 Section No: 1 Offered: M at 4:10 - 6:00 PM In Room: ARTS25 Professor: Jackie Chan Total of 2 students enrolled, as follows: Zachary Palmer Gerson Lopez
====================== Professor Information: ====================== Faculty: Person Information: Name: Jacquie Barker ID number: 123-45-6789 Professor-Specific Information: Title: Adjunct Professor Teaches for Dept.: Information Technology Teaching Assignments for Jacquie Barker: Course No.: OBJ101 Section No.: 1 Course Name: Object Methods for Software Development Day and Time: R - 4:10 - 6:00 PM ----- Course No.: CMP283 Section No.: 1 Course Name: Higher Level Languages (C#) Day and Time: M - 6:10 - 8:00 PM ----- Person Information: Name: John Carson ID number: 567-81-2345 Professor-Specific Information: Title: Full Professor Teaches for Dept.: Information Technology Teaching Assignments for John Carson: Course No.: CMP101 Section No.: 2 Course Name: Beginning Computer Technology Day and Time: W - 6:10 - 8:00 PM ----- Course No.: CMP999 Section No.: 1 Course Name: Living Brain Computers Day and Time: R - 4:10 - 6:00 PM ----- Person Information: Name: Jackie Chan ID number: 987-65-4321 Professor-Specific Information: Title: Full Professor
Teaches for Dept.: Physical Education Teaching Assignments for Jackie Chan: Course No.: CMP101 Section No.: 1 Course Name: Beginning Computer Technology Day and Time: M - 8:10 - 10:00 PM ----- Course No.: OBJ101 Section No.: 2 Course Name: Object Methods for Software Development Day and Time: T - 6:10 - 8:00 PM ----- Course No.: ART101 Section No.: 1 Course Name: Beginning Basketweaving Day and Time: M - 4:10 - 6:00 PM ----- ==================== Student Information: ==================== Person Information: Name: Zachary Palmer ID number: 111-11-1111 Student-Specific Information: Major: Math Degree: M.S. Course Schedule for Zachary Palmer Course No.: CMP101 Section No.: 1 Course Name: Beginning Computer Technology Meeting Day and Time Held: M - 8:10 - 10:00 PM Room Location: GOVT101 Professor's Name: Jackie Chan ----- Course No.: ART101 Section No.: 1 Course Name: Beginning Basketweaving Meeting Day and Time Held: M - 4:10 - 6:00 PM Room Location: ARTS25 Professor's Name: Jackie Chan ----- Transcript for: Zachary Palmer (111-11-1111) [M.S. - Math] (no entries) Person Information:
Name: Gerson Lopez ID number: 222-22-2222 Student-Specific Information: Major: Information Technology Degree: Ph. D. Course Schedule for Gerson Lopez Course No.: ART101 Section No.: 1 Course Name: Beginning Basketweaving Meeting Day and Time Held: M - 4:10 - 6:00 PM Room Location: ARTS25 Professor's Name: Jackie Chan ----- Transcript for: Gerson Lopez (222-22-2222) [Ph. D. - Information Technology] (no entries) Person Information: Name: Mary Smith ID number: 333-33-3333 Student-Specific Information: Major: Physics Degree: B.S. Course Schedule for Mary Smith Transcript for: Mary Smith (333-33-3333) [B.S. - Physics] (no entries)
In summary, Table 15-1 shows how we had to modify our Chapter 14 version of the SRS to achieve file persistence.
Class | Modifications? |
---|---|
CourseCatalog | (New class) |
ScheduleOfClasses | Yes: it now reads data from a file, and we added a FindSection method. |
Faculty | (New class) |
Course | Yes: we changed the signature of the ScheduleSection method to accept an explicit section number as an argument, because we're now reading it from a file. |
Student | Yes: we did a lot! We created a method to read student data from a file, and we created a method that writes the current state of a student's registration situation to a file when he or she logs off. |
SRS | Yes: it was revamped—and streamlined!—to take advantage of all of the new collections that we've created. |
Person | No |
Professor | No |
Section | No |
Transcript | No |
TranscriptEntry | No |
This concludes the work that we're going to do with respect to persistence in the SRS application. We'll finish rounding out the SRS application by adding a GUI in Chapter 16.