CourseCatalog


The student information system requires a CourseCatalog class to store a list of all available course sessions. CourseCatalog will be responsible for persisting basic course information (department, course number, start date, and number of credits) to a file so that the application can be restarted without any data loss.


The CourseCatalog provides a load method that reads all Session objects from a DataOutputStream into a collection. It also provides a store method to write the collection to a DataOutputStream.

 package sis.studentinfo; import junit.framework.*; import java.util.*; import java.io.*; public class CourseCatalogTest extends TestCase {    private CourseCatalog catalog;    private Session session1;    private Session session2;    private Course course1;    private Course course2;    protected void setUp() {       catalog = new CourseCatalog();       course1 = new Course("a", "1");       course2 = new Course("a", "1");       session1 =          CourseSession.create(             course1, DateUtil.createDate(1, 15, 2005));       session1.setNumberOfCredits(3);       session2 =          CourseSession.create(             course2, DateUtil.createDate(1, 17, 2005));       session2.setNumberOfCredits(5);       catalog.add(session1);       catalog.add(session2);    }    public void testStoreAndLoad() throws IOException {       final String filename = "CourseCatalogTest.testAdd.txt";       catalog.store(filename);       catalog.clearAll();       assertEquals(0, catalog.getSessions().size());       catalog.load(filename);       List<Session> sessions = catalog.getSessions();       assertEquals(2, sessions.size());       assertSession(session1, sessions.get(0));       assertSession(session2, sessions.get(1));    }    private void assertSession(Session expected, Session retrieved) {       assertNotSame(expected, retrieved);       assertEquals(expected.getNumberOfCredits(),          retrieved.getNumberOfCredits());       assertEquals(expected.getStartDate(), retrieved.getStartDate());       assertEquals(expected.getDepartment(), retrieved.getDepartment());       assertEquals(expected.getNumber(), retrieved.getNumber());    } } 

The test loads a couple courses into the catalog, calls the store method, clears the catalog, and then calls the load method. It asserts that the catalog contains the two courses initially inserted.

The code other than load and store in CourseCatalog is trivial:

 package sis.studentinfo; import java.util.*; import java.io.*; public class CourseCatalog {    private List<Session> sessions =       new ArrayList<Session>();    public void add(Session session) {       sessions.add(session);    }    public List<Session> getSessions() {       return sessions;    }    public void clearAll() {       sessions.clear();    }    // ... } 

The store method creates a DataOutputStream by wrapping a FileOutputStream. It first writes an int that represents the number of course sessions to be stored. The method then loops through all sessions, writing the start date, number of credits, department, and course number for each.

 public void store(String filename) throws IOException {    DataOutputStream output = null;    try {       output =          new DataOutputStream(new FileOutputStream(filename));       output.writeInt(sessions.size());       for (Session session: sessions) {          output.writeLong(session.getStartDate().getTime());          output.writeInt(session.getNumberOfCredits());          output.writeUTF(session.getDepartment());          output.writeUTF(session.getNumber());       }    }    finally {       output.close();    } } 

You'll need to create a getter method in Session to return the number of credits:

 public int getNumberOfCredits() {    return numberOfCredits; } 

The load method creates a DataInputStream by wrapping a FileInputStream. It reads the count of sessions to determine how many sessions are stored in the file. Using counts in this manner is preferred to the alternative, which is to anticipate an end-of-file exception with each read operation against the file.

The load method assumes that the session being read is a CourseSession, not a SummerCourseSession or other Session subclass. If the CourseCatalog needed to support more than one type, you would need to store the type of Session. The type information would allow code in the load method to know what class to instantiate when reading each object.

 public void load(String filename) throws IOException {    DataInputStream input = null;    try {       input = new DataInputStream(new FileInputStream(filename));       int numberOfSessions = input.readInt();       for (int i = 0; i < numberOfSessions; i++) {          Date startDate = new Date(input.readLong());          int credits = input.readInt();          String department = input.readUTF();          String number = input.readUTF();          Course course = new Course(department, number);          Session session =             CourseSession.create(course, startDate);          session.setNumberOfCredits(credits);          sessions.add(session);       }    }    finally {       input.close();    } } 

Both load and store methods ensure that the associated data streams are closed by use of a finally block.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net