Having encapsulated so much functionality into the 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.
First, in addition to the public static scheduleOfClasses attribute that we provided in Chapter 14, we now will provide two more such attributes—faculty and courseCatalog:
// SRS.cs // A main driver for the command-line driven version of the SRS, with // file persistence added. using System; using System.Collections; public class SRS { // We can effectively create "global" data by declaring // PUBLIC STATIC attributes in the main class. // Entry points/"roots" for getting at objects. public static Faculty faculty = new Faculty(); public static CourseCatalog courseCatalog = new CourseCatalog(); public static ScheduleOfClasses scheduleOfClasses = new ScheduleOfClasses("SP2004"); // We don't create a collection for Student objects, because // we're only going to handle one Student object at a time -- namely, // an object representing whichever Student is currently logged on.
In the Main method, we take advantage of our InitializeObjects method to read data from the various ASCII files that we've provided, automatically initializing the faculty, courseCatalog, and scheduleOfClasses collections with the appropriate objects.
static void Main() { // Initialize the key objects by reading data from files. // Setting the second argument to true causes the // InitializeObjects method to use the ParseData // method instead of ParseData2. faculty.InitializeObjects("Faculty.dat", true); courseCatalog.InitializeObjects("CourseCatalog.dat", true); scheduleOfClasses.InitializeObjects("SoC_SP2004.dat", true);
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. 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 ssn.dat 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 ssn.dat file (see Student.cs // for details). Student s1 = new Student("111-11-1111"); Student s2 = new Student("222-22-2222"); Student s3 = new Student("333-33-3333");
We invoke the InitializeObjects method a second time for the CourseCatalog and Faculty classes, respectively, to enable us to read their respective supplemental data files:
// Establish some prerequisites (c1 => c2 => c3 => c4). // Setting the second argument to false causes the // InitializeObjects method to use the ParseData2 // method instead of ParseData. courseCatalog.InitializeObjects("Prerequisites.dat", false); // Recruit a professor to teach each of the sections. // Setting the second argument to false causes the // InitializeObjects method to use the ParseData2 // method instead of ParseData. faculty.InitializeObjects("TeachingAssignments.dat", false);
Now, we'll simulate having Student s2 enroll in a Section, so that we may exercise and test the Persist method. We use the FindSection convenience method of the ScheduleOfClasses class to obtain a handle on a Section object based on a particular course and section number, and 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 ssn.dat file (see Student.cs // for details). Section sec = scheduleOfClasses.FindSection("ART101 - 1"); sec.Enroll(s2);
Now, we invoke the Persist method on Student object s2.
s2.Persist();
Before running the SRS application, the contents of the 222-22-2222.dat file consisted of a single record as follows:
222-22-2222<tab>Gerson Lopez<tab>Information Technology<tab>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 find that a record has been added to persist the fact that this student is now enrolled in ART101 section 1:
222-22-2222<tab>Gerson Lopez<tab>Information Technology<tab>Ph. D. ART101 - 1
and so the Persist 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 will compile the SRS and name 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.0 Prerequisite Courses: (none) Offered As Section(s): 1 2 Course Information: Course No.: CMP283 Course Name: Higher Level Languages (C#) Credits: 3.0 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.0 Prerequisite Courses: CMP283: Higher Level Languages (C#) Offered As Section(s): 1 Course Information: Course No.: ART101 Course Name: Beginning Basketweaving Credits: 3.0 Prerequisite Courses: (none) Offered As Section(s): 1 Course Information: Course No.: OBJ101 Course Name: Object Methods for Software Development Credits: 3.0 Prerequisite Courses: CMP101: Beginning Computer Technology Offered As Section(s): 1 2 ==================== Schedule of Classes: ==================== Schedule of Classes for SP2001 Section Information: Semester: SP2001 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: SP2001 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: Joe Blow Section Information: Semester: SP2001 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: SP2001 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: SP2001 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: SP2001 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: SP2001 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: Gerson Lopez Joe Blow ====================== Professor Information: ====================== Faculty: Person Information: Name: John Carson Soc. Security No.: 567-81-2345 Professor-Specific Information: Title: Full Professor Teaches for Dept.: Info. 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: Jacquie Barker Soc. Security No.: 123-45-6789 Professor-Specific Information: Title: Asst. Professor Teaches for Dept.: Info. 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: Jackie Chan Soc. Security No.: 987-65-4321 Professor-Specific Information: Title: Full Professor Teaches for Dept.: Info. Technology 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: Joe Blow Soc. Security No.: 111-11-1111 Student-Specific Information: Major: Math Degree: M.S. Course Schedule for Joe Blow 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: Joe Blow (111-11-1111) [M.S. - Math] (no entries) Person Information: Name: Gerson Lopez Soc. Security No.: 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 Soc. Security No.: 333-33-3333 Student-Specific Information: Major: Physics Degree: B.S. Course Schedule for Mary Smith (none) Transcript for: Mary Smith (333-33-3333) [B.S. - Physics] (no entries)
In summary, Table 15-1 shows how our Chapter 14 version of the SRS had to be modified to achieve file persistence.
Class |
Modifications? |
---|---|
CollectionWrapper |
(New class) |
CourseCatalog |
(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. |
Faculty |
(New class) |
Person |
No. |
Professor |
No. |
Section |
No. |
ScheduleOfClasses |
Yes: it now extends the CollectionWrapper class; we implemented the ParseData and ParseData2 methods; and we added a FindSection method. |
SRS |
Yes: it was revamped—and streamlined!—to take advantage of all of the new collections that we've created. |
Student |
Yes: we did a lot! We revamped the constructor to do dynamic data loading at logon from a student's private data file by using a version of ParseData and ParseData2 written specifically for Student; we created a StudentSuccessfullyInitialized method; we created a Persist method that will record the state of a student's registration situation when he or she logs off. |
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 graphical user interface in Chapter 16.