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/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/.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..7254afd5 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,33 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher{ + DOLIO (new Instructor(1l,"Dolio")), + LEON (new Instructor(2l,"Leon")), + KRIS (new Instructor(3l,"Kris")); + + private final Instructor instructor; + private double timeWorked; + + Educator(Instructor instructor) { + this.instructor=instructor; + } + + public void teach(Learner learner, double numberOfHours) { + this.instructor.teach(learner,numberOfHours); + timeWorked+=numberOfHours; + } + + public Instructor getInstructor() { + return instructor; + } + + public void lecture(Learner[] learners, double numberOfHours) { + Students students=Students.getInstance(); + this.instructor.lecture(learners,numberOfHours); + timeWorked+=numberOfHours/students.personList.size(); + } + + 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..4516db14 --- /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..57522ff6 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,17 @@ +package io.zipcoder.interfaces; + +public class Instructors extends People{ + private static final Instructors instance=new Instructors(); + + private Instructors(){ + this.add(new Instructor(1l,"Dolio")); + this.add(new Instructor(2l,"Leon")); + this.add(new Instructor(3l,"Kris")); + } + + public static Instructors getInstance(){return instance;} + + public Instructor[] getArray() { + return super.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..c3d7dc51 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Learner.java @@ -0,0 +1,6 @@ +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..9648ebde --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,53 @@ +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) + if (person.getId() == (id)) + return person; + return null; + } + + public boolean contains(E person) { + if(personList.contains(person)) + return true; + return false; + } + + public void remove(E person) { + if(contains(person)) { + personList.remove(person); + } + } + + public void removeById(Long id) { + E person = findByID(id); + remove(person); + } + + public void removeAll() { + personList.clear(); + } + + public Integer count() { + return personList.size(); + } + + public abstract E[] getArray(); + + public Iterator iterator() { + return personList.iterator(); + } + +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..e183fc01 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..18367b49 --- /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=0.0; + + public Student(Long id, String name) { + super(id, name); + } + + public void learn(double numberOfHours) { + totalStudyTime=getTotalStudyTime()+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..5082cdd6 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,22 @@ +package io.zipcoder.interfaces; + +public class Students extends People{ + private static final Students instance=new Students(); + + private Students() { + Student student1=new Student(1l,"Lisa"); + this.add(student1); + this.add(new Student(2l,"Margarita")); + this.add(new Student(3l,"Kabi")); + this.add(new Student(4l,"Sam")); + this.add(new Student(5l,"Sona")); + } + + public static Students getInstance() { + return instance; + } + + public Student[] getArray() { + return super.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..2f9acc8a --- /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..b9a76fa3 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipcodeWilmington.java @@ -0,0 +1,27 @@ +package io.zipcoder.interfaces; + +import java.util.HashMap; +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.getArray(),numberOfHours); + } + + public void hostLecture(long id,double numberOfHours){ + Teacher teacher=instructors.findByID(id); + hostLecture(teacher,numberOfHours); + } + + public Map getStudyMap(){ + Map studyMap=new HashMap(); + for (Student student : students) { + studyMap.put(student,student.getTotalStudyTime()); + } + return studyMap; + } +} 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..e455b764 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestEducator.java @@ -0,0 +1,39 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestEducator { + @Test + public void testImplementation(){ + Educator educator=Educator.KRIS; + Assert.assertTrue(educator instanceof Teacher); + } + + @Test + public void testTeach(){ + //given + Learner dipinti=new Student(5l,"dipinti"); + Educator educator=Educator.DOLIO; + double expectedValue=2.0; + //when + educator.teach(dipinti,expectedValue); + double actualValue=educator.getTimeWorked(); + //then + Assert.assertEquals(expectedValue,actualValue,0.0); + } + + @Test + public void testLecture(){ + //given + Students students=Students.getInstance(); + Educator educator=Educator.KRIS; + int numberOfStudents=students.personList.size(); + double expectedValue=25/numberOfStudents; + //when + educator.lecture(students.getArray(),25); + double actualValue=educator.getTimeWorked(); + //then + Assert.assertEquals(expectedValue,actualValue,0.0); + } +} 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..7ccdac59 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,56 @@ +package io.zipcoder.interfaces; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor extends TestCase { + + @Test + public void testImplementation(){ + //given + Teacher instructor=new Instructor(3l,"sarangi"); + //when + boolean expectedValue=instructor instanceof Teacher; + //then + Assert.assertTrue(expectedValue); + } + + @Test + public void testInheritance(){ + //given + Person instructor=new Instructor(7l,"sangramjit"); + //when + boolean expectedValue=instructor instanceof Person; + //then + Assert.assertTrue(expectedValue); + } + + @Test + public void testTeach(){ + //given + Instructor instructor=new Instructor(5l,"pujari"); + Student student=new Student(null,null); + double expectedValue=5.0; + //when + instructor.teach(student,5); + double actualValue=student.getTotalStudyTime(); + //then + Assert.assertEquals(expectedValue,actualValue,0); + } + + @Test + public void testLecture(){ + //given + Instructor instructor=new Instructor(5l,"pujari"); + Student student1=new Student(null,null); + Student student2=new Student(null,null); + Student[] studentArray={student1,student2}; + double expectedValue=10/2; + //when + instructor.lecture(studentArray,10); + double actualValue=student1.getTotalStudyTime(); + //then + Assert.assertEquals(expectedValue,actualValue,0); + } +} \ 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..720c3198 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructors.java @@ -0,0 +1,17 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + @Test + public void test(){ + //given + int expectedValue=3; + Instructors instance=Instructors.getInstance(); + //when + int actualValue=instance.personList.size(); + //then + Assert.assertEquals(expectedValue,actualValue); + } +} 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..a1322349 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,44 @@ +package io.zipcoder.interfaces; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class TestPeople extends TestCase { + People people = Students.getInstance(); + + @Test + public void testAdd() { + //given + Person person = new Person(5L, "Dips"); + //when + people.add(person); + //then + Assert.assertTrue(people.contains(person)); + } + + @Test + public void testRemove() { + //given + Person person = new Person(5l, "Dips"); + //when + people.add(person); + people.remove(person); + //then + Assert.assertFalse(people.contains(person)); + } + + @Test + public void testFindById() { + //given + Person expectedValue = new Person(9L, "Dolio"); + //when + people.add(expectedValue); + Person actualValue = people.findByID(9L); + //then + Assert.assertEquals(expectedValue, actualValue); + } +} \ 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..8cd0b56a 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,33 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor(){ + //given + Long expectedId=21l; + String expectedName="amilia"; + //when + Person amilia=new Person(expectedId,expectedName); + Long actualId=amilia.getId(); + String actuaName=amilia.getName(); + //then + Assert.assertEquals(expectedId,actualId); + Assert.assertEquals(expectedName,actuaName); + } + + @Test + public void testSetName(){ + //given + Person person=new Person(23l,"georgia"); + String expectedName="fabiana"; + //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..c2d86968 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,41 @@ +package io.zipcoder.interfaces; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent extends TestCase { + + @Test + public void testImplementation(){ + //given + Learner student=new Student(3l,"fiona"); + //when + boolean expectedValue=student instanceof Learner; + //then + Assert.assertTrue(expectedValue); + } + + @Test + public void testInheritance(){ + //given + Person student=new Student(4l,"kimmy"); + //when + boolean expectedValue=student instanceof Person; + //then + Assert.assertTrue(expectedValue); + } + + @Test + public void testLearn(){ + //given + Student newStudent=new Student(15l,"magnolia"); + Double expectedValue=5.0; + //when + newStudent.learn(5); + Double actualValue=newStudent.getTotalStudyTime(); + //then + Assert.assertEquals(expectedValue,actualValue); + } + +} \ 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..47343dd7 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -0,0 +1,20 @@ +package io.zipcoder.interfaces; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents extends TestCase { + + @Test + public void test(){ + //given + int expectedValue=5; + Students instance=Students.getInstance(); + //when + int actualValue=instance.personList.size(); + //then + Assert.assertEquals(expectedValue,actualValue); + } + +} \ 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..13ace7bb --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipcodeWilmington.java @@ -0,0 +1,36 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +public class TestZipcodeWilmington { + @Test + public void testHostLecture(){ + //given + Students students=Students.getInstance(); + double expectedValue=2.0; + //when + ZipcodeWilmington zcw=new ZipcodeWilmington(); + zcw.hostLecture(1l,10); + Student actualStudent=students.personList.get(0); + double actualValue=actualStudent.getTotalStudyTime(); + //then + Assert.assertEquals(expectedValue,actualValue,0.0); + } + + @Test + public void testHostLectureByEducator(){ + //given + Educator educator=Educator.LEON; + Students students=Students.getInstance(); + double expectedValue=5.0; + //when + ZipcodeWilmington zcw=new ZipcodeWilmington(); + zcw.hostLecture(educator,25); + double actualValue=zcw.getStudyMap().get(students.findByID(2l)); + //then + Assert.assertEquals(expectedValue,actualValue,0.0); + } +}