From 2ff57b727861c0e8f1c7dbb2d8171409ef1b6ef9 Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Thu, 14 Nov 2019 09:23:21 -0500 Subject: [PATCH 1/5] Complete Up to 6.1 --- .idea/$PRODUCT_WORKSPACE_FILE$ | 19 +++++++ .idea/.gitignore | 2 + .idea/.name | 1 + .idea/compiler.xml | 16 ++++++ .idea/encodings.xml | 6 +++ .idea/libraries/Maven__junit_junit_4_12.xml | 13 +++++ .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 +++++ .idea/misc.xml | 13 +++++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 +++ interfaces-1.iml | 16 ++++++ .../io/zipcoder/interfaces/Instructor.java | 18 +++++++ .../java/io/zipcoder/interfaces/Learner.java | 6 +++ .../java/io/zipcoder/interfaces/People.java | 4 ++ .../java/io/zipcoder/interfaces/Student.java | 19 +++++++ .../java/io/zipcoder/interfaces/Teacher.java | 6 +++ .../interfaces/ZipCodeWilmington.java | 4 ++ .../zipcoder/interfaces/InstructorTest.java | 52 +++++++++++++++++++ .../io/zipcoder/interfaces/StudentTest.java | 28 ++++++++++ 19 files changed, 250 insertions(+) create mode 100644 .idea/$PRODUCT_WORKSPACE_FILE$ create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 interfaces-1.iml create mode 100644 src/main/java/io/zipcoder/interfaces/Instructor.java create mode 100644 src/main/java/io/zipcoder/interfaces/Learner.java create mode 100644 src/main/java/io/zipcoder/interfaces/People.java create mode 100644 src/main/java/io/zipcoder/interfaces/Student.java create mode 100644 src/main/java/io/zipcoder/interfaces/Teacher.java create mode 100644 src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java create mode 100644 src/test/java/io/zipcoder/interfaces/InstructorTest.java create mode 100644 src/test/java/io/zipcoder/interfaces/StudentTest.java diff --git a/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ new file mode 100644 index 00000000..79be3548 --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,19 @@ + + + + + + + 11 + + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..5c98b428 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..a0f2c3a1 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +interfaces-1 \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..fdc60f4f --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..b26911bd --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 00000000..d4110417 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 00000000..f58bbc11 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..2545dc75 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..711b3a53 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/interfaces-1.iml b/interfaces-1.iml new file mode 100644 index 00000000..0ddf51c1 --- /dev/null +++ b/interfaces-1.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/interfaces/Instructor.java b/src/main/java/io/zipcoder/interfaces/Instructor.java new file mode 100644 index 00000000..60dbfddc --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -0,0 +1,18 @@ +package io.zipcoder.interfaces; + +public class Instructor extends Person implements Teacher { + public Instructor(long id, String name) { + super(id, name); + } + + + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + } + + public void lecture(Learner[] learner, double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learner.length; + for(Learner each:learner) + each.learn(numberOfHours); + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/interfaces/Learner.java b/src/main/java/io/zipcoder/interfaces/Learner.java new file mode 100644 index 00000000..1f3079a1 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Learner.java @@ -0,0 +1,6 @@ +package io.zipcoder.interfaces; + +public interface Learner { + void learn(double numberOfHours); + double getTotalStudyTime(); +} diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java new file mode 100644 index 00000000..3b164ce8 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,4 @@ +package io.zipcoder.interfaces; + +public class People { +} diff --git a/src/main/java/io/zipcoder/interfaces/Student.java b/src/main/java/io/zipcoder/interfaces/Student.java new file mode 100644 index 00000000..b392adc0 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -0,0 +1,19 @@ +package io.zipcoder.interfaces; + +public class Student extends Person implements Learner { + double totalStudyTime; + + public Student(long id, String name) { + super(id, name); + } + + public void learn(double numberOfHours) { + totalStudyTime += numberOfHours; + } + + public double getTotalStudyTime() { + return totalStudyTime; + } + + +} diff --git a/src/main/java/io/zipcoder/interfaces/Teacher.java b/src/main/java/io/zipcoder/interfaces/Teacher.java new file mode 100644 index 00000000..66bb58a3 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -0,0 +1,6 @@ +package io.zipcoder.interfaces; + +public interface Teacher { + void teach(Learner learner, double numberOfHours); + void lecture(Learner[] learners, double numberOfHours); +} diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java new file mode 100644 index 00000000..f9bad7e6 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,4 @@ +package io.zipcoder.interfaces; + +public class ZipCodeWilmington { +} diff --git a/src/test/java/io/zipcoder/interfaces/InstructorTest.java b/src/test/java/io/zipcoder/interfaces/InstructorTest.java new file mode 100644 index 00000000..01c3ed75 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/InstructorTest.java @@ -0,0 +1,52 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class InstructorTest { + Instructor instructor; + + + @Before + public void setUp() throws Exception { + instructor = new Instructor(100, "Charlotte"); + } + + @Test + public void testImplementation() { + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void testInheritance() { + Assert.assertTrue(instructor instanceof Person); + } + + @Test + public void teachTest() { + Student student = new Student(678978, "Val"); + double expected = 100.0; + instructor.teach(student, expected); + + Assert.assertEquals(expected, student.getTotalStudyTime()); + } + + @Test + public void lectureTest() { + Student[] students = new Student[4]; + + for (int i = 0; i < students.length; i++) { + students[i] = new Student(i, "Charlotte"); + instructor.lecture(students, 12); + Double expected = 3.0; + + double totalStudyTime +=( double numberOfHours/students.length); + + Assert.assertEquals(expected, students[1].getTotalStudyTime()); + } + } +} + diff --git a/src/test/java/io/zipcoder/interfaces/StudentTest.java b/src/test/java/io/zipcoder/interfaces/StudentTest.java new file mode 100644 index 00000000..e78415ff --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/StudentTest.java @@ -0,0 +1,28 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StudentTest { + Student student; + + @Before + public void setUp() throws Exception { + student = new Student(1, "Julia"); + } + + @Test + public void testImplementation() { + Assert.assertTrue(student instanceof Learner); + } + @Test + public void testInheritance() { + Assert.assertTrue(student instanceof Person); + } + @Test + public void testLearn() { + student.learn(14.0); + Assert.assertEquals(14.0, student.getTotalStudyTime(), 0); + } +} \ No newline at end of file From f91823b653eb463c8140dd8beefc5d812a9d4b8c Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Fri, 15 Nov 2019 14:27:51 -0500 Subject: [PATCH 2/5] Part 1 - up to 9.0 completed --- .idea/compiler.xml | 2 +- README.md | 12 ++-- interfaces-1.iml | 2 +- pom.xml | 14 ++++- .../io/zipcoder/interfaces/Instructor.java | 6 +- .../io/zipcoder/interfaces/Instructors.java | 19 ++++++ .../java/io/zipcoder/interfaces/People.java | 60 ++++++++++++++++++- .../java/io/zipcoder/interfaces/Person.java | 18 ++++++ .../java/io/zipcoder/interfaces/Students.java | 25 ++++++++ .../interfaces/ZipCodeWilmington.java | 32 +++++++++- .../zipcoder/interfaces/InstructorTest.java | 13 ++-- .../zipcoder/interfaces/InstructorsTest.java | 25 ++++++++ .../io/zipcoder/interfaces/PeopleTest.java | 51 ++++++++++++++++ .../io/zipcoder/interfaces/PersonTest.java | 14 +++++ .../io/zipcoder/interfaces/StudentsTest.java | 22 +++++++ .../io/zipcoder/interfaces/TestPerson.java | 5 -- .../interfaces/ZipCodeWilmingtonTest.java | 26 ++++++++ 17 files changed, 324 insertions(+), 22 deletions(-) create mode 100644 src/main/java/io/zipcoder/interfaces/Instructors.java create mode 100644 src/main/java/io/zipcoder/interfaces/Students.java create mode 100644 src/test/java/io/zipcoder/interfaces/InstructorsTest.java create mode 100644 src/test/java/io/zipcoder/interfaces/PeopleTest.java create mode 100644 src/test/java/io/zipcoder/interfaces/PersonTest.java create mode 100644 src/test/java/io/zipcoder/interfaces/StudentsTest.java delete mode 100644 src/test/java/io/zipcoder/interfaces/TestPerson.java create mode 100644 src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index fdc60f4f..8f6ee9db 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index 96882643..ac0eacd9 100644 --- a/README.md +++ b/README.md @@ -141,14 +141,18 @@ * Create a `ZipCodeWilmington` singleton. * The class should declare a field that references the instance of `Students` called `students`. * The class should declare a field that references the instance of `Instructors` called `instructors`. - * The class should define a method `hostLecture` which makes use of a `Teacher teacher, double numberOfHours` parameter to host a `lecture` to the composite `personList` field in the `students` reference. - * The class should define a method `hostLecture` which makes use of a `long id, double numberOfHours` parameter to identify a respective `Instructor` to host a `lecture` to the composite `personList` field in the `cohort` reference. - * The class should define a method `getStudyMap` which returns a new instance of a _mapping_ from `Student` objects to `Double` objects, representative of each respective student's `totalStudyTime`. + * The class should define a method `hostLecture` which makes use of a `Teacher teacher, double numberOfHours` parameter to host + a `lecture` to the composite `personList` field in the `students` reference. + * The class should define a method `hostLecture` which makes use of a `long id, double numberOfHours` parameter to identify + a respective `Instructor` to host a `lecture` to the composite `personList` field in the `cohort` reference. + * The class should define a method `getStudyMap` which returns a new instance of a _mapping_ from `Student` objects + to `Double` objects, representative of each respective student's `totalStudyTime`. - ### Part 9.0 - Test `ZipCodeWilmington` * Create a `TestZipCodeWilmington` class. - * Create a `testHostLecture` method which ensures that each of the `Student`'s `totalStudyTime` instance variable is incremented by the specified `numberOfHours` upon invoking the `hostLecture` method. + * Create a `testHostLecture` method which ensures that each of the `Student`'s `totalStudyTime` instance variable is incremented by + the specified `numberOfHours` upon invoking the `hostLecture` method. diff --git a/interfaces-1.iml b/interfaces-1.iml index 0ddf51c1..4e3316bc 100644 --- a/interfaces-1.iml +++ b/interfaces-1.iml @@ -1,6 +1,6 @@ - + diff --git a/pom.xml b/pom.xml index f0effe12..d2d2b237 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,19 @@ io.zipcoder interfaces-1 0.0.1-SNAPSHOT - jar + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + jar interfaces-1 http://maven.apache.org diff --git a/src/main/java/io/zipcoder/interfaces/Instructor.java b/src/main/java/io/zipcoder/interfaces/Instructor.java index 60dbfddc..a735ec33 100644 --- a/src/main/java/io/zipcoder/interfaces/Instructor.java +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -1,6 +1,7 @@ package io.zipcoder.interfaces; public class Instructor extends Person implements Teacher { + public Instructor(long id, String name) { super(id, name); } @@ -12,7 +13,8 @@ public void teach(Learner learner, double numberOfHours) { public void lecture(Learner[] learner, double numberOfHours) { double numberOfHoursPerLearner = numberOfHours / learner.length; - for(Learner each:learner) - each.learn(numberOfHours); + for (Learner each : learner) { + each.learn(numberOfHoursPerLearner); + } } } \ No newline at end of file diff --git a/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java new file mode 100644 index 00000000..a9766fa4 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,19 @@ +package io.zipcoder.interfaces; + +public final class Instructors extends People { + + private static final Instructors INSTANCE = new Instructors(); + + protected Instructors(){ + + this.add(new Instructor(75, "Dolio")); + this.add(new Instructor(74, "Kris")); + this.add(new Instructor(73 , "Christopher")); + this.add(new Instructor(72, "Roberto")); + this.add(new Instructor(71, "Froilan")); + } + + public static Instructors getInstance() { + return INSTANCE; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java index 3b164ce8..6e27b95b 100644 --- a/src/main/java/io/zipcoder/interfaces/People.java +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -1,4 +1,62 @@ package io.zipcoder.interfaces; -public class People { + + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class People implements Iterable{ + private ArrayList personList; + + public People(ArrayList personList){ + this.personList = personList; + } + public People(){ + this.personList = new ArrayList(); + } + + public void add(Person person) { + this.personList.add(person); + } + + public Person findById(long id) { + for(Person person : this.personList){ + if(person.getId() == id) { + return person; + } + } return null; + } + public boolean contains(Person person) { + return this.personList.contains(person); + } + + public void remove(Person person) { + this.personList.remove(person); + } + + public void remove(long id){ + Person personToBeFound = this.findById(id); + if(personToBeFound != null) { + this.remove(personToBeFound); + } + } + + public void removeAll() { + this.personList.clear(); + } + + public int count() { + return this.personList.size(); + } + + public Person[] toArray() { + return this.personList.toArray(new Person[this.count()]); + } + + + public Iterator iterator() { + return this.personList.iterator(); + } } diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..a4193f68 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -1,5 +1,23 @@ package io.zipcoder.interfaces; public class Person { + final long id; + String name; + + public Person(long id, String name) { + this.id = id; + this.name = name; + } + public long getId() { + return id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } } + diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java new file mode 100644 index 00000000..78d5616f --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,25 @@ +package io.zipcoder.interfaces; + +public final class Students extends People { + + private static final Students INSTANCE = new Students(); + + protected Students(){ + + this.add(new Student(44, "Malcolm")); + this.add(new Student(17, "Charlotte")); + this.add(new Student(16 , "Chung")); + this.add(new Student(6, "Val")); + this.add(new Student(9, "Grace")); + this.add(new Student(3, "Manny")); + this.add(new Student(15, "Eddie")); + } + + public static Students getInstance() { + return INSTANCE; + } + + + + +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java index f9bad7e6..20da1271 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -1,4 +1,34 @@ package io.zipcoder.interfaces; +import io.zipcoder.*; +import java.util.Arrays; +import java.util.HashMap; -public class ZipCodeWilmington { +public final class ZipCodeWilmington { + Students students; + Instructors instructors; + + private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington(); + + + protected ZipCodeWilmington() { + this.instructors = Instructors.getInstance(); + this.students = Students.getInstance(); + } + + public void hostLecture(Teacher teacher, double numberOfHours) { + Learner[] newArr = Arrays.copyOf(students.toArray(), students.count(), Learner[].class); + teacher.lecture(newArr, numberOfHours); + } + public void hostLecture(long id, double numberOfHours) { + Teacher teacher = (Teacher) instructors.findById(id); + hostLecture(teacher, numberOfHours); + } + + public HashMap getStudyMap() { + HashMap studyMap = new HashMap<>(); + for (Person student : students.toArray()) { + studyMap.put((Student) student,((Student) student).getTotalStudyTime()); + } + return studyMap; + } } diff --git a/src/test/java/io/zipcoder/interfaces/InstructorTest.java b/src/test/java/io/zipcoder/interfaces/InstructorTest.java index 01c3ed75..f9e4a14c 100644 --- a/src/test/java/io/zipcoder/interfaces/InstructorTest.java +++ b/src/test/java/io/zipcoder/interfaces/InstructorTest.java @@ -8,7 +8,8 @@ public class InstructorTest { Instructor instructor; - + double totalStudyTime; + double numberOfHours; @Before public void setUp() throws Exception { @@ -31,7 +32,7 @@ public void teachTest() { double expected = 100.0; instructor.teach(student, expected); - Assert.assertEquals(expected, student.getTotalStudyTime()); + Assert.assertEquals(expected, student.getTotalStudyTime(), 0.000001); } @Test @@ -40,13 +41,13 @@ public void lectureTest() { for (int i = 0; i < students.length; i++) { students[i] = new Student(i, "Charlotte"); + } instructor.lecture(students, 12); - Double expected = 3.0; - double totalStudyTime +=( double numberOfHours/students.length); + totalStudyTime += numberOfHours/students.length; - Assert.assertEquals(expected, students[1].getTotalStudyTime()); + Assert.assertEquals(3.0, students[1].getTotalStudyTime(), 0.0000001); } } -} + diff --git a/src/test/java/io/zipcoder/interfaces/InstructorsTest.java b/src/test/java/io/zipcoder/interfaces/InstructorsTest.java new file mode 100644 index 00000000..f91ccdfb --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/InstructorsTest.java @@ -0,0 +1,25 @@ +package io.zipcoder.interfaces; + +import org.junit.Test; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class InstructorsTest { + + + @Test + public void getInstance () { + Assert.assertEquals("Dolio", Instructors.getInstance().findById(75).getName()); + Assert.assertEquals("Kris", Instructors.getInstance().findById(74).getName()); + Assert.assertEquals("Christopher", Instructors.getInstance().findById(73).getName()); + Assert.assertEquals("Roberto", Instructors.getInstance().findById(72).getName()); + Assert.assertEquals("Froilan", Instructors.getInstance().findById(71).getName()); + + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/PeopleTest.java b/src/test/java/io/zipcoder/interfaces/PeopleTest.java new file mode 100644 index 00000000..b53997c8 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/PeopleTest.java @@ -0,0 +1,51 @@ +package io.zipcoder.interfaces; + +import org.junit.Test; +import org.junit.Before; +import org.junit.Assert; + + +public class PeopleTest { + + private People people; + private Person person1; + private Person person2; + private Person person3; + + @Before + public void setUp() throws Exception { + people = new People(); + person1 = new Person(17, "Charlotte"); + person2 = new Person(44, "Val"); + person3 = new Person(75, "Malcolm"); + } + + @Test + public void addTest() { + Assert.assertEquals(0, people.count()); + people.add(person1); + Assert.assertEquals(1, people.count()); + } + + @Test + public void findByIdTest() { + people.add(person1); + people.add(person2); + people.add(person3); + + Assert.assertEquals(person2, people.findById(44)); + } + + @Test + public void removeTest() { + people.add(person1); + people.add(person2); + Assert.assertEquals(2, people.count()); + Assert.assertTrue(people.contains(person1)); + people.remove(person1); + Assert.assertEquals(1, people.count()); + Assert.assertFalse(people.contains(person1)); + } + + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/PersonTest.java b/src/test/java/io/zipcoder/interfaces/PersonTest.java new file mode 100644 index 00000000..57dff642 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/PersonTest.java @@ -0,0 +1,14 @@ +package io.zipcoder.interfaces; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PersonTest { + String name; + + @Test + public void setName() { + this.name = name; + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/StudentsTest.java b/src/test/java/io/zipcoder/interfaces/StudentsTest.java new file mode 100644 index 00000000..c00be463 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/StudentsTest.java @@ -0,0 +1,22 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StudentsTest { + + + @Test + public void getInstance () { + Assert.assertEquals("Malcolm", Students.getInstance().findById(44).getName()); + Assert.assertEquals("Charlotte", Students.getInstance().findById(17).getName()); + Assert.assertEquals("Chung", Students.getInstance().findById(16).getName()); + Assert.assertEquals("Val", Students.getInstance().findById(6).getName()); + Assert.assertEquals("Grace", Students.getInstance().findById(9).getName()); + Assert.assertEquals("Manny", Students.getInstance().findById(3).getName()); + Assert.assertEquals("Eddie", Students.getInstance().findById(15).getName()); + } + } diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java deleted file mode 100644 index d64cd2f0..00000000 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ /dev/null @@ -1,5 +0,0 @@ -package io.zipcoder.interfaces; - -public class TestPerson { - -} diff --git a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java new file mode 100644 index 00000000..1d2afcff --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -0,0 +1,26 @@ +package io.zipcoder.interfaces; + +import org.junit.Test; +import org.junit.Assert; + +import static org.junit.Assert.*; +import java.util.HashMap; + +public class ZipCodeWilmingtonTest { + + + + @Test + public void testHostLecture() { + ZipCodeWilmington mySchool = new ZipCodeWilmington(); + + mySchool.hostLecture(1, 20); + + HashMap map = mySchool.getStudyMap(); + + for (Student student : map.keySet()) { + Assert.assertEquals(4, student.getTotalStudyTime(), .01); + } + } +} + From c843f42c9f8b82697eb72344b36d378e6941d6c7 Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Fri, 15 Nov 2019 20:12:01 -0500 Subject: [PATCH 3/5] Corrected ZipCodeWilmingtonTest --- src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java | 5 +++++ .../java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java index 20da1271..0f885cb5 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -19,6 +19,7 @@ public void hostLecture(Teacher teacher, double numberOfHours) { Learner[] newArr = Arrays.copyOf(students.toArray(), students.count(), Learner[].class); teacher.lecture(newArr, numberOfHours); } + public void hostLecture(long id, double numberOfHours) { Teacher teacher = (Teacher) instructors.findById(id); hostLecture(teacher, numberOfHours); @@ -31,4 +32,8 @@ public HashMap getStudyMap() { } return studyMap; } + + public static ZipCodeWilmington getInstance(){ + return INSTANCE; + } } diff --git a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java index 1d2afcff..4b5e2807 100644 --- a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -12,14 +12,14 @@ public class ZipCodeWilmingtonTest { @Test public void testHostLecture() { - ZipCodeWilmington mySchool = new ZipCodeWilmington(); + ZipCodeWilmington mySchool = ZipCodeWilmington.getInstance(); - mySchool.hostLecture(1, 20); + mySchool.hostLecture(74, 70); HashMap map = mySchool.getStudyMap(); for (Student student : map.keySet()) { - Assert.assertEquals(4, student.getTotalStudyTime(), .01); + Assert.assertEquals(10, student.getTotalStudyTime(), .01); } } } From 263355754f0e12df13195fbd366a7ac7d7ad1720 Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Fri, 15 Nov 2019 20:57:43 -0500 Subject: [PATCH 4/5] Complete up to 10.0 or second part --- README.md | 2 +- .../io/zipcoder/interfaces/Instructors.java | 12 ++++- .../java/io/zipcoder/interfaces/People.java | 48 +++++++++---------- .../java/io/zipcoder/interfaces/Students.java | 10 +++- .../interfaces/ZipCodeWilmington.java | 9 ++-- .../io/zipcoder/interfaces/PeopleTest.java | 47 +++++++++--------- 6 files changed, 72 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index ac0eacd9..353594a2 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ * Modify `people` field to enforce that is a container of objects of type `E`. * Modify the `add` method to ensure that it handles object of type `E`. * Modify the `findById` method to ensure that it returns an object of type `E`. -* Modify the `getArray` method signature by declaring it `abstract` of return tyoe `E`. +* Modify the `getArray` method signature by declaring it `abstract` of return type `E`. * An abstract method is a subclass's contractual agreement to the deferment of an implementation of a respective method. - diff --git a/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java index a9766fa4..0500be62 100644 --- a/src/main/java/io/zipcoder/interfaces/Instructors.java +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -1,6 +1,7 @@ package io.zipcoder.interfaces; +import java.util.ArrayList; -public final class Instructors extends People { +public final class Instructors extends People { private static final Instructors INSTANCE = new Instructors(); @@ -16,4 +17,13 @@ protected Instructors(){ public static Instructors getInstance() { return INSTANCE; } + + public Instructor[] toArray() { + Instructor[] result = new Instructor[INSTANCE.count()]; + int i = 0; + for (Instructor instructor: INSTANCE) { + result[i++] = instructor; + } + return result; + } } diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java index 6e27b95b..a3e7b3a4 100644 --- a/src/main/java/io/zipcoder/interfaces/People.java +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -7,56 +7,54 @@ import java.util.Iterator; import java.util.List; -public class People implements Iterable{ - private ArrayList personList; +public abstract class People implements Iterable{ + private ArrayList eList; - public People(ArrayList personList){ - this.personList = personList; + public People(ArrayList eList){ + this.eList = eList; } public People(){ - this.personList = new ArrayList(); + this.eList = new ArrayList(); } - public void add(Person person) { - this.personList.add(person); + public void add(E e) { + this.eList.add(e); } - public Person findById(long id) { - for(Person person : this.personList){ - if(person.getId() == id) { - return person; + public E findById(long id) { + for(E e : this.eList){ + if(e.getId() == id) { + return e; } } return null; } - public boolean contains(Person person) { - return this.personList.contains(person); + public boolean contains(E e) { + return this.eList.contains(e); } - public void remove(Person person) { - this.personList.remove(person); + public void remove(E e) { + this.eList.remove(e); } public void remove(long id){ - Person personToBeFound = this.findById(id); - if(personToBeFound != null) { - this.remove(personToBeFound); + E eToBeFound = this.findById(id); + if(eToBeFound != null) { + this.remove(eToBeFound); } } public void removeAll() { - this.personList.clear(); + this.eList.clear(); } public int count() { - return this.personList.size(); + return this.eList.size(); } - public Person[] toArray() { - return this.personList.toArray(new Person[this.count()]); - } + abstract E[] toArray(); - public Iterator iterator() { - return this.personList.iterator(); + public Iterator iterator() { + return this.eList.iterator(); } } diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java index 78d5616f..30816ae8 100644 --- a/src/main/java/io/zipcoder/interfaces/Students.java +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -1,6 +1,6 @@ package io.zipcoder.interfaces; -public final class Students extends People { +public final class Students extends People { private static final Students INSTANCE = new Students(); @@ -18,6 +18,14 @@ protected Students(){ public static Students getInstance() { return INSTANCE; } + public Student[] toArray() { + Student[] result = new Student[INSTANCE.count()]; + int i = 0; + for (Student student: INSTANCE) { + result[i++] = student; + } + return result; + } diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java index 0f885cb5..0643022b 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -16,19 +16,18 @@ protected ZipCodeWilmington() { } public void hostLecture(Teacher teacher, double numberOfHours) { - Learner[] newArr = Arrays.copyOf(students.toArray(), students.count(), Learner[].class); - teacher.lecture(newArr, numberOfHours); + teacher.lecture(students.toArray(), numberOfHours); } public void hostLecture(long id, double numberOfHours) { - Teacher teacher = (Teacher) instructors.findById(id); + Teacher teacher = instructors.findById(id); hostLecture(teacher, numberOfHours); } public HashMap getStudyMap() { HashMap studyMap = new HashMap<>(); - for (Person student : students.toArray()) { - studyMap.put((Student) student,((Student) student).getTotalStudyTime()); + for (Student student : students.toArray()) { + studyMap.put(student, student.getTotalStudyTime()); } return studyMap; } diff --git a/src/test/java/io/zipcoder/interfaces/PeopleTest.java b/src/test/java/io/zipcoder/interfaces/PeopleTest.java index b53997c8..04cfc946 100644 --- a/src/test/java/io/zipcoder/interfaces/PeopleTest.java +++ b/src/test/java/io/zipcoder/interfaces/PeopleTest.java @@ -7,44 +7,45 @@ public class PeopleTest { - private People people; - private Person person1; - private Person person2; - private Person person3; + private Students students; + private Student student1; + private Student student2; + private Student student3; @Before public void setUp() throws Exception { - people = new People(); - person1 = new Person(17, "Charlotte"); - person2 = new Person(44, "Val"); - person3 = new Person(75, "Malcolm"); + // people = new People(); + this.students = Students.getInstance(); + student1 = new Student(7, "Val"); + student2 = new Student(14, "Charlotte"); + student3 = new Student(19, "Chung"); } - + @Test public void addTest() { - Assert.assertEquals(0, people.count()); - people.add(person1); - Assert.assertEquals(1, people.count()); + Assert.assertEquals(7, students.count()); + students.add(student1); + Assert.assertEquals(8, students.count()); } @Test public void findByIdTest() { - people.add(person1); - people.add(person2); - people.add(person3); + students.add(student1); + students.add(student2); + students.add(student3); - Assert.assertEquals(person2, people.findById(44)); + Assert.assertEquals(student3, students.findById(19)); } @Test public void removeTest() { - people.add(person1); - people.add(person2); - Assert.assertEquals(2, people.count()); - Assert.assertTrue(people.contains(person1)); - people.remove(person1); - Assert.assertEquals(1, people.count()); - Assert.assertFalse(people.contains(person1)); + students.add(student1); + students.add(student2); + Assert.assertEquals(10, students.count()); + Assert.assertTrue(students.contains(student1)); + students.remove(student1); + Assert.assertEquals(9, students.count()); + Assert.assertFalse(students.contains(student1)); } From 5dac8176d764703020033e9f2d587785abdca339 Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Fri, 15 Nov 2019 21:36:23 -0500 Subject: [PATCH 5/5] Complete --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ README.md | 18 ++- .../java/io/zipcoder/interfaces/Educator.java | 39 ++++++ .../io/zipcoder/interfaces/Instructors.java | 13 +- .../interfaces/ZipCodeWilmington.java | 4 +- .../io/zipcoder/interfaces/EducatorTest.java | 32 +++++ .../interfaces/ZipCodeWilmingtonTest.java | 16 ++- 7 files changed, 232 insertions(+), 14 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/io/zipcoder/interfaces/Educator.java create mode 100644 src/test/java/io/zipcoder/interfaces/EducatorTest.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 00000000..e96534fb --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 353594a2..9256c5e1 100644 --- a/README.md +++ b/README.md @@ -196,19 +196,25 @@ - - # Notice the Design Flaw - Non-Intuitive Orientation -* You may have noticed that `findById` makes it difficult to intuitively identify _which_ `Person` object is being returned. To remedy this issue, we can make use of an `enum` which manipulates a composite `instructor` object. +* You may have noticed that `findById` makes it difficult to intuitively identify _which_ +`Person` object is being returned. To remedy this issue, we can make use of an `enum` which +manipulates a composite `instructor` object. - ### Part 11.1 - Create `Educator` enum * Create an enum named `Educator`. * The enum should implement `Teacher`. - * The enum should have an enumeration for each of the instructors represented in the `Instructors` class. - * Upon construction each enumeration of the enum should instantiate a respective `Instructor` and assign it to a final `instructor` field upon construction. The `instructor` should be added to the `Instructors` singleton. - * Calls to the `teach` and `lecture` method should be deferred to the composite `instructor` reference. - * The enum should have a `double timeWorked` field which keeps track of the hours that the `Educator` has taught. + * The enum should have an enumeration for each of the instructors represented in the + `Instructors` class. + * Upon construction each enumeration of the enum should instantiate a respective + `Instructor` and assign it to a final `instructor` field upon construction. The + `instructor` should be added to the `Instructors` singleton. + * Calls to the `teach` and `lecture` method should be deferred to the composite + `instructor` reference. + * The enum should have a `double timeWorked` field which keeps track of the hours + that the `Educator` has taught. - ### Part 11.0 - Test `Educator` diff --git a/src/main/java/io/zipcoder/interfaces/Educator.java b/src/main/java/io/zipcoder/interfaces/Educator.java new file mode 100644 index 00000000..498e7a74 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,39 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher{ + + KRIS(1, "Kris"), CHRIS(9, "Chris"), FROILAN(10, "Froilan"), DOLIO(11, "Dolio"), ROBERTO(6, "Roberto"); + + private long id; + private String name; + private double timeWorked; + + Educator(long id, String name) { + this.id = id; + this.name = name; + //Instructors.getInstance().add(new Instructor(this.id, this.name)); + } + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public double getTimeWorked() { + return timeWorked; + } + + public void teach(Learner learner, double numberOfHours) { + Instructors.getInstance().findById(this.id).teach(learner, numberOfHours); + this.timeWorked += numberOfHours; + } + + public void lecture(Learner[] learners, double numberOfHours) { + Instructors.getInstance().findById(this.id).lecture(learners, numberOfHours); + this.timeWorked += numberOfHours; + } + +} diff --git a/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java index 0500be62..b44c0da5 100644 --- a/src/main/java/io/zipcoder/interfaces/Instructors.java +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -7,11 +7,14 @@ public final class Instructors extends People { protected Instructors(){ - this.add(new Instructor(75, "Dolio")); - this.add(new Instructor(74, "Kris")); - this.add(new Instructor(73 , "Christopher")); - this.add(new Instructor(72, "Roberto")); - this.add(new Instructor(71, "Froilan")); + for (Educator ed : Educator.values()) { + this.add(new Instructor(ed.getId(), ed.getName())); + } +// this.add(new Instructor(1, "Kris")); +// this.add(new Instructor(9, "Chris")); +// this.add(new Instructor(10, "Froilan")); +// this.add(new Instructor(11, "Dolio")); +// this.add(new Instructor(6, "Roberto")); } public static Instructors getInstance() { diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java index 0643022b..efb2a326 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -6,11 +6,11 @@ public final class ZipCodeWilmington { Students students; Instructors instructors; - + private Educator educator; private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington(); - protected ZipCodeWilmington() { + public ZipCodeWilmington() { this.instructors = Instructors.getInstance(); this.students = Students.getInstance(); } diff --git a/src/test/java/io/zipcoder/interfaces/EducatorTest.java b/src/test/java/io/zipcoder/interfaces/EducatorTest.java new file mode 100644 index 00000000..5c2e972f --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/EducatorTest.java @@ -0,0 +1,32 @@ +package io.zipcoder.interfaces; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + + public class EducatorTest { + + @Test + public void testImplementation() { + Assert.assertTrue(Educator.DOLIO instanceof Teacher); + } + + @Test + public void testTeach() { + Student student = new Student(23,"Miff"); + Educator.KRIS.teach(student, 12); + Assert.assertEquals(12, student.getTotalStudyTime(), .01); + Assert.assertEquals(12, Educator.KRIS.getTimeWorked(), .01); + } + + @Test + public void testLecture() { + Student student1 = new Student(23,"Miff"); + Student student2 = new Student(24,"Biff"); + Educator.FROILAN.lecture(new Student[]{student1,student2}, 12); + Assert.assertEquals(6, student1.getTotalStudyTime(), .01); + Assert.assertEquals(6, student2.getTotalStudyTime(), .01); + Assert.assertEquals(12, Educator.FROILAN.getTimeWorked(), .01); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java index 4b5e2807..5f0fcf20 100644 --- a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -14,7 +14,7 @@ public class ZipCodeWilmingtonTest { public void testHostLecture() { ZipCodeWilmington mySchool = ZipCodeWilmington.getInstance(); - mySchool.hostLecture(74, 70); + mySchool.hostLecture(6, 70); HashMap map = mySchool.getStudyMap(); @@ -22,5 +22,19 @@ public void testHostLecture() { Assert.assertEquals(10, student.getTotalStudyTime(), .01); } } + + @Test + public void testHostLecturEducator() { + ZipCodeWilmington mySchool = new ZipCodeWilmington(); + + mySchool.hostLecture(Educator.ROBERTO, 70); + + HashMap map = mySchool.getStudyMap(); + + for (Student student : map.keySet()) { + Assert.assertEquals(10, student.getTotalStudyTime(), .01); + } + Assert.assertEquals(70, Educator.ROBERTO.getTimeWorked(), .01); + } }