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..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/interfaces-1.iml b/interfaces-1.iml new file mode 100644 index 00000000..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..54582a47 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,28 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher{ + ORACLE, + DOLIO, + Kris; + + + private final Instructor instructor; + double timeWorked; + + Educator() { + this.instructor = new Instructor((long) ordinal(), name()); + } + + + @Override + public void teach(Learner learner, double numberOfHours) { + instructor.teach(learner, numberOfHours); + + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + instructor.lecture(learners, numberOfHours); + + } +} 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..4474eb3d --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -0,0 +1,24 @@ +package io.zipcoder.interfaces; + +public class Instructor extends Person implements Teacher { + + public Instructor(Long id, String name) { + super(id, name); + } + + @Override + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learners.length; + + for(int i =0; i { + + private static final Instructors INSTANCE = new Instructors(); + + private Instructors(){ + Instructor instructor = new Instructor(10l, "Oracle"); + Instructor instructor1 = new Instructor(11l, "Rapper Dolio"); + Instructor instructor2 = new Instructor(12l, "Kris"); + + + personList.add(instructor); + personList.add(instructor1); + personList.add(instructor2); + + } + 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..7b0b1e5b --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Learner.java @@ -0,0 +1,9 @@ +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..6794c1ae --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,62 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public abstract class People implements Iterable{ + List personList; + + public People(){ + personList = new ArrayList(); + } + + public void add(E person){ + personList.add(person); + } + + public E findById(Long id){ + for(E i : personList) { + Long acualId = i.getid(); + if(acualId.equals(id)){ + return i; + } + } + return null; + } + + public boolean contains(E person){ + return personList.contains(person); + } + + public void remove(E person){ + personList.remove(person); + } + + public void removeId(long id){ + for(Person i : personList){ + if(i.getid() == id){ + remove((E) i); + } + } + } + + public void removeAll(){ + personList.clear(); + } + + public int count(){ + return personList.size(); + } + + public E[] toArray(){ + + return (E[]) personList.toArray(new Person[0]); + } + + + @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..dffe064f 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 long getid(){ + return id; + } + + public String getName(){ + return name; + } + + public void setName(String name){ + this.name = name; + } } diff --git a/src/main/java/io/zipcoder/interfaces/Student.java b/src/main/java/io/zipcoder/interfaces/Student.java new file mode 100644 index 00000000..b9d7849c --- /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; + + public Student(long id, String name) { + super(id, name); + } + + @Override + public void learn(double numberOfHours) { + totalStudyTime += numberOfHours; + } + + @Override + public Double getTotalStudyTime() { + return totalStudyTime; + } +} \ No newline at end of file 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..6af402ae --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,21 @@ +package io.zipcoder.interfaces; + +public final class Students extends People { + + private static final Students INSTANCE = new Students(); + + private Students(){ + Student student = new Student(2, "John"); + Student student1 = new Student(3, "Bob"); + Student student2 = new Student(4, "Mikey"); + + personList.add(student); + personList.add(student1); + personList.add(student2); + + } + 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..fad489d6 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -0,0 +1,9 @@ +package io.zipcoder.interfaces; + +public interface Teacher { + + void teach(Learner learner, double numberOfHours); + + void lecture(Learner[] learners, double numberOfHours); + +} diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java new file mode 100644 index 00000000..07d3f6ab --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,37 @@ +package io.zipcoder.interfaces; + +import java.util.HashMap; +import java.util.Map; + +public final class ZipCodeWilmington { + + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); + + private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington(); + + private ZipCodeWilmington(){} + + public void hostLecture(Teacher teacher, double numberOfHours){ + Learner[] learners = new Learner[0]; + teacher.lecture(students.personList.toArray(new Learner[0]), numberOfHours); + } + + public void hostLecture(long id, double numberOfHours){ + hostLecture((Teacher) instructors.findById(id), numberOfHours); + } + + public Map getStudyMap(){ + Map map = new HashMap<>(); + for(Person person : students){ + Student student = (Student) person; + Double totalStudyTime = student.getTotalStudyTime(); + map.put(student, totalStudyTime); + } + return map; + } + public static ZipCodeWilmington getInstance(){ + return INSTANCE; + } + +} diff --git a/src/test/java/io/zipcoder/interfaces/TestEducator.java b/src/test/java/io/zipcoder/interfaces/TestEducator.java new file mode 100644 index 00000000..b480ac30 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestEducator.java @@ -0,0 +1,47 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestEducator { + @Test + public void testImplementation() { + + + Assert.assertTrue(Educator.ORACLE instanceof Teacher); + } + + + @Test + public void teach() { + Educator instructor = Educator.ORACLE; + Student student = new Student(11, "John"); + Double expected = 8.0; + + instructor.teach(student, 8); + Double actual = student.getTotalStudyTime(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testLecture() { + //Given + Educator instructor = Educator.ORACLE; + Student student1 = new Student(11, "John"); + Student student2 = new Student(12, "JoJo"); + Student student3 = new Student(13, "Mikey"); + Student student4 = new Student(14, "Blake"); + + //When + Student[] array = {student1, student2, student3, student4}; + instructor.lecture(array, 8); + Double expected1 = 2.0; + Double actual1 = student1.getTotalStudyTime(); + + //Then + Assert.assertEquals(expected1, actual1); + + + } +} 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..91d1e134 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,58 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + @Test + public void testImplementation() { + //Given + Instructor instructor = new Instructor(11l, "Java Oracle"); + + //Then + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void testInheritance() { + //Given + Instructor instructor = new Instructor(11l, "Java Oracle"); + + //THen + Assert.assertTrue(instructor instanceof Person); + } + + @Test + public void teach() { + Instructor instructor = new Instructor(11l, "Java Oracle"); + Student student = new Student(11, "John"); + Double expected = 8.0; + + instructor.teach(student, 8); + Double actual = student.getTotalStudyTime(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testLecture() { + //Given + Instructor instructor = new Instructor(11l, "Java Oracle"); + Student student1 = new Student(11, "John"); + Student student2 = new Student(12, "JoJo"); + Student student3 = new Student(13, "Mikey"); + Student student4 = new Student(14, "Blake"); + + //When + Student[] array = {student1, student2, student3, student4}; + instructor.lecture(array, 8); + Double expected1 = 2.0; + Double actual1 = student1.getTotalStudyTime(); + + //Then + Assert.assertEquals(expected1, actual1); + + + } + +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructors.java b/src/test/java/io/zipcoder/interfaces/TestInstructors.java new file mode 100644 index 00000000..0396e178 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructors.java @@ -0,0 +1,21 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + + @Test + public void getInstructors(){ + //Given + Instructors instructors = Instructors.getInstance(); + Integer expected = 3; + + //When + Integer actual = instructors.personList.size(); + + //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..b83b5f50 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,49 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class TestPeople { + @Test + public void testAdd() { + //Given + List personList = new ArrayList<>(); + Person person = new Person(2l, "John"); + + //When + personList.add(person); + + //Then + Assert.assertTrue(personList.contains(person)); + } + + @Test + public void testRemove() { + //Given + List personList = new ArrayList<>(); + Person person = new Person(2l, "John"); + + //When + personList.remove(person); + + //THen + Assert.assertFalse(personList.contains(person)); + } + + @Test + public void testFindById() { + //Given + List personList = new ArrayList<>(); + Person person = new Person(2l, "John"); + + //when + person.getid(); + + //then + Assert.assertEquals(2, person.getid()); + + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java index d64cd2f0..a6867295 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,37 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor(){ + //Given + Person person = new Person(82l, "John"); + + //When + long expectedId = 82; + String expectedName = "John"; + + //Then + Assert.assertEquals(expectedId, person.getid()); + Assert.assertEquals(expectedName, person.getName()); + + } + + @Test + public void testSetName(){ + //Given + Person person = new Person(24l, "John"); + String expectedName = "John"; + + //When + person.setName(expectedName); + String actualName = person.getName(); + + //Then + Assert.assertEquals(expectedName, actualName); + + } } diff --git a/src/test/java/io/zipcoder/interfaces/TestStudent.java b/src/test/java/io/zipcoder/interfaces/TestStudent.java new file mode 100644 index 00000000..101b282f --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,44 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + @Test + public void testImplementation() { + //Given + Student student = new Student(25, "John"); + + //When + Boolean actual = student instanceof Learner; + + //Then + Assert.assertTrue(student instanceof Learner); + + + } + + @Test + public void testInheritance(){ + //Given + Student student = new Student(25, "John"); + + //Then + Assert.assertTrue(student instanceof Person); + } + + @Test + public void testLearn(){ + Student student = new Student(25, "John"); + Double expected = 16.0; + + //When + student.learn(16); + Double actual = student.getTotalStudyTime(); + + //Then + Assert.assertEquals(expected, actual); + + } + +} diff --git a/src/test/java/io/zipcoder/interfaces/TestStudents.java b/src/test/java/io/zipcoder/interfaces/TestStudents.java new file mode 100644 index 00000000..ca14c793 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -0,0 +1,21 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents { + @Test + public void getStudents(){ + //Given + Students students = Students.getInstance(); + Integer expected = 3; + + //When + Integer actual = students.personList.size(); + + //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..b76064d1 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,62 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; +import java.util.Set; + +public class TestZipCodeWilmington { + + @Test + public void testHostLecture(){ + //Given + ZipCodeWilmington zipCodeWilmington = ZipCodeWilmington.getInstance(); + Instructors instructors = Instructors.getInstance(); + Students students = Students.getInstance(); + Instructor instructor = (Instructor) Instructors.getInstance().findById(10l); + Map preStudyMap = zipCodeWilmington.getStudyMap(); + Double numberOfHoursToTeach = 9999.0; + Double expectedNumberOfHoursLearned = numberOfHoursToTeach / students.count(); + + //When + zipCodeWilmington.hostLecture(instructor, numberOfHoursToTeach); + Map postStudyMap = zipCodeWilmington.getStudyMap(); + Set keySet = postStudyMap.keySet(); + for(Student student : keySet){ + Double preStudyTime = preStudyMap.get(student); + Double expectedStudyTime = preStudyTime + expectedNumberOfHoursLearned; + Double actualStudyTime = postStudyMap.get(student); + + //Then + Assert.assertEquals(expectedStudyTime, actualStudyTime); + } + + } + + @Test + public void testEducator(){ + //Given + ZipCodeWilmington zipCodeWilmington = ZipCodeWilmington.getInstance(); + + Students students = Students.getInstance(); + Educator oracle = Educator.ORACLE; + Map preStudyMap = zipCodeWilmington.getStudyMap(); + Double numberOfHoursToTeach = 9999.0; + Double expectedNumberOfHoursLearned = numberOfHoursToTeach / students.count(); + + //When + zipCodeWilmington.hostLecture(oracle, numberOfHoursToTeach); + Map postStudyMap = zipCodeWilmington.getStudyMap(); + Set keySet = postStudyMap.keySet(); + for(Student student : keySet){ + Double preStudyTime = preStudyMap.get(student); + Double expectedStudyTime = preStudyTime + expectedNumberOfHoursLearned; + Double actualStudyTime = postStudyMap.get(student); + + //Then + Assert.assertEquals(expectedStudyTime, actualStudyTime); + } + + } +}