Commit 3b144884 by Paktalin

Lesson 4 is done

parent f20ab55b
......@@ -14,13 +14,23 @@ class CourseSession {
private String number;
private ArrayList<Student> students = new ArrayList<>();
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.number = number;
this.startDate = startDate;
}
static CourseSession create(
String department,
String number,
Date startDate) {
incrementCount();
return new CourseSession(department, number, startDate);
}
String getDepartment() {
return department;
}
......@@ -31,6 +41,7 @@ class CourseSession {
void enroll(Student student) {
students.add(student);
student.addCredits(numberOfCredits);
}
Student get(int index) {
......@@ -62,4 +73,20 @@ class CourseSession {
ArrayList<Student> getAllStudents() {
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;
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) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.clear();
......
......@@ -6,12 +6,37 @@ package com.example.paktalin.agilejava;
class Student {
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) {
this.name = name;
credits = 0;
}
String getName() {
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 {
private CourseSession session;
private Date startDate;
private static final int CREDITS = 3;
public void setUp() {
startDate = DateUtil.createDate(2003, 1, 6);
session = new CourseSession("ENGL", "101", startDate);
session = createCourseSession();
}
public void testCreate() {
......@@ -28,11 +29,13 @@ public class CourseSessionTest extends TestCase {
public void testEnrollStudents() {
Student student1 = new Student("Cain DiVoe");
session.enroll(student1);
assertEquals(CREDITS, student1.getCredits());
assertEquals(student1, session.get(0));
assertEquals(1, session.getNumberOfStudents());
Student student2 = new Student("Coralee DeVaughn");
session.enroll(student2);
assertEquals(CREDITS, student1.getCredits());
assertEquals(student2, session.get(1));
assertEquals(2, session.getNumberOfStudents());
}
......@@ -41,4 +44,18 @@ public class CourseSessionTest extends TestCase {
Date sixteenWeeksOut = DateUtil.createDate(2003, 4, 25);
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;
public class DateUtilTest extends TestCase {
public void testCreateDate() {
Date date = new DateUtil().createDate(2000, 1, 1);
Date date = DateUtil.createDate(2000, 1, 1);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
assertEquals(2000, calendar.get(Calendar.YEAR));
......
......@@ -22,4 +22,14 @@ public class LanguageTests extends TestCase {
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;
import junit.framework.TestCase;
import static com.example.paktalin.agilejava.RosterReporter.NEW_LINE;
/**
* Created by Paktalin on 17/05/2018.
......@@ -10,7 +10,7 @@ import junit.framework.TestCase;
public class RosterReporterTest extends TestCase {
public void testRosterReport() {
CourseSession session = new CourseSession("ENGL", "101",
CourseSession session = CourseSession.create("ENGL", "101",
DateUtil.createDate(2003, 1, 6));
session.enroll(new Student("A"));
......@@ -21,9 +21,9 @@ public class RosterReporterTest extends TestCase {
assertEquals(
RosterReporter.ROSTER_REPORT_HEADER +
"A" + RosterReporter.NEW_LINE +
"B" + RosterReporter.NEW_LINE +
"A" + NEW_LINE +
"B" + NEW_LINE +
RosterReporter.ROSTER_REPORT_FOOTER + "2" +
RosterReporter.NEW_LINE, rosterReport);
NEW_LINE, rosterReport);
}
}
......@@ -18,4 +18,31 @@ public class StudentTest extends TestCase {
Student secondStudent = new Student(secondStudentName);
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