From 91c3b8c4f75f9c8322a9418b6deb51a8229660a6 Mon Sep 17 00:00:00 2001 From: dmholland Date: Mon, 20 Jul 2020 14:29:00 -0400 Subject: [PATCH 1/7] This is part 1 --- .../java/com/github/curriculeon/Person.java | 19 ++++++++++++ .../github/curriculeon/ConstructorTest.java | 29 +++++++++++++++++++ .../com/github/curriculeon/SetNameTest.java | 26 +++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 5 ---- 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/github/curriculeon/ConstructorTest.java create mode 100644 src/test/java/com/github/curriculeon/SetNameTest.java delete mode 100644 src/test/java/com/github/curriculeon/TestPerson.java diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..e2bc2b7 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,24 @@ package com.github.curriculeon; public class Person { + final long id; + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + String name; + Person(long id, String name){ + + this.id = id; + this.name=name; + } } diff --git a/src/test/java/com/github/curriculeon/ConstructorTest.java b/src/test/java/com/github/curriculeon/ConstructorTest.java new file mode 100644 index 0000000..e1824a2 --- /dev/null +++ b/src/test/java/com/github/curriculeon/ConstructorTest.java @@ -0,0 +1,29 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class ConstructorTest { + + + private void test(String expected) { + // Given + Person person = new Person(2,"Bojangles"); + // When + String actual = person.getName(); + + // Then + Assert.assertEquals(expected, actual); + } + + + @Test + public void test1() { + test("Bojangles"); + } + + + } + + + diff --git a/src/test/java/com/github/curriculeon/SetNameTest.java b/src/test/java/com/github/curriculeon/SetNameTest.java new file mode 100644 index 0000000..d3430d2 --- /dev/null +++ b/src/test/java/com/github/curriculeon/SetNameTest.java @@ -0,0 +1,26 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class SetNameTest { + private void test(String expected) { + // Given + Person person = new Person(2,"Bojangles"); + // When + person.setName("Wendys"); + String actual=person.getName(); + + // Then + Assert.assertEquals(expected, actual); + } + + + @Test + public void test1() { + test("Wendys"); + } + + +} + 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 fcb15dfd35308947c47e12b4ce994e4e2b01333a Mon Sep 17 00:00:00 2001 From: dmholland Date: Tue, 21 Jul 2020 17:18:03 -0400 Subject: [PATCH 2/7] Done all the way to 6 --- .../com/github/curriculeon/Instructor.java | 33 ++++++ .../java/com/github/curriculeon/Learner.java | 7 ++ .../java/com/github/curriculeon/People.java | 79 +++++++++++++ .../java/com/github/curriculeon/Person.java | 2 +- .../java/com/github/curriculeon/Student.java | 28 +++++ .../java/com/github/curriculeon/Teacher.java | 25 +++++ .../github/curriculeon/ConstructorTest.java | 28 ++--- .../github/curriculeon/InstructorTest.java | 104 ++++++++++++++++++ .../com/github/curriculeon/PeopleTest.java | 78 +++++++++++++ .../com/github/curriculeon/SetNameTest.java | 4 +- .../com/github/curriculeon/StudentTest.java | 66 +++++++++++ 11 files changed, 437 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Instructor.java create mode 100644 src/main/java/com/github/curriculeon/Learner.java create mode 100644 src/main/java/com/github/curriculeon/People.java create mode 100644 src/main/java/com/github/curriculeon/Student.java create mode 100644 src/main/java/com/github/curriculeon/Teacher.java create mode 100644 src/test/java/com/github/curriculeon/InstructorTest.java create mode 100644 src/test/java/com/github/curriculeon/PeopleTest.java create mode 100644 src/test/java/com/github/curriculeon/StudentTest.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..8219f8e --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,33 @@ +package com.github.curriculeon; + +/* +Create an Instructor class such that: +Instructor is a subclass of Person +Instructor implements the Teacher interface +Instructor should have a concrete implementation of the teach method which invokes the learn method on the specified Learner object. +Instructor should have a concrete implementation of the lecture method, + which invokes the learn method on each of the elements in the specified array of Learner objects. +numberOfHours should be evenly split amongst the learners. +double numberOfHoursPerLearner = numberOfHours / learners.length; + + */ +public class Instructor extends Person implements Teacher { + Double numberOfHoursPerLearner; + + 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) { + this.numberOfHoursPerLearner = numberOfHours / learners.length; + for (Learner learner : learners) {//I got this from Chris fulton + this.teach(learner, numberOfHours); + } + } +} 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..661dd43 --- /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 numOfHours); + 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..d3f5fc5 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,79 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +/* +The class should instantiate a List field of Person objects named personList. +The class should define a method named add which adds a Person to the personList. +The class should define a method named findById which makes use of a long id parameter to return a Person object with the respective id field. +The class should define a named contains which makes use of a Person person parameter + to return true if the personList contains the respective Person object. +The class should define a method named remove which makes use of a Person person parameter to remove a respective Person object. +The class should define a method named remove which makes use of a long id parameter to remove a Person object with the respective id field. +The class should define a named removeAll which clears our personList field. +The class should define a method named count which returns the size of personList. +The class should define a method named toArray which returns an array representation of the personList field. +The class should implement Iterable and define a method named iterator which makes use of the personList field to generate a new a Iterator. + */ +public class People implements Iterable { + ArrayList personList; + + public void add(Person person) { + this.personList.add(person); + } + + People(ArrayList people){ + this.personList=people; + } + + public Boolean findByID(Long id) { + for (Person person : this.personList) { + if (id == person.getId()) return true; + } + return false; + } + + public Boolean removePerson(Person person) { + for (Person p : this.personList) { + if (person.equals(p)) this.personList.remove(p); + return true; + } + return false; + + } + + public Boolean removePerson(Long id) { + for (Person p : this.personList) { + if (id.equals(p)) this.personList.remove(p); + return true; + } + return false; + + } + + public void removeAll() { + for (Person p : this.personList) this.personList.remove(p); + } + + public Integer sizeList() { + return this.personList.size(); + } + + public Person[] toArr() { + Person[] p = new Person[this.personList.size() - 1]; + for (int i = 0; i < p.length; i++) { + p[i] = this.personList.get(i + 1); + } + return p; + } + + @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 e2bc2b7..ea8a517 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,7 +1,7 @@ package com.github.curriculeon; public class Person { - final long id; + final Long id; public long getId() { return id; } 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..3f5c5b6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +/* +Create a Student class such that: +Student is a subclass of Person +Student implements the Learner interface +Student should have an instance variable totalStudyTime of type double +Student should have a concrete implementation of the learn method which increments the totalStudyTime variable by the specified numberOfHours argument. +Student should have a getTotalStudyTime() method which returns the totalStudyTime instance variable. + */ + +public class Student extends Person implements Learner { +Double totalStudyTime=0.0; + + Student(long id, String name) { + super(id, name); + } + + @Override + public void learn(Double numOfHours) { + this.totalStudyTime+=numOfHours; + } + + @Override + public Double getTotalStudyTime() { + return this.totalStudyTime; + } +} 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..5d58a23 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,25 @@ +package com.github.curriculeon; +/* +Create a Teacher interface. +Teacher should declare a teach method signature: + +Method name: teach +Method parameters: +Learner learner +double numberOfHours +Method return-type: void +Teacher should declare a lecture method signature: + +Method name: lecture +Method parameters: +Learner[] learners +double numberOfHours +Method return-type: void + + */ +public interface Teacher { + void teach(Learner learner, Double numberOfHours); +void lecture(Learner[] learners, Double numberOfHours); + +} + diff --git a/src/test/java/com/github/curriculeon/ConstructorTest.java b/src/test/java/com/github/curriculeon/ConstructorTest.java index e1824a2..ad6bccd 100644 --- a/src/test/java/com/github/curriculeon/ConstructorTest.java +++ b/src/test/java/com/github/curriculeon/ConstructorTest.java @@ -6,24 +6,24 @@ public class ConstructorTest { - private void test(String expected) { - // Given - Person person = new Person(2,"Bojangles"); - // When - String actual = person.getName(); - - // Then - Assert.assertEquals(expected, actual); - } + private void test(String expected) { + // Given + Person person = new Person(2,"Bojangles"); + // When + String actual = person.getName(); + + // Then + Assert.assertEquals(expected, actual); + } - @Test - public void test1() { - test("Bojangles"); - } + @Test + public void test1() { + test("Bojangles"); + } - } +} 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..9cb2e27 --- /dev/null +++ b/src/test/java/com/github/curriculeon/InstructorTest.java @@ -0,0 +1,104 @@ +package com.github.curriculeon; +/* +Create a TestInstructor class. +Create a testImplementation method that asserts that an Instructor is an instanceof a Teacher. +Create a testInheritance method that asserts that a Instructor is an instanceof a Person. +Create a testTeach method that ensures when an Instructor invokes the teach method, +a respective student's totalStudyTime instance variable is incremented by the specified numberOfHours. +Create a testLecture method that ensures when an Instructor invokes the lecture method, + a respective array of students' totalStudyTime instance variables is incremented by numberOfHours/students.length. + */ + +import org.junit.Assert; +import org.junit.Test; + +public class InstructorTest { + private void testImplementation(boolean tru) { + // Given + Teacher instructor = new Instructor(2,"B.M.Quickly"); + + + // Then + Assert.assertTrue(instructor instanceof Instructor); + + } + + private void testInheritance(boolean tru) { + // Given + Person student = new Student(2,"B.M.Quickly"); + + + // Then + Assert.assertTrue(student instanceof Person); + + } + + private void testTeach(boolean tru) { + // Given + Teacher instructor = new Instructor(2,"B.M.Quickly"); + Learner student = new Student(1,"Carlos"); + Double expected= 4.0; + + //When + instructor.teach(student,expected); + Double actual=student.getTotalStudyTime(); + + + // Then + + Assert.assertEquals(actual,expected); + } + + private void testLecture(boolean tru) { + // Given + Teacher instructor = new Instructor(2,"B.M.Quickly"); + Learner student1 = new Student(1,"Carlos"); + Learner student2 = new Student(20,"James"); + Learner student3 = new Student(21,"Smasher"); + Learner student4 = new Student(22,"Jesse"); + Learner[] learnerz = new Learner[]{student1,student2,student3,student4}; + + Double expected= 4.0; + + //When + instructor.lecture(learnerz,expected); + Double actual1=student1.getTotalStudyTime(); + Double actual2=student2.getTotalStudyTime(); + Double actual3=student3.getTotalStudyTime(); + Double actual4=student4.getTotalStudyTime(); + + + // Then + + Assert.assertEquals(actual1,expected); + Assert.assertEquals(actual2,expected); + Assert.assertEquals(actual3,expected); + Assert.assertEquals(actual4,expected); + + } + + + @Test + public void test1() { + testImplementation(true); + } + + @Test + public void test2() + {testLecture(true);} + + @Test + public void test3( + ){ + testInheritance(true); + } + + @Test + public void test4( + ){ + testTeach(true); + } + + +} + 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..f9b4061 --- /dev/null +++ b/src/test/java/com/github/curriculeon/PeopleTest.java @@ -0,0 +1,78 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +/* +Create a TestPeople class. +Create a testAdd method which ensures that our personList in our People class populated with respective Person objects following invokation of the add method. +Create a testRemove method which ensures that the personList in a People object is depopulated with a respective Person object following the invokation of the remove method. +Create a testFindById method which ensures that a respective Person object with a respective id field is returned upon invokation of the findById method on a respective People object. + + */ +public class PeopleTest { + Person p1=new Person(1,"Tom"); + Person p2=new Person(2,"Larry"); + Person p3=new Person(3,"Gush"); + Person p4=new Person(4,"Dare"); + ArrayList pp =new ArrayList (Arrays.asList(p1,p2,p3)); + + + private void testAdd(boolean tru) { + + // Given + People people =new People(pp); + people.add(p4); + + + // Then + Assert.assertTrue(people.personList.contains(p4)); + + } + + private void testRemove(boolean tru) { + // Given + People people =new People(pp); + people.removePerson(1L); + + + // Then + Assert.assertTrue(people.personList.contains(p1)); + + } + + private void testFindByID(boolean tru) { + // Given + People people =new People(pp); + Boolean expected= true; + + //When + + Boolean actual=people.findByID(3L); + + + // Then + + Assert.assertEquals(actual,expected); + } + + + @Test + public void test1() { + testAdd(true); + } + + @Test + public void test2() + {testRemove(true);} + + @Test + public void test3( + ){ + testFindByID(true); + } + +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/SetNameTest.java b/src/test/java/com/github/curriculeon/SetNameTest.java index d3430d2..bdfe052 100644 --- a/src/test/java/com/github/curriculeon/SetNameTest.java +++ b/src/test/java/com/github/curriculeon/SetNameTest.java @@ -8,11 +8,11 @@ private void test(String expected) { // Given Person person = new Person(2,"Bojangles"); // When - person.setName("Wendys"); + person.setName(expected); String actual=person.getName(); // Then - Assert.assertEquals(expected, actual); + Assert.assertEquals(expected,actual); } 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..e3b7d24 --- /dev/null +++ b/src/test/java/com/github/curriculeon/StudentTest.java @@ -0,0 +1,66 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +/* +Create a TestStudent class. +Create a testImplementation method that asserts that a Student is an instanceof a Learner. +Create a testInheritance method that asserts that a Student is an instanceof a Person. +Create a testLearn method that ensures a Student's totalStudyTime instance variable is +incremented by the specified numberOfHours by invoking the .learn method. + */ +public class StudentTest { + + private void testImplementation(boolean tru) { + // Given + Learner student = new Student(2,"B.M.Quickly"); + + + // Then + Assert.assertTrue(student instanceof Learner); + + } + + private void testInheritance(boolean tru) { + // Given + Person student = new Student(2,"B.M.Quickly"); + + + // Then + Assert.assertTrue(student instanceof Person); + + } + + private void testLearn(boolean tru) { + // Given + Learner student = new Student(2,"B.M.Quickly"); + Double expected= 4.0; + + //When + student.learn(expected); + Double actual=student.getTotalStudyTime(); + + + // Then + + Assert.assertEquals(actual,expected); + } + + + @Test + public void test1() { + testImplementation(true); + } + + @Test + public void test2() + {testLearn(true);} + + @Test + public void test3( + ){ + testInheritance(true); + } + +} \ No newline at end of file From 3d19787b6deced3a0f1c6d896985df8fe2bc3a4a Mon Sep 17 00:00:00 2001 From: dmholland Date: Wed, 22 Jul 2020 22:04:51 -0400 Subject: [PATCH 3/7] Factory method added, could have done it better --- .../java/com/github/curriculeon/People.java | 7 ++- .../java/com/github/curriculeon/Students.java | 49 +++++++++++++++++++ .../com/github/curriculeon/StudentTest.java | 12 ++--- .../com/github/curriculeon/StudentsTest.java | 34 +++++++++++++ 4 files changed, 95 insertions(+), 7 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 d3f5fc5..46b2d3b 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -19,7 +19,12 @@ The class should implement Iterable and define a method named iterator which makes use of the personList field to generate a new a Iterator. */ public class People implements Iterable { - ArrayList personList; + ArrayList personList =new ArrayList(); + + public People() { + + } + public void add(Person person) { this.personList.add(person); 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..3aa91c3 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,49 @@ +package com.github.curriculeon; +/* +Part 7.1 - Create Students singleton +Note: The creation of this class will demonstrate an implementation of singleton design pattern. +Create a Students class. (Done) +The class should be an unextendable subclass of the People class. (Done with final) +The class should statically instantiate a final field named INSTANCE of type Students. +The class should define a private nullary constructor which populates the INSTANCE field with respective +Student representations of your colleagues. +Each student should have a relatively unique id field. +The class should define a getInstance method which returns the INSTANCE field. +Part 7.0 - Test Students singleton +Create a TestStudents class. +Create a test method which ensures that each of the students in your current cohort are in your Students singleton. + + */ + + +import java.util.ArrayList; + +public final class Students extends People { + final static Students instance = new Students(); + String [] cohort = new String[]{"David","Chris","Marcus","Leon","Mondira","Deepti","Steve","Yuru","Monica","Akila","Julia","David","Alonzo","Rachid","Emmanuel","Lionel","Solomon","People who don't speak"}; + private Students(){ + super(); + + for(Person person:peopleMaker(cohort)){ + this.personList.add(person); + } + } + + public static Students getInstance(){ + return instance; + } + + public ArrayList peopleMaker(String[] names){ + People people = new People(); + for(int i =0;i Date: Thu, 23 Jul 2020 17:14:23 -0400 Subject: [PATCH 4/7] Couldn't get the classroom test to pass --- .../com/github/curriculeon/Classroom.java | 43 +++++++++++++++++ .../com/github/curriculeon/Instructor.java | 4 +- .../com/github/curriculeon/Instructors.java | 48 +++++++++++++++++++ .../java/com/github/curriculeon/People.java | 6 +-- .../java/com/github/curriculeon/Person.java | 1 + .../java/com/github/curriculeon/Student.java | 5 +- .../java/com/github/curriculeon/Students.java | 30 +++++++++--- .../com/github/curriculeon/ClassroomTest.java | 34 +++++++++++++ .../github/curriculeon/InstructorsTest.java | 29 +++++++++++ .../com/github/curriculeon/PeopleTest.java | 5 +- 10 files changed, 188 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Classroom.java create mode 100644 src/main/java/com/github/curriculeon/Instructors.java create mode 100644 src/test/java/com/github/curriculeon/ClassroomTest.java create mode 100644 src/test/java/com/github/curriculeon/InstructorsTest.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..bfd1106 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,43 @@ +package com.github.curriculeon; +/* +Create a Classroom singleton. +The class should declare a field that references the instance of Students called students. DONE +The class should declare a field that references the instance of Instructors called instructors. Done +The class should define a method hostLecture which makes use of a Teacher teacher, + double numberOfHours parameter to host a lecture to the composite personList field in the students reference.Done +The class should define a method hostLecture which makes use of a long id, + double numberOfHours parameter to identify a respective Instructor to host a lecture to the composite personList field + in the cohort reference. Done +The class should define a method getStudyMap which returns a new instance of a mapping from Student objects to + Double objects, representative of each respective student's totalStudyTime. + + */ + +import java.util.ArrayList; +import java.util.Map; +import java.util.TreeMap; + +public class Classroom { + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); + + public void hostLecture(Teacher teacher, Double numberOfHours) { + teacher.lecture(students.toArr(), numberOfHours); + } + + public void hostLecture(Long id, Double numberOfHours) { + instructors.findByID(id).lecture(students.toArr(), numberOfHours); + } + + public Map getStudyMap() { + Map map = new TreeMap<>(); + ArrayList slist = students.personList; + for (Student stu : slist) { + + map.put(stu.getId(),stu.getTotalStudyTime()); + } + return map; + } + +} + diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java index 8219f8e..bdddcfc 100644 --- a/src/main/java/com/github/curriculeon/Instructor.java +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -12,7 +12,7 @@ */ public class Instructor extends Person implements Teacher { - Double numberOfHoursPerLearner; + Double numberOfHoursPerLearner =0.0; Instructor(long id, String name) { super(id, name); @@ -25,7 +25,7 @@ public void teach(Learner learner, Double numberOfHours) { @Override public void lecture(Learner[] learners, Double numberOfHours) { - this.numberOfHoursPerLearner = numberOfHours / learners.length; + this.numberOfHoursPerLearner += numberOfHours; for (Learner learner : learners) {//I got this from Chris fulton this.teach(learner, 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..3e43342 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,48 @@ +package com.github.curriculeon; +/* +Use Part 7 as a reference. +Create a Instructors singleton which represents the set of instructors. +Create a TestInstructors class. + + */ + + +import java.util.ArrayList; + +public class Instructors extends People { + ArrayList personList=new ArrayList(); + final static Instructors instance = new Instructors(); + String [] cohort = new String[]{"Leon","Haseeb"}; + private Instructors(){ + super(); + + for(Instructor instructor:peopleMaker(cohort)){ + this.personList.add(instructor); + } + } + + public static Instructors getInstance(){ + return instance; + } + + public ArrayList peopleMaker(String[] names){ + ArrayList people=new ArrayList(); + for(int i =0;i personList=new ArrayList<>(); final static Students instance = new Students(); String [] cohort = new String[]{"David","Chris","Marcus","Leon","Mondira","Deepti","Steve","Yuru","Monica","Akila","Julia","David","Alonzo","Rachid","Emmanuel","Lionel","Solomon","People who don't speak"}; private Students(){ super(); - for(Person person:peopleMaker(cohort)){ - this.personList.add(person); + for(Student student:peopleMaker(cohort)){ + this.personList.add(student); } } @@ -33,17 +34,32 @@ public static Students getInstance(){ return instance; } - public ArrayList peopleMaker(String[] names){ - People people = new People(); + public ArrayList peopleMaker(String[] names){ + ArrayList stu =new ArrayList<>(); for(int i =0;i Date: Thu, 23 Jul 2020 17:33:36 -0400 Subject: [PATCH 5/7] Done with 9 --- src/main/java/com/github/curriculeon/Student.java | 2 +- src/main/java/com/github/curriculeon/Students.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java index a184f5a..7d04586 100644 --- a/src/main/java/com/github/curriculeon/Student.java +++ b/src/main/java/com/github/curriculeon/Student.java @@ -18,7 +18,7 @@ public class Student extends Person implements Learner { } @Override - public void learn(Double numOfHours) { + public void learn(Double numOfHours) { this.totalStudyTime=this.totalStudyTime+numOfHours; } diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java index a02f735..e8dfbed 100644 --- a/src/main/java/com/github/curriculeon/Students.java +++ b/src/main/java/com/github/curriculeon/Students.java @@ -49,15 +49,15 @@ public Student studentMaker(Long id,String name){ } @Override public Student[] toArr() { - Student[] p = new Student[this.personList.size() - 1]; + Student[] p = new Student[this.personList.size()]; for (int i = 0; i < p.length; i++) { - p[i] = this.personList.get(i + 1); + p[i] = this.personList.get(i); } return p; } @Override public Student findByID(Long id) { - for (Student student : this.personList) { + for (Student student : instance.personList) { if (id == student.getId()) return student; } return null; From 5cf9190d61a0d5182fce890c72c71558b785948b Mon Sep 17 00:00:00 2001 From: dmholland Date: Tue, 28 Jul 2020 21:32:33 -0400 Subject: [PATCH 6/7] Hmmm, it seemed I sent some bad code, fixed it, starting on 10 --- src/main/java/com/github/curriculeon/Classroom.java | 8 ++++---- src/main/java/com/github/curriculeon/Instructors.java | 8 ++++---- src/main/java/com/github/curriculeon/Students.java | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java index bfd1106..acf20c7 100644 --- a/src/main/java/com/github/curriculeon/Classroom.java +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -31,10 +31,10 @@ public void hostLecture(Long id, Double numberOfHours) { public Map getStudyMap() { Map map = new TreeMap<>(); - ArrayList slist = students.personList; - for (Student stu : slist) { - - map.put(stu.getId(),stu.getTotalStudyTime()); + ArrayList slist = students.personList; + for (Person stu : slist) { + Student sts=(Student)stu; + map.put(stu.getId(),sts.getTotalStudyTime()); } return map; } diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index 3e43342..8ad878a 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -10,14 +10,14 @@ import java.util.ArrayList; public class Instructors extends People { - ArrayList personList=new ArrayList(); + // ArrayList personList=new ArrayList(); final static Instructors instance = new Instructors(); String [] cohort = new String[]{"Leon","Haseeb"}; private Instructors(){ super(); for(Instructor instructor:peopleMaker(cohort)){ - this.personList.add(instructor); + this.add(instructor); } } @@ -40,8 +40,8 @@ public Instructor studentMaker(Long id,String name){ } @Override public Instructor findByID(Long id) { - for (Instructor instructor : this.personList) { - if (id == instructor.getId()) return instructor; + for (Person instructor : this.personList) { + if (id == instructor.getId()) return (Instructor)instructor; } return null; } diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java index e8dfbed..bb28cd9 100644 --- a/src/main/java/com/github/curriculeon/Students.java +++ b/src/main/java/com/github/curriculeon/Students.java @@ -19,14 +19,14 @@ import java.util.ArrayList; public final class Students extends People { - ArrayList personList=new ArrayList<>(); + // ArrayList personList=new ArrayList<>(); final static Students instance = new Students(); String [] cohort = new String[]{"David","Chris","Marcus","Leon","Mondira","Deepti","Steve","Yuru","Monica","Akila","Julia","David","Alonzo","Rachid","Emmanuel","Lionel","Solomon","People who don't speak"}; private Students(){ super(); for(Student student:peopleMaker(cohort)){ - this.personList.add(student); + this.add(student); } } @@ -51,14 +51,14 @@ public Student studentMaker(Long id,String name){ public Student[] toArr() { Student[] p = new Student[this.personList.size()]; for (int i = 0; i < p.length; i++) { - p[i] = this.personList.get(i); + p[i] = (Student)this.personList.get(i); } return p; } @Override public Student findByID(Long id) { - for (Student student : instance.personList) { - if (id == student.getId()) return student; + for (Person student : instance.personList) { + if (id == student.getId()) return (Student)student; } return null; } From b7720aa89d48bcaff557a72bd1ff38b02328f9d4 Mon Sep 17 00:00:00 2001 From: dmholland Date: Wed, 29 Jul 2020 13:46:18 -0400 Subject: [PATCH 7/7] Finished Enum but confused about tests --- .../com/github/curriculeon/Classroom.java | 11 ++--- .../java/com/github/curriculeon/Educator.java | 48 +++++++++++++++++++ .../com/github/curriculeon/Instructors.java | 18 +++++-- .../java/com/github/curriculeon/People.java | 41 ++++++++-------- .../java/com/github/curriculeon/Students.java | 11 ++--- .../github/curriculeon/InstructorTest.java | 2 +- .../com/github/curriculeon/PeopleTest.java | 20 +++----- 7 files changed, 99 insertions(+), 52 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Educator.java diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java index acf20c7..ef6feb3 100644 --- a/src/main/java/com/github/curriculeon/Classroom.java +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -22,19 +22,18 @@ public class Classroom { Instructors instructors = Instructors.getInstance(); public void hostLecture(Teacher teacher, Double numberOfHours) { - teacher.lecture(students.toArr(), numberOfHours); + teacher.lecture(students.toArray(), numberOfHours); } public void hostLecture(Long id, Double numberOfHours) { - instructors.findByID(id).lecture(students.toArr(), numberOfHours); + instructors.findByID(id).lecture(students.toArray(), numberOfHours); } public Map getStudyMap() { Map map = new TreeMap<>(); - ArrayList slist = students.personList; - for (Person stu : slist) { - Student sts=(Student)stu; - map.put(stu.getId(),sts.getTotalStudyTime()); + ArrayList slist = students.personList; + for (Student stu : slist) { + map.put(stu.getId(),stu.getTotalStudyTime()); } 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..08ede48 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,48 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher { + LEON, + HASEEB; + + private Double hoursWorked; + private final Instructor instructor; + + @Override + public void teach(Learner learner, Double numberOfHours) { + + } + + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + + } + Educator() { + long id = this.ordinal(); // inherited from `Enum` implicit super class + String name = this.name(); // inherited from `Enum` implicit super class + this.instructor = new Instructor(id, name); + Instructors.getInstance().add(instructor); + } +} + /* private Double hoursWorked; + private final Instructor instructor; + + Educator() { + long id = this.ordinal(); // inherited from `Enum` implicit super class + String name = this.name(); // inherited from `Enum` implicit super class + this.instructor = new Instructor(id, name); + Instructors.getInstance().add(instructor); + } + + @Override + public void teach(Learner learner, Double numberOfHours) { + instructor.teach(learner, numberOfHours); + hoursWorked += numberOfHours; + } + + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + instructor.lecture(learners, numberOfHours); + hoursWorked += numberOfHours; + } +} +*/ \ No newline at end of file diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index 8ad878a..f410dab 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -9,7 +9,7 @@ import java.util.ArrayList; -public class Instructors extends People { +public class Instructors extends People { // ArrayList personList=new ArrayList(); final static Instructors instance = new Instructors(); String [] cohort = new String[]{"Leon","Haseeb"}; @@ -21,6 +21,16 @@ private Instructors(){ } } + @Override + public Instructor[] toArray() { + Instructor[] p = new Instructor[this.personList.size()]; + for (int i = 0; i < p.length; i++) { + p[i] = this.personList.get(i); + } + return p; + + } + public static Instructors getInstance(){ return instance; } @@ -38,11 +48,11 @@ public Instructor studentMaker(Long id,String name){ return new Instructor(id,name); } - @Override + /* @Override public Instructor findByID(Long id) { - for (Person instructor : this.personList) { + for (Instructor instructor : this.personList) { if (id == instructor.getId()) return (Instructor)instructor; } return null; - } + }*/ } diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 5abe9a1..5834bd7 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -18,15 +18,15 @@ The class should define a method named toArray which returns an array representation of the personList field. The class should implement Iterable and define a method named iterator which makes use of the personList field to generate a new a Iterator. */ -public class People implements Iterable { - ArrayList personList =new ArrayList(); +abstract public class People implements Iterable { + ArrayList personList =new ArrayList(); public People() { } - public void add(Person person) { + public void add(SomePerson person) { this.personList.add(person); } @@ -34,19 +34,16 @@ public void add(Person person) { this.personList=people; } - public Person findByID(Long id) { - for (Person person : this.personList) { - if (id == person.getId()) return person; - } - return null; - } + public SomePerson findByID(Long id) { + for (SomePerson person : this.personList) { + if (id == person.getId()) return person; + } + return null; + } - public Boolean removePerson(Person person) { - for (Person p : this.personList) { - if (person.equals(p)) this.personList.remove(p); - return true; - } - return false; + + public Boolean removePerson(SomePerson person) { + return personList.remove(person); } @@ -60,25 +57,25 @@ public Boolean removePerson(Long id) { } public void removeAll() { - for (Person p : this.personList) this.personList.remove(p); + for (SomePerson p : this.personList) this.personList.remove(p); } public Integer sizeList() { return this.personList.size(); } - public Person[] toArr() { - Person[] p = new Person[this.personList.size() - 1]; + /* public SomePerson[] toArr() { + SomePerson[] p = new SomePerson[this.personList.size() - 1]; for (int i = 0; i < p.length; i++) { p[i] = this.personList.get(i + 1); } return p; } - +*/ @Override - public Iterator iterator() { + public Iterator iterator() { return personList.iterator(); - - } + + abstract public SomePerson[] toArray(); } diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java index bb28cd9..a825aa2 100644 --- a/src/main/java/com/github/curriculeon/Students.java +++ b/src/main/java/com/github/curriculeon/Students.java @@ -18,7 +18,7 @@ import java.util.ArrayList; -public final class Students extends People { +public class Students extends People { // ArrayList personList=new ArrayList<>(); final static Students instance = new Students(); String [] cohort = new String[]{"David","Chris","Marcus","Leon","Mondira","Deepti","Steve","Yuru","Monica","Akila","Julia","David","Alonzo","Rachid","Emmanuel","Lionel","Solomon","People who don't speak"}; @@ -29,7 +29,6 @@ private Students(){ this.add(student); } } - public static Students getInstance(){ return instance; } @@ -48,17 +47,17 @@ public Student studentMaker(Long id,String name){ return new Student(id,name); } @Override - public Student[] toArr() { +public Student[] toArray() { Student[] p = new Student[this.personList.size()]; for (int i = 0; i < p.length; i++) { - p[i] = (Student)this.personList.get(i); + p[i] = this.personList.get(i); } return p; } @Override public Student findByID(Long id) { - for (Person student : instance.personList) { - if (id == student.getId()) return (Student)student; + for (Student student : instance.personList) { + if (id == student.getId()) return student; } return null; } diff --git a/src/test/java/com/github/curriculeon/InstructorTest.java b/src/test/java/com/github/curriculeon/InstructorTest.java index 9cb2e27..6958de0 100644 --- a/src/test/java/com/github/curriculeon/InstructorTest.java +++ b/src/test/java/com/github/curriculeon/InstructorTest.java @@ -15,7 +15,7 @@ public class InstructorTest { private void testImplementation(boolean tru) { // Given - Teacher instructor = new Instructor(2,"B.M.Quickly"); + Instructor instructor = new Instructor(2,"B.M.Quickly"); // Then diff --git a/src/test/java/com/github/curriculeon/PeopleTest.java b/src/test/java/com/github/curriculeon/PeopleTest.java index 82e53b4..139e44d 100644 --- a/src/test/java/com/github/curriculeon/PeopleTest.java +++ b/src/test/java/com/github/curriculeon/PeopleTest.java @@ -18,13 +18,14 @@ public class PeopleTest { Person p2=new Person(2,"Larry"); Person p3=new Person(3,"Gush"); Person p4=new Person(4,"Dare"); - ArrayList pp =new ArrayList (Arrays.asList(p1,p2,p3)); + ArrayList pp =new ArrayList (Arrays.asList(p1,p2,p3)); + People people= Instructors.getInstance(); + private void testAdd(boolean tru) { // Given - People people =new People(pp); people.add(p4); @@ -35,27 +36,20 @@ private void testAdd(boolean tru) { private void testRemove(boolean tru) { // Given - People people =new People(pp); - people.removePerson(1L); - + people.removePerson(1L); // Then - Assert.assertTrue(people.personList.contains(p1)); + Assert.assertFalse(people.personList.contains(p1)); } private void testFindByID(boolean tru) { // Given - People people =new People(pp); - Person expected= p3; - //When - - Person actual=people.findByID(3L); - + String expected="Leon"; // Then - Assert.assertEquals(actual,expected); + Assert.assertEquals(people.findByID(1L).getName(),expected); }