Commit 3b144884 by Paktalin

Lesson 4 is done

parent f20ab55b
...@@ -14,13 +14,23 @@ class CourseSession { ...@@ -14,13 +14,23 @@ class CourseSession {
private String number; private String number;
private ArrayList<Student> students = new ArrayList<>(); private ArrayList<Student> students = new ArrayList<>();
private Date startDate; private Date startDate;
private static int count = 0;
private int numberOfCredits;
CourseSession(String department, String number, Date startDate) { private CourseSession(String department, String number, Date startDate) {
this.department = department; this.department = department;
this.number = number; this.number = number;
this.startDate = startDate; this.startDate = startDate;
} }
static CourseSession create(
String department,
String number,
Date startDate) {
incrementCount();
return new CourseSession(department, number, startDate);
}
String getDepartment() { String getDepartment() {
return department; return department;
} }
...@@ -31,6 +41,7 @@ class CourseSession { ...@@ -31,6 +41,7 @@ class CourseSession {
void enroll(Student student) { void enroll(Student student) {
students.add(student); students.add(student);
student.addCredits(numberOfCredits);
} }
Student get(int index) { Student get(int index) {
...@@ -62,4 +73,20 @@ class CourseSession { ...@@ -62,4 +73,20 @@ class CourseSession {
ArrayList<Student> getAllStudents() { ArrayList<Student> getAllStudents() {
return students; return students;
} }
static int getCount() {
return count;
}
static void resetCount() {
CourseSession.count = 0;
}
private static void incrementCount() {
count++;
}
void setNumberOfCredits(int numberOfCredits) {
this.numberOfCredits = numberOfCredits;
}
} }
...@@ -10,6 +10,16 @@ import java.util.GregorianCalendar; ...@@ -10,6 +10,16 @@ import java.util.GregorianCalendar;
class DateUtil { class DateUtil {
//That's how static initialization block works
static {
long now = System.currentTimeMillis();
then = new Date(now + 86400000);
}
private static Date then;
private DateUtil() {}
static Date createDate(int year, int month, int date) { static Date createDate(int year, int month, int date) {
GregorianCalendar calendar = new GregorianCalendar(); GregorianCalendar calendar = new GregorianCalendar();
calendar.clear(); calendar.clear();
......
...@@ -6,12 +6,37 @@ package com.example.paktalin.agilejava; ...@@ -6,12 +6,37 @@ package com.example.paktalin.agilejava;
class Student { class Student {
private String name; private String name;
private int credits;
static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12;
static final String IN_STATE = "CO";
private String state = "";
Student(String name) { Student(String name) {
this.name = name; this.name = name;
credits = 0;
} }
String getName() { String getName() {
return name; return name;
} }
boolean isFullTime() {
return credits >= Student.CREDITS_REQUIRED_FOR_FULL_TIME;
}
int getCredits() {
return credits;
}
void addCredits(int credits) {
this.credits += credits;
}
boolean isInState() {
return state.equals(Student.IN_STATE);
}
void setState(String state) {
this.state = state.toUpperCase();
}
} }
...@@ -12,10 +12,11 @@ public class CourseSessionTest extends TestCase { ...@@ -12,10 +12,11 @@ public class CourseSessionTest extends TestCase {
private CourseSession session; private CourseSession session;
private Date startDate; private Date startDate;
private static final int CREDITS = 3;
public void setUp() { public void setUp() {
startDate = DateUtil.createDate(2003, 1, 6); startDate = DateUtil.createDate(2003, 1, 6);
session = new CourseSession("ENGL", "101", startDate); session = createCourseSession();
} }
public void testCreate() { public void testCreate() {
...@@ -28,11 +29,13 @@ public class CourseSessionTest extends TestCase { ...@@ -28,11 +29,13 @@ public class CourseSessionTest extends TestCase {
public void testEnrollStudents() { public void testEnrollStudents() {
Student student1 = new Student("Cain DiVoe"); Student student1 = new Student("Cain DiVoe");
session.enroll(student1); session.enroll(student1);
assertEquals(CREDITS, student1.getCredits());
assertEquals(student1, session.get(0)); assertEquals(student1, session.get(0));
assertEquals(1, session.getNumberOfStudents()); assertEquals(1, session.getNumberOfStudents());
Student student2 = new Student("Coralee DeVaughn"); Student student2 = new Student("Coralee DeVaughn");
session.enroll(student2); session.enroll(student2);
assertEquals(CREDITS, student1.getCredits());
assertEquals(student2, session.get(1)); assertEquals(student2, session.get(1));
assertEquals(2, session.getNumberOfStudents()); assertEquals(2, session.getNumberOfStudents());
} }
...@@ -41,4 +44,18 @@ public class CourseSessionTest extends TestCase { ...@@ -41,4 +44,18 @@ public class CourseSessionTest extends TestCase {
Date sixteenWeeksOut = DateUtil.createDate(2003, 4, 25); Date sixteenWeeksOut = DateUtil.createDate(2003, 4, 25);
assertEquals(sixteenWeeksOut, session.getEndDate()); assertEquals(sixteenWeeksOut, session.getEndDate());
} }
public void testCount() {
CourseSession.resetCount();
createCourseSession();
assertEquals(1, CourseSession.getCount());
createCourseSession();
assertEquals(2, CourseSession.getCount());
}
private CourseSession createCourseSession() {
CourseSession session = CourseSession.create("ENGL", "101", startDate);
session.setNumberOfCredits(CREDITS);
return session;
}
} }
...@@ -13,7 +13,7 @@ import java.util.GregorianCalendar; ...@@ -13,7 +13,7 @@ import java.util.GregorianCalendar;
public class DateUtilTest extends TestCase { public class DateUtilTest extends TestCase {
public void testCreateDate() { public void testCreateDate() {
Date date = new DateUtil().createDate(2000, 1, 1); Date date = DateUtil.createDate(2000, 1, 1);
Calendar calendar = new GregorianCalendar(); Calendar calendar = new GregorianCalendar();
calendar.setTime(date); calendar.setTime(date);
assertEquals(2000, calendar.get(Calendar.YEAR)); assertEquals(2000, calendar.get(Calendar.YEAR));
......
...@@ -22,4 +22,14 @@ public class LanguageTests extends TestCase { ...@@ -22,4 +22,14 @@ public class LanguageTests extends TestCase {
assertEquals("abcd", "ab" + "c" + "d"); assertEquals("abcd", "ab" + "c" + "d");
} }
public void testIncrement() {
int i = 5;
assertEquals(12, ++i * 2);
assertEquals(6, i);
i = 5;
assertEquals(10, i++ * 2);
assertEquals(6, i);
}
} }
package com.example.paktalin.agilejava; package com.example.paktalin.agilejava;
import junit.framework.TestCase; import junit.framework.TestCase;
import static com.example.paktalin.agilejava.RosterReporter.NEW_LINE;
/** /**
* Created by Paktalin on 17/05/2018. * Created by Paktalin on 17/05/2018.
...@@ -10,7 +10,7 @@ import junit.framework.TestCase; ...@@ -10,7 +10,7 @@ import junit.framework.TestCase;
public class RosterReporterTest extends TestCase { public class RosterReporterTest extends TestCase {
public void testRosterReport() { public void testRosterReport() {
CourseSession session = new CourseSession("ENGL", "101", CourseSession session = CourseSession.create("ENGL", "101",
DateUtil.createDate(2003, 1, 6)); DateUtil.createDate(2003, 1, 6));
session.enroll(new Student("A")); session.enroll(new Student("A"));
...@@ -21,9 +21,9 @@ public class RosterReporterTest extends TestCase { ...@@ -21,9 +21,9 @@ public class RosterReporterTest extends TestCase {
assertEquals( assertEquals(
RosterReporter.ROSTER_REPORT_HEADER + RosterReporter.ROSTER_REPORT_HEADER +
"A" + RosterReporter.NEW_LINE + "A" + NEW_LINE +
"B" + RosterReporter.NEW_LINE + "B" + NEW_LINE +
RosterReporter.ROSTER_REPORT_FOOTER + "2" + RosterReporter.ROSTER_REPORT_FOOTER + "2" +
RosterReporter.NEW_LINE, rosterReport); NEW_LINE, rosterReport);
} }
} }
...@@ -18,4 +18,31 @@ public class StudentTest extends TestCase { ...@@ -18,4 +18,31 @@ public class StudentTest extends TestCase {
Student secondStudent = new Student(secondStudentName); Student secondStudent = new Student(secondStudentName);
assertEquals(secondStudentName, secondStudent.getName()); assertEquals(secondStudentName, secondStudent.getName());
} }
public void testStudentStatus() {
Student student = new Student("a");
assertEquals(0, student.getCredits());
assertFalse(student.isFullTime());
student.addCredits(3);
assertEquals(3, student.getCredits());
assertFalse(student.isFullTime());
student.addCredits(4);
assertEquals(7, student.getCredits());
assertFalse(student.isFullTime());
student.addCredits(5);
assertEquals(Student.CREDITS_REQUIRED_FOR_FULL_TIME, student.getCredits());
assertTrue("Not enough credits for FullTime status", student.isFullTime());
}
public void testInState() {
Student student = new Student("a");
assertFalse(student.isInState());
student.setState(Student.IN_STATE);
assertTrue(student.isInState());
student.setState("MD");
assertFalse(student.isInState());
}
} }
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