From 0baab9cf0a143d73bea17dfc07a2348c52411c93 Mon Sep 17 00:00:00 2001 From: Dee Date: Sat, 24 Jul 2021 00:40:46 -0400 Subject: [PATCH 1/4] parts 1 thru 9 complete --- .idea/.gitignore | 3 + .idea/compiler.xml | 16 +++++ .idea/encodings.xml | 6 ++ .idea/jarRepositories.xml | 20 ++++++ .idea/libraries/Maven__junit_junit_4_12.xml | 13 ++++ .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 ++++ .idea/misc.xml | 11 +++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ interfaces-1.iml | 16 +++++ pom.xml | 12 ++++ .../io/zipcoder/interfaces/Instructor.java | 25 +++++++ .../io/zipcoder/interfaces/Instructors.java | 15 ++++ .../java/io/zipcoder/interfaces/Learner.java | 7 ++ .../java/io/zipcoder/interfaces/People.java | 54 ++++++++++++++ .../java/io/zipcoder/interfaces/Person.java | 18 +++++ .../java/io/zipcoder/interfaces/Student.java | 20 ++++++ .../java/io/zipcoder/interfaces/Students.java | 32 +++++++++ .../java/io/zipcoder/interfaces/Teacher.java | 8 +++ .../zipcoder/interfaces/TestInstructor.java | 72 +++++++++++++++++++ .../interfaces/TestInstructorsSingleton.java | 21 ++++++ .../io/zipcoder/interfaces/TestPeople.java | 61 ++++++++++++++++ .../io/zipcoder/interfaces/TestPerson.java | 38 ++++++++++ .../io/zipcoder/interfaces/TestStudent.java | 50 +++++++++++++ .../interfaces/TestStudentsSingleton.java | 21 ++++++ 25 files changed, 566 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jarRepositories.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/Instructors.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/Students.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/TestInstructorsSingleton.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestPeople.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestStudent.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestStudentsSingleton.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..0f447bc0 --- /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..63e90019 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 00000000..712ab9d9 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ 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..7c74ef97 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,11 @@ + + + + + + + \ 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..94a25f7f --- /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..450282ac --- /dev/null +++ b/interfaces-1.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index f0effe12..b2196a16 100644 --- a/pom.xml +++ b/pom.xml @@ -5,6 +5,18 @@ io.zipcoder interfaces-1 0.0.1-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 14 + 14 + + + + jar interfaces-1 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..b55b7da3 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -0,0 +1,25 @@ +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 hoursPerLearner = numberOfHours/ learners.length; + + for (Learner learner : learners) { + learner.learn(hoursPerLearner); + } + + } +} 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..39956cbd --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,15 @@ +package io.zipcoder.interfaces; + +public final class Instructors extends People{ + private static final Instructors INSTANCE = new Instructors(); + + private Instructors (){ + this.add(new Instructor(444L, "Dolio")); + this.add(new Instructor(445L, "Leon")); + this.add(new Instructor(446L, "Kris")); + } + + public static Instructors getInstance(){ + return INSTANCE; + } +} 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..8fbf0de9 --- /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..a6b5f456 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,54 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class People implements Iterator { + + List personList = new ArrayList<>(); + + public void add(Person person){ + personList.add(person); + } + + public Person findById(Long id){ + for (Person person : personList) { + if (person.getId() == id) + return person; + } + return null; + } + + public void remove(Person person){ + personList.remove(person); + } + + public void removeById(Long id){ + personList.removeIf(person -> person.getId() == id); + + } + public void removeAll(){ + personList.clear(); + } + + public int count(){ + return personList.size(); + } + + public Person[] toArray(){ + return personList.toArray(new Person[0]); + } + + public Iterator iterator(){ + return personList.iterator(); + } + + public boolean hasNext() { + return false; + } + + public Person next() { + return null; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..666c24fc 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; + private String name; + public Person(Long id, String name) { + this.id = id; + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String newName) { + this.name = newName; + } + + public Long getId() { + return id; + } } 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..1d1aecc5 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -0,0 +1,20 @@ +package io.zipcoder.interfaces; + +public class Student extends Person implements Learner{ + + Double totalStudyTime = 0.0; + + public Student(Long id, String name) { + super(id, name); + } + + + public void learn(Double hours){ + totalStudyTime += hours; + } + + public Double getTotalStudyTime(){ + return totalStudyTime; + } +} + 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..8f15e380 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,32 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; + +public final class Students extends People { + private static final Students INSTANCE = new Students(); + + private Students (){ + this.add(new Student(644L, "Nick")); + this.add(new Student(645L, "Char")); + this.add(new Student (646L, "Sitara")); + this.add(new Student(647L, "Zach")); + this.add(new Student(648L, "Dipinti")); + this.add(new Student(649L, "Jeremy")); + this.add(new Student(650L, "Tatiana")); + this.add(new Student(651L, "Ray")); + this.add(new Student(652L, "Nisha")); + this.add(new Student(653L, "Manny")); + this.add(new Student(654L, "Nathan")); + this.add(new Student(655L, "Laura")); + this.add(new Student(656L, "Jen")); + this.add(new Student(657L, "Zachary")); + this.add(new Student(658L, "John")); + this.add(new Student(659L, "Bobbi")); + this.add(new Student(660L, "Rex")); + + } + + public static Students getInstance(){ + return INSTANCE; + } +} 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..00218154 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -0,0 +1,8 @@ +package io.zipcoder.interfaces; + +public interface Teacher { + + public void teach(Learner learner, Double numberOfHours); + + public 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..23169ec0 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,72 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + + @Test + public void testImplementation(){ + //given + String name = "Mr. Feeney"; + Long id = 7463L; + + //when + Object instructor = new Instructor(id, name); + + //then + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void testInheritance(){ + //given + String name = "Mr. Feeney"; + Long id = 7463L; + + //when + Object instructor = new Instructor(id, name); + + //then + Assert.assertTrue(instructor instanceof Person); + } + + @Test + public void testTeach(){ + //given + String name = "Mr. Feeney"; + Long id = 7463L; + Instructor teacher = new Instructor(id, name); + Student student = new Student(555L , "Tim"); + Double hours = 3.5; + + //when + teacher.teach(student, hours); + Double actual = student.getTotalStudyTime(); + + //then + Assert.assertEquals(hours, actual); + + } + + @Test + public void testLecture(){ + //given + String name = "Mr. Feeney"; + Long id = 7463L; + Instructor teacher = new Instructor(id, name); + Student student = new Student(555L , "Tim"); + Student student2 = new Student(552L , "Jim"); + Double hours = 10.0; + Learner[] learners = {student, student2}; + + //when + teacher.lecture(learners, hours); + Double actual = student.getTotalStudyTime(); + + //then + Double expected = 5.0; + Assert.assertEquals(expected, actual); + + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java b/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java new file mode 100644 index 00000000..234c1b4a --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java @@ -0,0 +1,21 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructorsSingleton { + + @Test + public void getInstructorsTest(){ + //given + People people = Instructors.getInstance(); + int expected = 3; + + //when + int actual = people.count(); + + //then + Assert.assertEquals(expected, actual); + + } +} 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..1cab0bca --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,61 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + + @Test + public void addPersonTest(){ + //given + People people = new People(); + Person person1 = new Student(76767L, "James"); + Person person2 = new Instructor(5555L, "Dr.Jay"); + int expected = 2; + + //when + people.add(person1); + people.add(person2); + int actual = people.count(); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void removePersonTest(){ + //given + People people = new People(); + Person person1 = new Student(76767L, "James"); + Person person2 = new Instructor(5555L, "Dr.Jay"); + int expected = 1; + + //when + people.add(person1); + people.add(person2); + people.remove(person1); + int actual = people.count(); + Person present = people.personList.get(0); + + //then + Assert.assertEquals(expected, actual); + Assert.assertEquals(person2, present); + } + + @Test + public void findByIdTest(){ + //given + People people = new People(); + Person person1 = new Student(76767L, "James"); + Person person2 = new Instructor(5555L, "Dr.Jay"); + + //when + people.add(person1); + people.add(person2); + people.removeById(5555L); + Person actual = people.findById(5555L); + + //then + Assert.assertNull(actual); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java index d64cd2f0..c5ed8fe2 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,43 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + + @Test + public void constructorTest() { + //given + String name = "Louie"; + Long id = 34567L; + + //when + Person person = new Person(id, name); + String actual = person.getName(); + Long actualId = person.getId(); + + //then + Assert.assertEquals(name, actual); + Assert.assertEquals(id, actualId); + + } + + @Test + public void setNameTest(){ + //given + String name = "Louie"; + Long id = 23456L; + String expected = "Tom"; + + //when + Person person = new Person(id, name); + person.setName(expected); + + //then + String actual = person.getName(); + Assert.assertEquals(expected, actual); + + } + } 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..734e5ec9 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,50 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + + @Test + public void testImplementation(){ + //given + String name = "Farland"; + Long id = 7463L; + + //when + Object student = new Student(id, name); + + //then + Assert.assertTrue(student instanceof Learner); + } + + @Test + public void testInheritance(){ + //given + String name = "Farland"; + Long id = 7463L; + + //when + Object student = new Student(id, name); + + //then + Assert.assertTrue(student instanceof Person); + } + + @Test + public void testLearn(){ + //given + String name = "Farland"; + Long id = 7463L; + Student student = new Student(id, name); + + //when + Double hours = 4.0; + student.learn(hours); + Double actual = student.getTotalStudyTime(); + + //then + Assert.assertEquals(hours, actual); + + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestStudentsSingleton.java b/src/test/java/io/zipcoder/interfaces/TestStudentsSingleton.java new file mode 100644 index 00000000..8afa9937 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudentsSingleton.java @@ -0,0 +1,21 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudentsSingleton { + + @Test + public void getStudentsTest(){ + //given + People people = Students.getInstance(); + int expected = 17; + + //when + int actual = people.count(); + + //then + Assert.assertEquals(expected, actual); + + } +} From 3427846073831c230e8f8800f321a06ea94a710b Mon Sep 17 00:00:00 2001 From: Dee Date: Sat, 24 Jul 2021 18:54:57 -0400 Subject: [PATCH 2/4] parameterized people --- .../io/zipcoder/interfaces/Instructors.java | 6 ++- .../java/io/zipcoder/interfaces/People.java | 21 +++++----- .../java/io/zipcoder/interfaces/Person.java | 15 +++++++ .../java/io/zipcoder/interfaces/Students.java | 13 +++++-- .../interfaces/ZipCodeWilmington.java | 39 +++++++++++++++++++ .../interfaces/TestInstructorsSingleton.java | 15 +++++++ .../interfaces/TestZipCodeWilmington.java | 25 ++++++++++++ 7 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java diff --git a/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java index 39956cbd..91852543 100644 --- a/src/main/java/io/zipcoder/interfaces/Instructors.java +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -1,6 +1,6 @@ package io.zipcoder.interfaces; -public final class Instructors extends People{ +public final class Instructors extends People{ private static final Instructors INSTANCE = new Instructors(); private Instructors (){ @@ -12,4 +12,8 @@ private Instructors (){ public static Instructors getInstance(){ return INSTANCE; } + + public Instructor[] getArray(){ + return personList.toArray(new Instructor[0]); + } } diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java index a6b5f456..cb5f3845 100644 --- a/src/main/java/io/zipcoder/interfaces/People.java +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -4,28 +4,30 @@ import java.util.Iterator; import java.util.List; -public class People implements Iterator { +public abstract class People implements Iterator { - List personList = new ArrayList<>(); + List personList = new ArrayList<>(); - public void add(Person person){ + public void add(E person){ personList.add(person); } public Person findById(Long id){ for (Person person : personList) { - if (person.getId() == id) + Long currentId = person.getId(); + boolean isSameId = currentId.equals(id); + if (isSameId) return person; } return null; } - public void remove(Person person){ + public void remove(E person){ personList.remove(person); } public void removeById(Long id){ - personList.removeIf(person -> person.getId() == id); + personList.removeIf(person -> person.getId().equals(id)); } public void removeAll(){ @@ -36,11 +38,10 @@ public int count(){ return personList.size(); } - public Person[] toArray(){ - return personList.toArray(new Person[0]); - } + public abstract E[] getArray(); + - public Iterator iterator(){ + 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 666c24fc..c97e0e51 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -1,5 +1,7 @@ package io.zipcoder.interfaces; +import java.util.Objects; + public class Person { private final Long id; private String name; @@ -20,4 +22,17 @@ public void setName(String newName) { public Long getId() { return id; } + + @Override + public String toString() { + return "Person{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } + + @Override + public boolean equals(Object o){ + return o.toString().equals(toString()); + } } diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java index 8f15e380..c22cb2b4 100644 --- a/src/main/java/io/zipcoder/interfaces/Students.java +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -2,13 +2,13 @@ import java.util.ArrayList; -public final class Students extends People { +public final class Students extends People { private static final Students INSTANCE = new Students(); - private Students (){ + private Students() { this.add(new Student(644L, "Nick")); this.add(new Student(645L, "Char")); - this.add(new Student (646L, "Sitara")); + this.add(new Student(646L, "Sitara")); this.add(new Student(647L, "Zach")); this.add(new Student(648L, "Dipinti")); this.add(new Student(649L, "Jeremy")); @@ -26,7 +26,12 @@ private Students (){ } - public static Students getInstance(){ + public static Students getInstance() { return INSTANCE; } + + @Override + public Student[] getArray() { + return personList.toArray(new Student[0]); + } } 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..790bbac7 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,39 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class ZipCodeWilmington { + private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington(); + static final Instructors instructors = Instructors.getInstance(); + static final Students students = Students.getInstance(); + + private ZipCodeWilmington(){} + + public static ZipCodeWilmington getInstance() { + return INSTANCE; + } + + + public void hostLecture(Teacher teacher, Double numberOfHours){ + Student[] learners = students.toArray(); + teacher.lecture(learners, numberOfHours); + } + + public void hostLecture(Long id, Double numberOfHours){ + Teacher teacher = (Teacher) instructors.findById(id); + Student[] learners = students.toArray(); + teacher.lecture(learners, numberOfHours); + } + + public Map getStudyMap(){ + Map studyMap = new HashMap<>(); + + for (Person student : students.personList) { + Student thisStudent = (Student) student; + studyMap.put(thisStudent, thisStudent.getTotalStudyTime()); + } + return studyMap; + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java b/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java index 234c1b4a..0083400f 100644 --- a/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java +++ b/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java @@ -18,4 +18,19 @@ public void getInstructorsTest(){ Assert.assertEquals(expected, actual); } + + @Test + public void findByIdTest(){ + //given + People people = Instructors.getInstance(); + Long id = 444L; + Person expected = new Person(id, "Dolio"); + + //when + Person actual = people.findById(id); + + //then + Assert.assertEquals(expected,actual); + + } } 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..59da4b3c --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,25 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestZipCodeWilmington { + + @Test + public void hostLectureTest(){ + //given + ZipCodeWilmington zipcode = ZipCodeWilmington.getInstance(); + Person student = zipcode.students.findById(650L); + Double expected = 1.0; + + //when + Teacher dolio = (Teacher) Instructors.getInstance().findById(444L); + zipcode.hostLecture(dolio, 17.0); + Double actual = zipcode.getStudyMap().get(student); + + //then + Assert.assertEquals(expected, actual); + + + } +} From 0640b9e558f6c71e53b13ddd17679179bf9569ea Mon Sep 17 00:00:00 2001 From: Dee Date: Sat, 24 Jul 2021 20:29:34 -0400 Subject: [PATCH 3/4] enums passed --- .../java/io/zipcoder/interfaces/Educator.java | 40 ++++++++++++ .../java/io/zipcoder/interfaces/People.java | 15 ++--- .../interfaces/ZipCodeWilmington.java | 11 ++-- .../interfaces/TestInstuctorsEnum.java | 61 +++++++++++++++++++ .../io/zipcoder/interfaces/TestPeople.java | 33 +++++----- .../interfaces/TestZipCodeWilmington.java | 38 +++++++++++- 6 files changed, 162 insertions(+), 36 deletions(-) create mode 100644 src/main/java/io/zipcoder/interfaces/Educator.java create mode 100644 src/test/java/io/zipcoder/interfaces/TestInstuctorsEnum.java 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..abbac692 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,40 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher{ + LEON (new Instructor(76L, "Leon")), + DOLIO (new Instructor(70L, "Dolio")), + KRIS (new Instructor(72L, "Kris")); + + private final Instructor instructor; + private double timeWorked; + + Educator(Instructor instructor){ + this.instructor = instructor; + Instructors.getInstance().add(instructor); + } + + public Instructor getInstructor(){ + return instructor; + } + + public Double getTimeWorked(){ + return timeWorked; + } + + @Override + public void teach(Learner learner, Double numberOfHours) { + this.timeWorked += numberOfHours; + learner.learn(numberOfHours); + + } + + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + this.timeWorked += numberOfHours; + double hoursLearned = numberOfHours/ learners.length; + for (Learner learner : learners) { + learner.learn(hoursLearned); + } + } + +} diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java index cb5f3845..d08a64e5 100644 --- a/src/main/java/io/zipcoder/interfaces/People.java +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -4,7 +4,7 @@ import java.util.Iterator; import java.util.List; -public abstract class People implements Iterator { +public abstract class People implements Iterable { List personList = new ArrayList<>(); @@ -12,8 +12,8 @@ public void add(E person){ personList.add(person); } - public Person findById(Long id){ - for (Person person : personList) { + public E findById(Long id){ + for (E person : personList) { Long currentId = person.getId(); boolean isSameId = currentId.equals(id); if (isSameId) @@ -40,16 +40,9 @@ public int count(){ public abstract E[] getArray(); - + @Override public Iterator iterator(){ return personList.iterator(); } - public boolean hasNext() { - return false; - } - - public Person next() { - return null; - } } diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java index 790bbac7..10057ad5 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -17,22 +17,21 @@ public static ZipCodeWilmington getInstance() { public void hostLecture(Teacher teacher, Double numberOfHours){ - Student[] learners = students.toArray(); + Student[] learners = students.getArray(); teacher.lecture(learners, numberOfHours); } public void hostLecture(Long id, Double numberOfHours){ - Teacher teacher = (Teacher) instructors.findById(id); - Student[] learners = students.toArray(); + Teacher teacher = instructors.findById(id); + Student[] learners = students.getArray(); teacher.lecture(learners, numberOfHours); } public Map getStudyMap(){ Map studyMap = new HashMap<>(); - for (Person student : students.personList) { - Student thisStudent = (Student) student; - studyMap.put(thisStudent, thisStudent.getTotalStudyTime()); + for (Student student : students.personList) { + studyMap.put(student, student.getTotalStudyTime()); } return studyMap; } diff --git a/src/test/java/io/zipcoder/interfaces/TestInstuctorsEnum.java b/src/test/java/io/zipcoder/interfaces/TestInstuctorsEnum.java new file mode 100644 index 00000000..a1e9c000 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstuctorsEnum.java @@ -0,0 +1,61 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstuctorsEnum { + + @Test + public void testImplementation() { + //given + Educator instructor = Educator.KRIS; + + //then + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void testLecture(){ + //given + Educator educator = Educator.LEON; + Double hourstaught = 34.0; + Student[] zipcoders = Students.getInstance().getArray(); + + //when + educator.lecture(zipcoders, hourstaught); + Student testStudent = Students.getInstance().findById(645L); + Double actual = testStudent.getTotalStudyTime(); + Double expected = 2.0; + + // + Assert.assertEquals(expected, actual); + + } + + @Test + public void testTeach(){ + //given + Educator educator = Educator.DOLIO; + Double expected = 3.0; + Student student1 = new Student(34L, "Yasmine"); + Student student2 = new Student(45L, "Ssst"); + + //when + educator.teach(student1, 1.5); + educator.teach(student2, 1.5); + + //then + Double actual = educator.getTimeWorked(); + Assert.assertEquals(expected, actual); + + + } + +// public void testInheritance(){ +// //given +// Educator instructor = Educator.DOLIO; +// +// //then +// Assert.assertTrue(instructor instanceof Person); +// } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestPeople.java b/src/test/java/io/zipcoder/interfaces/TestPeople.java index 1cab0bca..ba661234 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPeople.java +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -8,52 +8,49 @@ public class TestPeople { @Test public void addPersonTest(){ //given - People people = new People(); - Person person1 = new Student(76767L, "James"); + People people = Instructors.getInstance(); Person person2 = new Instructor(5555L, "Dr.Jay"); - int expected = 2; + int expected = 4; //when - people.add(person1); people.add(person2); int actual = people.count(); //then Assert.assertEquals(expected, actual); + people.remove(person2); } @Test public void removePersonTest(){ //given - People people = new People(); + People people = Students.getInstance(); Person person1 = new Student(76767L, "James"); - Person person2 = new Instructor(5555L, "Dr.Jay"); - int expected = 1; - //when people.add(person1); - people.add(person2); + Assert.assertEquals(person1,people.findById(person1.getId())); + //when people.remove(person1); - int actual = people.count(); - Person present = people.personList.get(0); + + //then - Assert.assertEquals(expected, actual); - Assert.assertEquals(person2, present); + Assert.assertEquals(null,people.findById(person1.getId())); + + } @Test public void findByIdTest(){ //given - People people = new People(); + People people = Students.getInstance(); Person person1 = new Student(76767L, "James"); - Person person2 = new Instructor(5555L, "Dr.Jay"); + //when people.add(person1); - people.add(person2); - people.removeById(5555L); - Person actual = people.findById(5555L); + people.removeById(76767L); + Person actual = people.findById(76767L); //then Assert.assertNull(actual); diff --git a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java index 59da4b3c..eb4b94b6 100644 --- a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -13,7 +13,7 @@ public void hostLectureTest(){ Double expected = 1.0; //when - Teacher dolio = (Teacher) Instructors.getInstance().findById(444L); + Teacher dolio = Instructors.getInstance().findById(444L); zipcode.hostLecture(dolio, 17.0); Double actual = zipcode.getStudyMap().get(student); @@ -21,5 +21,41 @@ public void hostLectureTest(){ Assert.assertEquals(expected, actual); + } + + @Test + public void hostLectureLeonTest(){ + //given + ZipCodeWilmington zipcode = ZipCodeWilmington.getInstance(); + Person student = zipcode.students.findById(651L); + Double expected = 3.0; + + //when + Teacher leon = Instructors.getInstance().findById(445L); + zipcode.hostLecture(leon, 51.0); + Double actual = zipcode.getStudyMap().get(student); + + //then + Assert.assertEquals(expected, actual); + + + } + + @Test + public void hostLectureInstanceTest(){ + //given + ZipCodeWilmington zipcode = ZipCodeWilmington.getInstance(); + Person student = zipcode.students.findById(651L); + Double expected = 3.0; + + //when + Educator leon = Educator.LEON; //instance of Leon + zipcode.hostLecture(leon, 51.0); //can host a lecture in Zip Code + Double actual = zipcode.getStudyMap().get(student); + + //then + Assert.assertEquals(expected, actual); + + } } From 4a6716be4554f9e8c56787cd42609288a6625a75 Mon Sep 17 00:00:00 2001 From: Dee Date: Sat, 24 Jul 2021 20:33:30 -0400 Subject: [PATCH 4/4] notes added --- src/test/java/io/zipcoder/interfaces/TestPeople.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/io/zipcoder/interfaces/TestPeople.java b/src/test/java/io/zipcoder/interfaces/TestPeople.java index ba661234..4ac707db 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPeople.java +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -7,6 +7,7 @@ public class TestPeople { @Test public void addPersonTest(){ + //test will fail if run after Enum instances //given People people = Instructors.getInstance(); Person person2 = new Instructor(5555L, "Dr.Jay");