Commit 82096a8e by Paktalin

Lesson 7 complete

parent 601e4709
package com.example.paktalin.agilejava;
/**
* Created by Paktalin on 02/06/2018.
*/
class Performance {
private int[] tests;
double average() {
if (tests.length == 0) return 0;
double total = 0;
for (int score : tests)
total += score;
return total / tests.length;
}
void setNumberOfTests(int numberOfTests) {
tests = new int[numberOfTests];
}
void set(int index, int score) {
tests[index] = score;
}
void setScores(int... scores) {
if (tests == null)
tests = new int[scores.length];
System.arraycopy(scores, 0, tests, 0, tests.length);
}
int get(int index) {
return tests[index];
}
}
...@@ -6,13 +6,14 @@ import java.util.ArrayList; ...@@ -6,13 +6,14 @@ import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Created by Paktalin on 30/05/2018. * Created by Paktalin on 30/05/2018.
*/ */
public abstract class Session implements Comparable<Session>{ public abstract class Session implements Comparable<Session>, Iterable<Student>{
private String department; private String department;
private String number; private String number;
private List<Student> students = new ArrayList<>(); private List<Student> students = new ArrayList<>();
...@@ -33,6 +34,12 @@ public abstract class Session implements Comparable<Session>{ ...@@ -33,6 +34,12 @@ public abstract class Session implements Comparable<Session>{
return this.getNumber().compareTo(that.getNumber()); return this.getNumber().compareTo(that.getNumber());
} }
@NonNull
@Override
public Iterator<Student> iterator() {
return students.iterator();
}
void setNumberOfCredits(int numberOfCredits) { void setNumberOfCredits(int numberOfCredits) {
this.numberOfCredits = numberOfCredits; this.numberOfCredits = numberOfCredits;
} }
...@@ -44,13 +51,15 @@ public abstract class Session implements Comparable<Session>{ ...@@ -44,13 +51,15 @@ public abstract class Session implements Comparable<Session>{
double getAverageGpaForPartTimeStudents() { double getAverageGpaForPartTimeStudents() {
double total = 0; double total = 0;
int counter = 0; int count = 0;
for (Student student : getAllStudents()) for (Student student : getAllStudents()) {
if (!student.isFullTime()) { if (student.isFullTime())
counter++; continue;
total += student.getGpa(); count++;
} total += student.getGpa();
return total / counter; }
if (count == 0) return 0.0;
return total / count;
} }
protected abstract int getSessionLength(); protected abstract int getSessionLength();
......
...@@ -7,7 +7,7 @@ import java.util.List; ...@@ -7,7 +7,7 @@ import java.util.List;
* Created by Paktalin on 15/05/2018. * Created by Paktalin on 15/05/2018.
*/ */
class Student { public class Student {
private String name; private String name;
private String firstName = "", middleName = "", lastName; private String firstName = "", middleName = "", lastName;
private int credits; private int credits;
...@@ -16,6 +16,7 @@ class Student { ...@@ -16,6 +16,7 @@ class Student {
private String state = ""; private String state = "";
private List<Grade> grades = new ArrayList<>(); private List<Grade> grades = new ArrayList<>();
private BasicGradingStrategy gradingStrategy = new BasicGradingStrategy(); private BasicGradingStrategy gradingStrategy = new BasicGradingStrategy();
private List<Integer> charges = new ArrayList<>();
enum Grade { enum Grade {
A(4), B(3), C(2), D(1), F(0); A(4), B(3), C(2), D(1), F(0);
...@@ -28,7 +29,7 @@ class Student { ...@@ -28,7 +29,7 @@ class Student {
} }
} }
Student(String fullName) { public Student(String fullName) {
setNames(fullName); setNames(fullName);
credits = 0; credits = 0;
} }
...@@ -41,6 +42,17 @@ class Student { ...@@ -41,6 +42,17 @@ class Student {
grades.add(grade); grades.add(grade);
} }
void addCharge(int charge) {
charges.add(charge);
}
int totalCharges() {
int total = 0;
for (Integer charge : charges)
total += charge;
return total;
}
private void setNames(String fullName) { private void setNames(String fullName) {
this.name = fullName; this.name = fullName;
String[] nameSplit = fullName.split(" "); String[] nameSplit = fullName.split(" ");
...@@ -89,7 +101,7 @@ class Student { ...@@ -89,7 +101,7 @@ class Student {
return credits; return credits;
} }
String getName() { public String getName() {
return name; return name;
} }
......
...@@ -3,7 +3,7 @@ package com.example.paktalin.agilejava; ...@@ -3,7 +3,7 @@ package com.example.paktalin.agilejava;
import com.example.paktalin.agilejava.language_tests.LanguageTests; import com.example.paktalin.agilejava.language_tests.LanguageTests;
import com.example.paktalin.agilejava.language_tests.LoopTest; import com.example.paktalin.agilejava.language_tests.LoopTest;
import com.example.paktalin.agilejava.language_tests.SuperClassTest; import com.example.paktalin.agilejava.language_tests.SuperClassTest;
import com.example.paktalin.agilejava.summer.SummerCourseSession; import com.example.paktalin.agilejava.summer.SummerCourseSessionTest;
import junit.framework.TestSuite; import junit.framework.TestSuite;
...@@ -26,6 +26,7 @@ public class AllTests extends TestSuite { ...@@ -26,6 +26,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(ReportCardTest.class); suite.addTestSuite(ReportCardTest.class);
suite.addTestSuite(BasicGradingStrategyTest.class); suite.addTestSuite(BasicGradingStrategyTest.class);
suite.addTestSuite(HonorsGradingStrategyTest.class); suite.addTestSuite(HonorsGradingStrategyTest.class);
suite.addTestSuite(PerformanceTest.class);
suite.addTestSuite(LanguageTests.class); suite.addTestSuite(LanguageTests.class);
suite.addTestSuite(SuperClassTest.class); suite.addTestSuite(SuperClassTest.class);
......
package com.example.paktalin.agilejava;
import junit.framework.TestCase;
/**
* Created by Paktalin on 02/06/2018.
*/
public class PerformanceTest extends TestCase {
static private final double TOLERANCE = 0.005;
Performance performance;
@Override
protected void setUp() throws Exception {
performance = new Performance();
}
public void testAverage() {
performance.setNumberOfTests(4);
performance.set(0, 98);
performance.set(1, 92);
performance.set(2, 81);
performance.set(3, 72);
assertEquals(92, performance.get(1));
assertEquals(85.75, performance.average(), TOLERANCE);
}
public void testVarargs() {
performance.setScores(80, 90, 100);
assertEquals(80, performance.get(0));
}
}
...@@ -2,7 +2,9 @@ package com.example.paktalin.agilejava; ...@@ -2,7 +2,9 @@ package com.example.paktalin.agilejava;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* Created by Paktalin on 30/05/2018. * Created by Paktalin on 30/05/2018.
...@@ -78,6 +80,14 @@ abstract public class SessionTest extends TestCase { ...@@ -78,6 +80,14 @@ abstract public class SessionTest extends TestCase {
assertEquals(2.5, session.getAverageGpaForPartTimeStudents(), 0.05); assertEquals(2.5, session.getAverageGpaForPartTimeStudents(), 0.05);
} }
public void testIterate() {
enrollStudents(session);
List<Student> results = new ArrayList<>();
for (Student student : session)
results.add(student);
assertEquals(session.getAllStudents(), results);
}
private void createPartTimer(String name, Student.Grade grade) { private void createPartTimer(String name, Student.Grade grade) {
Student partTimer = new Student(name); Student partTimer = new Student(name);
partTimer.addGrade(grade); partTimer.addGrade(grade);
...@@ -91,4 +101,10 @@ abstract public class SessionTest extends TestCase { ...@@ -91,4 +101,10 @@ abstract public class SessionTest extends TestCase {
student.addCredits(Student.CREDITS_REQUIRED_FOR_FULL_TIME); student.addCredits(Student.CREDITS_REQUIRED_FOR_FULL_TIME);
return student; return student;
} }
private void enrollStudents(Session session) {
session.enroll(new Student("1"));
session.enroll(new Student("2"));
session.enroll(new Student("3"));
}
} }
...@@ -80,6 +80,14 @@ public class StudentTest extends TestCase { ...@@ -80,6 +80,14 @@ public class StudentTest extends TestCase {
assertGpa(student, 2.8); assertGpa(student, 2.8);
} }
public void testCharges() {
Student student = new Student("a");
student.addCharge(500);
student.addCharge(200);
student.addCharge(399);
assertEquals(1099, student.totalCharges());
}
private void assertGpa(Student student, double expectedGpa) { private void assertGpa(Student student, double expectedGpa) {
final double GRADE_TOLERANCE = 0.05; final double GRADE_TOLERANCE = 0.05;
assertEquals(expectedGpa, student.getGpa(), GRADE_TOLERANCE); assertEquals(expectedGpa, student.getGpa(), GRADE_TOLERANCE);
......
package com.example.paktalin.agilejava.language_tests; package com.example.paktalin.agilejava.language_tests;
import com.example.paktalin.agilejava.Student;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
/** /**
* Created by Paktalin on 17/05/2018. * Created by Paktalin on 17/05/2018.
...@@ -78,7 +82,39 @@ public class LanguageTests extends TestCase { ...@@ -78,7 +82,39 @@ public class LanguageTests extends TestCase {
break; break;
} }
assertEquals(6, totalPoints); assertEquals(6, totalPoints);
}
//Old way casting
public void testCasting() {
List students = new ArrayList();
students.add(new Student("a"));
students.add(new Student("b"));
List names = new ArrayList();
Iterator it = students.iterator();
while (it.hasNext()) {
Student student = (Student)it.next();
names.add(student.getName());
}
assertEquals("a", names.get(0));
assertEquals("b", names.get(1));
}
//Old way splitting
public void testTokenizer() {
String name = "Jane Doe";
assertEquals("Jane", split(name).get(0));
assertEquals("Doe", split(name).get(1));
}
private List<String> split(String string) {
List<String> results = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(string, " ");
while (tokenizer.hasMoreTokens())
results.add(tokenizer.nextToken());
return results;
} }
} }
...@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava.language_tests; ...@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava.language_tests;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
/** /**
* Created by Paktalin on 01/06/2018. * Created by Paktalin on 01/06/2018.
*/ */
...@@ -77,4 +80,34 @@ public class LoopTest extends TestCase { ...@@ -77,4 +80,34 @@ public class LoopTest extends TestCase {
} }
return source.substring(0, i + 1); return source.substring(0, i + 1);
} }
public void testLabeledBreak() {
List<List<String>> table = new ArrayList<>();
List<String> row1 = new ArrayList<>();
row1.add("5");
row1.add("2");
List<String> row2 = new ArrayList<>();
row2.add("3");
row2.add("4");
table.add(row1);
table.add(row2);
assertTrue(found(table, "3"));
}
private boolean found(List<List<String>> table, String target) {
boolean found = false;
search:
for (List<String> row : table)
for (String value : row) {
if (value.equals(target)) {
found = true;
break search;
}
}
return found;
}
} }
package com.example.paktalin.agilejava; package com.example.paktalin.agilejava.summer;
import com.example.paktalin.agilejava.DateUtil;
import com.example.paktalin.agilejava.SessionTest;
import com.example.paktalin.agilejava.summer.SummerCourseSession; import com.example.paktalin.agilejava.summer.SummerCourseSession;
import java.util.Date; import java.util.Date;
......
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