Sorting: Preparation


The school needs a report of all course sessions. You must sort the report first by department, then by course number. This implies that all courses for a given department will be listed together. The groups of departments will be ordered in ascending alphabetical order. Within a department, the sessions will be ordered by course number.


To get started, get a simple report working of all course sessions. Don't worry yet about its order.

 package sis.report; import junit.framework.*; import java.util.*; import sis.studentinfo.*; import static sis.report.ReportConstant.NEWLINE; public class CourseReportTest extends TestCase {    public void testReport() {       final Date date = new Date();       CourseReport report = new CourseReport();       report.add(CourseSession.create("ENGL", "101", date));       report.add(CourseSession.create("CZEC", "200", date));       report.add(CourseSession.create("ITAL", "410", date));       assertEquals(             "ENGL 101" + NEWLINE +             "CZEC 200" + NEWLINE +             "ITAL 410" + NEWLINE,          report.text());    } } 

The report is bare boned, showing simply a course department and number on each line. Currently the report lists sessions in the order in which you added them to the CourseReport object.

The production class, CourseReport, looks similar to RosterReporter:

 package sis.report; import java.util.*; import sis.studentinfo.*; import static sis.report.ReportConstant.NEWLINE; public class CourseReport {    private ArrayList<CourseSession> sessions =       new ArrayList<CourseSession>();    public void add(CourseSession session) {       sessions.add(session);    }    public String text() {       StringBuilder builder = new StringBuilder();       for (CourseSession session: sessions)          builder.append(             session.getDepartment() + " " +             session.getNumber() + NEWLINE);       return builder.toString();    } } 

In order to get CourseReport to compile, you'll have to designate the CourseSession methods getdepartment and getNumber as public.



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