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..d30d09e2 --- /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..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..905bd16d --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1573841788344 + + + + + + + + + \ 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/ConcretePeople.java b/src/main/java/io/zipcoder/interfaces/ConcretePeople.java new file mode 100644 index 00000000..ceebfa18 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ConcretePeople.java @@ -0,0 +1,8 @@ +package io.zipcoder.interfaces; + +public class ConcretePeople extends People { + public Person[] toArray(){ + Person[] arr = new Person[personList.size()]; + return personList.toArray(arr); + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Educator.java b/src/main/java/io/zipcoder/interfaces/Educator.java new file mode 100644 index 00000000..e3b22868 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,29 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher { + INSTRUCTOR0(1L, "instructor0"), INSTRUCTOR1(2L, "instructor1"), + INSTRUCTOR2(3L, "instructor2"); + + private Instructor instructor; + private Double timeWorked = 0.0; + + Educator(Long id, String name) { + this.instructor = new Instructor(id, name); + } + + public void teach(Learner learner, Double numberOfHours) { + timeWorked += numberOfHours; + learner.learn(numberOfHours); + } + + public void lecture(Learner[] learners, Double numberOfHours) { + Double numberOfHoursPerLearner = numberOfHours / learners.length; + for (Learner learner : learners) { + teach(learner, numberOfHoursPerLearner); + } + } + + public Double getTimeWorked() { + return timeWorked; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Instructor.java b/src/main/java/io/zipcoder/interfaces/Instructor.java new file mode 100644 index 00000000..56426b5a --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -0,0 +1,17 @@ +package io.zipcoder.interfaces; + +public class Instructor extends Person implements Teacher{ + + public Instructor(long id, String name) { + super(id, name); + } + + public void teach(Learner learner, Double numberOfHours) { learner.learn(numberOfHours); } + + public void lecture(Learner[] learners, Double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learners.length; + for(int i = 0; i < learners.length; i++){ + learners[i].learn(numberOfHoursPerLearner); + } + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java new file mode 100644 index 00000000..f627234c --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,21 @@ +package io.zipcoder.interfaces; + +public class Instructors extends People { + private static final Instructors INSTANCE = new Instructors(); + + public Instructors() { + super(); + super.add(new Instructor(1, "Froilan")); + super.add(new Instructor(2, "Dolio")); + super.add(new Instructor(3, "Roberto")); + } + + public Instructor[] toArray() { + return personList.toArray(new Instructor[0]); + } + + 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..ee44c762 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Learner.java @@ -0,0 +1,7 @@ +package io.zipcoder.interfaces; + +public interface Learner { + void learn(double numberOfHours); + Double getTotalStudyTime(); +} + diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java new file mode 100644 index 00000000..c8e14f46 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,52 @@ +package io.zipcoder.interfaces; + +import java.lang.reflect.Array; +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(T person) { + personList.add(person); + } + + public T findById(long id) { + for (T p : personList) { + if (p.getId() == id) { + return p; + } + } + return null; + } + + public boolean contains(T person){ + return personList.contains(person); + } + + public void remove(T person){ + personList.remove(person); + } + public void removeById(long id){ + T person = findById(id); + personList.remove(person); + } + public void removeAll(){ + personList.clear(); + } + public int count(){ + return personList.size(); + } + public abstract T[] toArray(); +// T[] arr = new T[personList.size()]; +// return personList.toArray(arr); + + public Iterator iterator() { + return personList.iterator(); + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..fd2955fa 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -1,5 +1,23 @@ package io.zipcoder.interfaces; public class Person { + private final long id; + protected String name; + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Person(long id, String name) { + this.id = id; + this.name = name; + } } diff --git a/src/main/java/io/zipcoder/interfaces/Student.java b/src/main/java/io/zipcoder/interfaces/Student.java new file mode 100644 index 00000000..e3241213 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -0,0 +1,17 @@ +package io.zipcoder.interfaces; + +public class Student extends Person implements Learner{ + private double totalStudyTime; + + public Student(long id, String name) { + super(id, name); + } + + public void learn(double numberOfHours) { + totalStudyTime += numberOfHours; + } + + public Double getTotalStudyTime() { + return totalStudyTime; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java new file mode 100644 index 00000000..b3e0ceb5 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,21 @@ +package io.zipcoder.interfaces; + +public class Students extends People{ + private static final Students INSTANCE = new Students(); + + public Students(){ + super(); + super.add(new Student(1, "Morgan")); + super.add(new Student(2, "Julio")); + super.add(new Student(3, "Cuervo")); + } + + public Student[] toArray() { + return personList.toArray(new Student[0]); + } + + public static Students getInstance(){ + return INSTANCE; + } + +} diff --git a/src/main/java/io/zipcoder/interfaces/Teacher.java b/src/main/java/io/zipcoder/interfaces/Teacher.java new file mode 100644 index 00000000..fe62cb8c --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -0,0 +1,6 @@ +package io.zipcoder.interfaces; + +public interface Teacher { + void teach(Learner learner, Double numberOfHours); + void lecture(Learner[] learners, Double numberOfHours); +} diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java new file mode 100644 index 00000000..7437ddd9 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,29 @@ +package io.zipcoder.interfaces; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class ZipCodeWilmington { + private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington(); + private static final Students students = Students.getInstance(); + private static final Instructors instructors = Instructors.getInstance(); + + public static void hostLecture(Teacher teacher, double numberOfHours){ + teacher.lecture(students.toArray(), numberOfHours); + } + + public static void hostLecture(long id, double numberOfHours){ + Teacher teacher = instructors.findById(id); + teacher.lecture(students.toArray(), numberOfHours); + + //instructors.findById(id).lecture(students.toArray(), numberOfHours); + } + + public static LinkedHashMap getStudyMap(){ + LinkedHashMap map = new LinkedHashMap(); + for(Student student : students) { + map.put(student, student.getTotalStudyTime()); + } + return map; + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestEducator.java b/src/test/java/io/zipcoder/interfaces/TestEducator.java new file mode 100644 index 00000000..a0121e47 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestEducator.java @@ -0,0 +1,38 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TestEducator { + @Test + public void testTeach() { + Student student = new Student(1L, "Morgan"); + Educator.INSTRUCTOR0.teach(student, 100.0); + Assert.assertEquals(student.getTotalStudyTime(), Educator.INSTRUCTOR0.getTimeWorked(), 0.00); + } + + @Test + public void testLecture() { + Student student1 = new Student(1, "Morgan"); + Student student2 = new Student(2, "Jose"); + Student[] students = {student1, student2}; + Double numberOfHours = 40.0; + Double expected = numberOfHours/students.length; + + Educator.INSTRUCTOR1.lecture(students, 40.0); + + Assert.assertEquals(expected, student1.getTotalStudyTime()); + Assert.assertEquals(expected, student2.getTotalStudyTime()); + } + + @Test + public void testGetTimeWorked() { + Student student = new Student(2, "Bobby"); + Double expected = 100.0; + Educator.INSTRUCTOR0.teach(student, 100.0); + Assert.assertEquals(expected, Educator.INSTRUCTOR0.getTimeWorked()); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructor.java b/src/test/java/io/zipcoder/interfaces/TestInstructor.java new file mode 100644 index 00000000..50ef1ac1 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,40 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + @Test + public void testImplementation(){ + Instructor i = new Instructor(1, "Dolio"); + + Assert.assertTrue(i instanceof Teacher); + } + @Test + public void testInheritance(){ + Instructor i = new Instructor(1, "Dolio"); + + Assert.assertTrue(i instanceof Person); + } + @Test + public void testTeach(){ + Instructor i = new Instructor(2, "Bobbu"); + Student student = new Student(1, "Timmy"); + i.teach(student, 5.0); + Assert.assertEquals(5, student.getTotalStudyTime(), .00001); + i.teach(student, 4.0); + Assert.assertEquals(9, student.getTotalStudyTime(), .0001); + } + @Test + public void testLecture(){ + Instructor i = new Instructor(2, "Babadook"); + Student s1 = new Student(1, "Grace"); + Student s2 = new Student(2, "Barry"); + Student s3 = new Student(3, "Joby"); + Student[] sArr = {s1, s2, s3}; + i.lecture(sArr, 9.0); + Assert.assertEquals(3, s1.getTotalStudyTime(), .00001); + Assert.assertEquals(3, s2.getTotalStudyTime(), .00001); + Assert.assertEquals(3, s3.getTotalStudyTime(), .00001); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructors.java b/src/test/java/io/zipcoder/interfaces/TestInstructors.java new file mode 100644 index 00000000..4ed0cae5 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructors.java @@ -0,0 +1,15 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + + @Test + public void testGetInstance() { + Instructors i = Instructors.getInstance(); + Person dolio = i.findById(2); + + Assert.assertEquals("Dolio", dolio.getName()); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestPeople.java b/src/test/java/io/zipcoder/interfaces/TestPeople.java new file mode 100644 index 00000000..9d009ba8 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,70 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +public class TestPeople { + People people; + Person p1, p2, p3; + + @Before + public void setUp(){ + people = new ConcretePeople(); + p1 = new Person(1L, "Bob"); + p2 = new Person(2L, "Pat"); + p3 = new Person(3L, "Billy"); + people.add(p1); + people.add(p2); + } + + @Test + public void add() { + people.add(p3); + Assert.assertTrue(people.contains(p3)); + } + + @Test + public void findById() { + Person actual = people.findById(2L); + Person expected = p2; + Assert.assertEquals(expected, actual); + } + + @Test + public void remove() { + people.remove(p2); + Assert.assertFalse(people.contains(p2)); + } + + @Test + public void testRemoveById(){ + Assert.assertTrue(people.contains(p2)); + people.removeById(2L); + Assert.assertFalse(people.contains(p2)); + } + + @Test + public void testRemoveAll(){ + people.removeAll(); + Assert.assertEquals(people.count(), 0); + } + + @Test + public void testCount(){ + people.add(p3); + people.count(); + Assert.assertEquals(people.count(), 3); + } + + @Test + public void testToArray(){ + Person[] personArr = people.toArray(); + Assert.assertEquals(2, personArr.length); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java index d64cd2f0..1fde831b 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,31 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor1(){ + //given + String expectedName = "BillyBob"; + long expectedId = 2; + Person person = new Person(expectedId, expectedName); + //when + String actualName = person.getName(); + long actualId = person.getId(); + //then + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } + @Test + public void testSetName(){ + //given + String expectedName = "BillyBob"; + Person person = new Person(1, expectedName); + //when + String actualName = person.getName(); + //then + Assert.assertEquals(expectedName, actualName); + } } diff --git a/src/test/java/io/zipcoder/interfaces/TestStudent.java b/src/test/java/io/zipcoder/interfaces/TestStudent.java new file mode 100644 index 00000000..ecf871a5 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,29 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + @Test + public void testImplementation(){ + Student student = new Student(2, "Billy"); + + Assert.assertTrue(student instanceof Learner); + } + @Test + public void testInheritance(){ + Student student = new Student(2, "Billy"); + + Assert.assertTrue(student instanceof Person); + } + @Test + public void testLearn(){ + Student student = new Student(2, "Joe"); + + student.learn(2.5); + Assert.assertEquals(2.5, student.getTotalStudyTime(), .0001); + + student.learn(2.0); + Assert.assertEquals(4.5, student.getTotalStudyTime(), .0001); + } +} \ No newline at end of file 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..1be576d2 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -0,0 +1,15 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents { + + @Test + public void testGetInstance(){ + Students s = Students.getInstance(); + Person julio = s.findById(2); + + Assert.assertEquals("Julio", julio.getName()); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java new file mode 100644 index 00000000..ce0a69c5 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,58 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestZipCodeWilmington { + @Test + public void testHostLecture1() { + Instructor teacher = Instructors.getInstance().findById(1); + Students students = Students.getInstance(); + + ZipCodeWilmington.hostLecture(teacher, 30); + + Double expected = 10.0; + Double actual = 30D / students.personList.size(); + + Assert.assertEquals(expected, actual, 0.0); + } + + @Test + public void testHostLecture2() { + Long teacherId = 1L; + Students students = Students.getInstance(); + + ZipCodeWilmington.hostLecture(teacherId, 60); + + Double expected = 20.0; + Double actual = 60D / students.personList.size(); + + Assert.assertEquals(expected, actual, 0.0); + } + + @Test + public void testHostLecture3() { + Students students = Students.getInstance(); + + ZipCodeWilmington.hostLecture(Educator.INSTRUCTOR2, 30); + + Double expected = 10.0; + Double actual = 30D / students.personList.size(); + + Assert.assertEquals(expected, actual, 0.0); + } + + @Test + public void testGetStudyMap() { + Student expectedS1 = Students.getInstance().findById(1); + Student expectedS2 = Students.getInstance().findById(2); + Double expectedTimeS1 = Students.getInstance().findById(1).getTotalStudyTime(); + Double expectedTimeS2 = Students.getInstance().findById(1).getTotalStudyTime(); + + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsKey(expectedS1)); + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsKey(expectedS2)); + + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsValue(expectedTimeS1)); + Assert.assertTrue(ZipCodeWilmington.getStudyMap().containsValue(expectedTimeS2)); + } +}