diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..0e40fe8f --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ + +# Default ignored files +/workspace.xml \ No newline at end of file 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/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..0ce1ecbe --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -0,0 +1,46 @@ +package io.zipcoder.interfaces; + +public enum Educator implements Teacher { + + YOUNGER(101L, "Christopher"), + SLEUTH(102L, "Dolio"), + DEDEUS(103L, "Roberto"), + MIRANDA(104L, "chris"), + NOBLES(105L, "Sian"); + + private long id; + private String name; + private double timeWorked; + + Educator(long id, String name) { + this.id = id; + this.name = name; + } + + public Long getID() { + return this.id; + } + + public String getName() { + return this.name; + } + + public double getTimeWorked() { + return timeWorked; + } + + + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + this.timeWorked += numberOfHours; + } + + + public void lecture(Learner[] learners, double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learners.length; + for (Learner learner : learners) { + learner.learn(numberOfHoursPerLearner); + } + this.timeWorked += numberOfHours; + } +} \ No newline at end of file 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..393d8ba5 --- /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 { + 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 learners1: learners){ + learners1.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..1a163c8f --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,31 @@ +package io.zipcoder.interfaces; + +public class Instructors extends People{ + private final static Instructors INSTANCE=new Instructors(); + private Instructors(){ + final Instructor INSTANCE=new Instructor(Long.MIN_VALUE,""); + Instructor t1=new Instructor(101L,"Christopher"); + super.add(t1); + Instructor t2=new Instructor(102L,"Dolio"); + super.add(t2); + Instructor t3=new Instructor(103L,"Roberto"); + super.add(t3); + Instructor t4=new Instructor(104L,"Chris"); + super.add(t4); + Instructor t5=new Instructor(105L,"Sian"); + super.add(t5); + } + static Instructors getInstance(){ + return INSTANCE; + } + public Instructor[] toArray() { + Instructor[]arr=new Instructor[INSTANCE.count()]; + int j=0; + for(int i=0;i implements Iterable { + List personList; + + public People(ArrayListpersonList) { + this.personList = personList; + } + + public People(){ + this.personList=new ArrayList(); + } + + void add(E e) { + + personList.add(e); + } + + public E findById(Long id) { + + for (E p : personList) { + if (p.getId() == id) { + return p; + } + } + return null; + } + + public Boolean contains(E person) { + if (personList.contains(person)) { + return true; + } + return false; + } + + public void remove(E person) { + if (personList.contains(person)) + personList.remove(person); + + } + + public void remove(Long id) { + for (E p : personList) { + if (p.getId() == id) + personList.remove(p); + + } + + } + + public void removeAll() { + personList.clear(); + } + + public int count() { + return personList.size(); + } + + //we have to modify the method to add generics + public abstract E[] toArray(); + /*Person[]arr=new Person[personList.size()]; + int j=0; + for(int i=0;i iterator() { + return personList.iterator(); + // return null; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..c6ac0ac2 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -1,5 +1,22 @@ package io.zipcoder.interfaces; public class Person { + final Long id; + String name; + Person(Long personId,String personName){ + this.name=personName; + this.id=personId; + } + 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..d08fdd86 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -0,0 +1,18 @@ +package io.zipcoder.interfaces; + +public class Student extends Person implements Learner { + double totalStudyTime; + Student(Long id,String name,double totalStudyTime){ + super(id,name); + this.totalStudyTime=totalStudyTime; + } + + + public void learn(double numberOfHours) { + totalStudyTime=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..91ff7bc3 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,52 @@ +package io.zipcoder.interfaces; + +public class Students extends People { + private final static Students INSTANCE=new Students(); + + private Students() { + + //final Student INSTANCE = new Student(Long.MIN_VALUE, "", 0); + + Student s1= new Student(1L, "Julia", 4); + super.add(s1); + Student s2 = new Student(2L, "Miral", 5); + super.add(s2); + Student s3 = new Student(3L, "Chris", 3); + super.add(s3); + Student s4 = new Student(4L, "Mike", 4); + super.add(s4); + Student s5 = new Student(5L, "Grace", 7); + super.add(s5); + Student s6 = new Student(6L, "Ron", 4); + super.add(s6); + Student s7 = new Student(7L, "Usha", 1.30); + super.add(s7); + Student s8 = new Student(8L, "Kane", 4); + super.add(s8); + Student s9 = new Student(9L, "Val", 6); + super.add(s9); + Student s10 = new Student(10L, "Ryan", 4); + super.add(s10); + Student s11 = new Student(11L, "Brian", 9); + super.add(s11); + + + } + + static Students getInstance() { + + return INSTANCE; + + } + public Student[] toArray() { + Student[]arr=new Student[INSTANCE.count()]; + int j=0; + for(int i=0;i getStudyMap() { + Map studyMap = new HashMap(); + for (Student s : students) { + studyMap.put(s, s.getTotalStudyTime()); + } + return studyMap; + } + public static ZipCodeWilmington getInstance() { + return zcwINSTANCE; + } + +} 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..daba8d28 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,38 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + @Test + public void testImplementation(){ + Instructor instructor=new Instructor(Long.MIN_VALUE,""); + Assert.assertTrue(instructor instanceof Teacher); + } + @Test + public void testInheritance(){ + Instructor instructor=new Instructor(Long.MIN_VALUE,""); + Assert.assertTrue(instructor instanceof Person); + } + @Test + public void testTeach(){ + Instructor instructor=new Instructor(Long.MIN_VALUE,""); + Student student=new Student(Long.MIN_VALUE,"",2); + Double expectedTotalStudyTime=4.0; + instructor.teach(student,2); + Double actualTotalStudyTime=student.getTotalStudyTime(); + Assert.assertEquals(expectedTotalStudyTime,actualTotalStudyTime); + Assert.assertEquals(expectedTotalStudyTime,actualTotalStudyTime,0.0); + } + @Test + public void testLecture(){ + Instructor instructor=new Instructor(Long.MIN_VALUE,"Froilan"); + Student s1=new Student(64L,"Usha",3); + Student s2=new Student(23L,"Mike",6); + Student[] vidyrthi={s1,s2}; + instructor.lecture(vidyrthi,8.0); + Assert.assertEquals(7,s1.getTotalStudyTime(),0.0); + Assert.assertEquals(10,s2.getTotalStudyTime(),0.0); + } + +} 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..1d3970a9 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructors.java @@ -0,0 +1,32 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + @Test + public void existenceTest(){ + // Instructor t=new Instructor(Long.MIN_VALUE,""); + Instructors instructors=Instructors.getInstance(); + /*Instructor t1=new Instructor(101L,"Christopher"); + Instructor t2=new Instructor(102L,"Dolio"); + Instructor t3=new Instructor(103L,"Roberto"); + Instructor t4=new Instructor(104L,"Chris"); + Instructor t5=new Instructor(105L,"Sian"); + + instructors.add(t1); + instructors.add(t2); + instructors.add(t3); + instructors.add(t4); + instructors.add(t5);*/ + Assert.assertEquals("Christopher",instructors.getInstance().findById(101L).getName()); + Assert.assertEquals("Dolio",instructors.getInstance().findById(102L).getName()); + Assert.assertEquals("Roberto",instructors.getInstance().findById(103L).getName()); + Assert.assertEquals("Chris",instructors.getInstance().findById(104L).getName()); + Assert.assertEquals("Sian",instructors.getInstance().findById(105L).getName()); + + + + + } +} 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..3bc080ba --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,52 @@ +package io.zipcoder.interfaces; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TestPeople +{ + + + +// @Test +// public void addTest(){ +// Person p1=new Person(12L,"Kendra"); +// Person p2=new Person(13L,"Ron"); +// +// +// People people=new People(); +// people.add(p1); +// people.add(p2); +// Assert.assertTrue(people.contains(p1)); +// Assert.assertTrue(people.contains(p2)); +// } +// +// @Test +// public void testRemove(){ +// Person p1=new Person(12L,"Kendra"); +// Person p2=new Person(13L,"Ron"); +// //People people=new People(personList); +// People people=new People(); +// people.remove(p1); +// Assert.assertFalse(people.contains(p1)); +// } +// @Test +// public void testFindById(){ +// Person p1=new Person(12L,"Kendra"); +// Person p2=new Person(13L,"Ron"); +// People people=new People(); +// people.add(p1); +// people.add(p2); +// Person expected=p2; +// System.out.println(expected); +// Person actual=people.findById(13L); +// System.out.println(actual); +// Assert.assertEquals(expected,actual); +// } +// +} diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java index d64cd2f0..2252a76a 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,30 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { +@Test + public void constructorTest(){ +//given + + Long expectedId=Long.MIN_VALUE; + String expectedName="Usha"; + Person person = new Person(Long.MIN_VALUE,"Usha"); + String actualName = person.getName(); + Long actualId = person.getId(); + Assert.assertEquals(expectedName,actualName); + Assert.assertEquals(expectedId,actualId); + +} +@Test + public void setNameTest(){ + Long expectedId=Long.MIN_VALUE; + String expectedName=""; + Person person=new Person(expectedId,expectedName); + String actualName=person.getName(); + 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..48f6559d --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,26 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + @Test + public void testImplementation(){ + + Student student=new Student(Long.MIN_VALUE,"",2); + Assert.assertTrue(student instanceof Learner); + } + @Test + public void testInheritance(){ + Student student=new Student(Long.MIN_VALUE,"",2); + Assert.assertTrue(student instanceof Person); + } + @Test + public void testLearn(){ + Student student=new Student(Long.MIN_VALUE,"",2); + Double expectedTotalStudyTime=4.0; + student.learn(2.0); + Double actualTotalStudyTime=student.getTotalStudyTime(); + Assert.assertEquals(expectedTotalStudyTime,actualTotalStudyTime); + } +} 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..6f7eb04b --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -0,0 +1,48 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents { + @Test + public void testExistence(){ +Student s=new Student(Long.MIN_VALUE,"",0); +Students students= Students.getInstance(); + /* Student s1= new Student(1L, "Julia", 4); + Student s2 = new Student(2L, "Miral", 5); + Student s3 = new Student(3L, "Chris", 3); + Student s4 = new Student(4L, "Mike", 4); + Student s5 = new Student(5L, "Grace", 7); + Student s6 = new Student(6L, "Ron", 4); + Student s7 = new Student(7L, "Usha", 4); + Student s8 = new Student(8L, "Kane", 4); + Student s9 = new Student(9L, "Val", 4); + Student s10 = new Student(10L, "Ryan", 4); + Student s11 = new Student(11L, "Brian", 4); + + students.add(s1); + students.add(s2); + students.add(s3); + students.add(s4); + students.add(s5); + students.add(s6); + students.add(s7); + students.add(s8); + students.add(s9); + students.add(s10); + students.add(s11);*/ + + + + + + Assert.assertEquals("Chris",students.getInstance().findById(3L).getName()); + Assert.assertEquals("Usha",students.getInstance().findById(7L).getName()); + Assert.assertEquals("Brian",students.getInstance().findById(11L).getName()); + Assert.assertNotEquals("Chris",students.getInstance().findById(8L).getName()); + Assert.assertEquals("Miral",students.getInstance().findById(2L).getName()); + + + + } +} 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..fe61ad58 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,41 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestZipCodeWilmington { + @Test + public void testHostLectureByTeacher() { + //declarations + ZipCodeWilmington zcwINSTANCE = ZipCodeWilmington.getInstance(); + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); + double numberOfHours = 7; + Instructor instructor = instructors.findById(103L); + zcwINSTANCE.hostLecture(instructor,7); + Student grace= students.findById(5L); +Assert.assertEquals(17.63,grace.getTotalStudyTime(),0.01); + } + @Test + public void testHostLectureByStudent() { + //declarations + ZipCodeWilmington zcwINSTANCE = ZipCodeWilmington.getInstance(); + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); + double numberOfHours = 110; + Student student= students.findById(10L); + zcwINSTANCE.hostLecture(103L,110); + Student Ryan= students.findById(10L); + Assert.assertEquals(14.0,Ryan.getTotalStudyTime(),0.01); + } + +} + + + + + +