diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java new file mode 100644 index 0000000..6b1a8c9 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,35 @@ +package com.github.curriculeon; + +import java.util.HashMap; +import java.util.Map; + +public enum Classroom { + INSTANCE; + private Students students = Students.getInstance(); + private Instructors instructors = Instructors.getInstance(); + + private Classroom() { + students = Students.getInstance(); + instructors = Instructors.getInstance(); + } + + public void hostLecture(Teacher teacher, double numberOfHours) { + teacher.lecture(students.toArray(), numberOfHours); + } + + public void hostLecture(long id, double numberOfHours) { + hostLecture(instructors.findById(id), numberOfHours); + } + + public Map getStudyMap() { + Map studentTotalTimeMap = new HashMap<>(); + for (Student currentStudent : students.toArray()) { + studentTotalTimeMap.put(currentStudent, currentStudent.getTotalStudyTime()); + } + return studentTotalTimeMap; + } + + public static Classroom getInstance() { + return INSTANCE; + } +} diff --git a/src/main/java/com/github/curriculeon/Educator.java b/src/main/java/com/github/curriculeon/Educator.java new file mode 100644 index 0000000..dfd44d1 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher { + LEON, + HASSEB; + + private double hoursWorked; + private final Instructor instructor; + + Educator() { + long id = this.ordinal(); + String name = this.name(); + this.instructor = new Instructor(id, name); + Instructors.getInstance().add(instructor); + } + + @Override + public void teach(Learner learner, double numberOfHours) { + instructor.teach(learner, numberOfHours); + hoursWorked += numberOfHours; + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + instructor.lecture(learners, numberOfHours); + hoursWorked += numberOfHours; + } +} 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..168c0ce --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher { + + public Instructor() { + super(); + } + + public 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) learner.learn(numberOfHoursPerLearner); + } +} diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java new file mode 100644 index 0000000..07ff603 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; + +public class Instructors extends People { + public static Instructors INSTANCE = new Instructors(); + + private Instructors() { + this.add(new Instructor(0L, "Leon")); + this.add(new Instructor(1L, "Hasseb")); + } + + @Override + public Instructor[] toArray() { + Instructor[] instructorList = new Instructor[this.count()]; + for (int i = 0; i < instructorList.length; i++) { + instructorList[i] = this.findById(i); + } + return instructorList; + } + + public static Instructors getInstance() { + return INSTANCE; + } +} 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..c69a07b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Learner { + void learn(double numberOfHours); + 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..188489e --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,52 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public abstract class People implements Iterable { + private List list; + + public People() { + list = new ArrayList<>(); + } + + public void add(E e) { + list.add(e); + } + + public E findById(long id) { + E found = null; + for(E e : list) { + if(e.getId() == id) found = e; + } + return found; + } + + public boolean contains(E e) { + return list.contains(e); + } + + public void remove(E e) { + list.remove(e); + } + + public void remove(long id) { + list.remove(findById(id)); + } + + public void removeAll() { + list.clear(); + } + + public int count() { + return list.size(); + } + + public abstract E[] toArray(); + + @Override + public Iterator iterator() { + return list.iterator(); + } +} diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..317c7af 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,27 @@ package com.github.curriculeon; public class Person { + private final long id; + private String name; + public Person() { + this(0, ""); + } + + 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/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java new file mode 100644 index 0000000..91c9061 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner { + private double totalStudyTime; + + public Student() { + super(); + } + + public Student(Long id, String name) { + super(id, name); + } + + @Override + public void learn(double numberOfHours) { + totalStudyTime += numberOfHours; + } + + @Override + public Double getTotalStudyTime() { + return totalStudyTime; + } +} diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java new file mode 100644 index 0000000..d19e17e --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; + +public final class Students extends People { + private final static Students INSTANCE = new Students(); + + private Students() { + this.add(new Student(0L, "Lionel")); + this.add(new Student(1L, "Aita")); + this.add(new Student(2L, "Flagel")); + } + + public static Students getInstance() { + return INSTANCE; + } + + @Override + public Student[] toArray() { + Student[] studentList = new Student[this.count()]; + for (int i = 0; i < studentList.length; i++) { + studentList[i] = this.findById(i); + } + return studentList; + } +} 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..2d9a34b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Teacher { + void teach(Learner learner, double numberOfHours); + void lecture(Learner[] learners, double numberOfHours); +} diff --git a/src/test/java/com/github/curriculeon/TestClassroom.java b/src/test/java/com/github/curriculeon/TestClassroom.java new file mode 100644 index 0000000..a275ecf --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestClassroom { + @Test + public void testHostLecture() { + //Given + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); + Classroom classroom = Classroom.getInstance(); + + //When + classroom.hostLecture((Teacher)instructors.findById(0), 30); + classroom.hostLecture((Teacher)instructors.findById(0), 60); + classroom.hostLecture((Teacher)instructors.findById(1), 36); + classroom.hostLecture((Teacher)instructors.findById(1), 66); + + //Then + for (Student student : (Student[]) students.toArray()) { + double expectedStudyTime = student.getTotalStudyTime(); + double actualStudyTime = classroom.getStudyMap().get(student); + System.out.printf("Expecting studying time for student %d: %s\n", student.getId(), expectedStudyTime); + Assert.assertEquals(expectedStudyTime, actualStudyTime, 0.1); + } + } +} 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..945cfb8 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,58 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + + @Test + public void testImplementation() { + Instructor instructor = new Instructor(); + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void testInheritance() { + Instructor instructor = new Instructor(); + Assert.assertTrue(instructor instanceof Person); + } + + @Test + public void testTeach() { + //Given + Instructor instructor = new Instructor(); + Learner student = new Student(); + double expectedNumberOfHours = 2.14; + + //When + instructor.teach(student, expectedNumberOfHours); + double actualNumberOfHours = student.getTotalStudyTime(); + + //Then + Assert.assertEquals(expectedNumberOfHours, actualNumberOfHours, 0.1); + } + + @Test + public void testLecture() { + //Given + Instructor instructor = new Instructor(); + Learner student1 = new Student(); + Learner student2 = new Student(); + Learner student3 = new Student(); + Learner student4 = new Student(); + Learner student5 = new Student(); + + Learner[] learners = {student1, student2, student3, student4, student5}; + double TotalNumberOfHours = 10.0; + double expectedNumberOfHoursPerLearner = 10.0/learners.length; + + //When + instructor.lecture(learners, TotalNumberOfHours); + + //Then + for(Learner student : learners) + Assert.assertEquals(expectedNumberOfHoursPerLearner, student.getTotalStudyTime(), 0.1); + } + + +} diff --git a/src/test/java/com/github/curriculeon/TestInstructors.java b/src/test/java/com/github/curriculeon/TestInstructors.java new file mode 100644 index 0000000..322751c --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,18 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + @Test + public void test() { + //Given + Instructors instance = Instructors.getInstance(); + + //When + //Then + for(int i = 0; i < instance.count(); i++) { + Assert.assertTrue(instance.findById(i) instanceof Instructor); + } + } +} 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..8795e74 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,58 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + + @Test + public void testAdd() { + //Given + Person person = new Person(234L, "Lionel"); + People people = Instructors.getInstance(); + + //When + people.add(person); + + //Then + Assert.assertTrue(people.contains(person)); + } + + @Test + public void testRemove() { + //Given + Person person1 = new Person(234L, "Lionel"); + Person person2 = new Person(212L, "Ricky"); + Person person3 = new Person(546L, "Bryan"); + People people = Instructors.getInstance(); + + //When + people.add(person1); + people.add(person2); + people.add(person3); + + people.remove(person2); + + //Then + Assert.assertFalse(people.contains(person2)); + } + + @Test + public void testFindById() { + //Given + Person person1 = new Person(234L, "Lionel"); + Person person2 = new Person(212L, "Ricky"); + Person person3 = new Person(546L, "Bryan"); + People people = Instructors.getInstance(); + + //When + people.add(person1); + people.add(person2); + people.add(person3); + + //Then + for(Object person : people) Assert.assertTrue(people.contains(people.findById(((Person)person).getId()))); + } + + +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..47cf238 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,72 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + private void testConstructor(long expectedId, String expectedName) { + Person person = new Person(expectedId, expectedName); + long actualId = person.getId(); + String actualName = person.getName(); + Assert.assertEquals(actualId, expectedId); + Assert.assertEquals(actualName, expectedName); + } + + private void testSetName(String expectedName) { + Person person = new Person(); + person.setName(expectedName); + String actualName = person.getName(); + Assert.assertEquals(actualName, expectedName); + } + + @Test + public void testConstructor0() { + testConstructor(0, null); + } + + @Test + public void testConstructor1() { + testConstructor(234122, "Johnny"); + } + + @Test + public void testConstructor2() { + testConstructor(3260, "Zack"); + } + + @Test + public void testConstructor3() { + testConstructor(1, "Dona"); + } + + @Test + public void testConstructor4() { + testConstructor(4320, "Ryan"); + } + + @Test + public void testSetName0() { + testSetName(null); + } + + @Test + public void testSetName1() { + testSetName("Tom"); + } + + @Test + public void testSetName2() { + testSetName("Jerry"); + } + + @Test + public void testSetName3() { + testSetName("Mack"); + } + + @Test + public void testSetName4() { + testSetName("Alan"); + } } 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..af1d16d --- /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(); + Assert.assertTrue(student instanceof Learner); + } + + @Test + public void testInheritance() { + Student student = new Student(); + Assert.assertTrue(student instanceof Person); + } + + @Test + public void testLearn() { + Student student = new Student(); + double expectedNumberOfHours = 2.5; + student.learn(expectedNumberOfHours); + double actualNumberOfHours = student.getTotalStudyTime(); + Assert.assertEquals(expectedNumberOfHours, actualNumberOfHours, 0.1); + + } + + +} diff --git a/src/test/java/com/github/curriculeon/TestStudents.java b/src/test/java/com/github/curriculeon/TestStudents.java new file mode 100644 index 0000000..6153e33 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,17 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents { + @Test + public void test() { + // Given + Students instance = Students.getInstance(); + + //Then + for(int i = 0; i < instance.count(); i++) { + Assert.assertTrue(instance.findById(i) instanceof Student); + } + } +}