From 2f7743aa10f1a9b3d6c64477ebd032b66f67d287 Mon Sep 17 00:00:00 2001 From: Julia Waclawek Date: Tue, 28 Jul 2020 15:34:28 -0400 Subject: [PATCH] working on part 11 and 12 --- .../com/github/curriculeon/Classroom.java | 38 ++++++++ .../java/com/github/curriculeon/Educator.java | 32 +++++++ .../com/github/curriculeon/Instructor.java | 38 ++++++++ .../com/github/curriculeon/Instructors.java | 33 +++++++ .../java/com/github/curriculeon/Learner.java | 17 ++++ .../java/com/github/curriculeon/People.java | 86 +++++++++++++++++++ .../java/com/github/curriculeon/Person.java | 26 +++++- .../java/com/github/curriculeon/Student.java | 28 ++++++ .../java/com/github/curriculeon/Students.java | 34 ++++++++ .../java/com/github/curriculeon/Teacher.java | 22 +++++ .../com/github/curriculeon/TestClassroom.java | 37 ++++++++ .../com/github/curriculeon/TestEducator.java | 4 + .../github/curriculeon/TestInstructor.java | 78 +++++++++++++++++ .../github/curriculeon/TestInstructors.java | 22 +++++ .../com/github/curriculeon/TestPeople.java | 76 ++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 41 ++++++++- .../com/github/curriculeon/TestStudent.java | 53 ++++++++++++ .../com/github/curriculeon/TestStudents.java | 28 ++++++ 18 files changed, 691 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Classroom.java create mode 100644 src/main/java/com/github/curriculeon/Educator.java create mode 100644 src/main/java/com/github/curriculeon/Instructor.java create mode 100644 src/main/java/com/github/curriculeon/Instructors.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/Students.java create mode 100644 src/main/java/com/github/curriculeon/Teacher.java create mode 100644 src/test/java/com/github/curriculeon/TestClassroom.java create mode 100644 src/test/java/com/github/curriculeon/TestEducator.java create mode 100644 src/test/java/com/github/curriculeon/TestInstructor.java create mode 100644 src/test/java/com/github/curriculeon/TestInstructors.java create mode 100644 src/test/java/com/github/curriculeon/TestPeople.java create mode 100644 src/test/java/com/github/curriculeon/TestStudent.java create mode 100644 src/test/java/com/github/curriculeon/TestStudents.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..ea71856 --- /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; + +// Create a Classroom singleton. +public enum Classroom { + INSTANCE; + // The class should declare a field that references the instance of Students called students. + private Students students = Students.getInstance(); + // The class should declare a field that references the instance of Instructors called instructors. + private Instructors instructors = Instructors.getInstance(); + + // 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. + public void hostLecture(Teacher teacher, Double numberOfHours) { + teacher.lecture(students.toArray(), numberOfHours); + } + // 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. + public void hostLecture(long id, double numberOfHours) { + Teacher instructor = instructors.findById(id); + instructor.lecture(students.toArray(), numberOfHours); + } + // 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. + public Map getStudyMap() { + Map result = new HashMap<>(); + for(Student student : students.toArray()) { + Double studyTime = student.getTotalStudyTime(); + result.put(student, studyTime); + } + return result; + } +} \ No newline at end of file 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..49d3074 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,32 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher { + LEON, + HASEEB; + 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; + } +} + + /*Create an enum named Educator. + The enum should implement Teacher. + The enum should have an enumeration for each of the instructors represented in the Instructors class. + Upon construction each enumeration of the enum should instantiate a respective Instructor and assign it to a final instructor field upon construction. The instructor should be added to the Instructors singleton. + Calls to the teach and lecture method should be deferred to the composite instructor reference. + The enum should have a double timeWorked field which keeps track of the hours that the Educator has taught.*/ \ No newline at end of file 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..e8e54b9 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,38 @@ +package com.github.curriculeon; + + +// Create an Instructor class such that: + //Instructor is a subclass of Person + //Instructor implements the Teacher interface +public class Instructor extends Person implements Teacher { + + // create 'Instructor' constructor + public Instructor(Long id, String name) { + super(id, name); + } + +// Instructor should have a concrete implementation of the teach method +// which invokes the learn method on the specified Learner object. + @Override + public void teach(Learner learner, Double numberOfHours) { + learner.learn(numberOfHours); +} + +//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; + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learners.length; + for (int i = 0; i < learners.length; i++) { + Learner learner = learners[i]; + learner.learn(numberOfHoursPerLearner); + } +} + + + + + +} \ 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 new file mode 100644 index 0000000..e9363e8 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,33 @@ +package com.github.curriculeon; + +import java.util.List; + +// Create a Instructors singleton which represents the set of instructors. +// Create a Instructors class. +// The class should be an unextendable subclass of the People class. +public class Instructors extends People { + + // The class should statically instantiate a final field named INSTANCE of type Instructors. + private static final Instructors instance = new Instructors(); + + // The class should define a private nullary constructor + // which populates the INSTANCE field with respective Instructor representations of your colleagues. + public Instructors() { + this.add(new Instructor(0L, "Leon Hunter")); // Each instructor should have a relatively unique id field. + this.add(new Instructor(1L, "Haseeb Muhammad")); + } + @Override + public Instructor[] toArray() { + int sizeOfArray = count(); + Instructor[] destinationArray = new Instructor[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + // The class should define a getInstance method which returns the INSTANCE field + public static Instructors getInstance() { + return instance; + } +} + + + 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..1c01929 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,17 @@ +package com.github.curriculeon; + +//Create a Learner interface. +public interface Learner { + void learn(Double numberOfHours); +/*Learner should declare method signature: + /*Method name: learn + Method parameters: double numberOfHours + Method return-type: void + */ + +/*Learner should declare method signature: + Method name: getTotalStudyTime + Method return-type: Double + */ + 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..155a346 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,86 @@ +package com.github.curriculeon; + +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +//Create a People class +// The class should instantiate a List field of Person objects named personList. +abstract public class People implements Iterable { + List personList; + + public People() { + this(new ArrayList<>()); + } + // Create 'People' Constructor + public People(List personList) { + //set a List field of Person objects named personList to the respective value + this.personList = personList; + } + + // The class should define a method named add which adds a Person to the personList. + public void add(SomeLearnerType person) { + this.personList.add(person); + } + // 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. + public SomeLearnerType findById(long id) { + for (int i = 0; i < personList.size(); i++) { + SomeLearnerType person = personList.get(i); + if (person.getId() == id) { //if the id is correct, + return person; // return person + } else { // if its the wrong id, + continue; // keep looking + } + } // finished loop; we've finished looking + return null; // we were not able to find the person with the id + } + + // 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. + public SomeLearnerType findByIdExpanded(long id) { + for (int i = 0; i < personList.size(); i++) { + SomeLearnerType person = personList.get(i); + if (person.getId() == id) { //if the id is correct, + return person; // return person + } else { // if its the wrong id, + continue; // keep looking + } + } // finished loop; we've finished looking + return null; // we were not able to find the person with the id + } + public Boolean contains(SomeLearnerType specifiedPerson) { + return personList.contains(specifiedPerson); + } + // The class should define a method named remove + // which makes use of a Person person parameter to remove a respective Person object. + public void remove(SomeLearnerType someSpecificPerson) { + personList.remove(someSpecificPerson); + } + + // 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. + public void removeAll() { + personList.clear(); + } + // The class should define a method named count + // which returns the size of personList. + public int count() { + return personList.size(); + } + + // The class should define a method named toArray + // which returns an array representation of the personList field. + + abstract public SomeLearnerType[] toArray(); + // The class should implement Iterable and define a method named iterator + // which makes use of the personList field to generate a new a Iterator + + @Override + public Iterator iterator() { + return personList.iterator(); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..2f85fcd 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,29 @@ package com.github.curriculeon; +//create a 'Person' class public class Person { + private final Long id;//declare final field named 'id' of type 'long' + private String name;//declare a field named 'name' of type String -} + // create 'Person' constructor + public Person() { + this.id = null; + } + public Person(Long id, String name) { // with parameters of type long and String + //sets the id and name field to the respective values + this.id = id; + this.name = name; + } + //class should define a getId() method which returns the Person object's id field. + public Long getId() { + return id; + } + //define a getName() method which returns the Person object's name field. + public String getName() { + return name; + } + //define a setName() method which sets the Person object's name field. + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file 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..460f466 --- /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 +public class Student extends Person implements Learner { + //Student should have an instance variable totalStudyTime of type double + private double totalStudyTime; + //create 'Student' constructor + public Student(Long id, String name) { + //sets the id and name field to the respective values + super(id, name); + } + + //Student should have a concrete implementation of the learn method + // which increments (+) the totalStudyTime variable by the specified numberOfHours argument. + @Override + public void learn(Double numberOfHours) { + this.totalStudyTime = totalStudyTime + numberOfHours; + } + //Student should have a getTotalStudyTime() method + // which returns the totalStudyTime instance variable. + @Override + public Double getTotalStudyTime() { + return totalStudyTime; + } +} \ No newline at end of file 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..a44fc04 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,34 @@ +package com.github.curriculeon; + +import java.util.List; + +// Students singleton - create a Students class. +//The class should be an unextendable subclass of the People class. +public class Students extends People{ + // The class should statically instantiate a final field named INSTANCE of type Students. + private static final Students instance = new Students(); + + // The class should define a private nullary constructor + // which populates the INSTANCE field with respective Student representations of your colleagues. + //private constructor to avoid client applications to use constructor + private Students(){ + super(); // optional invocation + this.add(new Student(0L, "Julia")); // Each student should have a relatively unique id field. + this.add(new Student(1L, "David Y")); + this.add(new Student(2L, "Ghasan")); + } + @Override + public Student[] toArray() { + int sizeOfArray = count(); + Student[] destinationArray = new Student[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + + // The class should define a getInstance method which returns the INSTANCE field + public static Students getInstance(){ + return instance; + } +} + + 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..6ab015b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,22 @@ +package com.github.curriculeon; + +// Create a Teacher interface. +public interface Teacher { + /*Teacher should declare a teach method signature: + Method name: teach + Method parameters: + Learner learner + double numberOfHours + Method return-type: void + */ + void teach(Learner learner, Double numberOfHours); + + /* Teacher should declare a lecture method signature: + Method name: lecture + Method parameters: + Learner[] learners + double numberOfHours + Method return-type: void + */ + void lecture(Learner[] learners, Double numberOfHours); +} 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..19a71f4 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -0,0 +1,37 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; +import java.util.Set; + + +public class TestClassroom { + @Test + // Create a testHostLecture method which ensures that each of the Student's totalStudyTime instance variable + // is incremented by the specified numberOfHours upon invoking the hostLecture method. + public void testHostLecture() { + // given + Classroom classroom = Classroom.INSTANCE; + Teacher teacher = Instructors.getInstance().findById(0L); + Integer numberOfStudents = Students.getInstance().count(); + Double numberOfHoursToLecture = numberOfStudents.doubleValue(); + Double expectedNumberOfHoursLearned = numberOfHoursToLecture / numberOfStudents; + Map preStudyMap = classroom.getStudyMap(); + + // when + classroom.hostLecture(teacher, numberOfHoursToLecture); + Map postStudyMap = classroom.getStudyMap(); + Set keySet = postStudyMap.keySet(); + for (Student student : keySet) { + Double preStudyTime = preStudyMap.get(student); + Double expectedStudyTime = preStudyTime + expectedNumberOfHoursLearned; + Double actualStudyTime = postStudyMap.get(student); + + // then + Assert.assertEquals(expectedStudyTime, actualStudyTime); + } + + } +} 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..bd10c9e --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestEducator.java @@ -0,0 +1,4 @@ +package com.github.curriculeon; + +public class TestEducator { +} 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..917fb8b --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,78 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + + +//Create a TestInstructor class. +public class TestInstructor { + @Test + //Create a testImplementation method that asserts + // that an Instructor is an instanceof a Teacher. + public void testImplementation() { + // given + Instructor instructor = new Instructor(null, null); + + // when + boolean outcome = instructor instanceof Teacher; + + // then + Assert.assertTrue(outcome); + } + //Create a testInheritance method + // that asserts that a Instructor is an instanceof a Person. + public void testInheritance() { + // given + Instructor instructor = new Instructor(null, null); + + // when + boolean outcome = instructor instanceof Person; + + // then + Assert.assertTrue(outcome); + } + + @Test + //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. + public void testTeach() { + // given + Instructor instructor = new Instructor(null, null); + Learner learner = new Student(null, null); + Double numberOfHoursToTeach = 134.0; + Double preStudyTime = learner.getTotalStudyTime(); + Double expected = preStudyTime + numberOfHoursToTeach; + + // when + instructor.teach(learner, numberOfHoursToTeach); + Double actual = learner.getTotalStudyTime(); + + // then + Assert.assertEquals(expected, actual); + } + + @Test + //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. + public void testLecture() { + // given + Teacher teacher = new Instructor(null, null); + Learner[] learners = new Learner[]{ + new Student(0L, "Leon"), + new Student(1L, "Christopher"), + new Student(2L, "Hunter"), + }; + Double numberOfHours = 128.0; + Double expected = numberOfHours/learners.length; + + // when + teacher.lecture(learners, numberOfHours); + + // then + for (int i = 0; i < learners.length; i++) { + Learner learner = learners[i]; + Double actual = learner.getTotalStudyTime(); + Assert.assertEquals(expected, actual); + } + } +} 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..b68d39c --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,22 @@ +package com.github.curriculeon; + + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +// Create a TestInstructors class. +public class TestInstructors { + @Test + // Create a test method which ensures that each of the instructors in your current cohort + // are in your Instructors singleton. + public void test() { + Instructors instructors = Instructors.getInstance(); + List expectedNames = Arrays.asList("Leon Hunter", "Haseeb Muhammad"); + for(Instructor instructor : instructors) { + Assert.assertTrue(expectedNames.contains(instructor.getName())); + } + } +} 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..e4a406a --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,76 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +// Create a TestPeople class +public class TestPeople { + + // Create a testAdd method which ensures that our personList in our People class + // populated with respective Person objects following invocation of the add method. + public void testAdd() { + // given + People people = Instructors.getInstance(); + Person person1 = new Person(); + + Assert.assertFalse(people.contains(person1)); + + // when + people.add(person1); + + + // then + Assert.assertTrue(people.contains(person1)); + + } + + + // Create a testRemove method which ensures that the personList in a People object + // is depopulated with a respective Person object following the invocation of the remove method. + @Test + public void testRemove() { + // given + People people = Instructors.getInstance(); + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + + people.add(person1); + people.add(person2); + people.add(person3); + + Assert.assertTrue(people.contains(person1)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + + // when + people.remove(person1); + people.remove(person2); + people.remove(person3); + + // then + Assert.assertFalse(people.contains(person1)); + Assert.assertFalse(people.contains(person2)); + Assert.assertFalse(people.contains(person3)); + } + + // Create a testFindById method which ensures that a respective Person object with a respective id field + // is returned upon invocation of the findById method on a respective People object. + @Test + public void testFindById() { + // given + People people = Instructors.getInstance(); + people.removeAll(); + Person expected = new Person(0L, null); + + people.add(expected); + + Assert.assertTrue(people.contains(expected)); + + // when + Person actual = people.findById(expected.getId()); + + // then + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..88710b9 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,44 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + +import java.util.OptionalLong; + +// Create a TestPerson class. public class TestPerson { + @Test + // Create a testConstructor method + // to ensure that a Person object's id and name field are being set upon construction. + public void testConstructor() { + // given + Long expectedId = 0L; + String expectedName = "Some name"; + + // 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 + // Create a testSetName method + // ensure that a Person object's name variable is being set by invoking the .setName method. + public void testSetName() { + // given + Person person = new Person(null, null); + String expectedName = "Some name"; + Assert.assertNotEquals(expectedName, person.getName()); + + // when + person.setName(expectedName); + String actualName = person.getName(); -} + // then + Assert.assertEquals(expectedName, actualName); + } +} \ No newline at end of file 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..2a5ce8a --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,53 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +// Create a TestStudent class. +public class TestStudent { + @Test + // Create a testImplementation method that asserts + // that a Student is an instanceof a Learner. + public void testImplementation() { + // given + Student student = new Student(null, null); + + // when + Boolean result = student instanceof Learner; + + // then + Assert.assertTrue(result); // 'result' is always true + } + + @Test + // Create a testInheritance method that asserts + // that a Student is an instanceof a Person. + public void testInheritance() { + // given + Student student = new Student(null, null); + + // when + Boolean result = student instanceof Person; + + // then + Assert.assertTrue(result); // or true + } + + @Test + // Create a testLearn method that ensures a Student's totalStudyTime instance variable + // is incremented by the specified numberOfHours by invoking the .learn method. + public void testLearn() { + // given + Student student = new Student(null, null); + Double numberOfHoursToLearn = 98.0; + Double preStudyTime = student.getTotalStudyTime(); + Double expected = preStudyTime + numberOfHoursToLearn; + + // when + student.learn(numberOfHoursToLearn); + Double actual = student.getTotalStudyTime(); + + // then + Assert.assertEquals(expected, actual, 0.01); + } +} \ No newline at end of file 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..ede00bd --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +// Create a TestStudents class +public class TestStudents { + // Create a test method which ensures that + // each of the students in your current cohort are in your Students singleton. + @Test + public void test() { + Students students = Students.getInstance(); + String[] studentNameArray = {"Julia", "David Y", "Ghasan"}; + List studentNameList = Arrays.asList(studentNameArray); + + Person[] studentArray = students.toArray(); + for (int i = 0; i < studentArray.length; i++) { + Person person = studentArray[i]; + String personName = person.getName(); + boolean hasPersonWithName = studentNameList.contains(personName); + Assert.assertTrue(hasPersonWithName); + } + } +} \ No newline at end of file