diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..e1373df Binary files /dev/null and b/.DS_Store differ 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..59eb9c1 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,29 @@ +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(); + + + public void hostLecture(Teacher teacher, Double numberOfHours){ + Student[] personArray = students.toArray(); + teacher.lecture(personArray, numberOfHours); + } + public void hostLecture(Long id, Double numberOfHours){ + Teacher teacher = Instructors.getInstance().findById(id); + hostLecture(teacher, numberOfHours); + } + public Map getStudyMap(){ + Map map = new HashMap<>(); + for(Student student : Students.getInstance().toArray()){ + double numberOfHours = student.getTotalStudyTime(); + map.put(student, numberOfHours); + } + return map; + } + +} 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..da2d05b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,26 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher{ + MOHAMMED, AYAD, NASER; + private final Instructor instructor; + private Double hoursWorked = 0.0; + + + Educator() { + this.instructor = new Instructor(); + Instructors.getInstance().add(this.instructor); + + } + + @Override + public void lecture(Learner[] learners, Double time) { + this.instructor.lecture(learners, time); + this.hoursWorked += time; + } + + @Override + public void teach(Learner learner, Double time) { +this.instructor.teach(learner, time); +this.hoursWorked+= time; + } +} diff --git a/src/main/java/com/github/curriculeon/Employee.java b/src/main/java/com/github/curriculeon/Employee.java new file mode 100644 index 0000000..0b27a83 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Employee.java @@ -0,0 +1,42 @@ +package com.github.curriculeon; + +public class Employee { + public static Employee LEON = new Employee(5l, "leon", 555.0); + public static Employee MOHAMMED = new Employee(1l, "mohammed", 4444.0); + private Long id; + private String name; + private Double salary; + + private Employee() { + } + + private Employee(Long id, String name, Double salary) { + this.id = id; + this.name = name; + this.salary = salary; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getSalary() { + return salary; + } + + public void setSalary(Double salary) { + this.salary = salary; + } +} 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..b5dd73f --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,21 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher{ + public Instructor() { + } + + public Instructor(Long id, String name) { + super(id, name); + } + + public void teach(Learner learner, Double time){ + learner.learn(time); + } + public void lecture (Learner[] learners, Double time){ + double numberOfHoursPerLearner = time / learners.length; + for(Learner learner : learners){ + teach(learner, 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..c0c6e02 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,21 @@ +package com.github.curriculeon; + +public final class Instructors extends People{ + private static Instructors INSTANCE = new Instructors(); + private Instructors() { + this.add(new Instructor(1l, "mohammed")); + this.add(new Instructor(2l, "ayad")); + this.add(new Instructor(3l, "naser")); + + } + + @Override + public Instructor[] toArray() { + return this.personList.toArray(new Instructor[0]); + } + + 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..44cb9b9 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,8 @@ +package com.github.curriculeon; + +public interface Learner { + void learn(Double hours); + Double getTotalStudyTime(); + + +} diff --git a/src/main/java/com/github/curriculeon/Main.java b/src/main/java/com/github/curriculeon/Main.java new file mode 100644 index 0000000..03d3a2f --- /dev/null +++ b/src/main/java/com/github/curriculeon/Main.java @@ -0,0 +1,11 @@ +package com.github.curriculeon; + +public class Main { + public static void main(String[] args) { + Employee employee = Employee.LEON; + Employee employee2 = Employee.MOHAMMED; + System.out.println(employee.getId() + employee.getName()); + System.out.println(employee2.getName()); + + } +} 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..6fbba86 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,54 @@ +package com.github.curriculeon; + +import java.util.*; + +abstract class People implements Iterable{ + //protected indicates that only subclasses have access to this field + protected List personList = new ArrayList<>(); + public People() { + } + public void add(PersonType person){ + personList.add(person); + } + public PersonType findById(Long id){ + for(PersonType person : personList){ + if(person.getId() == id){ + return person; + } + } + return null; + } + + public boolean contains(PersonType person){ + boolean results = false; + for(PersonType ele: personList){ + if(ele.equals(person)){ + results = true; + break; + } + } + + return results; + } + public void remove(PersonType person){ + personList.remove(person); + } + + public void remove(Long id){ + personList.remove(findById(id)); + } + + public void removeAll(){ + this.personList.clear(); + } + public Integer count(){ + return this.personList.size(); + } + abstract public PersonType[] toArray(); + + + @Override + public Iterator iterator() { + return personList.iterator(); + } +} diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..cfce295 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,30 @@ package com.github.curriculeon; public class Person { + private Long id; + private String name; + public Person() { + } + + public Person(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = 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..34f42d9 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,27 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner{ + private Double totalStudyTime=0.0; + + public Student(Double totalStudyTime) { + this.totalStudyTime = totalStudyTime; + } + + public Student(Long id, String name) { + super(id, name); + } + + public Student() { + + } + + @Override + public void learn(Double hours) { + totalStudyTime+=hours; + } + + @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..d08eb15 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,20 @@ +package com.github.curriculeon; + +public final class Students extends People{ + private static Students INSTANCE = new Students(); + private Students() { + this.add(new Student(5l, "mohammed")); + this.add(new Student(9l, "naser")); + this.add(new Student(4l, "ayad")); + + } + + @Override + public Student[] toArray() { + return this.personList.toArray(new Student[0]); + } + + public static Students getInstance(){ + return INSTANCE; + } +} 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..834cdfa --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Teacher { + void lecture (Learner [] learners, Double numberOfHours); + void teach(Learner learner, 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..bc70be6 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -0,0 +1,34 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +public class TestClassroom { +@Test + public void testHostLecture(){ + // given + Classroom classroom = Classroom.INSTANCE; + //Teacher teacher = (Teacher) Instructors.getInstance().findById(1l); + Educator teacher = Educator.AYAD; + double numberOfHours = Students.getInstance().count(); + double hoursPerStudent = Students.getInstance().count() / numberOfHours; + MappreStudyMap = classroom.getStudyMap(); + + //when + classroom.hostLecture(teacher, numberOfHours); + Map postStudyMap = classroom.getStudyMap(); + + for(Map.Entry entry : preStudyMap.entrySet()){ + Student student = entry.getKey(); + double preStudyTime = entry.getValue(); + double actualStudyTime = postStudyMap.get(student); + double expectedStudyTime = preStudyTime + hoursPerStudent; + //then + Assert.assertEquals(expectedStudyTime, actualStudyTime, 0.0001); + } + +} + +} diff --git a/src/test/java/com/github/curriculeon/TestEducator.java b/src/test/java/com/github/curriculeon/TestEducator.java new file mode 100644 index 0000000..15e07ec --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestEducator.java @@ -0,0 +1,55 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestEducator { + + @Test + public void testImplementation(){ + //given + Educator instructor = Educator.AYAD; + + //when + boolean isInstance = instructor instanceof Teacher; + + + //then + Assert.assertTrue(isInstance); + + } + + + @Test + public void testTeach(){ + //given + Educator instructor = Educator.AYAD; + Learner student = new Student(); + //when + Double initialTime = student.getTotalStudyTime(); + Double studyTime = 5.0; + instructor.teach(student, studyTime); + Double addedTime = initialTime + studyTime; + + //then + Assert.assertEquals(student.getTotalStudyTime(), addedTime); + } + + @Test + public void testLecture(){ + //given + Educator instructor = Educator.AYAD; + Student[] students = new Student[]{new Student(), new Student()}; + Student firstStudent = students[0]; + //when + Double lectureTime = 50.0; + Double lectureTimeForStudent = lectureTime / students.length; + Assert.assertNotEquals(firstStudent.getTotalStudyTime(), lectureTimeForStudent); + instructor.lecture(students, lectureTime); + + + //then + Assert.assertEquals(firstStudent.getTotalStudyTime(), lectureTimeForStudent); + + } +} 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..70c86e5 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,68 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + @Test + public void testImplementation(){ + //given + Instructor instructor = new Instructor(); + + //when + boolean isInstance = instructor instanceof Teacher; + + + //then + Assert.assertTrue(isInstance); + + } + + @Test + public void testInheritance(){ + //given + Instructor instructor = new Instructor(); + + //when + boolean isInstance = instructor instanceof Person; + + //then + Assert.assertTrue(isInstance); + + } + + @Test + public void testTeach(){ + //given + Instructor instructor = new Instructor(); + Learner student = new Student(); + //when + Double initialTime = student.getTotalStudyTime(); + Double studyTime = 5.0; + instructor.teach(student, studyTime); + Double addedTime = initialTime + studyTime; + + //then + Assert.assertEquals(student.getTotalStudyTime(), addedTime); + } + + @Test + public void testLecture(){ + //given + Instructor instructor = new Instructor(); + Student[] students = new Student[]{new Student(), new Student()}; + Student firstStudent = students[0]; + //when + Double lectureTime = 50.0; + Double lectureTimeForStudent = lectureTime / students.length; + Assert.assertNotEquals(firstStudent.getTotalStudyTime(), lectureTimeForStudent); + instructor.lecture(students, lectureTime); + + + //then + Assert.assertEquals(firstStudent.getTotalStudyTime(), lectureTimeForStudent); + + } + + +} 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..5da505d --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class TestInstructors { + + @Test + public void test(){ + // given + String []names = "mohammed ayad naser".split(" "); + Instructors instructors = Instructors.getInstance(); + //when + List listNames = Arrays.stream(Instructors.getInstance().toArray()) + .map(Person::getName) + .collect(Collectors.toList()); + + + //then + for(String name: names){ + Assert.assertTrue(listNames.contains(name)); + } + } +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..71f44dd 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,52 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testPersonConstructor(){ + //given + Long expectedId = 5L; + String expectedName = "mohammed"; + //when + Person person = new Person(expectedId, expectedName); + Long actualId = person.getId(); + String actualName = person.getName(); + + //then + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedId, actualId); + + } + + @Test + public void testSetName(){ + //given + String expectedName = "mohammed"; + //when + Person person = new Person(); + Assert.assertNotEquals(expectedName, person.getName()); + person.setName(expectedName); + + //then + Assert.assertEquals(expectedName,person.getName()); + + } + + @Test + public void testSetId(){ + //given + Long expectedId = 1L; + //when + Person person = new Person(); + Assert.assertNotEquals(person.getId(), expectedId); + person.setId(expectedId); + + //then + Assert.assertEquals(expectedId,person.getId()); + + } + } 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..3f96371 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,33 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Optional; + +public class TestStudent { + + @Test + public void testImplementation(){ + Student b = new Student(); + boolean isInstance = (b instanceof Learner); + Assert.assertTrue(isInstance); + } + @Test + public void testLearn(){ + //given + Student student = new Student(); + //when + Double num = student.getTotalStudyTime(); + Double expected = 0.0; + Assert.assertEquals(num, expected); + Double hoursStudied = 1.0; + student.learn(hoursStudied); + //then + Assert.assertEquals(student.getTotalStudyTime(), hoursStudied); + + + + } + +} 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..3327f69 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,30 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class TestStudents { +@Test + public void test(){ + // given + Students students = Students.getInstance(); + String[] names = "mohammed ayad naser".split(" "); + + //when + List nameList = Arrays + .stream(Students.getInstance().toArray()) + .map(Person::getName) + .collect(Collectors.toList()); + + //then + for(String expectedName : names){ + Assert.assertTrue(nameList.contains(expectedName)); + } + + +} +}