From b4819bf0ab32d23f805d17a9b91a1da9689c31ad Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Mon, 20 Jul 2020 21:11:42 -0400 Subject: [PATCH 1/8] completed Parts 1.1, 1.0, 2.0, 3.1, 3.0 --- .../java/com/github/curriculeon/Learner.java | 9 ++++ .../java/com/github/curriculeon/Person.java | 25 ++++++++++ .../java/com/github/curriculeon/Student.java | 31 ++++++++++++ .../com/github/curriculeon/LearnerTest.java | 7 +++ .../com/github/curriculeon/PersonTest.java | 40 ++++++++++++++++ .../com/github/curriculeon/StudentTest.java | 48 +++++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 5 -- 7 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Learner.java create mode 100644 src/main/java/com/github/curriculeon/Student.java create mode 100644 src/test/java/com/github/curriculeon/LearnerTest.java create mode 100644 src/test/java/com/github/curriculeon/PersonTest.java create mode 100644 src/test/java/com/github/curriculeon/StudentTest.java delete mode 100644 src/test/java/com/github/curriculeon/TestPerson.java 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/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..1c14c8d 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..947bc12 --- /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/test/java/com/github/curriculeon/LearnerTest.java b/src/test/java/com/github/curriculeon/LearnerTest.java new file mode 100644 index 0000000..b726eaf --- /dev/null +++ b/src/test/java/com/github/curriculeon/LearnerTest.java @@ -0,0 +1,7 @@ +package com.github.curriculeon; + +import junit.framework.TestCase; + +public class LearnerTest extends TestCase { + +} \ No newline at end of file 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..2486e14 --- /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 { + + public 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); + } + + public void testSetName(String expectedName) { + + Person person = new Person(); + + person.setName(expectedName); + String actualName = person.getName(); + + Assert.assertEquals(actualName, expectedName); + Assert.assertEquals(actualName, expectedName); + } + + @Test + public void testConst0() { + testConstructor(555668888, "Ghassan Nasr"); + } + + @Test + public void testSetName0() { + testSetName("Ghassan nasr"); + } + +} 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..5082d59 --- /dev/null +++ b/src/test/java/com/github/curriculeon/StudentTest.java @@ -0,0 +1,48 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class StudentTest { + + public Boolean testImplementation() { + + Student student = new Student(); + return (student instanceof Learner); + } + + public Boolean testInheritance() { + + Student student = new Student(); + return (student instanceof Person); + } + + public void testLearn(Double expectedTotalStudyTime, Double actualNumberOfHours, Student student) { + student.learn(actualNumberOfHours); + Double actualTotalStudyTime = student.getTotalStudyTime(); + Assert.assertEquals(actualTotalStudyTime, expectedTotalStudyTime); + } + + @Test + public void testLearn0() { + Student student = new Student(); + testLearn(7D, 7D, student); + } + + @Test + public void testLearn1() { + Student student = new Student(5D); + testLearn(11D, 6D, student); + } + + @Test + public void testImplementation0() { + testImplementation(); + } + + @Test + public void testInheritance0() { + testInheritance(); + } + +} 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 { - -} From a4bfda236e164dc0303eb2ecf9d9ea9acbd6471f Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Tue, 21 Jul 2020 12:53:42 -0400 Subject: [PATCH 2/8] completed parts 4.0, 5.1, 5.0 --- .../com/github/curriculeon/Instructor.java | 19 +++++ .../java/com/github/curriculeon/Teacher.java | 8 +++ .../github/curriculeon/InstructorTest.java | 71 +++++++++++++++++++ .../com/github/curriculeon/LearnerTest.java | 7 -- 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Instructor.java create mode 100644 src/main/java/com/github/curriculeon/Teacher.java create mode 100644 src/test/java/com/github/curriculeon/InstructorTest.java delete mode 100644 src/test/java/com/github/curriculeon/LearnerTest.java 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..9d77338 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,19 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher { + + @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/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/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/LearnerTest.java b/src/test/java/com/github/curriculeon/LearnerTest.java deleted file mode 100644 index b726eaf..0000000 --- a/src/test/java/com/github/curriculeon/LearnerTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.curriculeon; - -import junit.framework.TestCase; - -public class LearnerTest extends TestCase { - -} \ No newline at end of file From 63681b6583c5404e58a7b2e0a8796609010fb6c0 Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Tue, 21 Jul 2020 15:31:50 -0400 Subject: [PATCH 3/8] made the test methods more readable and simpler --- .../com/github/curriculeon/PersonTest.java | 28 +++++----- .../com/github/curriculeon/StudentTest.java | 55 +++++++++---------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/test/java/com/github/curriculeon/PersonTest.java b/src/test/java/com/github/curriculeon/PersonTest.java index 2486e14..16786c6 100644 --- a/src/test/java/com/github/curriculeon/PersonTest.java +++ b/src/test/java/com/github/curriculeon/PersonTest.java @@ -5,36 +5,36 @@ public class PersonTest { - public void testConstructor(long expectedId, String expectedName) { + @Test + public void testConstructor() { + //given + Long expectedId = 5556668888L; + String expectedName = "Tom Jones"; Person person = new Person(expectedId, expectedName); - long actualId = person.getId(); + //when + Long actualId = person.getId(); String actualName = person.getName(); + //then Assert.assertEquals(actualId, expectedId); Assert.assertEquals(actualName, expectedName); } - public void testSetName(String 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); } - - @Test - public void testConst0() { - testConstructor(555668888, "Ghassan Nasr"); - } - - @Test - public void testSetName0() { - testSetName("Ghassan nasr"); - } - } diff --git a/src/test/java/com/github/curriculeon/StudentTest.java b/src/test/java/com/github/curriculeon/StudentTest.java index 5082d59..58871d5 100644 --- a/src/test/java/com/github/curriculeon/StudentTest.java +++ b/src/test/java/com/github/curriculeon/StudentTest.java @@ -5,44 +5,43 @@ public class StudentTest { - public Boolean testImplementation() { + @Test + public void testImplementation() { + //given + Learner student = new Student(); + + //when + Boolean result = student instanceof Person; - Student student = new Student(); - return (student instanceof Learner); + //then + Assert.assertTrue(result); } - public Boolean testInheritance() { + @Test + public void testInheritance() { - Student student = new Student(); - return (student instanceof Person); - } + //given + Learner student = new Student(); - public void testLearn(Double expectedTotalStudyTime, Double actualNumberOfHours, Student student) { - student.learn(actualNumberOfHours); - Double actualTotalStudyTime = student.getTotalStudyTime(); - Assert.assertEquals(actualTotalStudyTime, expectedTotalStudyTime); - } + //when + Boolean result = student instanceof Person; - @Test - public void testLearn0() { - Student student = new Student(); - testLearn(7D, 7D, student); + //then + Assert.assertTrue(result); } @Test - public void testLearn1() { - Student student = new Student(5D); - testLearn(11D, 6D, student); - } + public void testLearn() { + //given + Learner student = new Student(); + Double actualNumberOfHours = 98D; - @Test - public void testImplementation0() { - testImplementation(); - } + //when + student.learn(actualNumberOfHours); + Double actualTotalStudyTime = student.getTotalStudyTime(); - @Test - public void testInheritance0() { - testInheritance(); + //then + Double expectedTotalStudyTime = 98D; + Assert.assertEquals(actualTotalStudyTime, expectedTotalStudyTime); } - } From 628d40788ff2f3d1cf84b5e0fbe214e52ed854bd Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Wed, 22 Jul 2020 19:27:24 -0400 Subject: [PATCH 4/8] completed 6.1 and 6.0 --- .../java/com/github/curriculeon/People.java | 58 ++++++++++++++ .../com/github/curriculeon/PeopleTest.java | 77 +++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/People.java create mode 100644 src/test/java/com/github/curriculeon/PeopleTest.java 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..7e64369 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,58 @@ +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 class People implements Iterable { + + private List personList = new ArrayList<>(); + + public People() { + } + + public void add(Person person) { + 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 removeAll() { + personList.clear(); + } + + public int count() { + return personList.size(); + } + + public void toArray() { + personList.toArray(); + } + + @Override + public Iterator iterator() { + return personList.listIterator(); + //return new PersonIterator(personList.iterator()); + } + + + + +} 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..024a48c --- /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 = new People(); + 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 = new People(); + 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 = new People(); + 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); + } +} + From 2d32e1e98abe43018681da3604bc386808091db9 Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Thu, 23 Jul 2020 21:26:11 -0400 Subject: [PATCH 5/8] finished part 7, singleton class, consulted several sources and feel I have a better grip on the design pattern --- .../java/com/github/curriculeon/People.java | 6 ++-- .../java/com/github/curriculeon/Person.java | 2 +- .../java/com/github/curriculeon/Student.java | 2 +- .../java/com/github/curriculeon/Students.java | 32 +++++++++++++++++++ .../com/github/curriculeon/StudentsTest.java | 31 ++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Students.java create mode 100644 src/test/java/com/github/curriculeon/StudentsTest.java diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 7e64369..db1f5a8 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -6,9 +6,9 @@ import java.util.ListIterator; import java.lang.Iterable; -public class People implements Iterable { +public class People implements Iterable { - private List personList = new ArrayList<>(); + List personList = new ArrayList<>(); public People() { } @@ -17,7 +17,7 @@ public void add(Person person) { personList.add(person); } - public Person findById(Long id) { + public Person findById(long id) { for(Person person: personList) { if(person.getId() == id) { return person; diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 1c14c8d..8667275 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -2,7 +2,7 @@ public class Person { - private final Long id; + private final long id; private String name; public Person() { diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java index 947bc12..8aba9fb 100644 --- a/src/main/java/com/github/curriculeon/Student.java +++ b/src/main/java/com/github/curriculeon/Student.java @@ -9,7 +9,7 @@ public Student() { this.totalStudyTime = 0D; } - public Student(Long id, String name, Double totalStudyTime) { + public Student(long id, String name, Double totalStudyTime) { super(id, name); this.totalStudyTime = 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..4c4a02a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,32 @@ +package com.github.curriculeon; + +public class Students extends People { + + private static Students INSTANCE; + + private static People cohort; + + private Students() { + + } + + public static Students getINSTANCE() { + if(INSTANCE == null) { //lazy loading, as opposed to eager loading + INSTANCE = new Students(); + } + return INSTANCE; + } + + public People getCohort() { + if(cohort == null) { // lazy loading + cohort = new People(); + Student student1 = new Student(555, "Molly Fisher", 0D); + Student student2 = new Student(777, "Holly Becker", 0D); + Student student3 = new Student(999, "Brad Singer", 0D); + cohort.add(student1); + cohort.add(student2); + cohort.add(student3); + } + return cohort; + } +} 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..1fd45f7 --- /dev/null +++ b/src/test/java/com/github/curriculeon/StudentsTest.java @@ -0,0 +1,31 @@ +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 instance = Students.getINSTANCE(); + People cohort = instance.getCohort(); + + 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); + } + +} From 70aed703f818543dfdaf4ccf37ef2ac2b814365f Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Thu, 23 Jul 2020 22:47:28 -0400 Subject: [PATCH 6/8] completed part 8, added an Instructor constructor that gives an Instructor and id and a name by calling the Person constructor, since in the the singleton instructors exist as a List of Persons --- .../com/github/curriculeon/Instructor.java | 7 +++++ .../com/github/curriculeon/Instructors.java | 31 +++++++++++++++++++ .../github/curriculeon/InstructorsTest.java | 28 +++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Instructors.java create mode 100644 src/test/java/com/github/curriculeon/InstructorsTest.java diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java index 9d77338..ea60c48 100644 --- a/src/main/java/com/github/curriculeon/Instructor.java +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -2,6 +2,13 @@ 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); 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..b27a934 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,31 @@ +package com.github.curriculeon; + +public class Instructors extends People { + + private static Instructors INSTANCE; + + private static People instructors; + + private Instructors() { + + } + + public static Instructors getINSTANCE() { + if(INSTANCE == null) { //lazy loading, as opposed to eager loading + INSTANCE = new Instructors(); + } + return INSTANCE; + } + + public People getInstructors() { + if(instructors == null) { // lazy loading + instructors = new People(); + Instructor instructor1 = new Instructor(444, "Lincoln Barnes"); + instructors.add(instructor1); + //instructors.add(student2); + //instructors.add(student3); + } + return instructors; + } +} + 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..6a23fcd --- /dev/null +++ b/src/test/java/com/github/curriculeon/InstructorsTest.java @@ -0,0 +1,28 @@ +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 instance = Instructors.getINSTANCE(); + People instructors = instance.getInstructors(); + + String actualInstructorName1 = instructors.findById(444).getName(); + + //then + Assert.assertEquals(expectedInstructorName1, actualInstructorName1); + } + +} From 316903635d72cabed07d4e93af8a1a19f9ae9f8e Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Tue, 28 Jul 2020 23:22:04 -0400 Subject: [PATCH 7/8] made important changes to Students and Instructors singletons (parts 7 and 8), and implemented and tested Classroom (part 9) --- .../com/github/curriculeon/Classroom.java | 36 ++++++++++++++++ .../com/github/curriculeon/Instructors.java | 17 +------- .../java/com/github/curriculeon/People.java | 5 +-- .../java/com/github/curriculeon/Students.java | 26 ++++------- .../com/github/curriculeon/ClassroomTest.java | 43 +++++++++++++++++++ .../github/curriculeon/InstructorsTest.java | 3 +- .../com/github/curriculeon/StudentsTest.java | 3 +- 7 files changed, 92 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Classroom.java create mode 100644 src/test/java/com/github/curriculeon/ClassroomTest.java 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..ce37208 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,36 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.Iterator; + +public class Classroom { + + People students = Students.getINSTANCE(); + People instructors = Instructors.getINSTANCE(); + + public void hostLecture(Teacher teacher, Double numberOfHours) { + Person[] studentArray = new Student[students.count()]; + students.personList.toArray(studentArray); + teacher.lecture((Learner[])studentArray, 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(); + Iterator cohortIterator = students.iterator(); + while(cohortIterator.hasNext()) { + Student thisStudent = (Student)(cohortIterator.next()); + studentMap.put(thisStudent, thisStudent.getTotalStudyTime()); + } + return studentMap; + } +} diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index b27a934..9a6e097 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -4,28 +4,15 @@ public class Instructors extends People { private static Instructors INSTANCE; - private static People instructors; - - private Instructors() { - - } + 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 People getInstructors() { - if(instructors == null) { // lazy loading - instructors = new People(); - Instructor instructor1 = new Instructor(444, "Lincoln Barnes"); - instructors.add(instructor1); - //instructors.add(student2); - //instructors.add(student3); - } - return instructors; - } } diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index db1f5a8..d9ee532 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -42,9 +42,7 @@ public int count() { return personList.size(); } - public void toArray() { - personList.toArray(); - } + // public void toArray() { personList.toArray(); } @Override public Iterator iterator() { @@ -54,5 +52,4 @@ public Iterator iterator() { - } diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java index 4c4a02a..fd9cec0 100644 --- a/src/main/java/com/github/curriculeon/Students.java +++ b/src/main/java/com/github/curriculeon/Students.java @@ -1,32 +1,22 @@ package com.github.curriculeon; +import java.util.List; + public class Students extends People { private static Students INSTANCE; - private static People cohort; - - private Students() { - - } + private Students() { } public static Students getINSTANCE() { if(INSTANCE == null) { //lazy loading, as opposed to eager loading INSTANCE = new Students(); + INSTANCE.add(new Student(555, "Molly Fisher", 0D)); + INSTANCE.add(new Student(777, "Holly Becker", 0D)); + INSTANCE.add(new Student(999, "Brad Singer", 0D)); } return INSTANCE; } - public People getCohort() { - if(cohort == null) { // lazy loading - cohort = new People(); - Student student1 = new Student(555, "Molly Fisher", 0D); - Student student2 = new Student(777, "Holly Becker", 0D); - Student student3 = new Student(999, "Brad Singer", 0D); - cohort.add(student1); - cohort.add(student2); - cohort.add(student3); - } - return cohort; - } -} + +} \ No newline at end of file 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..d2ffdd7 --- /dev/null +++ b/src/test/java/com/github/curriculeon/ClassroomTest.java @@ -0,0 +1,43 @@ +package com.github.curriculeon; + +import org.junit.Test; +import org.junit.Assert; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class ClassroomTest { + + @Test + public void testHostLecture1() { + //given + double numHoursTaught = 21D; + Classroom classroom = new Classroom(); + Map studyMapBefore = (HashMap)(classroom.getStudyMap()); + Double[] expectedHours = new Double[studyMapBefore.size()]; + int studentCounter = 0; + for ( Map.Entry entry : studyMapBefore.entrySet()) { + expectedHours[studentCounter] = entry.getValue() + numHoursTaught / studyMapBefore.size(); + studentCounter++; + } + + //when + //get the first instructor's id of the instructors in the classroom + long instructorId = 444; + + classroom.hostLecture(instructorId, numHoursTaught); + + Map studyMapAfter = (HashMap)(classroom.getStudyMap()); + Double[] actualHours = new Double[studyMapAfter.size()]; + studentCounter = 0; + for ( Map.Entry entry : studyMapAfter.entrySet()) { + actualHours[studentCounter] = entry.getValue(); + studentCounter++; + } + + //then + Assert.assertArrayEquals(actualHours, expectedHours); + + } +} diff --git a/src/test/java/com/github/curriculeon/InstructorsTest.java b/src/test/java/com/github/curriculeon/InstructorsTest.java index 6a23fcd..4df284d 100644 --- a/src/test/java/com/github/curriculeon/InstructorsTest.java +++ b/src/test/java/com/github/curriculeon/InstructorsTest.java @@ -16,8 +16,7 @@ public void test() { String expectedInstructorName1 = "Lincoln Barnes"; //when - Instructors instance = Instructors.getINSTANCE(); - People instructors = instance.getInstructors(); + Instructors instructors = Instructors.getINSTANCE(); String actualInstructorName1 = instructors.findById(444).getName(); diff --git a/src/test/java/com/github/curriculeon/StudentsTest.java b/src/test/java/com/github/curriculeon/StudentsTest.java index 1fd45f7..b54698e 100644 --- a/src/test/java/com/github/curriculeon/StudentsTest.java +++ b/src/test/java/com/github/curriculeon/StudentsTest.java @@ -15,8 +15,7 @@ public void test() { String expectedStudentName3 = "Brad Singer"; //when - Students instance = Students.getINSTANCE(); - People cohort = instance.getCohort(); + Students cohort = Students.getINSTANCE(); String actualStudentName1 = cohort.findById(555).getName(); String actualStudentName2 = cohort.findById(777).getName(); From 42a5092ccac4a3c15b41f15a3979224b70cddca6 Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Sat, 1 Aug 2020 14:51:39 -0400 Subject: [PATCH 8/8] completed part 10 --- .../com/github/curriculeon/Classroom.java | 22 ++++----- .../com/github/curriculeon/Instructors.java | 11 ++++- .../java/com/github/curriculeon/People.java | 24 ++++++---- .../java/com/github/curriculeon/Students.java | 23 +++++---- .../com/github/curriculeon/ClassroomTest.java | 47 ++++++++----------- .../com/github/curriculeon/PeopleTest.java | 6 +-- 6 files changed, 69 insertions(+), 64 deletions(-) diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java index ce37208..5eba099 100644 --- a/src/main/java/com/github/curriculeon/Classroom.java +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -1,20 +1,16 @@ package com.github.curriculeon; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.HashMap; -import java.util.Iterator; -public class Classroom { +public enum Classroom { - People students = Students.getINSTANCE(); - People instructors = Instructors.getINSTANCE(); + INSTANCE; + private Students students = Students.getINSTANCE(); + private Instructors instructors = Instructors.getINSTANCE(); public void hostLecture(Teacher teacher, Double numberOfHours) { - Person[] studentArray = new Student[students.count()]; - students.personList.toArray(studentArray); - teacher.lecture((Learner[])studentArray, numberOfHours); + teacher.lecture(students.getArray(), numberOfHours); } public void hostLecture(long id, Double numberOfHours) { @@ -25,11 +21,9 @@ public void hostLecture(long id, Double numberOfHours) { } public Map getStudyMap() { - Map studentMap = new HashMap(); - Iterator cohortIterator = students.iterator(); - while(cohortIterator.hasNext()) { - Student thisStudent = (Student)(cohortIterator.next()); - studentMap.put(thisStudent, thisStudent.getTotalStudyTime()); + 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/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index 9a6e097..e6d8278 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -1,6 +1,8 @@ package com.github.curriculeon; -public class Instructors extends People { +import java.util.List; + +public class Instructors extends People { private static Instructors INSTANCE; @@ -14,5 +16,12 @@ public static Instructors getINSTANCE() { 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/People.java b/src/main/java/com/github/curriculeon/People.java index d9ee532..b2f0be2 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -6,19 +6,23 @@ import java.util.ListIterator; import java.lang.Iterable; -public class People implements Iterable { +public abstract class People implements Iterable { - List personList = new ArrayList<>(); + List personList; public People() { + this(new ArrayList<>()); } - public void add(Person person) { - personList.add(person); + public People(List personList) { + this.personList = personList; + } + public void add(E person) { + this.personList.add(person); } - public Person findById(long id) { - for(Person person: personList) { + public E findById(long id) { + for(E person: personList) { if(person.getId() == id) { return person; } @@ -26,11 +30,11 @@ public Person findById(long id) { return null; } - public Boolean contains(Person person) { + public Boolean contains(E person) { return personList.contains(person); } - public void remove(Person person) { + public void remove(E person) { personList.remove(person); } @@ -42,10 +46,10 @@ public int count() { return personList.size(); } - // public void toArray() { personList.toArray(); } + public abstract E[] getArray(); @Override - public Iterator iterator() { + public Iterator iterator() { return personList.listIterator(); //return new PersonIterator(personList.iterator()); } diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java index fd9cec0..9a112d9 100644 --- a/src/main/java/com/github/curriculeon/Students.java +++ b/src/main/java/com/github/curriculeon/Students.java @@ -2,21 +2,26 @@ import java.util.List; -public class Students extends People { +public class Students extends People { - private static Students INSTANCE; + private static Students INSTANCE = new Students(); - private 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() { - if(INSTANCE == null) { //lazy loading, as opposed to eager loading - INSTANCE = new Students(); - INSTANCE.add(new Student(555, "Molly Fisher", 0D)); - INSTANCE.add(new Student(777, "Holly Becker", 0D)); - INSTANCE.add(new Student(999, "Brad Singer", 0D)); - } 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/test/java/com/github/curriculeon/ClassroomTest.java b/src/test/java/com/github/curriculeon/ClassroomTest.java index d2ffdd7..e6bf992 100644 --- a/src/test/java/com/github/curriculeon/ClassroomTest.java +++ b/src/test/java/com/github/curriculeon/ClassroomTest.java @@ -6,38 +6,31 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.Set; + public class ClassroomTest { @Test public void testHostLecture1() { - //given - double numHoursTaught = 21D; - Classroom classroom = new Classroom(); - Map studyMapBefore = (HashMap)(classroom.getStudyMap()); - Double[] expectedHours = new Double[studyMapBefore.size()]; - int studentCounter = 0; - for ( Map.Entry entry : studyMapBefore.entrySet()) { - expectedHours[studentCounter] = entry.getValue() + numHoursTaught / studyMapBefore.size(); - studentCounter++; + 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); } - - //when - //get the first instructor's id of the instructors in the classroom - long instructorId = 444; - - classroom.hostLecture(instructorId, numHoursTaught); - - Map studyMapAfter = (HashMap)(classroom.getStudyMap()); - Double[] actualHours = new Double[studyMapAfter.size()]; - studentCounter = 0; - for ( Map.Entry entry : studyMapAfter.entrySet()) { - actualHours[studentCounter] = entry.getValue(); - studentCounter++; - } - - //then - Assert.assertArrayEquals(actualHours, expectedHours); - } } diff --git a/src/test/java/com/github/curriculeon/PeopleTest.java b/src/test/java/com/github/curriculeon/PeopleTest.java index 024a48c..2a06072 100644 --- a/src/test/java/com/github/curriculeon/PeopleTest.java +++ b/src/test/java/com/github/curriculeon/PeopleTest.java @@ -13,7 +13,7 @@ public void testAdd() { Person expectedPerson3 = new Person(5555L, "Sarah Kraut"); //when - People personList = new People(); + People personList = Instructors.getINSTANCE(); personList.add(expectedPerson1); personList.add(expectedPerson2); personList.add(expectedPerson3); @@ -31,7 +31,7 @@ public void testRemove() { Person Person2 = new Person(7777L, "Cindy Weatherspoon"); Person Person3 = new Person(5555L, "Sarah Kraut"); - People personList = new People(); + People personList = Instructors.getINSTANCE(); personList.add(Person1); personList.add(Person2); personList.add(Person3); @@ -58,7 +58,7 @@ public void testFindById () { Person expectedPerson2 = new Person(7777L, "Cindy Weatherspoon"); Person expectedPerson3 = new Person(5555L, "Sarah Kraut"); - People personList = new People(); + People personList = Students.getINSTANCE(); personList.add(expectedPerson1); personList.add(expectedPerson2); personList.add(expectedPerson3);