1.11 Application: Student RecordsMost computer applications are written to process and manipulate data that is stored external to the program. Data is commonly extracted from files stored on disk, from databases, and even from remote sites through web services. For example, suppose we have a collection of records stored on disk that contain information related to students at Smalltown College. We have been assigned the task to extract this information and produce a similar to the following in which the records are sorted by identification number. LIST OF STUDENTS ID NAME CLASS GPA ===== ========================= ========== ==== 10015 Smith, John Sophomore 3.01 10167 Jones, Wendy Junior 2.85 10175 Smith, Jane Senior 3.92 10188 Wales, Sam Senior 3.25 10200 Roberts, Sally Freshman 4.00 10208 Green, Patrick Freshman 3.95 10226 Nelson, Amy Sophomore 2.95 10334 Roberts, Jane Senior 3.81 10387 Taylor, Susan Sophomore 2.15 10400 Logan, Mark Junior 3.33 10485 Brown, Jessica Sophomore 2.91 ================================================== Number of students: 11 Our contact in the Registrar's office, who assigned the task, has provided some information about the data. We know each record contains five pieces of information for an individual student: (1) the student's id number represented as an integer; (2) their first and last names, which are strings; (3) an integer classification code in the range [1 ... 4] that indicates if the student is a freshman, sophomore, junior, or senior; and (4) their current grade point average represented as a floating-point value. What we have not been told, however, is how the data is stored on disk. It could be stored in a plain text file, in a binary file, or even in a database. In addition, if the data is stored in a text or binary file, we will need to know how the data is formatted in the file, and if it's in a relational database, we will need to know the type and the structure of the database. Designing a SolutionEven though we have not yet been told the type of file or the format used to store the data, we can begin designing and implementing a solution by working with an abstraction of the input source. No matter the source or format of the data, the extraction of data records from external storage requires similar steps: open a connection, extract the individual records, then close the connection. To aide in our effort, we define a Student File Reader ADT to represent the extraction of data from an external file or database. In computer programming, an object used to input data into a program is sometimes referred to as a reader while an object used to output data is referred to as a writer. The Student File Reader ADT
Creating the ReportThe program in the listing below uses the Student File Reader ADT to produce the sample report illustrated earlier. The program extracts the student records from the input source, sorts the records by student identification number, and produces the report. This program illustrates some of the advantages of applying abstraction to problem solving by focusing on the "what" instead of the "how." Program Listing
By using the Student File Reader ADT, we are able to design a solution and construct a program for the problem at hand without knowing exactly how the data is stored in the external source. We import the The Storage ClassWhen the data for an individual student is extracted from the input file, it will need to be saved in a storage object that can be added to a list in order to first sort and then print the records. We could use tuples to store the records, but we avoid the use of tuples when storing structured data since it's better practice to use classes with named fields. Thus, we define the class StudentRecord : def __init__( self ): self.idNum = 0 self.firstName = None self.lastName = None self.classCode = 0 self.gpa = 0.0 ImplementationThe ADT has to be implemented to extract data based on the format in which the data is stored. For this example, we are going to extract the data from a text file in which the records are listed one after the other. The five fields of the record are each stored on a separate line. The first line contains the id number, the second and third contain the first and last names, the fourth line contains the classification code, and the grade point average follows on the fifth line. The following text block illustrates the format for a file containing two records: 10015 John Smith 2 3.01 10334 Jane Roberts 4 3.81 10208 Patrick Green 1 3.95 The listing at the bottom of the page provides the implementation of the ADT for
extracting the records from the text file in the given format. The constructor simply initializes an instance of the class by creating two attributes, one to store the name the text file and the other to store a reference to the file object after it's opened. The The actual extraction of a record from the text file is handled by the The Student File Reader ADT provides a framework that can be used to extract any type of records from a text file. The only change required would be in the Program Listing
|