From a8a888a02a8a01ac2266772397b3ed1fc17f19d1 Mon Sep 17 00:00:00 2001 From: kshields412 Date: Tue, 12 Nov 2019 18:08:15 -0500 Subject: [PATCH 1/6] checkpoint --- .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/workspace.xml | 155 ++++++++++++++++++ interfaces-1.iml | 26 +++ .../io/zipcoder/interfaces/Instructor.java | 17 ++ .../java/io/zipcoder/interfaces/Learner.java | 7 + .../java/io/zipcoder/interfaces/People.java | 53 ++++++ .../java/io/zipcoder/interfaces/Person.java | 18 ++ .../java/io/zipcoder/interfaces/Student.java | 17 ++ .../java/io/zipcoder/interfaces/Teacher.java | 6 + .../zipcoder/interfaces/TestInstructor.java | 40 +++++ .../io/zipcoder/interfaces/TestPeople.java | 42 +++++ .../io/zipcoder/interfaces/TestPerson.java | 26 +++ .../io/zipcoder/interfaces/TestStudent.java | 29 ++++ 14 files changed, 450 insertions(+) create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/workspace.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/test/java/io/zipcoder/interfaces/TestInstructor.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestPeople.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestStudent.java diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..caf51dd6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ 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/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..2fd9cd92 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/interfaces-1.iml b/interfaces-1.iml new file mode 100644 index 00000000..140227ff --- /dev/null +++ b/interfaces-1.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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..2879ea9b --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -0,0 +1,17 @@ +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[] learners, double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learners.length; + for(int i = 0; i < learners.length; i++){ + learners[i].learn(numberOfHoursPerLearner); + } + } +} 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..ee44c762 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Learner.java @@ -0,0 +1,7 @@ +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..de7d8aa9 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,53 @@ +package io.zipcoder.interfaces; + +import java.lang.reflect.Array; +import java.util.Iterator; +import java.util.List; + +public class People implements Iterable{ + private List personList; + + public People() { + } + + public void add(Person person) { + personList.add(person); + } + + public Person findById(long id) { + for (Person p : personList) { + if (p.getId() == id) { + return p; + } + } + return null; + } + + public boolean contains(Person person){ + return personList.contains(person); + } + + public boolean remove(Person person){ + personList.remove(person); + return false; + } + public boolean removeById(long id){ + Person person = findById(id); + personList.remove(person); + return false; + } + public void removeAll(){ + personList.clear(); + } + public int count(){ + return personList.size(); + } + public Person[] toArray(){ + Person[] arr = new Person[personList.size()]; + return personList.toArray(arr); + } + + public Iterator iterator() { + return personList.iterator(); + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..fd2955fa 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 { + private final long id; + protected String name; + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Person(long id, String name) { + this.id = id; + this.name = name; + } } 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..e3241213 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -0,0 +1,17 @@ +package io.zipcoder.interfaces; + +public class Student extends Person implements Learner{ + private 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/test/java/io/zipcoder/interfaces/TestInstructor.java b/src/test/java/io/zipcoder/interfaces/TestInstructor.java new file mode 100644 index 00000000..060c01fa --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,40 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + @Test + public void testImplementation(){ + Instructor i = new Instructor(1, "Dolio"); + + Assert.assertTrue(i instanceof Teacher); + } + @Test + public void testInheritance(){ + Instructor i = new Instructor(1, "Dolio"); + + Assert.assertTrue(i instanceof Person); + } + @Test + public void testTeach(){ + Instructor i = new Instructor(2, "Bobbu"); + Student student = new Student(1, "Timmy"); + i.teach(student, 5); + Assert.assertEquals(5, student.getTotalStudyTime(), .00001); + i.teach(student, 4); + Assert.assertEquals(9, student.getTotalStudyTime(), .0001); + } + @Test + public void testLecture(){ + Instructor i = new Instructor(2, "Babadook"); + Student s1 = new Student(1, "Grace"); + Student s2 = new Student(2, "Barry"); + Student s3 = new Student(3, "Joby"); + Student[] sArr = {s1, s2, s3}; + i.lecture(sArr, 9); + Assert.assertEquals(3, s1.getTotalStudyTime(), .00001); + Assert.assertEquals(3, s2.getTotalStudyTime(), .00001); + Assert.assertEquals(3, s3.getTotalStudyTime(), .00001); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestPeople.java b/src/test/java/io/zipcoder/interfaces/TestPeople.java new file mode 100644 index 00000000..35ed7ea7 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,42 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +public class TestPeople { + + @Test + public void add() { + Person person = new Person(2, "Jose"); + List personList = new ArrayList(); + + personList.add(person); + + Assert.assertTrue("Jose", personList.contains(person)); + } + + @Test + public void findById() { + Person person = new Person(1, "BilliBob"); + List personList = new ArrayList(); + + person.getId(); + + Assert.assertEquals(1, person.getId()); + } + + @Test + public void remove() { + Person person = new Person(1, "Poppy"); + List personList = new ArrayList(); + + personList.remove(person); + + Assert.assertFalse(personList.contains(1)); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java index d64cd2f0..1fde831b 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,31 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor1(){ + //given + String expectedName = "BillyBob"; + long expectedId = 2; + Person person = new Person(expectedId, expectedName); + //when + String actualName = person.getName(); + long actualId = person.getId(); + //then + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } + @Test + public void testSetName(){ + //given + String expectedName = "BillyBob"; + Person person = new Person(1, expectedName); + //when + String actualName = person.getName(); + //then + Assert.assertEquals(expectedName, actualName); + } } diff --git a/src/test/java/io/zipcoder/interfaces/TestStudent.java b/src/test/java/io/zipcoder/interfaces/TestStudent.java new file mode 100644 index 00000000..ecf871a5 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,29 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + @Test + public void testImplementation(){ + Student student = new Student(2, "Billy"); + + Assert.assertTrue(student instanceof Learner); + } + @Test + public void testInheritance(){ + Student student = new Student(2, "Billy"); + + Assert.assertTrue(student instanceof Person); + } + @Test + public void testLearn(){ + Student student = new Student(2, "Joe"); + + student.learn(2.5); + Assert.assertEquals(2.5, student.getTotalStudyTime(), .0001); + + student.learn(2.0); + Assert.assertEquals(4.5, student.getTotalStudyTime(), .0001); + } +} \ No newline at end of file From fc8e54d056f72f172cbfe9218aaa869551a0650a Mon Sep 17 00:00:00 2001 From: kshields412 Date: Tue, 12 Nov 2019 18:47:09 -0500 Subject: [PATCH 2/6] checkpoint --- .idea/workspace.xml | 39 +++++++++---------- .../java/io/zipcoder/interfaces/People.java | 2 + .../java/io/zipcoder/interfaces/Students.java | 16 ++++++++ .../io/zipcoder/interfaces/TestStudents.java | 15 +++++++ 4 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 src/main/java/io/zipcoder/interfaces/Students.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestStudents.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2fd9cd92..deb9f8ed 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -55,8 +55,8 @@ - - + + @@ -65,14 +65,14 @@ - + @@ -81,14 +81,14 @@ - + @@ -97,14 +97,13 @@ - + @@ -113,14 +112,14 @@ - + @@ -129,8 +128,8 @@ + + + - - - diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java index de7d8aa9..df6c06f4 100644 --- a/src/main/java/io/zipcoder/interfaces/People.java +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -1,6 +1,7 @@ package io.zipcoder.interfaces; import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -8,6 +9,7 @@ public class People implements Iterable{ private List personList; public People() { + personList = new ArrayList(); } public void add(Person person) { 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..94ff262c --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,16 @@ +package io.zipcoder.interfaces; + +public class Students extends People{ + private static final Students INSTANCE = new Students(); + + public Students(){ + super(); + super.add(new Student(1, "Morgan")); + super.add(new Student(2, "Julio")); + super.add(new Student(3, "Jose")); + } + + public static Students getInstance(){ + return INSTANCE; + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestStudents.java b/src/test/java/io/zipcoder/interfaces/TestStudents.java new file mode 100644 index 00000000..6ea0662c --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -0,0 +1,15 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents { + + @Test + public void testStudent(){ + Students s = Students.getInstance(); + Person julio = s.findById(2); + + Assert.assertEquals("Julio", julio.getName()); + } +} \ No newline at end of file From 1755a4eff431ac9034d59973b2e7ac733efaa7ce Mon Sep 17 00:00:00 2001 From: kshields412 Date: Tue, 12 Nov 2019 18:54:32 -0500 Subject: [PATCH 3/6] checkpoint --- .idea/workspace.xml | 10 +++++----- .../java/io/zipcoder/interfaces/Instructors.java | 15 +++++++++++++++ .../java/io/zipcoder/interfaces/Students.java | 2 +- .../io/zipcoder/interfaces/ZipCodeWilmington.java | 5 +++++ .../io/zipcoder/interfaces/TestInstructors.java | 15 +++++++++++++++ .../java/io/zipcoder/interfaces/TestStudents.java | 2 +- 6 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/zipcoder/interfaces/Instructors.java create mode 100644 src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestInstructors.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index deb9f8ed..77b63af8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -55,8 +55,8 @@ - - + + @@ -65,8 +65,8 @@ + - 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..2d3c5204 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,15 @@ +package io.zipcoder.interfaces; + +public class Instructors extends People{ + private static final Instructors INSTANCE = new Instructors(); + + public Instructors(){ + super(); + super.add(new Instructor(1, "Froilan")); + super.add(new Instructor(2, "Dolio")); + super.add(new Instructor(3, "Roberto")); + } + public static Instructors getInstance(){ + return INSTANCE; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java index 94ff262c..f33329da 100644 --- a/src/main/java/io/zipcoder/interfaces/Students.java +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -7,7 +7,7 @@ public Students(){ super(); super.add(new Student(1, "Morgan")); super.add(new Student(2, "Julio")); - super.add(new Student(3, "Jose")); + super.add(new Student(3, "Cuervo")); } public static Students getInstance(){ 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..33dce402 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,5 @@ +package io.zipcoder.interfaces; + +public class ZipCodeWilmington { + +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructors.java b/src/test/java/io/zipcoder/interfaces/TestInstructors.java new file mode 100644 index 00000000..4ed0cae5 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructors.java @@ -0,0 +1,15 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + + @Test + public void testGetInstance() { + Instructors i = Instructors.getInstance(); + Person dolio = i.findById(2); + + Assert.assertEquals("Dolio", dolio.getName()); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestStudents.java b/src/test/java/io/zipcoder/interfaces/TestStudents.java index 6ea0662c..1be576d2 100644 --- a/src/test/java/io/zipcoder/interfaces/TestStudents.java +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -6,7 +6,7 @@ public class TestStudents { @Test - public void testStudent(){ + public void testGetInstance(){ Students s = Students.getInstance(); Person julio = s.findById(2); From 47ceb26c91ca11479b0250d967dc2310d56f9224 Mon Sep 17 00:00:00 2001 From: kshields412 Date: Fri, 15 Nov 2019 14:47:56 -0500 Subject: [PATCH 4/6] checkpoint --- .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 | 9 +- .idea/vcs.xml | 6 + .idea/workspace.xml | 121 +++++++++++------- interfaces-1.iml | 10 -- .../zipcoder/interfaces/ConcretePeople.java | 8 ++ .../io/zipcoder/interfaces/Instructors.java | 12 +- .../java/io/zipcoder/interfaces/People.java | 29 ++--- .../java/io/zipcoder/interfaces/Students.java | 7 +- .../interfaces/ZipCodeWilmington.java | 24 ++++ .../io/zipcoder/interfaces/TestPeople.java | 58 ++++++--- .../interfaces/TestZipCodeWilmington.java | 11 ++ 16 files changed, 253 insertions(+), 91 deletions(-) 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/vcs.xml create mode 100644 src/main/java/io/zipcoder/interfaces/ConcretePeople.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java 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 index caf51dd6..d30d09e2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,13 @@ - + + + + \ 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/.idea/workspace.xml b/.idea/workspace.xml index 77b63af8..2f560eb2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,25 @@ - + + + + + + + + + + + + + + + + + + + - - + + @@ -65,14 +73,14 @@ - + @@ -82,13 +90,13 @@ - + @@ -97,13 +105,14 @@ - + @@ -112,14 +121,14 @@ - + @@ -128,8 +137,8 @@ - - - - - + + + + + @@ -149,6 +158,28 @@ + + + 1573841788344 + + + + + + \ No newline at end of file diff --git a/interfaces-1.iml b/interfaces-1.iml index 140227ff..0ddf51c1 100644 --- a/interfaces-1.iml +++ b/interfaces-1.iml @@ -12,15 +12,5 @@ - - - - - - - - - - \ No newline at end of file diff --git a/src/main/java/io/zipcoder/interfaces/ConcretePeople.java b/src/main/java/io/zipcoder/interfaces/ConcretePeople.java new file mode 100644 index 00000000..ceebfa18 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ConcretePeople.java @@ -0,0 +1,8 @@ +package io.zipcoder.interfaces; + +public class ConcretePeople extends People { + public Person[] toArray(){ + Person[] arr = new Person[personList.size()]; + return personList.toArray(arr); + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java index 2d3c5204..f627234c 100644 --- a/src/main/java/io/zipcoder/interfaces/Instructors.java +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -1,15 +1,21 @@ package io.zipcoder.interfaces; -public class Instructors extends People{ +public class Instructors extends People { private static final Instructors INSTANCE = new Instructors(); - public Instructors(){ + public Instructors() { super(); super.add(new Instructor(1, "Froilan")); super.add(new Instructor(2, "Dolio")); super.add(new Instructor(3, "Roberto")); } - public static Instructors getInstance(){ + + public Instructor[] toArray() { + return personList.toArray(new Instructor[0]); + } + + 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 df6c06f4..c8e14f46 100644 --- a/src/main/java/io/zipcoder/interfaces/People.java +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -5,19 +5,19 @@ import java.util.Iterator; import java.util.List; -public class People implements Iterable{ - private List personList; +public abstract class People implements Iterable{ + List personList; public People() { - personList = new ArrayList(); + personList = new ArrayList(); } - public void add(Person person) { + public void add(T person) { personList.add(person); } - public Person findById(long id) { - for (Person p : personList) { + public T findById(long id) { + for (T p : personList) { if (p.getId() == id) { return p; } @@ -25,18 +25,16 @@ public Person findById(long id) { return null; } - public boolean contains(Person person){ + public boolean contains(T person){ return personList.contains(person); } - public boolean remove(Person person){ + public void remove(T person){ personList.remove(person); - return false; } - public boolean removeById(long id){ - Person person = findById(id); + public void removeById(long id){ + T person = findById(id); personList.remove(person); - return false; } public void removeAll(){ personList.clear(); @@ -44,10 +42,9 @@ public void removeAll(){ public int count(){ return personList.size(); } - public Person[] toArray(){ - Person[] arr = new Person[personList.size()]; - return personList.toArray(arr); - } + public abstract T[] toArray(); +// T[] arr = new T[personList.size()]; +// return personList.toArray(arr); public Iterator iterator() { return personList.iterator(); diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java index f33329da..b3e0ceb5 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 class Students extends People{ +public class Students extends People{ private static final Students INSTANCE = new Students(); public Students(){ @@ -10,7 +10,12 @@ public Students(){ super.add(new Student(3, "Cuervo")); } + public Student[] toArray() { + return personList.toArray(new Student[0]); + } + public static Students getInstance(){ return INSTANCE; } + } diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java index 33dce402..7437ddd9 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -1,5 +1,29 @@ package io.zipcoder.interfaces; +import java.util.LinkedHashMap; +import java.util.Map; + public class ZipCodeWilmington { + private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington(); + private static final Students students = Students.getInstance(); + private static final Instructors instructors = Instructors.getInstance(); + + public static void hostLecture(Teacher teacher, double numberOfHours){ + teacher.lecture(students.toArray(), numberOfHours); + } + + public static void hostLecture(long id, double numberOfHours){ + Teacher teacher = instructors.findById(id); + teacher.lecture(students.toArray(), numberOfHours); + + //instructors.findById(id).lecture(students.toArray(), numberOfHours); + } + public static LinkedHashMap getStudyMap(){ + LinkedHashMap map = new LinkedHashMap(); + for(Student student : students) { + map.put(student, student.getTotalStudyTime()); + } + return map; + } } diff --git a/src/test/java/io/zipcoder/interfaces/TestPeople.java b/src/test/java/io/zipcoder/interfaces/TestPeople.java index 35ed7ea7..9d009ba8 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPeople.java +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -1,6 +1,7 @@ package io.zipcoder.interfaces; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; @@ -9,34 +10,61 @@ import static org.junit.Assert.*; public class TestPeople { + People people; + Person p1, p2, p3; + + @Before + public void setUp(){ + people = new ConcretePeople(); + p1 = new Person(1L, "Bob"); + p2 = new Person(2L, "Pat"); + p3 = new Person(3L, "Billy"); + people.add(p1); + people.add(p2); + } @Test public void add() { - Person person = new Person(2, "Jose"); - List personList = new ArrayList(); - - personList.add(person); - - Assert.assertTrue("Jose", personList.contains(person)); + people.add(p3); + Assert.assertTrue(people.contains(p3)); } @Test public void findById() { - Person person = new Person(1, "BilliBob"); - List personList = new ArrayList(); + Person actual = people.findById(2L); + Person expected = p2; + Assert.assertEquals(expected, actual); + } - person.getId(); + @Test + public void remove() { + people.remove(p2); + Assert.assertFalse(people.contains(p2)); + } - Assert.assertEquals(1, person.getId()); + @Test + public void testRemoveById(){ + Assert.assertTrue(people.contains(p2)); + people.removeById(2L); + Assert.assertFalse(people.contains(p2)); } @Test - public void remove() { - Person person = new Person(1, "Poppy"); - List personList = new ArrayList(); + public void testRemoveAll(){ + people.removeAll(); + Assert.assertEquals(people.count(), 0); + } - personList.remove(person); + @Test + public void testCount(){ + people.add(p3); + people.count(); + Assert.assertEquals(people.count(), 3); + } - Assert.assertFalse(personList.contains(1)); + @Test + public void testToArray(){ + Person[] personArr = people.toArray(); + Assert.assertEquals(2, personArr.length); } } \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java new file mode 100644 index 00000000..401a814e --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,11 @@ +package io.zipcoder.interfaces; + +import org.junit.Before; +import org.junit.Test; + +public class TestZipCodeWilmington { + @Test + public void testSingleton(){ + + } +} From f07fcef03ef2140773e8ecb70b15202efdb7e06b Mon Sep 17 00:00:00 2001 From: kshields412 Date: Fri, 15 Nov 2019 17:49:42 -0500 Subject: [PATCH 5/6] checkpoint --- .idea/workspace.xml | 57 +++++++------------ .../interfaces/TestZipCodeWilmington.java | 38 ++++++++++++- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2f560eb2..cfd2f9c0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,22 +2,8 @@ - - - - - - - - - - - - - - - + - - + + @@ -74,13 +60,13 @@ - + @@ -89,14 +75,13 @@ - + @@ -105,14 +90,14 @@ - + @@ -121,14 +106,14 @@ - + @@ -137,8 +122,8 @@ + + + + - - - - @@ -164,7 +149,7 @@ diff --git a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java index 401a814e..b13bad1d 100644 --- a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -1,11 +1,47 @@ package io.zipcoder.interfaces; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class TestZipCodeWilmington { @Test - public void testSingleton(){ + public void testHostLecture1(){ + Instructor teacher = Instructors.getInstance().findById(1); + Students students = Students.getInstance(); + ZipCodeWilmington.hostLecture(teacher, 30); + + Double expected = 10.0; + Double actual = 30D/students.personList.size(); + + Assert.assertEquals(expected, actual, 0.0); + } + + @Test + public void testHostLecture2(){ + Long teacherId = 1L; + Students students = Students.getInstance(); + + ZipCodeWilmington.hostLecture(teacherId, 60); + + Double expected = 20.0; + Double actual = 60D/students.personList.size(); + + Assert.assertEquals(expected, actual, 0.0); + } + + @Test + public void testGetStudyMap(){ + Student expectedS1 = Students.getInstance().findById(1); + Student expectedS2 = Students.getInstance().findById(2); + Double expectedTimeS1 = Students.getInstance().findById(1).getTotalStudyTime(); + Double expectedTimeS2 = Students.getInstance().findById(1).getTotalStudyTime(); + + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsKey(expectedS1)); + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsKey(expectedS2)); + + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsValue(expectedTimeS1)); + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsValue(expectedTimeS2)); } } From ceb69b80058c25d40cac40c35337c0d172d12ae3 Mon Sep 17 00:00:00 2001 From: kshields412 Date: Fri, 15 Nov 2019 18:36:51 -0500 Subject: [PATCH 6/6] completed lab --- .idea/workspace.xml | 55 ++++++++++++------- .../java/io/zipcoder/interfaces/Educator.java | 29 ++++++++++ .../io/zipcoder/interfaces/Instructor.java | 4 +- .../java/io/zipcoder/interfaces/Teacher.java | 4 +- .../io/zipcoder/interfaces/TestEducator.java | 38 +++++++++++++ .../zipcoder/interfaces/TestInstructor.java | 6 +- .../interfaces/TestZipCodeWilmington.java | 23 ++++++-- 7 files changed, 127 insertions(+), 32 deletions(-) create mode 100644 src/main/java/io/zipcoder/interfaces/Educator.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestEducator.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index cfd2f9c0..905bd16d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,12 @@ + + + + + @@ -26,6 +33,8 @@ + + @@ -37,6 +46,14 @@ + + + + + + + + - - + + @@ -59,14 +76,14 @@ - + @@ -75,13 +92,14 @@ - + @@ -91,13 +109,12 @@ - + @@ -107,13 +124,13 @@ - + @@ -123,7 +140,7 @@ + + - - - - + + @@ -149,7 +166,7 @@ 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..e3b22868 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,29 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher { + INSTRUCTOR0(1L, "instructor0"), INSTRUCTOR1(2L, "instructor1"), + INSTRUCTOR2(3L, "instructor2"); + + private Instructor instructor; + private Double timeWorked = 0.0; + + Educator(Long id, String name) { + this.instructor = new Instructor(id, name); + } + + public void teach(Learner learner, Double numberOfHours) { + timeWorked += numberOfHours; + learner.learn(numberOfHours); + } + + public void lecture(Learner[] learners, Double numberOfHours) { + Double numberOfHoursPerLearner = numberOfHours / learners.length; + for (Learner learner : learners) { + teach(learner, numberOfHoursPerLearner); + } + } + + public Double getTimeWorked() { + return timeWorked; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Instructor.java b/src/main/java/io/zipcoder/interfaces/Instructor.java index 2879ea9b..56426b5a 100644 --- a/src/main/java/io/zipcoder/interfaces/Instructor.java +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -6,9 +6,9 @@ public Instructor(long id, String name) { super(id, name); } - public void teach(Learner learner, double numberOfHours) { learner.learn(numberOfHours); } + public void teach(Learner learner, Double numberOfHours) { learner.learn(numberOfHours); } - public void lecture(Learner[] learners, double numberOfHours) { + public void lecture(Learner[] learners, Double numberOfHours) { double numberOfHoursPerLearner = numberOfHours / learners.length; for(int i = 0; i < learners.length; i++){ learners[i].learn(numberOfHoursPerLearner); diff --git a/src/main/java/io/zipcoder/interfaces/Teacher.java b/src/main/java/io/zipcoder/interfaces/Teacher.java index 66bb58a3..fe62cb8c 100644 --- a/src/main/java/io/zipcoder/interfaces/Teacher.java +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -1,6 +1,6 @@ package io.zipcoder.interfaces; public interface Teacher { - void teach(Learner learner, double numberOfHours); - void lecture(Learner[] learners, double numberOfHours); + void teach(Learner learner, Double numberOfHours); + void lecture(Learner[] learners, Double numberOfHours); } diff --git a/src/test/java/io/zipcoder/interfaces/TestEducator.java b/src/test/java/io/zipcoder/interfaces/TestEducator.java new file mode 100644 index 00000000..a0121e47 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestEducator.java @@ -0,0 +1,38 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TestEducator { + @Test + public void testTeach() { + Student student = new Student(1L, "Morgan"); + Educator.INSTRUCTOR0.teach(student, 100.0); + Assert.assertEquals(student.getTotalStudyTime(), Educator.INSTRUCTOR0.getTimeWorked(), 0.00); + } + + @Test + public void testLecture() { + Student student1 = new Student(1, "Morgan"); + Student student2 = new Student(2, "Jose"); + Student[] students = {student1, student2}; + Double numberOfHours = 40.0; + Double expected = numberOfHours/students.length; + + Educator.INSTRUCTOR1.lecture(students, 40.0); + + Assert.assertEquals(expected, student1.getTotalStudyTime()); + Assert.assertEquals(expected, student2.getTotalStudyTime()); + } + + @Test + public void testGetTimeWorked() { + Student student = new Student(2, "Bobby"); + Double expected = 100.0; + Educator.INSTRUCTOR0.teach(student, 100.0); + Assert.assertEquals(expected, Educator.INSTRUCTOR0.getTimeWorked()); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructor.java b/src/test/java/io/zipcoder/interfaces/TestInstructor.java index 060c01fa..50ef1ac1 100644 --- a/src/test/java/io/zipcoder/interfaces/TestInstructor.java +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -20,9 +20,9 @@ public void testInheritance(){ public void testTeach(){ Instructor i = new Instructor(2, "Bobbu"); Student student = new Student(1, "Timmy"); - i.teach(student, 5); + i.teach(student, 5.0); Assert.assertEquals(5, student.getTotalStudyTime(), .00001); - i.teach(student, 4); + i.teach(student, 4.0); Assert.assertEquals(9, student.getTotalStudyTime(), .0001); } @Test @@ -32,7 +32,7 @@ public void testLecture(){ Student s2 = new Student(2, "Barry"); Student s3 = new Student(3, "Joby"); Student[] sArr = {s1, s2, s3}; - i.lecture(sArr, 9); + i.lecture(sArr, 9.0); Assert.assertEquals(3, s1.getTotalStudyTime(), .00001); Assert.assertEquals(3, s2.getTotalStudyTime(), .00001); Assert.assertEquals(3, s3.getTotalStudyTime(), .00001); diff --git a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java index b13bad1d..ce0a69c5 100644 --- a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -1,38 +1,49 @@ package io.zipcoder.interfaces; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; public class TestZipCodeWilmington { @Test - public void testHostLecture1(){ + public void testHostLecture1() { Instructor teacher = Instructors.getInstance().findById(1); Students students = Students.getInstance(); ZipCodeWilmington.hostLecture(teacher, 30); Double expected = 10.0; - Double actual = 30D/students.personList.size(); + Double actual = 30D / students.personList.size(); Assert.assertEquals(expected, actual, 0.0); } @Test - public void testHostLecture2(){ + public void testHostLecture2() { Long teacherId = 1L; Students students = Students.getInstance(); ZipCodeWilmington.hostLecture(teacherId, 60); Double expected = 20.0; - Double actual = 60D/students.personList.size(); + Double actual = 60D / students.personList.size(); Assert.assertEquals(expected, actual, 0.0); } @Test - public void testGetStudyMap(){ + public void testHostLecture3() { + Students students = Students.getInstance(); + + ZipCodeWilmington.hostLecture(Educator.INSTRUCTOR2, 30); + + Double expected = 10.0; + Double actual = 30D / students.personList.size(); + + Assert.assertEquals(expected, actual, 0.0); + } + + @Test + public void testGetStudyMap() { Student expectedS1 = Students.getInstance().findById(1); Student expectedS2 = Students.getInstance().findById(2); Double expectedTimeS1 = Students.getInstance().findById(1).getTotalStudyTime();