Commit 983b8d32 by Paktalin

Lesson 5 is done

parent 3b144884
package com.example.paktalin.agilejava;
/**
* Created by Paktalin on 18/05/2018.
*/
class BasicGradingStrategy implements GradingStrategy {
@Override
public int getGradePointsFor(Student.Grade grade) {
if(grade == Student.Grade.A) return 4;
if(grade == Student.Grade.B) return 3;
if(grade == Student.Grade.C) return 2;
if(grade == Student.Grade.D) return 1;
return 0;
}
}
package com.example.paktalin.agilejava;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by Paktalin on 18/05/2018.
*/
class CourseReport {
private List<CourseSession> sessions = new ArrayList<>();
void add(CourseSession session) {
sessions.add(session);
Collections.sort(sessions);
}
public String text() {
StringBuilder buffer = new StringBuilder();
for (CourseSession session : sessions) {
buffer.append(session.getDepartment());
buffer.append(" ");
buffer.append(session.getNumber());
buffer.append(RosterReporter.NEW_LINE);
}
return buffer.toString();
}
}
package com.example.paktalin.agilejava;
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
/**
* Created by Paktalin on 16/05/2018.
*/
class CourseSession {
class CourseSession implements Comparable<CourseSession>{
private String department;
private String number;
private ArrayList<Student> students = new ArrayList<>();
private List<Student> students = new ArrayList<>();
private Date startDate;
private static int count = 0;
private int numberOfCredits;
......@@ -70,7 +73,7 @@ class CourseSession {
return sessionLength * daysInWeek - daysFromFridayToMonday;
}
ArrayList<Student> getAllStudents() {
List<Student> getAllStudents() {
return students;
}
......@@ -89,4 +92,12 @@ class CourseSession {
void setNumberOfCredits(int numberOfCredits) {
this.numberOfCredits = numberOfCredits;
}
@Override
public int compareTo(@NonNull CourseSession that) {
int compare = this.getDepartment().compareTo(that.getDepartment());
if (compare != 0)
return compare;
return this.getNumber().compareTo(that.getNumber());
}
}
package com.example.paktalin.agilejava;
/**
* Created by Paktalin on 18/05/2018.
*/
interface GradingStrategy {
int getGradePointsFor(Student.Grade grade);
}
package com.example.paktalin.agilejava;
/**
* Created by Paktalin on 18/05/2018.
*/
class HonorsGradingStrategy implements GradingStrategy {
@Override
public int getGradePointsFor(Student.Grade grade) {
int points = new BasicGradingStrategy().getGradePointsFor(grade);
if (points != 0)
points++;
return points;
}
}
package com.example.paktalin.agilejava;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Paktalin on 15/05/2018.
*/
......@@ -10,6 +13,10 @@ class Student {
static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12;
static final String IN_STATE = "CO";
private String state = "";
private List<Grade> grades = new ArrayList<>();
private GradingStrategy gradingStrategy = new BasicGradingStrategy();
enum Grade {A, B, C, D, F};
Student(String name) {
this.name = name;
......@@ -39,4 +46,21 @@ class Student {
void setState(String state) {
this.state = state.toUpperCase();
}
void addGrade(Grade grade) {
grades.add(grade);
}
double getGpa() {
if (grades.isEmpty())
return 0.0;
double total = 0.0;
for (Grade grade : grades)
total += gradingStrategy.getGradePointsFor(grade);
return total/grades.size();
}
void setGradingStrategy(HonorsGradingStrategy gradingStrategy) {
this.gradingStrategy = gradingStrategy;
}
}
......@@ -14,6 +14,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(CourseSessionTest.class);
suite.addTestSuite(RosterReporterTest.class);
suite.addTestSuite(DateUtilTest.class);
suite.addTestSuite(CourseReportTest.class);
return suite;
}
}
package com.example.paktalin.agilejava;
import junit.framework.TestCase;
import java.util.Date;
import static com.example.paktalin.agilejava.RosterReporter.NEW_LINE;
/**
* Created by Paktalin on 18/05/2018.
*/
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));
report.add(CourseSession.create("CZEC", "220", date));
report.add(CourseSession.create("ITAL", "330", date));
assertEquals(
"CZEC 200" + NEW_LINE +
"CZEC 220" + NEW_LINE +
"ENGL 101" + NEW_LINE +
"ITAL 330" + NEW_LINE +
"ITAL 410" + NEW_LINE,
report.text());
}
}
......@@ -58,4 +58,19 @@ public class CourseSessionTest extends TestCase {
session.setNumberOfCredits(CREDITS);
return session;
}
public void testComparable() {
Date date = new Date();
CourseSession sessionA = CourseSession.create("CMSC", "101", date);
CourseSession sessionB = CourseSession.create("ENGL", "101", date);
assertTrue(sessionA.compareTo(sessionB) < 0);
assertTrue(sessionB.compareTo(sessionA) > 0);
CourseSession sessionC = CourseSession.create("CMSC", "101", date);
assertEquals(0, sessionA.compareTo(sessionC));
CourseSession sessionD = CourseSession.create("CMSC", "210", date);
assertTrue(sessionC.compareTo(sessionD) < 0);
assertTrue(sessionD.compareTo(sessionC) > 0);
}
}
package com.example.paktalin.agilejava;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
\ No newline at end of file
......@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Paktalin on 17/05/2018.
*/
......@@ -32,4 +35,24 @@ public class LanguageTests extends TestCase {
assertEquals(6, i);
}
public void testCollectionsSort() {
List<String> list = new ArrayList<>();
list.add("Heller");
list.add("Kafka");
list.add("Camus");
list.add("Boyle");
java.util.Collections.sort(list);
assertEquals("Boyle", list.get(0));
assertEquals("Camus", list.get(1));
assertEquals("Heller", list.get(2));
assertEquals("Kafka", list.get(3));
}
public void testCompareTo() {
assertTrue("A".compareTo("B") < 0);
assertEquals(0, "A".compareTo("A"));
assertTrue("B".compareTo("A") > 0);
}
}
......@@ -45,4 +45,47 @@ public class StudentTest extends TestCase {
student.setState("MD");
assertFalse(student.isInState());
}
public void testCalculateGpa() {
Student student = new Student("a");
assertGpa(student, 0.0);
student.addGrade(Student.Grade.A);
assertGpa(student, 4.0);
student.addGrade(Student.Grade.B);
assertGpa(student, 3.5);
student.addGrade(Student.Grade.C);
assertGpa(student, 3.0);
student.addGrade(Student.Grade.D);
assertGpa(student, 2.5);
student.addGrade(Student.Grade.F);
assertGpa(student, 2.0);
}
public void testCalculateHonorsStudentGpa() {
Student student = createHonorsStudent();
assertGpa(student, 0.0);
student.addGrade(Student.Grade.A);
assertGpa(student, 5.0);
student.addGrade(Student.Grade.B);
assertGpa(student, 4.5);
student.addGrade(Student.Grade.C);
assertGpa(student, 4.0);
student.addGrade(Student.Grade.D);
assertGpa(student, 3.5);
student.addGrade(Student.Grade.F);
assertGpa(student, 2.8);
}
private void assertGpa(Student student, double expectedGpa) {
final double GRADE_TOLERANCE = 0.05;
assertEquals(expectedGpa, student.getGpa(), GRADE_TOLERANCE);
}
private Student createHonorsStudent() {
Student student = new Student("a");
student.setGradingStrategy(new HonorsGradingStrategy());
return student;
}
}
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