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/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/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..91852543 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,19 @@ +package io.zipcoder.interfaces; + +public final class Instructors extends People{ + private static final Instructors INSTANCE = new Instructors(); + + 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; + } + + public Instructor[] getArray(){ + return personList.toArray(new Instructor[0]); + } +} 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..d08a64e5 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,48 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public abstract class People implements Iterable { + + List personList = new ArrayList<>(); + + public void add(E person){ + personList.add(person); + } + + public E findById(Long id){ + for (E person : personList) { + Long currentId = person.getId(); + boolean isSameId = currentId.equals(id); + if (isSameId) + return person; + } + return null; + } + + public void remove(E person){ + personList.remove(person); + } + + public void removeById(Long id){ + personList.removeIf(person -> person.getId().equals(id)); + + } + public void removeAll(){ + personList.clear(); + } + + public int count(){ + return personList.size(); + } + + public abstract E[] getArray(); + + @Override + 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..c97e0e51 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -1,5 +1,38 @@ package io.zipcoder.interfaces; +import java.util.Objects; + 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; + } + + @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/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..c22cb2b4 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,37 @@ +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; + } + + @Override + public Student[] getArray() { + return personList.toArray(new Student[0]); + } +} 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/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java new file mode 100644 index 00000000..10057ad5 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,38 @@ +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.getArray(); + teacher.lecture(learners, numberOfHours); + } + + public void hostLecture(Long id, Double numberOfHours){ + Teacher teacher = instructors.findById(id); + Student[] learners = students.getArray(); + teacher.lecture(learners, numberOfHours); + } + + public Map getStudyMap(){ + Map studyMap = new HashMap<>(); + + for (Student student : students.personList) { + studyMap.put(student, student.getTotalStudyTime()); + } + return studyMap; + } +} 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..0083400f --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructorsSingleton.java @@ -0,0 +1,36 @@ +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); + + } + + @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/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 new file mode 100644 index 00000000..4ac707db --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,59 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +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"); + int expected = 4; + + //when + people.add(person2); + int actual = people.count(); + + //then + Assert.assertEquals(expected, actual); + people.remove(person2); + } + + @Test + public void removePersonTest(){ + //given + People people = Students.getInstance(); + Person person1 = new Student(76767L, "James"); + + people.add(person1); + Assert.assertEquals(person1,people.findById(person1.getId())); + //when + people.remove(person1); + + + + //then + Assert.assertEquals(null,people.findById(person1.getId())); + + + } + + @Test + public void findByIdTest(){ + //given + People people = Students.getInstance(); + Person person1 = new Student(76767L, "James"); + + + //when + people.add(person1); + people.removeById(76767L); + Person actual = people.findById(76767L); + + //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); + + } +} 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..eb4b94b6 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,61 @@ +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 = Instructors.getInstance().findById(444L); + zipcode.hostLecture(dolio, 17.0); + Double actual = zipcode.getStudyMap().get(student); + + //then + 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); + + + } +}