From 3277a9928485499cda28d61d4e90dac4a56d147b Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 21 Jul 2020 07:59:52 -0400 Subject: [PATCH 01/10] Person class created and implemented. --- .../java/com/github/curriculeon/Person.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..552e11a 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,23 @@ package com.github.curriculeon; public class Person { + final private long id; + private String 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; + } } From 03509cab30ce8806b0e76791b43f3287178749a7 Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 21 Jul 2020 08:34:04 -0400 Subject: [PATCH 02/10] TestPerson created and tested on Person class. --- .../com/github/curriculeon/TestPerson.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..4e21fa6 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; -public class TestPerson { +import junit.framework.TestCase; +import org.junit.Test; -} +public class TestPerson extends TestCase { + + @Test + public void testConstructor() { + //given & when + long expectedID = 1; + String expectedName = "Ezra"; + Person guy = new Person(expectedID, expectedName); + //then + assertEquals(expectedID, guy.getId()); + assertEquals(expectedName, guy.getName()); + } + + @Test + public void testSetName() { + //given + Person guy = new Person(0,"test"); + //when + String expected = "Ezra"; + guy.setName(expected); + //then + assertEquals(expected, guy.getName()); + } +} \ No newline at end of file From febe2e30dca6e30be60668d32a70d267a9f21f3f Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 28 Jul 2020 21:11:59 -0400 Subject: [PATCH 03/10] Learner interface created. --- src/main/java/com/github/curriculeon/Learner.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Learner.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..1fe13c9 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,9 @@ +package com.github.curriculeon; +/* + Interfaces do not implement their own methods, they only provide the signature. + Classes that use the interface must implement the methods on their own. + */ +public interface Learner { + public void learn(double numberOfHours); + public Double getTotalStudyTime(); +} From 737feb3e37d7740b1455c5acb3bc6a56724958f1 Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 28 Jul 2020 21:29:58 -0400 Subject: [PATCH 04/10] Student class created. --- .../java/com/github/curriculeon/Student.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Student.java 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..3d0cfaf --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; +/* + The Student class is a Person and can learn. + Student must call parent's constructor using keyword super if that constructor uses arguments to be instantiated. + */ +public class Student extends Person implements Learner{ + private double totalStudyTime; + + public Student(long id, String name) { + super(id, name); + } + + + @Override + public void learn(double numberOfHours) { + totalStudyTime += numberOfHours; + } + + @Override + public Double getTotalStudyTime() { + return totalStudyTime; + } +} From 65a15c973a7782b21bb55301a97e2878b2af89fa Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 28 Jul 2020 21:49:45 -0400 Subject: [PATCH 05/10] Student class tested. --- .../java/com/github/curriculeon/Student.java | 1 + .../com/github/curriculeon/TestStudent.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/test/java/com/github/curriculeon/TestStudent.java diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java index 3d0cfaf..ee41229 100644 --- a/src/main/java/com/github/curriculeon/Student.java +++ b/src/main/java/com/github/curriculeon/Student.java @@ -8,6 +8,7 @@ public class Student extends Person implements Learner{ public Student(long id, String name) { super(id, name); + this.totalStudyTime = 0; } 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..fad95c1 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,38 @@ +package com.github.curriculeon; + +import junit.framework.TestCase; +import org.junit.Test; + +public class TestStudent extends TestCase { + + @Test + public void testImplementation(){ + //given & when + Student student = new Student(1, "Test"); + + //then + assert student instanceof Learner; + } + + @Test + public void testInheritance(){ + //given & when + Student student = new Student(1, "Test"); + + //then + assert student instanceof Person; + } + + @Test + public void testLearn() { + //given + Student student = new Student(1, "Test"); + + //when + double hours = 5; + student.learn(hours); + + //then + assertEquals(hours, student.getTotalStudyTime()); + } +} \ No newline at end of file From 03d4b28af709bcdb1b064d38cba61be21fdebd71 Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 28 Jul 2020 21:56:24 -0400 Subject: [PATCH 06/10] Teacher interface created. --- src/main/java/com/github/curriculeon/Teacher.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Teacher.java 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..7de72a1 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Teacher { + public void teach(Learner learner, double numberOfHours); + public void lecture(Learner[] learners, double numberOfHours); +} From 2896ceb1503cda101e6031a7e1d357771595041d Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 28 Jul 2020 22:14:47 -0400 Subject: [PATCH 07/10] Instructor class created. --- .../com/github/curriculeon/Instructor.java | 23 +++++++++++++++++++ .../java/com/github/curriculeon/Learner.java | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/github/curriculeon/Instructor.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..8fff551 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; +/* + The Instructor class is a person and can teach. + */ +public class Instructor extends Person implements Teacher{ + + public Instructor(long id, String name) { + super(id, name); + } + + @Override + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learners.length; + for (Learner learner : learners) { + learner.learn(numberOfHoursPerLearner); + } + } +} diff --git a/src/main/java/com/github/curriculeon/Learner.java b/src/main/java/com/github/curriculeon/Learner.java index 1fe13c9..591b6d7 100644 --- a/src/main/java/com/github/curriculeon/Learner.java +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -1,6 +1,6 @@ package com.github.curriculeon; /* - Interfaces do not implement their own methods, they only provide the signature. + Interfaces do not implement their own methods, they only provide the signature and final variables. Classes that use the interface must implement the methods on their own. */ public interface Learner { From 33ea1ae9aecc32015d0eac78a8b86b32d165d59d Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 28 Jul 2020 22:52:58 -0400 Subject: [PATCH 08/10] Instructor class tested. --- .../github/curriculeon/TestInstructor.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/java/com/github/curriculeon/TestInstructor.java diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java new file mode 100644 index 0000000..afb21af --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,62 @@ +package com.github.curriculeon; + +import junit.framework.TestCase; +import org.junit.Test; + +public class TestInstructor extends TestCase { + + @Test + public void testImplementation(){ + //given & when + Instructor instructor = new Instructor(1, "test"); + + //then + assert instructor instanceof Teacher; + } + + @Test + public void testInheritance(){ + //given & when + Instructor instructor = new Instructor(1, "test"); + + //then + assert instructor instanceof Person; + } + + @Test + public void testTeach() { + //given + Instructor instructor = new Instructor(1, "test"); + Student student = new Student(1, "test"); + + //when + double expected = 5; + instructor.teach(student, expected); + + //then + assertEquals(expected, student.getTotalStudyTime()); + } + + @Test + public void testLecture() { + //given + Instructor instructor = new Instructor(1, "test"); + Student student1 = new Student(1, "test1"); + Student student2 = new Student(2, "test2"); + Student student3 = new Student(3, "test3"); + Student student4 = new Student(4, "test4"); + Student[] students = new Student[]{ + student1, student2, student3, student4 + }; + double numberOfHours = 50; + double numberOfHoursPerStudent = numberOfHours / students.length; + + //when + instructor.lecture(students, numberOfHours); + + //then + for (Student student: students) { + assertEquals(numberOfHoursPerStudent, student.getTotalStudyTime()); + } + } +} \ No newline at end of file From 81e134d479ace5b5c79cb173f191808e653f8200 Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Tue, 4 Aug 2020 10:01:31 -0400 Subject: [PATCH 09/10] People class created. --- .gitignore | 1 + .../java/com/github/curriculeon/People.java | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/People.java diff --git a/.gitignore b/.gitignore index 53c8398..303840e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ local.properties .settings/ .loadpath .recommenders +*.plantuml # External tool builders .externalToolBuilders/ 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..561954e --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,46 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class People implements Iterable { + private List personList = new ArrayList(); + + public void add(Person person){ + personList.add(person); + } + + public Person findById(long id){ + return personList.get((int) id); + } + + public boolean contains(Person person){ + return personList.contains(person); + } + + public void remove(Person person){ + personList.remove(person); + } + + public void remove(long id){ + personList.remove((int)id); + } + + public void removeAll(){ + personList.clear(); + } + + public int count(){ + return personList.size(); + } + + public Person[] toArray(){ + return (Person[]) personList.toArray(); + } + + @Override + public Iterator iterator() { + return personList.iterator(); + } +} From 6f7b6d58f2b6ea698e73b45331982d0b034dcb32 Mon Sep 17 00:00:00 2001 From: Ezra Vance <3zravance@gmail.com> Date: Mon, 10 Aug 2020 10:53:09 -0400 Subject: [PATCH 10/10] Fixed findById method in People class to check the Peron id, not People List index. Fully tested People class. --- .../java/com/github/curriculeon/People.java | 23 +++++++---- .../java/com/github/curriculeon/Person.java | 2 +- .../com/github/curriculeon/TestPeople.java | 40 +++++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 2 +- 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/github/curriculeon/TestPeople.java diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 561954e..c1866a8 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -5,17 +5,26 @@ import java.util.List; public class People implements Iterable { - private List personList = new ArrayList(); + private List personList = new ArrayList<>(); public void add(Person person){ personList.add(person); } public Person findById(long id){ - return personList.get((int) id); - } - - public boolean contains(Person person){ + try { + for(Person person : personList){ + if(person.getId().equals(id)){ + return person; + } + } + } catch (Exception e) { + System.out.println("Person not found!!!"); + } + return null; + } + + public Boolean contains(Person person){ return personList.contains(person); } @@ -24,14 +33,14 @@ public void remove(Person person){ } public void remove(long id){ - personList.remove((int)id); + personList.remove(findById(id)); } public void removeAll(){ personList.clear(); } - public int count(){ + public Integer count(){ return personList.size(); } diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 552e11a..c86e2f9 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -9,7 +9,7 @@ public Person(long id, String name) { this.name = name; } - public long getId() { + public Long getId() { return id; } 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..f5a9e25 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,40 @@ +package com.github.curriculeon; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.ArrayList; + +public class TestPeople extends TestCase { + + @Test + public void testAdd() { + Person expectedPerson = new Person(22, "Ezra"); + People peopleList = new People(); + + peopleList.add(expectedPerson); + + assertTrue(peopleList.contains(expectedPerson)); + } + + @Test + public void testFindById() { + Long expectedId = 5L; + Person expectedPerson = new Person(expectedId, "Ezra"); + People peopleList = new People(); + peopleList.add(expectedPerson); + + assertEquals(expectedPerson.getId(), peopleList.findById(expectedId).getId()); + } + + @Test + public void testRemove() { + Person expectedPerson = new Person(7, "Ezra"); + People peopleList = new People(); + peopleList.add(expectedPerson); + + peopleList.remove(expectedPerson); + + assertFalse(peopleList.contains(expectedPerson)); + } +} \ 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 4e21fa6..c7b4892 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -8,7 +8,7 @@ public class TestPerson extends TestCase { @Test public void testConstructor() { //given & when - long expectedID = 1; + Long expectedID = 1L; String expectedName = "Ezra"; Person guy = new Person(expectedID, expectedName); //then