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..3cb4990 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,17 @@ +package com.github.curriculeon; + +public class Classroom { + private Students students = Students.getInstance(); + private Instructors instructors = Instructors.getInstance(); + + public void hostLecture(Teacher teacher, Double numberOfHours){ + teacher.lecture((Learner[])students.toArray(), numberOfHours); + } + + public void hostLecture(long id, Double numberOfHours) { + Person person = instructors.findById(id); + Instructor instructor = (Instructor)person; + instructor.lecture((Learner[])students.toArray(), 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..59b5289 --- /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(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 (int i = 0; i < learners.length; i++) { + Learner learner = learners[i]; + 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..0293ff6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,15 @@ +package com.github.curriculeon; + +public final class Instructors extends People { + private static final Instructors INSTANCE = new Instructors(); + + public Instructors() { + this.add(new Instructor(0L, "Leon")); + this.add(new Instructor(1L, "Haseeb")); + } + + 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..428e02e --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,7 @@ +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..6ad673e --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,63 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class People implements Iterable { + List personList = new ArrayList<>(); + + public void add (Person person) { + + this.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 remove(long id) { + for(Person person : personList) { + if (person.getId() == id) { + personList.remove(person); + } + } + } + + public void removeAll() { + + personList.clear(); + } + + public int count() { + + return personList.size(); + } + + public Person[] toArray() { + int arrayLength = personList.size(); + Person[] newArray = new Person[arrayLength]; + return personList.toArray(newArray); + } + + public Iterator iterator() { + + return personList.iterator(); + } + +} \ 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..09ec0ec 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,29 @@ package com.github.curriculeon; public class Person { + private final Long id; + private String name; + public Person() { + this.id = null; + } + + 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..26f1d68 --- /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 { + private Double totalStudyTime = 2.0; + + public Student(Long id, String name) { + super(id, name); + } + + @Override + public void learn(Double numberOfHours){ + this.totalStudyTime = 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..75c1eab --- /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 final Students INSTANCE = new Students(); + + private Students(){ + super(); + this.add(new Student(0L, "Student A")); + this.add(new Student(1L, "Student B")); + this.add(new Student(2L, "Student C")); + + } + + 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..7b644e6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,9 @@ +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/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java new file mode 100644 index 0000000..9503022 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,74 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + @Test + public void testImplementation(){ + //given + Instructor instructor = new Instructor(null, null); + + //when + boolean outcome = instructor instanceof Teacher; + + //then + Assert.assertTrue(outcome); + } + + + @Test + public void testInheritance() { + //given + Instructor instructor = new Instructor(null, null); + + //when + boolean outcome = instructor instanceof Person; + + //then + Assert.assertTrue(outcome); + } + + + @Test + public void testTeach(){ + //given + Instructor instructor = new Instructor(null, null); + Learner learner = new Student(null, null); + Double numberOfHoursToTeach = 134.0; + Double preStudyTime = learner.getTotalStudyTime(); + Double expected = preStudyTime + numberOfHoursToTeach; + + //when + instructor.teach(learner, numberOfHoursToTeach); + Double actual = learner.getTotalStudyTime(); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testLecture() { + //given + Teacher teacher = new Instructor(null, null); + Student baseStudent = new Student(null, null); + Learner[] learners = new Learner[]{ + new Student(0L, "John"), + new Student(1L, "Q"), + new Student(2L, "Public") + }; + Double numberOfHours = 128.0; + Double expected = numberOfHours/learners.length + baseStudent.getTotalStudyTime(); + + //when + teacher.lecture(learners, numberOfHours); + + //then + for (int i = 0; i < learners.length; i++) { + Learner learner = learners[i]; + Double actual = learner.getTotalStudyTime(); + Assert.assertEquals(expected, actual); + + } + } +} 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..9098487 --- /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; + +public class TestInstructors { + + @Test + public void test() { + //given + Instructors instructors = Instructors.getInstance(); + List instructorNameList = Arrays.asList("Leon", "Haseeb"); + + //when + Person[] instructorArray = instructors.toArray(); + for(int i = 0; i < instructorArray.length; i++) { + Person person = instructorArray[i]; + String personName = person.getName(); + boolean hasPersonWithName = instructorNameList.contains(personName); + + //then + Assert.assertTrue(hasPersonWithName); + } + } +} 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..e8bf1db --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,55 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + + @Test + public void testAdd() { + //given + People peopleList = new People(); + Person person1 = new Person(); + + //when + peopleList.add(person1); + + //then + Assert.assertTrue(peopleList.contains(person1)); + } + + @Test + public void testRemove() { + //given + People peopleList = new People(); + Person person1 = new Person(); + peopleList.add(person1); + Boolean expected = peopleList.contains(person1); + + //when + peopleList.remove(person1); + + //then + Assert.assertFalse(peopleList.contains(person1)); + } + + @Test + public void testFindById() { + //given + People peopleList = new People(); + Person person1 = new Person(575L, "Joe"); + peopleList.add(person1); + Boolean expected = peopleList.contains(person1); + + // when + peopleList.findById(575L); + + //then + Assert.assertTrue(peopleList.contains(peopleList.findById(575L))); + + + } + + + +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..381be98 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,45 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.Period; +import java.time.ZoneId; +import java.util.Date; + +/** + * Created by leon on 7/15/2020. + */ public class TestPerson { + @Test + public void testConstructor() { + //given + Long expectedId = 0L; + String expectedName = "Some Name"; + + // When + Person person = new Person(expectedId, expectedName); + + // Then + Long actualId = person.getId(); + String actualName = person.getName(); + + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } + @Test + public void testSetName() { + // given + Person person = new Person(null, 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..c615113 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,56 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + //Create a testImplementation method that asserts that a Student + // is an instanceof a Learner. + @Test + public void testImplementation() { + //given + Student student = new Student(null, null); + + //when + Boolean result = student instanceof Learner; + + //then + Assert.assertTrue(result); + } + + //Create a testInheritance method that asserts that a Student is an + // instanceof a Person. + @Test + public void testInheritance() { + //given + Student student = new Student(null, null); + + //when + Boolean result = student instanceof Person; + + //then + Assert.assertTrue(result); + + } + + //Create a testLearn method that ensures a Student's totalStudyTime + // instance variable is incremented by the specified numberOfHours + // by invoking the .learn method. + @Test + public void testLearn() { + //given + Student student = new Student(null,null); + Double numberOfHours = 98.0; + Double preStudyTime = student.getTotalStudyTime(); + Double expected = preStudyTime + numberOfHours; + + //when + student.learn(numberOfHours); + Double actual = student.getTotalStudyTime(); + + //then + Assert.assertEquals(expected, actual, 0.01); + + } + +} 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..1cd48b3 --- /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; + +public class TestStudents { + + @Test + public void test() { + //given + Students students = Students.getInstance(); + String[] studentNameArray = {"Student A", "Student B", "Student C"}; + List studentNameList = Arrays.asList(studentNameArray); + + //when + Person[] studentArray = students.toArray(); + for(int i = 0; i < studentArray.length; i++) { + Person person = studentArray[i]; + String personName = person.getName(); + boolean hasPersonWithName = studentNameList.contains(personName); + + //then + Assert.assertTrue(hasPersonWithName); + } + } + +}