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..06d14b1 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,25 @@ +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 method invoking learn() method which invokes learn method on specified Learner object + 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..68b9c7f --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,25 @@ +package com.github.curriculeon; + +import java.util.List; + + +public class Instructors extends People { + private static final Instructors instance = new Instructors(); + + public Instructors() { + this.add(new Instructor(0L, "Leon Hunter")); + this.add(new Instructor(1L, "Haseeb Muhammad")); + } + + @Override + public Instructor[] toArray() { + int sizeOfArray = count(); + Instructor[] destinationArray = new Instructor[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + + public static Instructors getInstance() { + return instance; + } +} \ 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..5b2da1a --- /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..e4fd19b --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,76 @@ +package com.github.curriculeon; + + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + + +//Created class People + +public abstract class People implements Iterable { + + //Instantiated a List field of Person objects named personList +//Only Declaration + List personList; + + public People() { + this(new ArrayList<>()); + } + + public People(List personList) { + this.personList = personList; + } + + public void add(SomeLearnerType person) { + this.personList.add(person); + } + + public SomeLearnerType findById(long id) { + for (int i = 0; i < personList.size(); i++) { + SomeLearnerType person = personList.get(i); + if (person.getId() == id) { //if the id is correct, + return person; // return person + } else { // if its the wrong id, + continue; // keep lookin'! + } + } // finished loop; we've finished lookin' + return null; // we were not able to find the person with the id + } + + public SomeLearnerType findByIdExpanded(long id) { + for (int i = 0; i < personList.size(); i++) { + SomeLearnerType person = personList.get(i); + if (person.getId() == id) { //if the id is correct, + return person; // return person + } else { // if its the wrong id, + continue; // keep lookin'! + } + } // finished loop; we've finished lookin' + return null; // we were not able to find the person with the id + } + + public Boolean contains(SomeLearnerType specifiedPerson) { + return personList.contains(specifiedPerson); + } + + public void remove(SomeLearnerType someSpecificPerson) { + personList.remove(someSpecificPerson); + } + + public void removeAll() { + personList.clear(); + } + + public int count() { + return personList.size(); + } + + abstract public SomeLearnerType[] 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..0d3eb17 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 { + //non primitive Long + //Changing variables to Private + private final Long id; + private String name; + public Person() { + this.id = null; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Person(Long id, String name) { + this.id = id; + 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..ce61f34 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,24 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner { + + //Instance variable + private double totalStudyTime; + + //Constructor Student takes the argument of Long and String, passes the parameters to SuperConstructor + public Student(Long id, String name) { + super(id, name); + } +//Implement the method learn which takes the argument Double + @Override + public void learn(Double numberOfHours) { + //increments totalStudyTime variable by specified numberOfHours argument + this.totalStudyTime+=numberOfHours; + } + //Implement the method getTotalStudyTime which returns totalStudyTime instance variable + @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..f8b58f6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +import java.util.List; + +public class Students extends People { + private static final Students instance = new Students(); + + //private constructor to avoid client applications to use constructor + private Students(){ + super(); // optional invocation + this.add(new Student(0L, "Julia")); + this.add(new Student(1L, "David Y")); + this.add(new Student(2L, "Ghasan")); + } + + + @Override + public Student[] toArray() { + int sizeOfArray = count(); + Student[] destinationArray = new Student[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + + public static Students getInstance(){ + return instance; + } +} \ 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..a5b2831 --- /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/TestConstructor.java b/src/test/java/com/github/curriculeon/TestConstructor.java new file mode 100644 index 0000000..2d11842 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestConstructor.java @@ -0,0 +1,98 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestConstructor { + //create a testImplementation method that asserts that an instructor is an instance of teacher + + @Test + public void testImplementation() { + + //given + Instructor instructor = new Instructor(null, null); + + //when + boolean outcome = instructor instanceof Teacher; + + + //then + Assert.assertTrue(outcome); + + // (all in one line)Assert.assertTrue(new Instructor(null, null)instanceof Teacher); + } + + @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(); + //total study time before giving the lecture + Double expected=preStudyTime + numberOfHoursToTeach; + + //when + //when an Instructor invokes teach method, respective student's totalStudyTime instance variable incremented by specified numberOfHours + //instructor is able to teach some learner for some hours, here teacher is teaching for some numberOfHoursToTeach + instructor.teach(learner, numberOfHoursToTeach); + Double actual=learner.getTotalStudyTime(); + + + + //then + //exected is equal to actual such that actual is equal to some TotalStudyTime + Assert.assertEquals(expected, actual); + + + } + + @Test + public void testLecture() { + //lecture method ensures when an instructor invokes lecture method, respective array of students totalStudyTime instance variables is incremented by numberOfHours/students.length + + //given + Teacher teacher=new Instructor(null, null); + Learner[] learners=new Learner[]{ + new Student(0L,"leon"), + new Student(1L, "Christopher"), + new Student(2L, "Leon Hunter") + + + }; + Double numberOfHours=128.0; + Double expected=numberOfHours/learners.length; + + //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); + //Assert.assertEquals(numberOfHoursToTeach, totalStudyTime); + + } + + + } + } 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..df26f37 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,19 @@ +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() { + Instructors instructors = Instructors.getInstance(); + List expectedNames = Arrays.asList("Leon Hunter", "Haseeb Muhammad"); + for(Instructor instructor : instructors) { + Assert.assertTrue(expectedNames.contains(instructor.getName())); + } + } +} \ 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..6708106 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,64 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + @Test + public void testAdd() { + // given + People people = Instructors.getInstance(); + Person person1 = new Person(); + + Assert.assertFalse(people.contains(person1)); + + // when + people.add(person1); + + // then + Assert.assertTrue(people.contains(person1)); + } + + @Test + public void testRemove() { + // given + People people = Instructors.getInstance(); + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + + people.add(person1); + people.add(person2); + people.add(person3); + + Assert.assertTrue(people.contains(person1)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + + // when + people.remove(person1); + people.remove(person2); + people.remove(person3); + + // then + Assert.assertFalse(people.contains(person1)); + Assert.assertFalse(people.contains(person2)); + Assert.assertFalse(people.contains(person3)); + } + + @Test + public void testFindById() { + // given + People people = Instructors.getInstance(); + people.removeAll(); + Person expected = new Person(0L, null); + people.add(expected); + Assert.assertTrue(people.contains(expected)); + + // when + Person actual = people.findById(expected.getId()); + + // then + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..ddb9d85 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,29 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor(){ + + //given + Long expectedId=0L; + String expectedName="Some name"; + + + + //when + //Giving the object we want to test, test when the constructor is called + Person person = new Person(expectedId, expectedName); + //Checking if actual output is same as expected + String actualName=person.getName(); + Long actualId=person.getId(); + + + //then + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } } 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..9ccf33e --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,55 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + @Test + public void testImplementation(){ + + //given + Student student=new Student(null, null); + + + + //when + Boolean result=student instanceof Learner; + + + //then + Assert.assertTrue(result); + } + + @Test + public void testInheritance(){ + + + //given + Student student=new Student(null, null); + + + //when + Boolean result=student instanceof Person; + + //then + Assert.assertTrue(result); + } + + @Test + public void testLearn(){ + + //given + Student student=new Student(null, null); + Double numberOfHoursToLearn=98.0; + Double preStudyTime=student.getTotalStudyTime(); + Double expected=preStudyTime+numberOfHoursToLearn; + + //when + student.learn(numberOfHoursToLearn); + Double actual=student.getTotalStudyTime(); + + //then + Assert.assertEquals(expected, actual); + } + +} 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..75eaf17 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,26 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class TestStudents { + @Test + public void test() { + Students students = Students.getInstance(); + String[] studentNameArray = {"Julia", "David Y", "Ghasan"}; + List studentNameList = Arrays.asList(studentNameArray); + + 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); + Assert.assertTrue(hasPersonWithName); + } + } +} \ No newline at end of file