From eba248baf288516e843449c33e12a1f43006faeb Mon Sep 17 00:00:00 2001 From: Ron Duwell Date: Tue, 12 Nov 2019 16:23:50 -0500 Subject: [PATCH 1/5] saving --- .idea/$PRODUCT_WORKSPACE_FILE$ | 30 +++++++++++++++++++ .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 | 7 +++++ .../java/io/zipcoder/interfaces/Person.java | 20 +++++++++++++ .../java/io/zipcoder/interfaces/Student.java | 18 +++++++++++ .../java/io/zipcoder/interfaces/Teacher.java | 7 +++++ .../zipcoder/interfaces/InstructorTest.java | 27 +++++++++++++++++ .../io/zipcoder/interfaces/StudentTest.java | 30 +++++++++++++++++++ .../io/zipcoder/interfaces/TestPerson.java | 25 ++++++++++++++++ 19 files changed, 276 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/Student.java create mode 100644 src/main/java/io/zipcoder/interfaces/Teacher.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..9fba8c61 --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,30 @@ + + + + + + + 1.8.0_201 + + + + + + + + + + + + + + + \ 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..3e62a0b4 --- /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..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..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..8d4df02d --- /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 l : learner){ + l.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..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/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..82a99c74 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -2,4 +2,24 @@ 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 Long getId(){ + return id; + } + + public void setName(String name){ + 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..fedce96f --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -0,0 +1,18 @@ +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..19eefb78 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -0,0 +1,7 @@ +package io.zipcoder.interfaces; + +public interface Teacher { + + void teach(Learner learner, Double numberOfHours); + void lecture(Learner[] learner, Double numberOfHours); +} 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..0c38669d --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/InstructorTest.java @@ -0,0 +1,27 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class InstructorTest { + @Test + public void testImplementation(){ + Instructor instructor = new Instructor(null,null); + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void testInheritance(){ + Instructor instructor = new Instructor(null,null); + Assert.assertTrue(instructor instanceof Person); + } + @Test + public void testTeach() { + } + + @Test + public void testLecture() { + } +} \ No newline at end of file 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..1389fab2 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/StudentTest.java @@ -0,0 +1,30 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StudentTest { + + @Test + public void learn() { + Student student = new Student(null,null); + Double expected = 10D; + student.learn(expected); + + Assert.assertEquals(expected,student.getTotalStudyTime()); + } + + @Test + public void testImplementation(){ + Student student = new Student(null,null); + Assert.assertTrue(student instanceof Person); + } + + @Test + public void testInheritance(){ + Student student = new Student(null,null); + Assert.assertTrue(student instanceof Learner); + } +} \ 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..e0cec1b4 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,30 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor(){ + Long expectedId = 34L; + String expectedName = "Gau"; + Person person = new Person( expectedId , expectedName); + Long actualId = person.getId(); + String actualName = person.getName(); + + Assert.assertEquals(expectedId,actualId); + Assert.assertEquals(expectedName,actualName); + } + + @Test + public void testSetName(){ + Person person = new Person( null , null); + String expected = "Gau"; + person.setName(expected); + String actual = person.getName(); + + Assert.assertEquals(expected,actual); + } + } From 7cced8036ad30bccdfb709da772513e04deb930d Mon Sep 17 00:00:00 2001 From: Ron Duwell Date: Thu, 14 Nov 2019 18:59:47 -0500 Subject: [PATCH 2/5] update --- .idea/compiler.xml | 2 +- .idea/uiDesigner.xml | 124 ++++++++++++++++++ README.md | 16 +-- interfaces-1.iml | 2 +- .../io/zipcoder/interfaces/Instructors.java | 23 ++++ .../java/io/zipcoder/interfaces/People.java | 64 +++++++++ .../java/io/zipcoder/interfaces/Students.java | 27 ++++ .../interfaces/ZipCodeWilmington.java | 25 ++++ .../zipcoder/interfaces/InstructorTest.java | 16 +++ .../zipcoder/interfaces/InstructorsTest.java | 33 +++++ .../io/zipcoder/interfaces/PeopleTest.java | 52 ++++++++ .../io/zipcoder/interfaces/StudentsTest.java | 33 +++++ .../interfaces/ZipCodeWilmingtonTest.java | 23 ++++ 13 files changed, 430 insertions(+), 10 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/io/zipcoder/interfaces/Instructors.java create mode 100644 src/main/java/io/zipcoder/interfaces/People.java create mode 100644 src/main/java/io/zipcoder/interfaces/Students.java create mode 100644 src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.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/StudentsTest.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/.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 96882643..8f79acec 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Leon's Lengthy Learner Lab -* **Objective** - to implement a `ZipCodeWilmington` class which _mediates_ a _composite_ `Students` and `Instructors` _singleton_ reference. +* **Objective** - to implement a `ZipCodeWilmington` class which _mediates_ a _composite_ `io.zipcoder.interfaces.Students` and `Instructors` _singleton_ reference. * **Purpose** - to demonstrate the use of * [interfaces](https://stackoverflow.com/questions/1321122/what-is-an-interface-in-java) * [abstract classes](https://stackoverflow.com/questions/1320745/abstract-class-in-java) @@ -111,11 +111,11 @@ - -### Part 7.1 - Create `Students` singleton +### Part 7.1 - Create `io.zipcoder.interfaces.Students` singleton * **Note:** The creation of this class will demonstrate an implementation of [singleton design pattern](https://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples#eager-initialization). -* Create a `Students` class. +* Create a `io.zipcoder.interfaces.Students` class. * The class should be an _unextendable_ subclass of the `People` class. - * The class should _statically instantiate_ a `final` field named `INSTANCE` of type `Students`. + * The class should _statically instantiate_ a `final` field named `INSTANCE` of type `io.zipcoder.interfaces.Students`. * The class should define a _private nullary constructor_ which populates the `INSTANCE` field with respective `Student` representations of your colleagues. * Each student should have a _relatively_ unique `id` field. * The class should define a `getInstance` method which returns the `INSTANCE` field. @@ -123,9 +123,9 @@ - -### Part 7.0 - Test `Students` singleton +### Part 7.0 - Test `io.zipcoder.interfaces.Students` singleton * Create a `TestStudents` class. - * Create a `test` method which ensures that each of the students in your current cohort are in your `Students` singleton. + * Create a `test` method which ensures that each of the students in your current cohort are in your `io.zipcoder.interfaces.Students` singleton. @@ -139,7 +139,7 @@ - ### Part 9.1 - Create `ZipCodeWilmington` Class * 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 `io.zipcoder.interfaces.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. @@ -176,7 +176,7 @@ - ### Part 10.2 - Modify `People` subclasses -* Modify the `Students` class signature to ensure that it is a subclass of `People` of parameterized type `Student`. +* Modify the `io.zipcoder.interfaces.Students` class signature to ensure that it is a subclass of `People` of parameterized type `Student`. * Modify the `Instructors` class signature to ensure that it is a subclass of `People` of parameterized type `Instructor`. * Provide concrete implementations of the `getArray` method in each of these classes. 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/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java new file mode 100644 index 00000000..d35d0657 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,23 @@ +package io.zipcoder.interfaces; + +public class Instructors extends People { + + public static final Instructors INSTANCE = new Instructors(); + + private Instructors(){ + Instructor instructor1 = new Instructor(2323L, "Boop"); + Instructor instructor2 = new Instructor(3232L, "Bip"); + personList.add(instructor1); + personList.add(instructor2); + + } + + public static Instructors getInstance(){ + return INSTANCE; + } + + @Override + public Instructor[] toArray() { + return personList.toArray(new Instructor[personList.size()]); + } +} 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..78a91531 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,64 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.function.Consumer; + +public abstract class People implements Iterable{ + public List personList = new ArrayList<>(); + + public void add(E person){ + this.personList.add(person); + } + + public E findByID(Long id){ + for(E p : personList){ + if(p.getId().equals(id)){ + return p; + } + } + return null; + } + + public Boolean contains(Person person){ + for(Person p : personList){ + if(p.toString().equals(person.toString())){ + return true; + } + } + return false; + } + + public void remove(E person){ + this.personList.remove(person); + } + + public void remove(Long id){ + Person p = findByID(id); + this.personList.remove(p); + } + + public void removeAll(){ + this.personList.clear(); + } + + public Integer count(){ + return personList.size(); + } + + public abstract E[] toArray(); + + public Iterator iterator() { + return personList.iterator(); + } + + public void forEach(Consumer action) { + + } + + public Spliterator spliterator() { + return null; + } +} 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..0c178830 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,27 @@ +package io.zipcoder.interfaces; + +public class Students extends People { + + public static final Students INSTANCE = new Students(); + + private Students(){ + Student student = new Student(34L,"Gau"); + Student student2 = new Student(45L, "Sabin"); + Student student3 = new Student(67L,"Cyan"); + Student student4 = new Student (81L,"Shadow"); + personList.add(student); + personList.add(student2); + personList.add(student3); + personList.add(student4); + } + + public static Students getInstance(){ + return INSTANCE; + } + + + @Override + public Student[] toArray() { + return personList.toArray(new Student[personList.size()]) ; + } +} 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..aac95807 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,25 @@ +package io.zipcoder.interfaces; + +import java.util.HashMap; + +public class ZipCodeWilmington { + + private static final Students students = Students.INSTANCE.getInstance(); + private static final Instructors instructors = Instructors.INSTANCE.getInstance(); + + public void hostLecture(Teacher teacher, Double numberOfHours){ + teacher.lecture((Learner[]) students.toArray(), numberOfHours); + } + + public void hostLecture(Long id, Double numberOfHours){ + Teacher teacher = instructors.findByID(id); + teacher.lecture((Learner[]) students.personList.toArray(),numberOfHours); + } + + public HashMap getStudyMap(Student student){ + HashMap studyMap= new HashMap<>(); + studyMap.put(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 0c38669d..d38e2f9e 100644 --- a/src/test/java/io/zipcoder/interfaces/InstructorTest.java +++ b/src/test/java/io/zipcoder/interfaces/InstructorTest.java @@ -19,9 +19,25 @@ public void testInheritance(){ } @Test public void testTeach() { + Instructor instructor = new Instructor(null,null); + Student student = new Student(null,null); + Double expected = 10D; + instructor.teach(student,expected); + + Assert.assertEquals(expected,student.getTotalStudyTime()); } @Test public void testLecture() { + Instructor instructor = new Instructor(null, null); + Student student = new Student(null, null); + Student student2 = new Student(null, null); + Student student3 = new Student(null, null); + Learner[] students = {student, student2, student3}; + Double numberOfHours = 9D; + Double totalHoursSplit = numberOfHours / students.length; + + instructor.lecture(students, numberOfHours); + Assert.assertEquals(totalHoursSplit, student.getTotalStudyTime()); } } \ No newline at end of file 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..fcbc5951 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/InstructorsTest.java @@ -0,0 +1,33 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class InstructorsTest { + + @Test + public void getInstanceTest1() { + Instructors instructors = Instructors.INSTANCE; + + Integer expectedSize = 2; + Integer actualSize = instructors.personList.size(); + + Assert.assertEquals(expectedSize,actualSize); + } + + @Test + public void getInstanceTest2(){ + Instructors instructors = Instructors.INSTANCE; + Boolean instructor = false; + + for(Person person : instructors.personList){ + if(person.getId() == 2323L && person.getName().equals("Boop")){ + instructor = true; + break; + } + } + Assert.assertTrue(instructor); + } +} \ 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..a05c36f4 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/PeopleTest.java @@ -0,0 +1,52 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + + +public class PeopleTest { + + @Test + public void addTest() { + Student student = new Student(34L,"Gau"); + Student student2 = new Student(45L,"Cyan"); + Students students = Students.getInstance(); + + students.add(student); + students.add(student2); + Assert.assertTrue(students.contains(student)); + } + + @Test + public void addTest2() { + Student student = new Student(34L,"Gau"); + Student student2 = new Student(45L,"Cyan"); + Students students = Students.getInstance(); + + students.add(student); + students.add(student2); + Assert.assertEquals(6,students.personList.size()); + } + + @Test + public void findByID() { + Long id = 34L; + String expected = "Gau"; + Person person = new Person(id,expected); + Students students = Students.getInstance(); + + String actual = students.findByID(id).getName(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void remove() { + Instructor instructor = new Instructor(null,null); + Instructors instructors = Instructors.getInstance(); + instructors.add(instructor); + instructors.remove(instructor); + + Assert.assertFalse(instructors.contains(instructor)); + } +} \ 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..6c07c32d --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/StudentsTest.java @@ -0,0 +1,33 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StudentsTest { + + @Test + public void getInstanceTest1() { + Students students = Students.INSTANCE; + + Integer expectedSize = 4; + Integer actualSize = students.personList.size(); + + Assert.assertEquals(expectedSize,actualSize); + } + + @Test + public void getInstanceTest2(){ + Students students = Students.INSTANCE; + boolean student = false; + + for(Person person : students.personList){ + if(person.getId() == 34L && person.getName().equals("Gau")){ + student = true; + break; + } + } + Assert.assertTrue(student); + } +} \ No newline at end of file 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..7ce8b797 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -0,0 +1,23 @@ +package io.zipcoder.interfaces; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ZipCodeWilmingtonTest { + + @Test + public void getStudyMap() { + Students students = Students.INSTANCE.getInstance(); + Instructors instructors = Instructors.INSTANCE.getInstance(); + Long expectedTeacherId = 2323L; + Teacher teacher = instructors.findByID(expectedTeacherId); + Long expectedStudentId = 34L; + Student student = students.findByID(expectedStudentId); + Double numberOfHours = 12D; + ZipCodeWilmington zipCodeWilmington = new ZipCodeWilmington(); + zipCodeWilmington.hostLecture(teacher,numberOfHours); + System.out.println(student.getTotalStudyTime()); + + } +} \ No newline at end of file From b5e709e03ba9c245e194b45b4a904f440573e3bc Mon Sep 17 00:00:00 2001 From: Ron Duwell Date: Thu, 14 Nov 2019 22:00:41 -0500 Subject: [PATCH 3/5] update --- .../java/io/zipcoder/interfaces/Educator.java | 18 +++++++++++++ .../java/io/zipcoder/interfaces/Students.java | 23 ++++++++-------- .../interfaces/ZipCodeWilmington.java | 8 +++--- .../interfaces/ZipCodeWilmingtonTest.java | 26 ++++++++++++++++--- 4 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 src/main/java/io/zipcoder/interfaces/Educator.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..1e9c7d35 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,18 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher { + + ONE(),TWO(); + + + + @Override + public void teach(Learner learner, Double numberOfHours) { + + } + + @Override + public void lecture(Learner[] learner, Double numberOfHours) { + + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java index 0c178830..135e0087 100644 --- a/src/main/java/io/zipcoder/interfaces/Students.java +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -4,18 +4,19 @@ public class Students extends People { public static final Students INSTANCE = new Students(); - private Students(){ - Student student = new Student(34L,"Gau"); - Student student2 = new Student(45L, "Sabin"); - Student student3 = new Student(67L,"Cyan"); - Student student4 = new Student (81L,"Shadow"); - personList.add(student); - personList.add(student2); - personList.add(student3); - personList.add(student4); - } + private Students() { + Student student = new Student(34L, "Gau"); + Student student2 = new Student(45L, "Sabin"); + Student student3 = new Student(67L, "Cyan"); + Student student4 = new Student(81L, "Shadow"); + personList.add(student); + personList.add(student2); + personList.add(student3); + personList.add(student4); + } + - public static Students getInstance(){ + 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 aac95807..c139cc2b 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -4,16 +4,16 @@ public class ZipCodeWilmington { - private static final Students students = Students.INSTANCE.getInstance(); - private static final Instructors instructors = Instructors.INSTANCE.getInstance(); + private static final Students students = Students.getInstance(); + private static final Instructors instructors = Instructors.getInstance(); public void hostLecture(Teacher teacher, Double numberOfHours){ - teacher.lecture((Learner[]) students.toArray(), numberOfHours); + teacher.lecture(students.toArray(), numberOfHours); } public void hostLecture(Long id, Double numberOfHours){ Teacher teacher = instructors.findByID(id); - teacher.lecture((Learner[]) students.personList.toArray(),numberOfHours); + teacher.lecture(students.toArray(),numberOfHours); } public HashMap getStudyMap(Student student){ diff --git a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java index 7ce8b797..977f394c 100644 --- a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -1,5 +1,6 @@ package io.zipcoder.interfaces; +import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.*; @@ -7,9 +8,9 @@ public class ZipCodeWilmingtonTest { @Test - public void getStudyMap() { - Students students = Students.INSTANCE.getInstance(); - Instructors instructors = Instructors.INSTANCE.getInstance(); + public void getStudyMapByNameTest() { + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); Long expectedTeacherId = 2323L; Teacher teacher = instructors.findByID(expectedTeacherId); Long expectedStudentId = 34L; @@ -17,7 +18,24 @@ public void getStudyMap() { Double numberOfHours = 12D; ZipCodeWilmington zipCodeWilmington = new ZipCodeWilmington(); zipCodeWilmington.hostLecture(teacher,numberOfHours); - System.out.println(student.getTotalStudyTime()); + Double expectedHours = 3D; + + Assert.assertEquals(expectedHours, student.getTotalStudyTime()); + + } + + @Test + public void getStudyMapByIdTest() { + Students students = Students.getInstance(); + Student student = new Student (98L, "Edgar"); + students.add(student); + Long expectedTeacherId = 2323L; + Double numberOfHours = 12D; + ZipCodeWilmington zipCodeWilmington = new ZipCodeWilmington(); + zipCodeWilmington.hostLecture(expectedTeacherId,numberOfHours); + Double expectedHours = 2.4; + + Assert.assertEquals(expectedHours, student.getTotalStudyTime()); } } \ No newline at end of file From 488d95c4fe6933d9b1f0536ea9adf0f85fe6f22e Mon Sep 17 00:00:00 2001 From: Ron Duwell Date: Fri, 15 Nov 2019 11:02:10 -0500 Subject: [PATCH 4/5] testing singletons sucks --- .../java/io/zipcoder/interfaces/Educator.java | 15 +++++-- .../io/zipcoder/interfaces/Instructor.java | 6 +-- .../java/io/zipcoder/interfaces/Student.java | 2 + .../interfaces/ZipCodeWilmington.java | 2 +- .../io/zipcoder/interfaces/EducatorTest.java | 29 ++++++++++++++ .../io/zipcoder/interfaces/PeopleTest.java | 14 +++++++ .../interfaces/ZipCodeWilmingtonTest.java | 39 ++++++++++++++++--- 7 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 src/test/java/io/zipcoder/interfaces/EducatorTest.java diff --git a/src/main/java/io/zipcoder/interfaces/Educator.java b/src/main/java/io/zipcoder/interfaces/Educator.java index 1e9c7d35..234c32ac 100644 --- a/src/main/java/io/zipcoder/interfaces/Educator.java +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -2,17 +2,24 @@ public enum Educator implements Teacher { - ONE(),TWO(); + BIP(new Instructor(2323L, "BIP")), BOOP(new Instructor(3232L, "BOOP")); + Instructor instructor; + Double timeWorked; + Educator(Instructor instructor) { + this.instructor = instructor; + } @Override public void teach(Learner learner, Double numberOfHours) { - + this.instructor.teach(learner,numberOfHours); + this.timeWorked += numberOfHours; } @Override - public void lecture(Learner[] learner, Double numberOfHours) { - + public void lecture(Learner[] learners, Double numberOfHours) { + this.instructor.lecture(learners, numberOfHours); + this.timeWorked += numberOfHours; } } diff --git a/src/main/java/io/zipcoder/interfaces/Instructor.java b/src/main/java/io/zipcoder/interfaces/Instructor.java index 8d4df02d..1131ac1a 100644 --- a/src/main/java/io/zipcoder/interfaces/Instructor.java +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -9,9 +9,9 @@ public void teach(Learner learner, Double numberOfHours) { learner.learn(numberOfHours); } - public void lecture(Learner[] learner, Double numberOfHours) { - Double numberOfHoursPerLearner = numberOfHours / learner.length; - for(Learner l : learner){ + public void lecture(Learner[] learners, Double numberOfHours) { + Double numberOfHoursPerLearner = numberOfHours / learners.length; + for(Learner l : learners){ l.learn(numberOfHoursPerLearner); } } diff --git a/src/main/java/io/zipcoder/interfaces/Student.java b/src/main/java/io/zipcoder/interfaces/Student.java index fedce96f..90e0dd7b 100644 --- a/src/main/java/io/zipcoder/interfaces/Student.java +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -15,4 +15,6 @@ public void learn(Double numberOfHours) { public Double getTotalStudyTime() { return totalStudyTime; } + + public void setTotalStudyTime(Double totalStudyTime) {this.totalStudyTime = totalStudyTime; } } diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java index c139cc2b..3d23a34a 100644 --- a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -3,7 +3,7 @@ import java.util.HashMap; public class ZipCodeWilmington { - + private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington(); private static final Students students = Students.getInstance(); private static final Instructors instructors = Instructors.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..f865f8f2 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/EducatorTest.java @@ -0,0 +1,29 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class EducatorTest { + + @Test + public void testImplementation(){ + Instructor instructor = new Instructor(null,null); + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void testInheritance(){ + Instructor instructor = new Instructor(null,null); + Assert.assertTrue(instructor instanceof Person); + } + + @Test + public void teach() { + } + + @Test + public void lecture() { + } +} \ 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 index a05c36f4..0b8a26d5 100644 --- a/src/test/java/io/zipcoder/interfaces/PeopleTest.java +++ b/src/test/java/io/zipcoder/interfaces/PeopleTest.java @@ -1,11 +1,17 @@ package io.zipcoder.interfaces; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; public class PeopleTest { + @Before + public void createInstance(){ + } + @Test public void addTest() { Student student = new Student(34L,"Gau"); @@ -49,4 +55,12 @@ public void remove() { Assert.assertFalse(instructors.contains(instructor)); } + + @After + public void clearInstance(){ + Students students = Students.getInstance(); + for(int i = 4; i < students.personList.size(); i++){ + students.personList.remove(i); + } + } } \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java index 977f394c..2235d89c 100644 --- a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -1,14 +1,25 @@ package io.zipcoder.interfaces; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import java.util.HashMap; + import static org.junit.Assert.*; public class ZipCodeWilmingtonTest { + @Before + public void singletonReset(){ + Students students = Students.getInstance(); + for(Student s: students.personList){ + s.setTotalStudyTime(0D); + } + } + @Test - public void getStudyMapByNameTest() { + public void hostLectureByTeacherTest() { Students students = Students.getInstance(); Instructors instructors = Instructors.getInstance(); Long expectedTeacherId = 2323L; @@ -21,21 +32,37 @@ public void getStudyMapByNameTest() { Double expectedHours = 3D; Assert.assertEquals(expectedHours, student.getTotalStudyTime()); - } @Test - public void getStudyMapByIdTest() { + public void hostLectureByIdTest() { Students students = Students.getInstance(); - Student student = new Student (98L, "Edgar"); - students.add(student); Long expectedTeacherId = 2323L; + Long expectedStudentId = 34L; + Student student = students.findByID(expectedStudentId); Double numberOfHours = 12D; ZipCodeWilmington zipCodeWilmington = new ZipCodeWilmington(); zipCodeWilmington.hostLecture(expectedTeacherId,numberOfHours); - Double expectedHours = 2.4; + Double expectedHours = 3D; Assert.assertEquals(expectedHours, student.getTotalStudyTime()); + } + + @Test + public void getStudyMapTest(){ + Students students = Students.getInstance(); + Long expectedTeacherId = 2323L; + Long expectedStudentId = 34L; + Student student = students.findByID(expectedStudentId); + Double expectedHours = 3D; + Double numberOfHours = 12D; + ZipCodeWilmington zipCodeWilmington = new ZipCodeWilmington(); + zipCodeWilmington.hostLecture(expectedTeacherId,numberOfHours); + HashMap expectedHashMap = new HashMap<>(); + + expectedHashMap.put(student,expectedHours); + HashMap actualHashMap = zipCodeWilmington.getStudyMap(student); + Assert.assertEquals(expectedHashMap.get(student), actualHashMap.get(student)); } } \ No newline at end of file From 33de046fcd614d5ed6c82d5dd3a0425003683da9 Mon Sep 17 00:00:00 2001 From: Ron Duwell Date: Fri, 15 Nov 2019 11:37:33 -0500 Subject: [PATCH 5/5] DONE FOREVER --- .../java/io/zipcoder/interfaces/Educator.java | 4 ++-- .../io/zipcoder/interfaces/EducatorTest.java | 24 +++++++++++++------ .../io/zipcoder/interfaces/PeopleTest.java | 4 ---- .../interfaces/ZipCodeWilmingtonTest.java | 14 +++++++++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/zipcoder/interfaces/Educator.java b/src/main/java/io/zipcoder/interfaces/Educator.java index 234c32ac..e09867c6 100644 --- a/src/main/java/io/zipcoder/interfaces/Educator.java +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -2,10 +2,10 @@ public enum Educator implements Teacher { - BIP(new Instructor(2323L, "BIP")), BOOP(new Instructor(3232L, "BOOP")); + ONE(new Instructor(2323L, "BIP")), BOOP(new Instructor(3232L, "BOOP")); Instructor instructor; - Double timeWorked; + Double timeWorked = 0D; Educator(Instructor instructor) { this.instructor = instructor; diff --git a/src/test/java/io/zipcoder/interfaces/EducatorTest.java b/src/test/java/io/zipcoder/interfaces/EducatorTest.java index f865f8f2..e4b883bc 100644 --- a/src/test/java/io/zipcoder/interfaces/EducatorTest.java +++ b/src/test/java/io/zipcoder/interfaces/EducatorTest.java @@ -9,21 +9,31 @@ public class EducatorTest { @Test public void testImplementation(){ - Instructor instructor = new Instructor(null,null); + Educator instructor = Educator.BOOP; Assert.assertTrue(instructor instanceof Teacher); } - @Test - public void testInheritance(){ - Instructor instructor = new Instructor(null,null); - Assert.assertTrue(instructor instanceof Person); - } - @Test public void teach() { + Student student = new Student(null,null); + Double numberOfHours = 3D; + Educator.BOOP.teach(student, numberOfHours); + Double expectedHours = 3D; + + Assert.assertEquals(expectedHours, student.getTotalStudyTime()); } @Test public void lecture() { + Instructor instructor = new Instructor(null, null); + Student student = new Student(null, null); + Student student2 = new Student(null, null); + Student student3 = new Student(null, null); + Learner[] students = {student, student2, student3}; + Double numberOfHours = 9D; + Double totalHoursSplit = numberOfHours / students.length; + Educator.BOOP.lecture(students, numberOfHours); + + Assert.assertEquals(totalHoursSplit, student.getTotalStudyTime()); } } \ 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 index 0b8a26d5..a060cf58 100644 --- a/src/test/java/io/zipcoder/interfaces/PeopleTest.java +++ b/src/test/java/io/zipcoder/interfaces/PeopleTest.java @@ -8,10 +8,6 @@ public class PeopleTest { - @Before - public void createInstance(){ - } - @Test public void addTest() { Student student = new Student(34L,"Gau"); diff --git a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java index 2235d89c..4e4ad0df 100644 --- a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -48,6 +48,20 @@ public void hostLectureByIdTest() { Assert.assertEquals(expectedHours, student.getTotalStudyTime()); } + @Test + public void hostByLectureEducatorTest() { + Students students = Students.getInstance(); + Educator educator = Educator.BOOP; + Long expectedStudentId = 34L; + Student student = students.findByID(expectedStudentId); + Double numberOfHours = 12D; + ZipCodeWilmington zipCodeWilmington = new ZipCodeWilmington(); + zipCodeWilmington.hostLecture(educator,numberOfHours); + Double expectedHours = 3D; + + Assert.assertEquals(expectedHours, student.getTotalStudyTime()); + } + @Test public void getStudyMapTest(){ Students students = Students.getInstance();