diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java new file mode 100644 index 0000000..4eeaff6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,20 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher { + 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 (Learner learner : learners){ + this.teach(learner,numberOfHoursPerLearner); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/github/curriculeon/Learner.java b/src/main/java/com/github/curriculeon/Learner.java new file mode 100644 index 0000000..4cd99c7 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Learner { + public void learn(double numberOfHours); + public Double getTotalStudyTime(); +} diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java new file mode 100644 index 0000000..fd96401 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,54 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.lang.Iterable; + +public class People implements Iterable { + + List personList = new ArrayList<>(); + + public People() { + } + + public void add(Person person) { + personList.add(person); + } + + public Person findById(long id) { + for(Person person: personList) { + if(person.getId() == id) { + return person; + } + } + return null; + } + + public Boolean contains(Person person) { + return personList.contains(person); + } + + public void remove(Person person) { + personList.remove(person); + } + + public void removeAll() { + personList.clear(); + } + + public int count() { + return personList.size(); + } + + public void toArray() { + personList.toArray(); + } + + @Override + public Iterator iterator() { + return personList.listIterator(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..e2fffa4 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,24 @@ package com.github.curriculeon; public class Person { + final long id; + private String name; -} + 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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java new file mode 100644 index 0000000..d4ab46b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,21 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner { + double totalStudyTime; + + Student(long id, String name) { + super(id, name); + } + + @Override + public void learn(double numberOfHours) { + this.totalStudyTime+=numberOfHours; + + } + + @Override + public Double getTotalStudyTime() { + return this.totalStudyTime; + } + +} diff --git a/src/main/java/com/github/curriculeon/Teacher.java b/src/main/java/com/github/curriculeon/Teacher.java new file mode 100644 index 0000000..f5d37cb --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,7 @@ +package com.github.curriculeon; + +public interface Teacher { + void teach(Learner learner, double numberOfHours); + void lecture(Learner[] learners, double numberOfHours); + +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java new file mode 100644 index 0000000..1d518bc --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,70 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + + @Test + public void testImplementation() { + //given + Object instructor = new Instructor(0,""); + //when + boolean output = instructor instanceof Teacher; + //then + Assert.assertTrue(output); + } + + @Test + public void testInheritance() { + // given + Object instructor = new Instructor(1, ""); + + // when + Boolean output = instructor instanceof Person; + + // then + Assert.assertTrue(output); + } + + public void testTeach(Learner learner,Double expected, Double numOfHours){ + // When + Instructor instructor = new Instructor(1, "Name"); + instructor.teach(learner,numOfHours); + + // Then + Double actual = learner.getTotalStudyTime(); + Assert.assertEquals(expected, actual); + } + + public void testLecture(Learner[] learners,Double expected, Double numOfHours){ + // When + Instructor instructor = new Instructor(1, "Name"); + instructor.lecture(learners,numOfHours); + + // Then + Double actual = learners[0].getTotalStudyTime(); + Assert.assertEquals(expected, actual); + } + + + @Test + public void test0(){ + Student student = new Student(1, ""); + testTeach(student,5.0,5.0); + } + + @Test + public void test1(){ + Student student = new Student(1, ""); + Student student2 = new Student(1, ""); + Student student3 = new Student(1, ""); + Learner[] students = new Learner[3]; + students[0] = student; + students[1] = student2; + students[2] = student3; + testLecture(students,3.0,9.0); + + } + +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestPeople.java b/src/test/java/com/github/curriculeon/TestPeople.java new file mode 100644 index 0000000..20b0244 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,76 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople{ + + @Test + public void testAdd() { + //given + Person expectedPerson1 = new Person(1111L, "John Doe"); + Person expectedPerson2 = new Person(2222L, "Jane Doe"); + Person expectedPerson3 = new Person(3333L, "Dennis Doe"); + + //when + People personList = new People(); + personList.add(expectedPerson1); + personList.add(expectedPerson2); + personList.add(expectedPerson3); + + //then + Assert.assertTrue(personList.contains(expectedPerson1)); + Assert.assertTrue(personList.contains(expectedPerson2)); + Assert.assertTrue(personList.contains(expectedPerson3)); + } + + @Test + public void testRemove() { + //given + Person Person1 = new Person(1111L, "John Doe"); + Person Person2 = new Person(2222L, "Jane Doe"); + Person Person3 = new Person(3333L, "Dennis Doe"); + + People personList = new People(); + personList.add(Person1); + personList.add(Person2); + personList.add(Person3); + + Assert.assertTrue(personList.contains(Person1)); + Assert.assertTrue(personList.contains(Person2)); + Assert.assertTrue(personList.contains(Person3)); + + //when + personList.remove(Person1); + personList.remove(Person2); + personList.remove(Person3); + + //then + Assert.assertFalse(personList.contains(Person1)); + Assert.assertFalse(personList.contains(Person2)); + Assert.assertFalse(personList.contains(Person3)); + } + + @Test + public void testFindById () { + //given + Person expectedPerson1 = new Person(1111L, "John Doe"); + Person expectedPerson2 = new Person(2222L, "Jane Doe"); + Person expectedPerson3 = new Person(3333L, "Dennis Doe"); + + People personList = new People(); + personList.add(expectedPerson1); + personList.add(expectedPerson2); + personList.add(expectedPerson3); + + //when + Person actualPerson1 = personList.findById(1111L); + Person actualPerson2 = personList.findById(2222L); + Person actualPerson3 = personList.findById(3333L); + + //then + Assert.assertEquals(expectedPerson1, actualPerson1); + Assert.assertEquals(expectedPerson2, actualPerson2); + Assert.assertEquals(expectedPerson3, actualPerson3); + } +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..2b6f157 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,36 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + public void testConstructor() { + // given + Long expectedId = 0L; + String expectedName = "Some name"; + + // when + Person person = new Person(expectedId, expectedName); + String actualName = person.getName(); + Long actualId = person.getId(); + + // then + Assert.assertEquals(expectedId,actualId); + Assert.assertEquals(expectedName,actualName); + } + + @Test + public void testSetName() { + // given + Person person = new Person(-1,null); + String expectedName = "Some name"; + Assert.assertNotEquals(expectedName, person.getName()); + + // when + person.setName(expectedName); + String actualName = person.getName(); -} + // then + Assert.assertEquals(expectedName,actualName); + } +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestStudent.java b/src/test/java/com/github/curriculeon/TestStudent.java new file mode 100644 index 0000000..d16ef56 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,31 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + + @Test + public void testImplementation() { + Student student = new Student(1, "Name"); + Assert.assertTrue(student instanceof Learner); + } + + @Test + public void testInheritance() { + Student student = new Student(1, "Name"); + Assert.assertTrue(student instanceof Person); + } + + @Test + public void testLearn() { + Student student = new Student(1, "Name"); + double expectedNumberOfHours = 2.5; + student.learn(expectedNumberOfHours); + double actualNumberOfHours = student.getTotalStudyTime(); + Assert.assertEquals(expectedNumberOfHours, actualNumberOfHours, 0.1); + + } + + +} \ No newline at end of file