Commit 0f8d14f1 by Paktalin

Page 116. Lesson 3 is not completed yet

parent f73a23d5
...@@ -15,11 +15,6 @@ class CourseSession { ...@@ -15,11 +15,6 @@ class CourseSession {
private ArrayList<Student> students = new ArrayList<>(); private ArrayList<Student> students = new ArrayList<>();
private Date startDate; private Date startDate;
CourseSession(String department, String number) {
this.department = department;
this.number = number;
}
CourseSession(String department, String number, Date startDate) { CourseSession(String department, String number, Date startDate) {
this.department = department; this.department = department;
this.number = number; this.number = number;
...@@ -63,4 +58,8 @@ class CourseSession { ...@@ -63,4 +58,8 @@ class CourseSession {
final int daysFromFridayToMonday = 3; final int daysFromFridayToMonday = 3;
return sessionLength * daysInWeek - daysFromFridayToMonday; return sessionLength * daysInWeek - daysFromFridayToMonday;
} }
ArrayList<Student> getAllStudents() {
return students;
}
} }
package com.example.paktalin.agilejava;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Created by Paktalin on 17/05/2018.
*/
class DateUtil {
Date createDate(int year, int month, int date) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, date);
return calendar.getTime();
}
}
package com.example.paktalin.agilejava;
/**
* Created by Paktalin on 17/05/2018.
*/
class RosterReporter {
static final String NEW_LINE = System.getProperty("line.separator");
static String ROSTER_REPORT_HEADER = "Student" + NEW_LINE +
"_____" + NEW_LINE;
static String ROSTER_REPORT_FOOTER = NEW_LINE + "# students = ";
private CourseSession session;
RosterReporter(CourseSession session) {
this.session = session;
}
String getReport() {
StringBuilder buffer = new StringBuilder();
buffer.append(ROSTER_REPORT_HEADER);
for (Student student : session.getAllStudents()) {
buffer.append(student.getName());
buffer.append(NEW_LINE);
}
buffer.append(ROSTER_REPORT_FOOTER + session.getAllStudents().size() + NEW_LINE);
return buffer.toString();
}
}
...@@ -12,6 +12,8 @@ public class AllTests extends TestSuite { ...@@ -12,6 +12,8 @@ public class AllTests extends TestSuite {
TestSuite suite = new TestSuite(); TestSuite suite = new TestSuite();
suite.addTestSuite(StudentTest.class); suite.addTestSuite(StudentTest.class);
suite.addTestSuite(CourseSessionTest.class); suite.addTestSuite(CourseSessionTest.class);
suite.addTestSuite(RosterReporterTest.class);
suite.addTestSuite(DateUtilTest.class);
return suite; return suite;
} }
} }
...@@ -2,9 +2,7 @@ package com.example.paktalin.agilejava; ...@@ -2,9 +2,7 @@ package com.example.paktalin.agilejava;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar;
/** /**
* Created by Paktalin on 16/05/2018. * Created by Paktalin on 16/05/2018.
...@@ -16,7 +14,7 @@ public class CourseSessionTest extends TestCase { ...@@ -16,7 +14,7 @@ public class CourseSessionTest extends TestCase {
private Date startDate; private Date startDate;
public void setUp() { public void setUp() {
startDate = createDate(2003, 1, 6); startDate = new DateUtil().createDate(2003, 1, 6);
session = new CourseSession("ENGL", "101", startDate); session = new CourseSession("ENGL", "101", startDate);
} }
...@@ -40,16 +38,7 @@ public class CourseSessionTest extends TestCase { ...@@ -40,16 +38,7 @@ public class CourseSessionTest extends TestCase {
} }
public void testCourseDates() { public void testCourseDates() {
Date sixteenWeeksOut = createDate(2003, 4, 25); Date sixteenWeeksOut = new DateUtil().createDate(2003, 4, 25);
assertEquals(sixteenWeeksOut, session.getEndDate()); assertEquals(sixteenWeeksOut, session.getEndDate());
} }
private Date createDate(int year, int month, int date) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, date);
return calendar.getTime();
}
} }
package com.example.paktalin.agilejava;
import junit.framework.TestCase;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Created by Paktalin on 17/05/2018.
*/
public class DateUtilTest extends TestCase {
public void testCreateDate() {
Date date = new DateUtil().createDate(2000, 1, 1);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
assertEquals(2000, calendar.get(Calendar.YEAR));
assertEquals(Calendar.JANUARY, calendar.get(Calendar.MONTH));
assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));
}
}
package com.example.paktalin.agilejava;
import junit.framework.TestCase;
/**
* Created by Paktalin on 17/05/2018.
*/
public class LanguageTests extends TestCase {
public void testCharValue() {
char capitalA = 'A';
assertEquals(65, capitalA);
assertEquals('\u0041', capitalA);
assertEquals('\101', capitalA);
}
public void testConcatenation() {
assertEquals("abcd", "ab".concat("cd"));
assertEquals("abcd", "ab" + "cd");
assertEquals("abcd", "ab" + "c" + "d");
}
}
package com.example.paktalin.agilejava;
import junit.framework.TestCase;
/**
* Created by Paktalin on 17/05/2018.
*/
public class RosterReporterTest extends TestCase {
public void testRosterReport() {
CourseSession session = new CourseSession("ENGL", "101",
new DateUtil().createDate(2003, 1, 6));
session.enroll(new Student("A"));
session.enroll(new Student("B"));
String rosterReport = new RosterReporter(session).getReport();
assertEquals(
RosterReporter.ROSTER_REPORT_HEADER +
"A" + RosterReporter.NEW_LINE +
"B" + RosterReporter.NEW_LINE +
RosterReporter.ROSTER_REPORT_FOOTER + "2" +
RosterReporter.NEW_LINE, rosterReport);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment