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/.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..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..43612005 --- /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..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/Educator.java b/src/main/java/io/zipcoder/interfaces/Educator.java new file mode 100644 index 00000000..3f2bdcc3 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,28 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher { + LEON, + DOLIO, + KRIS; + + + private final Instructor instructor; + double timeWorked; + + Educator() { + this.instructor = new Instructor((long) ordinal(),name()); + } + + public void teach(Learner learner, double numberOfHours) { + instructor.teach(learner, numberOfHours); + timeWorked += numberOfHours; + } + + public void lecture(Learner[] learners, double numberOfHours) { + instructor.lecture(learners,numberOfHours); + timeWorked += numberOfHours; + } + 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..1b31996e --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -0,0 +1,19 @@ +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(Learner learner : learners){ + learner.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..cfc4d0ed --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,18 @@ +package io.zipcoder.interfaces; + +public class Instructors extends People{ + private static final Instructors INSTANCE = new Instructors(); + private Instructors(){ + this.addPerson(new Instructor(100L, "Leon")); + this.addPerson(new Instructor(101L, "Kris")); + this.addPerson(new Instructor(102L, "Dolio")); + + }; + public static Instructors getInstance(){ + return INSTANCE; + } + + public Instructor[] toArray() { + return 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..2e1edf0d --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Learner.java @@ -0,0 +1,6 @@ +package io.zipcoder.interfaces; + +public interface Learner { + public void learn(double numberOfHours); + public 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..c6896df2 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,60 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public abstract class People implements Iterable { + + + public List personList = new ArrayList(); + + + public void addPerson(E person) { + personList.add(person); + + } + + public E findById(Long id) { + for (E person : personList) { + if (person.getId() == id) + return person; + } + + return null; + } + + public boolean containsPerson(E person) { + if (personList.contains(person)) { + return true; + } + return false; + } + + public void removePerson(E person) { + personList.remove(person); + } + + public void removeById(Long id) { + for (E person : personList) { + if (person.getId() == id) + personList.remove(person); + } + } + + public void removeAllPeople() { + personList.clear(); + } + + public Integer count() { + + return personList.size(); + } + + public abstract E[] toArray(); + + 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..7cfd4e34 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 { +public class Person { + private final Long id; + private String name; + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return 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..9a382442 --- /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..f5c2a2cd --- /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(){ + this.addPerson(new Student(100L, "Ray")); + this.addPerson(new Student(101L, "John")); + this.addPerson(new Student(102L, "Jeremy")); + this.addPerson(new Student(103L, "Tati")); + this.addPerson(new Student(104L, "Zach")); + this.addPerson(new Student(105L, "Dee")); + this.addPerson(new Student(106L, "Jen")); + }; + public static Students getInstance(){ + return INSTANCE; + } + + public Student[] toArray() { + return 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..b9be019a --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -0,0 +1,6 @@ +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..1cc0e669 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,25 @@ +package io.zipcoder.interfaces; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class ZipCodeWilmington { + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); + public void hostLecture(Teacher teacher, double numberOfHours){ + teacher.lecture(students.personList.toArray(new Learner[0]),numberOfHours); + } + public void hostLecture(Long id, double numberOfHours){ + hostLecture(instructors.findById(id),numberOfHours); + } + + public Map getStudyMap(){ + Map studentMap = new LinkedHashMap(); + for(Person student : students){ + Student student1 = (Student)student; + studentMap.put(student1, student1.getTotalStudyTime()); + } + + return studentMap; + } +} 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..8c1e6d0c --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestEducator.java @@ -0,0 +1,55 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestEducator { + @Test + public void implementationTest(){ + //given + Educator instructor = Educator.LEON; + + //when + Boolean check1 = instructor instanceof Teacher; + + //then + Assert.assertTrue(check1); + + } + @Test + public void teachTest(){ + //given + Educator instructor = Educator.LEON; + Student student = new Student(null, null); + + //when + instructor.teach(student, 20.0); + Double actual = student.getTotalStudyTime(); + Double expected = 20.0; + + //then + Assert.assertEquals(expected,actual); + } + @Test + public void lectureTest() { + //given + Educator instructor = Educator.LEON; + Student student1 = new Student(null, null); + Student student2 = new Student(null, null); + Learner[] learners = {student1, student2}; + + + //when + instructor.lecture(learners, 10.0); + + Double actual1 = student1.getTotalStudyTime(); + Double actual2 = student2.getTotalStudyTime(); + + Double expected1 = 5.0; + Double expected2 = 5.0; + + //then + Assert.assertEquals(expected1, actual1); + Assert.assertEquals(expected2, actual2); + } +} 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..51d92473 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,66 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Create a testLecture method that ensures when an Instructor invokes the lecture method, a respective + * array of students' totalStudyTime instance variables is incremented by numberOfHours/students.length. + */ + +public class TestInstructor { + @Test + public void implementationTest(){ + //given + Instructor instructor = new Instructor(null,null); + + //when + Boolean check1 = instructor instanceof Teacher; + Boolean check2 = instructor instanceof Person; + + //then + Assert.assertTrue(check1); + Assert.assertTrue(check2); + + } + @Test + public void teachTest(){ + //given + Instructor instructor = new Instructor(null,null); + Student student = new Student(null, null); + + //when + instructor.teach(student, 20.0); + Double actual = student.getTotalStudyTime(); + Double expected = 20.0; + + //then + Assert.assertEquals(expected,actual); + } + @Test + public void lectureTest(){ + //given + Instructor instructor = new Instructor(null, null); + Student student1 = new Student(null,null); + Student student2 = new Student(null,null); + Learner[] learners = {student1,student2}; + + + //when + instructor.lecture(learners, 10.0); + + Double actual1 = student1.getTotalStudyTime(); + Double actual2 = student2.getTotalStudyTime(); + + Double expected1 = 5.0; + Double expected2 = 5.0; + + //then + Assert.assertEquals(expected1,actual1); + Assert.assertEquals(expected2,actual2); + + + } + + +} 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..c7ff00c7 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructors.java @@ -0,0 +1,25 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +public class TestInstructors { + @Test + public void instructorsSingletonTest() { + //given + String[] zipcodersNames = {"Leon", "Dolio", "Kris"}; + List instructors = Arrays.asList(zipcodersNames); + + //when + for (Person person : Instructors.getInstance()) { + String personName = person.getName(); + + //then + Assert.assertTrue(instructors.contains(personName)); + + } + } +} 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..3aa73b33 --- /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.Test; + +import java.util.ArrayList; +import java.util.List; + +public class TestPeople { + @Test + public void testAdd(){ + //given + List personList = new ArrayList(); + People people = Students.getInstance(); + Person person1 = new Person(39L,"john"); + Person person2 = new Person(34L,"rob"); + Person person3 = new Person(293L,"haley"); + Person person4 = new Person(30L,"rick"); + + //when + people.addPerson(person1); + people.addPerson(person2); + people.addPerson(person3); + people.addPerson(person4); + + Integer actual = people.count(); + Integer expected = 4; + + //given + Assert.assertEquals(expected,actual); + + + } + @Test + public void testRemove(){ + People people = Students.getInstance(); + Person person1 = new Person(null, null); + Person person2 = new Person(null,null); + + people.addPerson(person1); + people.addPerson(person2); + + people.removePerson(person1); + + Integer expected = people.count(); + Integer actual = 1; + + Assert.assertEquals(expected,actual); + + } + @Test + public void testFindById(){ + //given + People migos = Students.getInstance(); + Person quavo = new Person(100L, "Quavo"); + Person takeoff = new Person(101L, "Takeoff"); + Person offset = new Person(102L, "Offset"); + + //when + migos.addPerson(quavo); + migos.addPerson(takeoff); + migos.addPerson(offset); + + Person actual = migos.findById(101L); + Person expected = takeoff; + + //then + Assert.assertEquals(actual,expected); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java index d64cd2f0..ae0d3b28 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,39 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void constructorTest() { + //given + Person person = new Person(10L,"bob"); + + //when + Long expectedId = 10L; + Long actualId = person.getId(); + + String expectedName = "bob"; + String actualName = person.getName(); + + //then + Assert.assertEquals(expectedId,actualId); + Assert.assertEquals(expectedName,actualName); + } + + @Test + public void setNameTest() { + //given + Person person = new Person(234L, "Jerry"); + + //when + person.setName("Rick"); + String actual = person.getName(); + String expected = "Rick"; + + //then + 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..577ce58b --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,39 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + + @Test + public void instanceOfTest(){ + //given + Student student = new Student(20L, "Bob"); + + //when + Boolean instanceOfLearner = student instanceof Learner; + Boolean instanceOfPerson = student instanceof Person; + + //then + Assert.assertTrue(instanceOfLearner); + Assert.assertTrue(instanceOfPerson); + } + @Test + public void learnTest(){ + //given + Student student = new Student(34L, "Jared"); + + //when + student.learn(5); + student.learn(3); + double expected = 8; + double actual = student.getTotalStudyTime(); + Boolean result = expected == actual; + + //then + Assert.assertTrue(result); + } + + + +} 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..1dfd921a --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -0,0 +1,25 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +public class TestStudents { + @Test + public void studentsSingletonTest(){ + //given + String[] zipcodersNames = {"Ray", "John", "Jeremy", "Zach","Tati", "Jen", "Dee"}; + List zipcoders = Arrays.asList(zipcodersNames); + + //when + for(Person person : Students.getInstance()){ + String personName = person.getName(); + + //then + Assert.assertTrue(zipcoders.contains(personName)); + + } + } +} 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..f1bc5be4 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,60 @@ +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 = new ZipCodeWilmington(); + Instructors instructors = Instructors.getInstance(); + Students students = Students.getInstance(); + Instructor leon = instructors.findById(100L); + Map preStudyMap = zipCodeWilmington.getStudyMap(); + Double numberOfHoursToTeach = 1000.0; + Double expectedNumberOfHoursLearned = numberOfHoursToTeach/students.count(); + + //when + zipCodeWilmington.hostLecture(leon, numberOfHoursToTeach); + Map postStudyMap = zipCodeWilmington.getStudyMap(); + Set keySet = postStudyMap.keySet(); + for(Student student : keySet) { + Double preStudyTime = preStudyMap.get(student); + Double expected = preStudyTime + expectedNumberOfHoursLearned; + Double actual = postStudyMap.get(student); + + + //then + Assert.assertEquals(expected, actual); + } + } + @Test + public void testEducator() { + //given + ZipCodeWilmington zipCodeWilmington = new ZipCodeWilmington(); + Students students = Students.getInstance(); + Educator leon = Educator.LEON; + Map preStudyMap = zipCodeWilmington.getStudyMap(); + Double numberOfHoursToTeach = 1000.0; + Double expectedNumberOfHoursLearned = numberOfHoursToTeach/students.count(); + + //when + zipCodeWilmington.hostLecture(leon, numberOfHoursToTeach); + Map postStudyMap = zipCodeWilmington.getStudyMap(); + Set keySet = postStudyMap.keySet(); + for(Student student : keySet) { + Double preStudyTime = preStudyMap.get(student); + Double expected = preStudyTime + expectedNumberOfHoursLearned; + Double actual = postStudyMap.get(student); + + + //then + Assert.assertEquals(expected, actual); + } + } + +} \ No newline at end of file