Courses


A Session represents the teaching of a course for a given start date. Most courses are taught once per term. Each course may thus be represented by many Session instances.[1]

[1] This is a small school, so we're ignoring the possibility that there may need to be two separate sections of a session in order to accommodate a large number of students.


Currently the Session class contains the department and number information for the associated course, as well as its number of credits. If there can be multiple sessions of a course, this implementation is inefficient and troublesome, since the course information will be repeated throughout each Session object.

What you want instead is a way of tying many session objects back to a single Course object. The UML diagram in Figure 9.1 shows this many-to-one relationship from Session to a new class named Course.

Figure 9.1. A Course Has Many Sessions


You will refactor your code to the above relationship. Start by creating a simple Course class that captures the department, number, and number of credits. A simple test declares that department and number are required to create a Course. The department and number combined represent the unique key for a Course. Two separate courses cannot have the same department and number.

 package sis.studentinfo; import junit.framework.*; public class CourseTest extends TestCase {    public void testCreate() {       Course course = new Course("CMSC", "120");       assertEquals("CMSC", course.getDepartment());       assertEquals("120", course.getNumber());    } } 

Course starts out as a simple data object. It contains a constructor and two getters for the key fields.

 package sis.studentinfo; public class Course {    private String department;    private String number;    public Course(String department, String number) {       this.department = department;       this.number = number;    }    public String getDepartment() {       return department;    }    public String getNumber() {       return number;    } } 



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