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..5eba099 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,30 @@ +package com.github.curriculeon; + +import java.util.Map; +import java.util.HashMap; + +public enum Classroom { + + INSTANCE; + private Students students = Students.getINSTANCE(); + private Instructors instructors = Instructors.getINSTANCE(); + + public void hostLecture(Teacher teacher, Double numberOfHours) { + teacher.lecture(students.getArray(), numberOfHours); + } + + public void hostLecture(long id, Double numberOfHours) { + Person instructor = instructors.findById(id); + Person[] studentArray = new Student[students.count()]; + students.personList.toArray(studentArray); + ((Teacher)instructor).lecture((Learner[])studentArray, numberOfHours); + } + + public Map getStudyMap() { + Map studentMap = new HashMap<>(); + for(Student student: students.getArray()) { + studentMap.put(student, student.getTotalStudyTime()); + } + return studentMap; + } +} 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..ea60c48 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,26 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher { + + public Instructor() { + } + + 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 numberOfHoursLearned = numberOfHours / learners.length; + + for (int i = 0; i < learners.length; i++) { + this.teach(learners[i], numberOfHoursLearned); + } + + } +} 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..e6d8278 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,27 @@ +package com.github.curriculeon; + +import java.util.List; + +public class Instructors extends People { + + private static Instructors INSTANCE; + + private Instructors() {} + + public static Instructors getINSTANCE() { + if(INSTANCE == null) { //lazy loading, as opposed to eager loading + INSTANCE = new Instructors(); + INSTANCE.add(new Instructor(444, "Lincoln Barnes")); + } + return INSTANCE; + } + + public Instructor[] getArray() { + int sizeOfArray = count(); + Instructor[] destinationArray = new Instructor[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + +} + 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..1af41e3 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,9 @@ +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..b2f0be2 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,59 @@ +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 abstract class People implements Iterable { + + List personList; + + public People() { + this(new ArrayList<>()); + } + + public People(List personList) { + this.personList = personList; + } + public void add(E person) { + this.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) { + return personList.contains(person); + } + + public void remove(E person) { + personList.remove(person); + } + + public void removeAll() { + personList.clear(); + } + + public int count() { + return personList.size(); + } + + public abstract E[] getArray(); + + @Override + public Iterator iterator() { + return personList.listIterator(); + //return new PersonIterator(personList.iterator()); + } + + + +} diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..8667275 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -2,4 +2,29 @@ public class Person { + private final long id; + private String name; + + public Person() { + this.id = Long.MIN_VALUE; + this.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/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java new file mode 100644 index 0000000..8aba9fb --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,31 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner { + + private Double totalStudyTime; + + public Student() { + super(); + this.totalStudyTime = 0D; + } + + public Student(long id, String name, Double totalStudyTime) { + super(id, name); + this.totalStudyTime = totalStudyTime; + } + + public Student(Double totalStudyTime) { + super(); + this.totalStudyTime = totalStudyTime; + } + + @Override + public void learn(Double numberOfHours) { + this.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..9a112d9 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,27 @@ +package com.github.curriculeon; + +import java.util.List; + +public class Students extends People { + + private static Students INSTANCE = new Students(); + + private Students() { + super(); + this.add(new Student(555, "Molly Fisher", 0D)); + this.add(new Student(777, "Holly Becker", 0D)); + this.add(new Student(999, "Brad Singer", 0D)); + } + + public static Students getINSTANCE() { + return INSTANCE; + } + + public Student[] getArray() { + int sizeOfArray = count(); + Student[] destinationArray = new Student[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + +} \ No newline at end of file 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..a44c1e0 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,8 @@ +package com.github.curriculeon; + +public interface Teacher { + + public void teach(Learner learner, Double numberOfHours); + public void lecture(Learner[] learners, Double numberOfHourss); + +} diff --git a/src/test/java/com/github/curriculeon/ClassroomTest.java b/src/test/java/com/github/curriculeon/ClassroomTest.java new file mode 100644 index 0000000..e6bf992 --- /dev/null +++ b/src/test/java/com/github/curriculeon/ClassroomTest.java @@ -0,0 +1,36 @@ +package com.github.curriculeon; + +import org.junit.Test; +import org.junit.Assert; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + + +public class ClassroomTest { + + @Test + public void testHostLecture1() { + Classroom classroom = Classroom.INSTANCE; + Teacher teacher = Instructors.getINSTANCE().findById(444L); + Integer numberOfStudents = Students.getINSTANCE().count(); + Double numberOfHoursToLecture = numberOfStudents.doubleValue(); + Double expectedNumberOfHoursLearned = numberOfHoursToLecture / numberOfStudents; + Map preStudyMap = classroom.getStudyMap(); + + // when + classroom.hostLecture(teacher, numberOfHoursToLecture); + Map postStudyMap = classroom.getStudyMap(); + Set keySet = postStudyMap.keySet(); + for (Student student : keySet) { + Double preStudyTime = preStudyMap.get(student); + Double expectedStudyTime = preStudyTime + expectedNumberOfHoursLearned; + Double actualStudyTime = postStudyMap.get(student); + + // then + Assert.assertEquals(expectedStudyTime, actualStudyTime); + } + } +} diff --git a/src/test/java/com/github/curriculeon/InstructorTest.java b/src/test/java/com/github/curriculeon/InstructorTest.java new file mode 100644 index 0000000..5487aa4 --- /dev/null +++ b/src/test/java/com/github/curriculeon/InstructorTest.java @@ -0,0 +1,71 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class InstructorTest { + + @Test + public void testInstance() { + //Given + Person teacher; + + //When + teacher = new Instructor(); + + //Then + Assert.assertTrue(teacher instanceof Person); + } + + @Test + public void testImplementation() { + //Given + Teacher teacher; + + //When + teacher = new Instructor(); + + //Then + Assert.assertTrue(teacher instanceof Teacher); + } + + @Test + public void testLecture() { + + //Given + Learner learner1 = new Student(5D); + Learner learner2 = new Student(); + Learner learner3 = new Student(6D); + Learner[] learners = { learner1, learner2, learner3 }; + Double[] expectedTotalStudyTimes = { 7D, 2D, 8D }; + + //When + Instructor instructor = new Instructor(); + instructor.lecture(learners, 6D); + Double[] actualTotalStudyTimes = new Double[3]; + for (int i = 0; i < learners.length; i++) { + actualTotalStudyTimes[i] = learners[i].getTotalStudyTime(); + } + + //Then + Assert.assertArrayEquals(expectedTotalStudyTimes, actualTotalStudyTimes); + } + + @Test + public void testTeach() { + //Given + Learner learner = new Student(5D); + Teacher teacher = new Instructor(); + + //When + teacher.teach(learner, 6D); + Double actualTotalStudentStudyTime = learner.getTotalStudyTime(); + + + //Then + Double expectedTotalStudentStudyTime = 5D + 6D; + Assert.assertEquals(expectedTotalStudentStudyTime, actualTotalStudentStudyTime); + } + + +} diff --git a/src/test/java/com/github/curriculeon/InstructorsTest.java b/src/test/java/com/github/curriculeon/InstructorsTest.java new file mode 100644 index 0000000..4df284d --- /dev/null +++ b/src/test/java/com/github/curriculeon/InstructorsTest.java @@ -0,0 +1,27 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class InstructorsTest { + + @Test + public void test() { + //given + // current instructors + + //given + // current cohort + + String expectedInstructorName1 = "Lincoln Barnes"; + + //when + Instructors instructors = Instructors.getINSTANCE(); + + String actualInstructorName1 = instructors.findById(444).getName(); + + //then + Assert.assertEquals(expectedInstructorName1, actualInstructorName1); + } + +} diff --git a/src/test/java/com/github/curriculeon/PeopleTest.java b/src/test/java/com/github/curriculeon/PeopleTest.java new file mode 100644 index 0000000..2a06072 --- /dev/null +++ b/src/test/java/com/github/curriculeon/PeopleTest.java @@ -0,0 +1,77 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class PeopleTest { + + @Test + public void testAdd() { + //given + Person expectedPerson1 = new Person(8888L, "Tom Jones"); + Person expectedPerson2 = new Person(7777L, "Cindy Weatherspoon"); + Person expectedPerson3 = new Person(5555L, "Sarah Kraut"); + + //when + People personList = Instructors.getINSTANCE(); + 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(8888L, "Tom Jones"); + Person Person2 = new Person(7777L, "Cindy Weatherspoon"); + Person Person3 = new Person(5555L, "Sarah Kraut"); + + People personList = Instructors.getINSTANCE(); + 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(8888L, "Tom Jones"); + Person expectedPerson2 = new Person(7777L, "Cindy Weatherspoon"); + Person expectedPerson3 = new Person(5555L, "Sarah Kraut"); + + People personList = Students.getINSTANCE(); + personList.add(expectedPerson1); + personList.add(expectedPerson2); + personList.add(expectedPerson3); + + //when + Person actualPerson1 = personList.findById(8888L); + Person actualPerson2 = personList.findById(7777L); + Person actualPerson3 = personList.findById(5555L); + + //then + Assert.assertEquals(expectedPerson1, actualPerson1); + Assert.assertEquals(expectedPerson2, actualPerson2); + Assert.assertEquals(expectedPerson3, actualPerson3); + } +} + diff --git a/src/test/java/com/github/curriculeon/PersonTest.java b/src/test/java/com/github/curriculeon/PersonTest.java new file mode 100644 index 0000000..16786c6 --- /dev/null +++ b/src/test/java/com/github/curriculeon/PersonTest.java @@ -0,0 +1,40 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class PersonTest { + + @Test + public void testConstructor() { + + //given + Long expectedId = 5556668888L; + String expectedName = "Tom Jones"; + Person person = new Person(expectedId, expectedName); + + //when + Long actualId = person.getId(); + String actualName = person.getName(); + + //then + Assert.assertEquals(actualId, expectedId); + Assert.assertEquals(actualName, expectedName); + } + + @Test + public void testSetName() { + + //given + String expectedName = "Tom Jones"; + Person person = new Person(); + + //when + person.setName(expectedName); + String actualName = person.getName(); + + //then + Assert.assertEquals(actualName, expectedName); + Assert.assertEquals(actualName, expectedName); + } +} diff --git a/src/test/java/com/github/curriculeon/StudentTest.java b/src/test/java/com/github/curriculeon/StudentTest.java new file mode 100644 index 0000000..58871d5 --- /dev/null +++ b/src/test/java/com/github/curriculeon/StudentTest.java @@ -0,0 +1,47 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class StudentTest { + + @Test + public void testImplementation() { + //given + Learner student = new Student(); + + //when + Boolean result = student instanceof Person; + + //then + Assert.assertTrue(result); + } + + @Test + public void testInheritance() { + + //given + Learner student = new Student(); + + //when + Boolean result = student instanceof Person; + + //then + Assert.assertTrue(result); + } + + @Test + public void testLearn() { + //given + Learner student = new Student(); + Double actualNumberOfHours = 98D; + + //when + student.learn(actualNumberOfHours); + Double actualTotalStudyTime = student.getTotalStudyTime(); + + //then + Double expectedTotalStudyTime = 98D; + Assert.assertEquals(actualTotalStudyTime, expectedTotalStudyTime); + } +} diff --git a/src/test/java/com/github/curriculeon/StudentsTest.java b/src/test/java/com/github/curriculeon/StudentsTest.java new file mode 100644 index 0000000..b54698e --- /dev/null +++ b/src/test/java/com/github/curriculeon/StudentsTest.java @@ -0,0 +1,30 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class StudentsTest { + + @Test + public void test() { + //given + // current cohort + + String expectedStudentName1 = "Molly Fisher"; + String expectedStudentName2 = "Holly Becker"; + String expectedStudentName3 = "Brad Singer"; + + //when + Students cohort = Students.getINSTANCE(); + + String actualStudentName1 = cohort.findById(555).getName(); + String actualStudentName2 = cohort.findById(777).getName(); + String actualStudentName3 = cohort.findById(999).getName(); + + //then + Assert.assertEquals(expectedStudentName1, actualStudentName1); + Assert.assertEquals(expectedStudentName2, actualStudentName2); + Assert.assertEquals(expectedStudentName3, actualStudentName3); + } + +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java deleted file mode 100644 index 6c523fe..0000000 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.curriculeon; - -public class TestPerson { - -}