From a094ff780c377f4e3ae5441bd144469e6a12268e Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Thu, 24 Aug 2023 14:32:25 -0400 Subject: [PATCH 01/23] Created and tested Person class --- .../java/com/github/curriculeon/Person.java | 20 ++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 24 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..c719857 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,25 @@ package com.github.curriculeon; public class Person { + final long id; + 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; + } } diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..faeba1f 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,29 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor(){ + Person test = new Person(1L, "Leon"); + + String expected = "Leon"; + String actual = test.getName(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetName(){ + Person test = new Person(1L, "Leon"); + + test.setName("Noel"); + + String expected = "Noel"; + String actual = test.getName(); + + Assert.assertEquals(expected, actual); + } } From 103f86d0a8e6607b6f9af11f930bb57279641787 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Thu, 24 Aug 2023 14:36:58 -0400 Subject: [PATCH 02/23] Learner interface added --- src/main/java/com/github/curriculeon/Learner.java | 7 +++++++ 1 file changed, 7 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..4dbc03b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,7 @@ +package com.github.curriculeon; + +public interface Learner { + public void learn(double numberOfHours); + + public double getTotalStudyTime(); +} From f9b7b8ed2676a1dc3b3c4c4f6d8ceee2aeee4b20 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Thu, 24 Aug 2023 14:40:34 -0400 Subject: [PATCH 03/23] Created student class --- .../java/com/github/curriculeon/Student.java | 20 +++++++++++++++++++ 1 file changed, 20 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..bb90bf8 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,20 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner { + + double totalStudyTime; + + public Student(long id, String name) { + super(id, name); + } + + @Override + public void learn(double numberOfHours) { + this.totalStudyTime += numberOfHours; + } + + @Override + public double getTotalStudyTime() { + return this.totalStudyTime; + } +} From 98bad62ab03f85573fab5d6005a506ea32d7cf11 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Thu, 24 Aug 2023 14:47:47 -0400 Subject: [PATCH 04/23] Created tests for Student class --- .../com/github/curriculeon/TestStudent.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/com/github/curriculeon/TestStudent.java 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..51e018a --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,33 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + + @Test + public void testImplementation(){ + Student test = new Student(1L, "Andrew"); + + Assert.assertTrue(test instanceof Learner); + } + + @Test + public void testInheritance(){ + Student test = new Student(1L, "Jarek"); + + Assert.assertTrue(test instanceof Person); + } + + @Test + public void testLearn(){ + Student test = new Student(1L, "Mary"); + + test.learn(12.0); + + double expected = 12.0; + double actual = test.getTotalStudyTime(); + + Assert.assertEquals(expected, actual, 0); + } +} From 7cd28e203f2087f19a1b6295263676465a7052ad Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Thu, 24 Aug 2023 15:15:45 -0400 Subject: [PATCH 05/23] Created Teacher interface --- src/main/java/com/github/curriculeon/Teacher.java | 8 ++++++++ 1 file changed, 8 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..fe7f6c3 --- /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 numberOfHours); + +} From ea5d0a584c9cde82137338c1042b66379ce4e267 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Fri, 25 Aug 2023 18:10:24 -0400 Subject: [PATCH 06/23] Began creating Instructor class --- .../com/github/curriculeon/Instructor.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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..2f4d01a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,18 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher { + + public Instructor(long id, String name) { + super(id, name); + } + + @Override + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + + } +} From 2acec56217c4580f4de84f96ac25e5207c79b1bb Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Fri, 25 Aug 2023 19:33:55 -0400 Subject: [PATCH 07/23] Finished Instructor class methods, need to make tests --- src/main/java/com/github/curriculeon/Instructor.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java index 2f4d01a..69cc4f4 100644 --- a/src/main/java/com/github/curriculeon/Instructor.java +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -13,6 +13,9 @@ public void teach(Learner learner, double numberOfHours) { @Override public void lecture(Learner[] learners, double numberOfHours) { - + double evenlySplitTime = numberOfHours / learners.length; + for (Learner learner:learners) { + learner.learn(evenlySplitTime); + } } } From d86459340526db508a3f4c7120593f3c2bd4e004 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Sat, 26 Aug 2023 17:43:09 -0400 Subject: [PATCH 08/23] Creating tests for Instructor class --- .../github/curriculeon/TestInstructor.java | 35 +++++++++++++++++++ 1 file changed, 35 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..97650ac --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,35 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + + @Test + public void testImplementation(){ + Instructor test = new Instructor(1, "Leon"); + + Assert.assertTrue(test instanceof Teacher); + } + + @Test + public void testInheritance(){ + Instructor test = new Instructor(1, "Leon"); + + Assert.assertTrue(test instanceof Person); + } + + @Test + public void testTeach(){ + Instructor leon = new Instructor(1, "Leon"); + + Student mary = new Student(1, "Mary"); + + leon.teach(mary, 4.5); + + double expected = 4.5; + double actual = mary.getTotalStudyTime(); + + Assert.assertEquals(expected, actual, 0); + } +} From 048b3e7ffa95c77a8ec484c5e90af41fb4b5d26f Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Sat, 26 Aug 2023 18:12:40 -0400 Subject: [PATCH 09/23] Added lecture method test, Instructor tests complete --- .../com/github/curriculeon/Instructor.java | 4 ++-- .../com/github/curriculeon/TestInstructor.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java index 69cc4f4..a27a0a6 100644 --- a/src/main/java/com/github/curriculeon/Instructor.java +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -13,9 +13,9 @@ public void teach(Learner learner, double numberOfHours) { @Override public void lecture(Learner[] learners, double numberOfHours) { - double evenlySplitTime = numberOfHours / learners.length; + double numberOfHoursPerLearner = numberOfHours / learners.length; for (Learner learner:learners) { - learner.learn(evenlySplitTime); + learner.learn(numberOfHoursPerLearner); } } } diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java index 97650ac..b0710dc 100644 --- a/src/test/java/com/github/curriculeon/TestInstructor.java +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -32,4 +32,22 @@ public void testTeach(){ Assert.assertEquals(expected, actual, 0); } + + @Test + public void testLecture(){ + Instructor leon = new Instructor(1, "Leon"); + + Student mary = new Student(1, "Mary"); + Student jarek = new Student(2, "Jarek"); + Student andrew = new Student(3, "Andrew"); + + Learner[] learners = {mary, jarek, andrew}; + + leon.lecture(learners, 9.0); + + double expected = 3.0; + double actual = andrew.getTotalStudyTime(); + + Assert.assertEquals(expected, actual, 0.01); + } } From da22e7110d1a1afeac904ad0a469e2b9e9350d5c Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Sat, 26 Aug 2023 18:57:09 -0400 Subject: [PATCH 10/23] Created People class; add, findById, and contains methods --- .../java/com/github/curriculeon/People.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/People.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..b15c6d6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,32 @@ +package com.github.curriculeon; + +import java.util.List; + +public class People { + + List personlist; + + 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){ + for (Person x: personlist) { + if(x.equals(person)){ + return true; + } + } + return false; + } + + +} From f57690322242fe683255eae2dbd5ad9eb23d2c53 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Sat, 26 Aug 2023 19:06:22 -0400 Subject: [PATCH 11/23] Added remove, removeAll, count, and toArray methods --- .../java/com/github/curriculeon/People.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index b15c6d6..73fc22a 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -28,5 +28,33 @@ public boolean contains(Person person){ return false; } + public void remove(long id){ + Person toRemove = findById(id); + if(toRemove != null){ + personlist.remove(toRemove); + } + else{ + System.out.println("ID not found in list"); + } + } + + public void removeAll(){ + personlist.clear(); + } + + public int count(){ + return personlist.size(); + } + + public Person[] toArray(){ + Person[] x = new Person[personlist.size()]; + int count = 0; + + for (Person person: personlist) { + x[count] = person; + count++; + } + return x; + } } From 4ec93d4d291007ee7e339d0445ba50e072da1750 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 29 Aug 2023 12:39:07 -0400 Subject: [PATCH 12/23] Updating classes and tests --- .../com/github/curriculeon/Instructor.java | 4 +-- .../java/com/github/curriculeon/Learner.java | 4 +-- .../java/com/github/curriculeon/People.java | 19 +++++++++--- .../java/com/github/curriculeon/Person.java | 6 ++-- .../java/com/github/curriculeon/Student.java | 2 +- .../java/com/github/curriculeon/Teacher.java | 4 +-- .../github/curriculeon/TestInstructor.java | 16 +++++----- .../com/github/curriculeon/TestPeople.java | 30 +++++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 6 ++-- 9 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 src/test/java/com/github/curriculeon/TestPeople.java diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java index a27a0a6..fb665a9 100644 --- a/src/main/java/com/github/curriculeon/Instructor.java +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -2,7 +2,7 @@ public class Instructor extends Person implements Teacher { - public Instructor(long id, String name) { + public Instructor(Long id, String name) { super(id, name); } @@ -15,7 +15,7 @@ public void teach(Learner learner, double numberOfHours) { public void lecture(Learner[] learners, double numberOfHours) { double numberOfHoursPerLearner = numberOfHours / learners.length; for (Learner learner:learners) { - learner.learn(numberOfHoursPerLearner); + teach(learner, numberOfHoursPerLearner); } } } diff --git a/src/main/java/com/github/curriculeon/Learner.java b/src/main/java/com/github/curriculeon/Learner.java index 4dbc03b..c934c30 100644 --- a/src/main/java/com/github/curriculeon/Learner.java +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -1,7 +1,7 @@ package com.github.curriculeon; public interface Learner { - public void learn(double numberOfHours); + void learn(double numberOfHours); - public double getTotalStudyTime(); + double getTotalStudyTime(); } diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 73fc22a..a15438b 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -1,16 +1,18 @@ package com.github.curriculeon; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; -public class People { +public class People implements Iterable { - List personlist; + List personlist = new ArrayList<>(); 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; @@ -28,7 +30,11 @@ public boolean contains(Person person){ return false; } - public void remove(long id){ + public void remove(Person person){ + personlist.remove(person); + } + + public void removeById(Long id){ Person toRemove = findById(id); if(toRemove != null){ @@ -57,4 +63,9 @@ public Person[] toArray(){ } return x; } + + @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 c719857..c11b62c 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,17 +1,17 @@ package com.github.curriculeon; public class Person { - final long id; + final Long id; String name; - public Person(long id, String name) { + public Person(Long id, String name) { this.id = id; this.name = name; } - public long getId() { + 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 index bb90bf8..14875a9 100644 --- a/src/main/java/com/github/curriculeon/Student.java +++ b/src/main/java/com/github/curriculeon/Student.java @@ -4,7 +4,7 @@ public class Student extends Person implements Learner { double totalStudyTime; - public Student(long id, String name) { + public Student(Long id, String name) { super(id, name); } diff --git a/src/main/java/com/github/curriculeon/Teacher.java b/src/main/java/com/github/curriculeon/Teacher.java index fe7f6c3..5555723 100644 --- a/src/main/java/com/github/curriculeon/Teacher.java +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -1,8 +1,8 @@ package com.github.curriculeon; public interface Teacher { - public void teach(Learner learner, double numberOfHours); + void teach(Learner learner, double numberOfHours); - public void lecture(Learner[] learners, double numberOfHours); + void lecture(Learner[] learners, double numberOfHours); } diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java index b0710dc..9c867e3 100644 --- a/src/test/java/com/github/curriculeon/TestInstructor.java +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -7,23 +7,23 @@ public class TestInstructor { @Test public void testImplementation(){ - Instructor test = new Instructor(1, "Leon"); + Instructor test = new Instructor(1L, "Leon"); Assert.assertTrue(test instanceof Teacher); } @Test public void testInheritance(){ - Instructor test = new Instructor(1, "Leon"); + Instructor test = new Instructor(1L, "Leon"); Assert.assertTrue(test instanceof Person); } @Test public void testTeach(){ - Instructor leon = new Instructor(1, "Leon"); + Instructor leon = new Instructor(1L, "Leon"); - Student mary = new Student(1, "Mary"); + Student mary = new Student(1L, "Mary"); leon.teach(mary, 4.5); @@ -35,11 +35,11 @@ public void testTeach(){ @Test public void testLecture(){ - Instructor leon = new Instructor(1, "Leon"); + Instructor leon = new Instructor(1L, "Leon"); - Student mary = new Student(1, "Mary"); - Student jarek = new Student(2, "Jarek"); - Student andrew = new Student(3, "Andrew"); + Student mary = new Student(1L, "Mary"); + Student jarek = new Student(2L, "Jarek"); + Student andrew = new Student(3L, "Andrew"); Learner[] learners = {mary, jarek, andrew}; 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..955ce43 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,30 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + + @Test + public void testAdd(){ + People people = new People(); + Person person = new Person(null, null); + Assert.assertFalse(people.contains(person)); + + people.add(person); + + Assert.assertTrue(people.contains(person)); + } + + @Test + public void testRemove(){ + People people = new People(); + Person person = new Person(null, null); + people.add(person); + Assert.assertTrue(people.contains(person)); + + + + + } +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index faeba1f..fc1729c 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -17,11 +17,13 @@ public void testConstructor(){ @Test public void testSetName(){ - Person test = new Person(1L, "Leon"); + Person test = new Person(null, null); + String expected = "Noel"; + + Assert.assertNotEquals(test.getName(), expected); test.setName("Noel"); - String expected = "Noel"; String actual = test.getName(); Assert.assertEquals(expected, actual); From 5f9c3d7060561b98d4ef540af75b0881100534e7 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 29 Aug 2023 13:08:35 -0400 Subject: [PATCH 13/23] Updated Student class, testStudent class --- .../java/com/github/curriculeon/Student.java | 2 +- .../com/github/curriculeon/TestPerson.java | 35 +++++++++++-------- .../com/github/curriculeon/TestStudent.java | 32 ++++++++++++----- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java index 14875a9..7a1104b 100644 --- a/src/main/java/com/github/curriculeon/Student.java +++ b/src/main/java/com/github/curriculeon/Student.java @@ -15,6 +15,6 @@ public void learn(double numberOfHours) { @Override public double getTotalStudyTime() { - return this.totalStudyTime; + return totalStudyTime; } } diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index fc1729c..f20be18 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -7,25 +7,32 @@ public class TestPerson { @Test public void testConstructor(){ - Person test = new Person(1L, "Leon"); - - String expected = "Leon"; - String actual = test.getName(); - - Assert.assertEquals(expected, actual); + //given + Long expectedId = 10L; + String expectedName = "Leon"; + + //when + Person person = new Person(expectedId, expectedName); + Long actualId = person.getId(); + String actualName = person.getName(); + + //then + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); } @Test public void testSetName(){ - Person test = new Person(null, null); - String expected = "Noel"; - - Assert.assertNotEquals(test.getName(), expected); - - test.setName("Noel"); + //given + String expectedName = "Leon"; + Person person = new Person(null, null); + Assert.assertNotEquals(expectedName, person.getName()); - String actual = test.getName(); + //when - the method that we are actually testing + person.setName(expectedName); + String actualName = person.getName(); - Assert.assertEquals(expected, actual); + //then + Assert.assertEquals(expectedName, actualName); } } diff --git a/src/test/java/com/github/curriculeon/TestStudent.java b/src/test/java/com/github/curriculeon/TestStudent.java index 51e018a..f383c26 100644 --- a/src/test/java/com/github/curriculeon/TestStudent.java +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -7,27 +7,41 @@ public class TestStudent { @Test public void testImplementation(){ - Student test = new Student(1L, "Andrew"); + //given + Student student = new Student(null, null); - Assert.assertTrue(test instanceof Learner); + //when + boolean isLearner = student instanceof Learner; + + //then + Assert.assertTrue(isLearner); } @Test public void testInheritance(){ - Student test = new Student(1L, "Jarek"); + //given + Student student = new Student(null, null); + + //when + boolean isPerson = student instanceof Person; - Assert.assertTrue(test instanceof Person); + //then + Assert.assertTrue(isPerson); } @Test public void testLearn(){ - Student test = new Student(1L, "Mary"); + //given + Student student = new Student(null, null); + double preStudyTime = student.getTotalStudyTime(); + double numberOfHours = 100.0; + double expectedTotalStudyTime = preStudyTime + numberOfHours; - test.learn(12.0); + //when + student.learn(numberOfHours); - double expected = 12.0; - double actual = test.getTotalStudyTime(); + //then + Assert.assertEquals(expectedTotalStudyTime, student.getTotalStudyTime(), 0.001); - Assert.assertEquals(expected, actual, 0); } } From 0399db69ebe2000ae8b32f83384daaffeea5ae44 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 29 Aug 2023 13:37:04 -0400 Subject: [PATCH 14/23] Updated Instructor class, TestInstructor class --- .../github/curriculeon/TestInstructor.java | 76 ++++++++++++------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java index 9c867e3..3c20489 100644 --- a/src/test/java/com/github/curriculeon/TestInstructor.java +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -7,47 +7,67 @@ public class TestInstructor { @Test public void testImplementation(){ - Instructor test = new Instructor(1L, "Leon"); + //given + Instructor instructor = new Instructor(null, null); - Assert.assertTrue(test instanceof Teacher); + //when + boolean isTeacher = instructor instanceof Teacher; + + //then + Assert.assertTrue(isTeacher); } @Test public void testInheritance(){ - Instructor test = new Instructor(1L, "Leon"); + //given + Instructor instructor = new Instructor(null, null); + + //when + boolean isPerson = instructor instanceof Person; - Assert.assertTrue(test instanceof Person); + //then + Assert.assertTrue(isPerson); } @Test public void testTeach(){ - Instructor leon = new Instructor(1L, "Leon"); - - Student mary = new Student(1L, "Mary"); - - leon.teach(mary, 4.5); - - double expected = 4.5; - double actual = mary.getTotalStudyTime(); - - Assert.assertEquals(expected, actual, 0); + //given + Student student = new Student(null, null); + Instructor instructor = new Instructor(null, null); + double preStudyTime = student.getTotalStudyTime(); + double lectureTime = 10.0; + double expectedStudyTime = preStudyTime + lectureTime; + + //when + instructor.teach(student, lectureTime); + double actualStudyTime = student.getTotalStudyTime(); + + //then + Assert.assertEquals(expectedStudyTime, actualStudyTime, 0.001); } @Test public void testLecture(){ - Instructor leon = new Instructor(1L, "Leon"); - - Student mary = new Student(1L, "Mary"); - Student jarek = new Student(2L, "Jarek"); - Student andrew = new Student(3L, "Andrew"); - - Learner[] learners = {mary, jarek, andrew}; - - leon.lecture(learners, 9.0); - - double expected = 3.0; - double actual = andrew.getTotalStudyTime(); - - Assert.assertEquals(expected, actual, 0.01); + //given + Student student1 = new Student(null, null); + Student student2 = new Student(null, null); + Student student3 = new Student(null, null); + Student student4 = new Student(null, null); + Student[] students = new Student[]{student1, student2, student3, student4}; + Instructor instructor = new Instructor(null, null); + double lectureTime = students.length; + double expectedStudyTime = lectureTime/ students.length; + for(Student student : students){ + Assert.assertNotEquals(expectedStudyTime, student.getTotalStudyTime()); + } + + //when + instructor.lecture(students, lectureTime); + for(Student student : students) { + double actualStudyTime = student.getTotalStudyTime(); + + //then + Assert.assertEquals(expectedStudyTime, actualStudyTime, 0.001); + } } } From 2aa9d3da1bea97b889968d355bd0ee91a99e3574 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 29 Aug 2023 14:20:14 -0400 Subject: [PATCH 15/23] Updated People class, updated and completed TestPeople class --- .../java/com/github/curriculeon/People.java | 29 +++--------- .../com/github/curriculeon/TestPeople.java | 45 ++++++++++++++++++- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index a15438b..44d2665 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -6,7 +6,7 @@ public class People implements Iterable { - List personlist = new ArrayList<>(); + private List personlist = new ArrayList<>(); public void add(Person person){ personlist.add(person); @@ -22,27 +22,15 @@ public Person findById(Long id){ } public boolean contains(Person person){ - for (Person x: personlist) { - if(x.equals(person)){ - return true; - } - } - return false; + return personlist.contains(person); } public void remove(Person person){ personlist.remove(person); } - public void removeById(Long id){ - Person toRemove = findById(id); - - if(toRemove != null){ - personlist.remove(toRemove); - } - else{ - System.out.println("ID not found in list"); - } + public void remove(Long id){ + remove(findById(id)); } public void removeAll(){ @@ -54,14 +42,7 @@ public int count(){ } public Person[] toArray(){ - Person[] x = new Person[personlist.size()]; - int count = 0; - - for (Person person: personlist) { - x[count] = person; - count++; - } - return x; + return personlist.toArray(new Person[0]); } @Override diff --git a/src/test/java/com/github/curriculeon/TestPeople.java b/src/test/java/com/github/curriculeon/TestPeople.java index 955ce43..d3d0cf5 100644 --- a/src/test/java/com/github/curriculeon/TestPeople.java +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -6,25 +6,66 @@ public class TestPeople { @Test - public void testAdd(){ + public void testAdd() { + //given People people = new People(); Person person = new Person(null, null); Assert.assertFalse(people.contains(person)); + //when people.add(person); + //then Assert.assertTrue(people.contains(person)); } @Test - public void testRemove(){ + public void testRemove() { + //given People people = new People(); Person person = new Person(null, null); people.add(person); Assert.assertTrue(people.contains(person)); + //when + people.remove(person); + //then + Assert.assertFalse(people.contains(person)); + + } + + @Test + public void testRemoveById() { + //given + Long id = Long.MAX_VALUE; + People people = new People(); + Person person = new Person(id, null); + people.add(person); + Assert.assertTrue(people.contains(person)); + + //when + people.remove(id); + + //then + Assert.assertFalse(people.contains(person)); + + } + + @Test + public void testFindById() { + //given + Long id = Long.MAX_VALUE; + People people = new People(); + Person expectedPerson = new Person(id, null); + people.add(expectedPerson); + Assert.assertTrue(people.contains(expectedPerson)); + + //when + Person actualPerson = people.findById(id); + //then + Assert.assertEquals(expectedPerson, actualPerson); } } From 67ed36320443f24d82a2fced8569c096654b208a Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 29 Aug 2023 16:28:49 -0400 Subject: [PATCH 16/23] Created Students singleton, created tests in TestStudents class --- .../java/com/github/curriculeon/Students.java | 16 ++++++++++ .../com/github/curriculeon/TestStudents.java | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Students.java create mode 100644 src/test/java/com/github/curriculeon/TestStudents.java 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..7904068 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,16 @@ +package com.github.curriculeon; + +public final class Students extends People { + + private static Students INSTANCE = new Students(); + + private Students(){ + this.add(new Student(1L, "Jarek")); + this.add(new Student(2L, "Mary")); + this.add(new Student(3L, "Andrew")); + } + + public static Students getInstance(){ + return INSTANCE; + } +} diff --git a/src/test/java/com/github/curriculeon/TestStudents.java b/src/test/java/com/github/curriculeon/TestStudents.java new file mode 100644 index 0000000..b803809 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,29 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class TestStudents { + + @Test + public void testConstructor() { + //given + String[] expectedNames = "Jarek,Andrew,Mary".split(","); + + //when + List nameList = Arrays + .stream(Students.getInstance().toArray()) + .map(student -> student.getName()) + .collect(Collectors.toList()); + + //then + for(String expectedName : expectedNames){ + Assert.assertTrue(nameList.contains(expectedName)); + } + + } +} From d80782b5e6d5e15cc29f06dfeaeb3f7cde57cedf Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 29 Aug 2023 16:40:18 -0400 Subject: [PATCH 17/23] Created Instructors singleton, created tests in TestInstructors class --- .../com/github/curriculeon/Instructors.java | 15 ++++++++++ .../github/curriculeon/TestInstructors.java | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Instructors.java create mode 100644 src/test/java/com/github/curriculeon/TestInstructors.java 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..f269619 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,15 @@ +package com.github.curriculeon; + +public final class Instructors extends People { + + private static final Instructors INSTANCE = new Instructors(); + + private Instructors(){ + this.add(new Person(1L, "Leon")); + this.add(new Person(2L, "Hunter")); + } + + public static Instructors getInstance() { + return INSTANCE; + } +} diff --git a/src/test/java/com/github/curriculeon/TestInstructors.java b/src/test/java/com/github/curriculeon/TestInstructors.java new file mode 100644 index 0000000..8a50691 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,29 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class TestInstructors { + + @Test + public void testConstructor(){ + //given + String[] expectedNames = "Leon,Hunter".split(","); + + //when + List nameList = Arrays + .stream(Instructors.getInstance().toArray()) + .map(instructor -> instructor.getName()) + .collect(Collectors.toList()); + + //then + for(String expectedName : expectedNames){ + Assert.assertTrue(nameList.contains(expectedName)); + } + + } +} From 82ef1d25b9d7631a20fa6d5465487dd6626385a4 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 29 Aug 2023 19:36:00 -0400 Subject: [PATCH 18/23] Created Classroom enum, added test in TestClassroom --- .../com/github/curriculeon/Classroom.java | 38 +++++++++++++++++++ .../com/github/curriculeon/Instructors.java | 4 +- .../com/github/curriculeon/TestClassroom.java | 35 +++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Classroom.java create mode 100644 src/test/java/com/github/curriculeon/TestClassroom.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..8bc3cc0 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,38 @@ +package com.github.curriculeon; + +import java.util.HashMap; +import java.util.Map; + +public enum Classroom { + INSTANCE; + + private Students students = Students.getInstance(); + private Instructors instructors = Instructors.getInstance(); + + public void hostLecture(Teacher teacher, double numberOfHours){ + Person[] personArray = students.toArray(); + Student[] studentArray = new Student[personArray.length]; + for (int i = 0; i < personArray.length; i++) { + Person person = personArray[i]; + Student student = (Student) person; + studentArray[i] = student; + } + teacher.lecture(studentArray, numberOfHours); + } + + public void hostLecture(Long id, double numberOfHours){ + Person person = Instructors.getInstance().findById(id); + Teacher teacher = (Teacher) person; + hostLecture(teacher, numberOfHours); + } + + public Map getStudyMap() { + Map map = new HashMap<>(); + for(Person person : Students.getInstance().toArray()){ + Student student = (Student) person; + double numberOfHours = student.getTotalStudyTime(); + map.put(student, numberOfHours); + } + return map; + } +} diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index f269619..ed05830 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -5,8 +5,8 @@ public final class Instructors extends People { private static final Instructors INSTANCE = new Instructors(); private Instructors(){ - this.add(new Person(1L, "Leon")); - this.add(new Person(2L, "Hunter")); + this.add(new Instructor(1L, "Leon")); + this.add(new Instructor(2L, "Hunter")); } public static Instructors getInstance() { diff --git a/src/test/java/com/github/curriculeon/TestClassroom.java b/src/test/java/com/github/curriculeon/TestClassroom.java new file mode 100644 index 0000000..f1e5768 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -0,0 +1,35 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +public class TestClassroom { + + @Test + public void testHostLecture() { + //given + Classroom classroom = Classroom.INSTANCE; + Teacher teacher = (Teacher) Instructors.getInstance().findById(1L); + double numberOfHours = Students.getInstance().count(); + double hoursPerStudent = numberOfHours / Students.getInstance().count(); + Map preStudyMap = classroom.getStudyMap(); + + //when + classroom.hostLecture(teacher, numberOfHours); + Map postStudyMap = classroom.getStudyMap(); + + for(Map.Entry entry : preStudyMap.entrySet()){ + Student student = entry.getKey(); + double preStudyTime = entry.getValue(); + double actualStudyTime = postStudyMap.get(student); + double expectedStudyTime = preStudyTime + hoursPerStudent; + + //then + Assert.assertEquals(expectedStudyTime, actualStudyTime, 0.001); + } + + + } +} From 4b64d4345f34aaab18c61ae3b623f24baa27e4fd Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Thu, 31 Aug 2023 14:47:36 -0400 Subject: [PATCH 19/23] Modified and refactored multiple classes and test to remove intermediate casting --- .../com/github/curriculeon/Classroom.java | 14 ++------ .../com/github/curriculeon/Instructors.java | 7 +++- .../java/com/github/curriculeon/People.java | 32 +++++++++---------- .../java/com/github/curriculeon/Students.java | 7 +++- .../com/github/curriculeon/TestPeople.java | 16 +++++----- 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java index 8bc3cc0..77158bd 100644 --- a/src/main/java/com/github/curriculeon/Classroom.java +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -10,26 +10,18 @@ public enum Classroom { private Instructors instructors = Instructors.getInstance(); public void hostLecture(Teacher teacher, double numberOfHours){ - Person[] personArray = students.toArray(); - Student[] studentArray = new Student[personArray.length]; - for (int i = 0; i < personArray.length; i++) { - Person person = personArray[i]; - Student student = (Student) person; - studentArray[i] = student; - } + Student[] studentArray = students.toArray(); teacher.lecture(studentArray, numberOfHours); } public void hostLecture(Long id, double numberOfHours){ - Person person = Instructors.getInstance().findById(id); - Teacher teacher = (Teacher) person; + Teacher teacher = Instructors.getInstance().findById(id); hostLecture(teacher, numberOfHours); } public Map getStudyMap() { Map map = new HashMap<>(); - for(Person person : Students.getInstance().toArray()){ - Student student = (Student) person; + for(Student student : Students.getInstance().toArray()){ double numberOfHours = student.getTotalStudyTime(); map.put(student, numberOfHours); } diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index ed05830..3b8ea6c 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -1,6 +1,6 @@ package com.github.curriculeon; -public final class Instructors extends People { +public final class Instructors extends People { private static final Instructors INSTANCE = new Instructors(); @@ -12,4 +12,9 @@ private Instructors(){ public static Instructors getInstance() { return INSTANCE; } + + @Override + public Instructor[] toArray() { + return this.list.toArray(new Instructor[0]); + } } diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 44d2665..8f6e4d8 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -4,16 +4,16 @@ import java.util.Iterator; import java.util.List; -public class People implements Iterable { +abstract public class People implements Iterable { - private List personlist = new ArrayList<>(); + protected List list = new ArrayList<>(); - public void add(Person person){ - personlist.add(person); + public void add(PersonType person){ + list.add(person); } - public Person findById(Long id){ - for (Person person: personlist) { + public PersonType findById(Long id){ + for (PersonType person: list) { if(person.getId() == id){ return person; } @@ -21,12 +21,12 @@ public Person findById(Long id){ return null; } - public boolean contains(Person person){ - return personlist.contains(person); + public boolean contains(PersonType person){ + return list.contains(person); } - public void remove(Person person){ - personlist.remove(person); + public void remove(PersonType person){ + list.remove(person); } public void remove(Long id){ @@ -34,19 +34,17 @@ public void remove(Long id){ } public void removeAll(){ - personlist.clear(); + list.clear(); } public int count(){ - return personlist.size(); + return list.size(); } - public Person[] toArray(){ - return personlist.toArray(new Person[0]); - } + abstract public PersonType[] toArray(); @Override - public Iterator iterator() { - return personlist.iterator(); + public Iterator iterator() { + return list.iterator(); } } diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java index 7904068..71898b6 100644 --- a/src/main/java/com/github/curriculeon/Students.java +++ b/src/main/java/com/github/curriculeon/Students.java @@ -1,6 +1,6 @@ package com.github.curriculeon; -public final class Students extends People { +public final class Students extends People { private static Students INSTANCE = new Students(); @@ -13,4 +13,9 @@ private Students(){ public static Students getInstance(){ return INSTANCE; } + + @Override + public Student[] toArray() { + return this.list.toArray(new Student[0]); + } } diff --git a/src/test/java/com/github/curriculeon/TestPeople.java b/src/test/java/com/github/curriculeon/TestPeople.java index d3d0cf5..d198230 100644 --- a/src/test/java/com/github/curriculeon/TestPeople.java +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -8,8 +8,8 @@ public class TestPeople { @Test public void testAdd() { //given - People people = new People(); - Person person = new Person(null, null); + People people = Instructors.getInstance(); + Instructor person = new Instructor(null, null); Assert.assertFalse(people.contains(person)); //when @@ -22,8 +22,8 @@ public void testAdd() { @Test public void testRemove() { //given - People people = new People(); - Person person = new Person(null, null); + People people = Students.getInstance(); + Student person = new Student(null, null); people.add(person); Assert.assertTrue(people.contains(person)); @@ -39,8 +39,8 @@ public void testRemove() { public void testRemoveById() { //given Long id = Long.MAX_VALUE; - People people = new People(); - Person person = new Person(id, null); + People people = Instructors.getInstance(); + Instructor person = new Instructor(id, null); people.add(person); Assert.assertTrue(people.contains(person)); @@ -56,8 +56,8 @@ public void testRemoveById() { public void testFindById() { //given Long id = Long.MAX_VALUE; - People people = new People(); - Person expectedPerson = new Person(id, null); + People people = Students.getInstance(); + Student expectedPerson = new Student(id, null); people.add(expectedPerson); Assert.assertTrue(people.contains(expectedPerson)); From c3640b11ae8ad6efb084f66610053bffc07b0b2b Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Thu, 31 Aug 2023 15:00:10 -0400 Subject: [PATCH 20/23] Created Educator enum --- .../java/com/github/curriculeon/Educator.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Educator.java 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..f457e9e --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,18 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher{ + LEON, + HUNTER; + + + + @Override + public void teach(Learner learner, double numberOfHours) { + + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + + } +} From 5a6c1fda0496f9d8e3ef359cc46f220fd36a48d8 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Sat, 2 Sep 2023 17:22:21 -0400 Subject: [PATCH 21/23] Added findByName method to People class, filled out Educator enum --- .../java/com/github/curriculeon/Educator.java | 15 ++++++++++++++- src/main/java/com/github/curriculeon/People.java | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/curriculeon/Educator.java b/src/main/java/com/github/curriculeon/Educator.java index f457e9e..11e1ad7 100644 --- a/src/main/java/com/github/curriculeon/Educator.java +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -4,15 +4,28 @@ public enum Educator implements Teacher{ LEON, HUNTER; + private Teacher teacher; + + private double timeWorked; + + Educator(){ + this.teacher = Instructors.getInstance().findByName(name()); + } @Override public void teach(Learner learner, double numberOfHours) { - + teacher.teach(learner, numberOfHours); + timeWorked += numberOfHours; } @Override public void lecture(Learner[] learners, double numberOfHours) { + teacher.lecture(learners, numberOfHours); + timeWorked += numberOfHours; + } + public double getTimeWorked(){ + return timeWorked; } } diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 8f6e4d8..024b0d6 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Objects; abstract public class People implements Iterable { @@ -21,6 +22,15 @@ public PersonType findById(Long id){ return null; } + public PersonType findByName(String name) { + for(PersonType person : list) { + if(Objects.requireNonNull(name).equals(person.getName())) { + return person; + } + } + return null; + } + public boolean contains(PersonType person){ return list.contains(person); } From 9128a34930283f460300e42a97625715a3e20c16 Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 5 Sep 2023 18:21:19 -0400 Subject: [PATCH 22/23] Added tests for Educator --- .../java/com/github/curriculeon/Educator.java | 14 ++-- .../com/github/curriculeon/Instructors.java | 4 +- .../com/github/curriculeon/TestEducator.java | 64 +++++++++++++++++++ 3 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/github/curriculeon/TestEducator.java diff --git a/src/main/java/com/github/curriculeon/Educator.java b/src/main/java/com/github/curriculeon/Educator.java index 11e1ad7..8d19c84 100644 --- a/src/main/java/com/github/curriculeon/Educator.java +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -4,28 +4,26 @@ public enum Educator implements Teacher{ LEON, HUNTER; - private Teacher teacher; private double timeWorked; - Educator(){ - this.teacher = Instructors.getInstance().findByName(name()); - } - - @Override public void teach(Learner learner, double numberOfHours) { - teacher.teach(learner, numberOfHours); + getTeacher().teach(learner, numberOfHours); timeWorked += numberOfHours; } @Override public void lecture(Learner[] learners, double numberOfHours) { - teacher.lecture(learners, numberOfHours); + getTeacher().lecture(learners, numberOfHours); timeWorked += numberOfHours; } public double getTimeWorked(){ return timeWorked; } + + private Teacher getTeacher() { + return Instructors.getInstance().findByName(name().toUpperCase()); + } } diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index 3b8ea6c..acb94e6 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -5,8 +5,8 @@ public final class Instructors extends People { private static final Instructors INSTANCE = new Instructors(); private Instructors(){ - this.add(new Instructor(1L, "Leon")); - this.add(new Instructor(2L, "Hunter")); + this.add(new Instructor(1L, "LEON")); + this.add(new Instructor(2L, "HUNTER")); } public static Instructors getInstance() { diff --git a/src/test/java/com/github/curriculeon/TestEducator.java b/src/test/java/com/github/curriculeon/TestEducator.java new file mode 100644 index 0000000..c259706 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestEducator.java @@ -0,0 +1,64 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestEducator { + + @Test + public void testImplementation(){ + Educator educator = Educator.LEON; + + boolean isTeacher = educator instanceof Teacher; + + Assert.assertTrue(isTeacher); + } + + @Test + public void testTeach(){ + Educator educator = Educator.LEON; + Student student = new Student(null, null); + double preStudyTime = student.getTotalStudyTime(); + double preWorkTime = educator.getTimeWorked(); + double lectureTime = 10.0; + double expectedStudyTime = preStudyTime + lectureTime; + double expectedWorkTime = preWorkTime + lectureTime; + + educator.teach(student, lectureTime); + double actualStudyTime = student.getTotalStudyTime(); + double actualWorkTime = educator.getTimeWorked(); + + Assert.assertEquals(expectedStudyTime, actualStudyTime, 0.001); + Assert.assertEquals(expectedWorkTime, actualWorkTime, 0.001); + + } + + @Test + public void testLecture(){ + /*Student student1 = new Student(null, null); + Student student2 = new Student(null, null); + Student student3 = new Student(null, null); + Student student4 = new Student(null, null);*/ + //Student[] students = new Student[]{student1, student2, student3, student4}; + Student[] students = Students.getInstance().toArray(); + Educator educator = Educator.HUNTER; + double lectureTime = students.length; + double preWorkTime = educator.getTimeWorked(); + double expectedStudyTime = lectureTime/ students.length; + double expectedWorkTime = preWorkTime + lectureTime; + for(Student student : students){ + Assert.assertNotEquals(expectedStudyTime, student.getTotalStudyTime()); + } + + educator.lecture(students, lectureTime); + double actualWorkTime = educator.getTimeWorked(); + for(Student student : students) { + double actualStudyTime = student.getTotalStudyTime(); + + //then + Assert.assertEquals(expectedStudyTime, actualStudyTime, 0.001); + } + + Assert.assertEquals(expectedWorkTime, actualWorkTime, 0.001); + } +} From e7bd96dffebed3c3c3c897de23205d6fcac0847a Mon Sep 17 00:00:00 2001 From: AndrewAscone Date: Tue, 5 Sep 2023 18:59:26 -0400 Subject: [PATCH 23/23] Updated Classroom tests --- .../java/com/github/curriculeon/TestClassroom.java | 14 ++++++++++---- .../java/com/github/curriculeon/TestEducator.java | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/github/curriculeon/TestClassroom.java b/src/test/java/com/github/curriculeon/TestClassroom.java index f1e5768..63bde03 100644 --- a/src/test/java/com/github/curriculeon/TestClassroom.java +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -6,12 +6,9 @@ import java.util.Map; public class TestClassroom { - - @Test - public void testHostLecture() { + public void testHostLecture(Teacher teacher) { //given Classroom classroom = Classroom.INSTANCE; - Teacher teacher = (Teacher) Instructors.getInstance().findById(1L); double numberOfHours = Students.getInstance().count(); double hoursPerStudent = numberOfHours / Students.getInstance().count(); Map preStudyMap = classroom.getStudyMap(); @@ -32,4 +29,13 @@ public void testHostLecture() { } + @Test + public void testHostLectureEducator() { + testHostLecture(Educator.LEON); + } + + @Test + public void testHostLectureInstructor(){ + testHostLecture(Instructors.getInstance().findById(1L)); + } } diff --git a/src/test/java/com/github/curriculeon/TestEducator.java b/src/test/java/com/github/curriculeon/TestEducator.java index c259706..73764e4 100644 --- a/src/test/java/com/github/curriculeon/TestEducator.java +++ b/src/test/java/com/github/curriculeon/TestEducator.java @@ -38,8 +38,9 @@ public void testLecture(){ /*Student student1 = new Student(null, null); Student student2 = new Student(null, null); Student student3 = new Student(null, null); - Student student4 = new Student(null, null);*/ - //Student[] students = new Student[]{student1, student2, student3, student4}; + Student student4 = new Student(null, null); + Student[] students = new Student[]{student1, student2, student3, student4};*/ + //SEE ALSO: Implementation in TestInstructor Student[] students = Students.getInstance().toArray(); Educator educator = Educator.HUNTER; double lectureTime = students.length;