Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/com/github/curriculeon/Instructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 method invoking learn() method which invokes learn method on specified Learner object
learner.learn(numberOfHours);
}

@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);

}


}
}
25 changes: 25 additions & 0 deletions src/main/java/com/github/curriculeon/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.curriculeon;

import java.util.List;


public class Instructors extends People<Instructor> {
private static final Instructors instance = new Instructors();

public Instructors() {
this.add(new Instructor(0L, "Leon Hunter"));
this.add(new Instructor(1L, "Haseeb Muhammad"));
}

@Override
public Instructor[] toArray() {
int sizeOfArray = count();
Instructor[] destinationArray = new Instructor[sizeOfArray];
List<Instructor> sourceList = personList;
return sourceList.toArray(destinationArray);
}

public static Instructors getInstance() {
return instance;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/curriculeon/Learner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.curriculeon;

public interface Learner {
void learn(Double numberOfHours);
Double getTotalStudyTime();
}
76 changes: 76 additions & 0 deletions src/main/java/com/github/curriculeon/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.curriculeon;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



//Created class People

public abstract class People<SomeLearnerType extends Person> implements Iterable<SomeLearnerType> {

//Instantiated a List field of Person objects named personList
//Only Declaration
List<SomeLearnerType> personList;

public People() {
this(new ArrayList<>());
}

public People(List<SomeLearnerType> personList) {
this.personList = personList;
}

public void add(SomeLearnerType person) {
this.personList.add(person);
}

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 lookin'!
}
} // finished loop; we've finished lookin'
return null; // we were not able to find the person with the id
}

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 lookin'!
}
} // finished loop; we've finished lookin'
return null; // we were not able to find the person with the id
}

public Boolean contains(SomeLearnerType specifiedPerson) {
return personList.contains(specifiedPerson);
}

public void remove(SomeLearnerType someSpecificPerson) {
personList.remove(someSpecificPerson);
}

public void removeAll() {
personList.clear();
}

public int count() {
return personList.size();
}

abstract public SomeLearnerType[] toArray();

@Override
public Iterator<SomeLearnerType> iterator() {
return personList.iterator();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/github/curriculeon/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package com.github.curriculeon;

public class Person {
//non primitive Long
//Changing variables to Private
private final Long id;
private String name;

public Person() {
this.id = null;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Person(Long id, String name) {
this.id = id;
this.name = name;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/github/curriculeon/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.curriculeon;

public class Student extends Person implements Learner {

//Instance variable
private double totalStudyTime;

//Constructor Student takes the argument of Long and String, passes the parameters to SuperConstructor
public Student(Long id, String name) {
super(id, name);
}
//Implement the method learn which takes the argument Double
@Override
public void learn(Double numberOfHours) {
//increments totalStudyTime variable by specified numberOfHours argument
this.totalStudyTime+=numberOfHours;
}
//Implement the method getTotalStudyTime which returns totalStudyTime instance variable
@Override
public Double getTotalStudyTime() {
return totalStudyTime;

}
}
28 changes: 28 additions & 0 deletions src/main/java/com/github/curriculeon/Students.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.curriculeon;

import java.util.List;

public class Students extends People<Student> {
private static final Students instance = new Students();

//private constructor to avoid client applications to use constructor
private Students(){
super(); // optional invocation
this.add(new Student(0L, "Julia"));
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<Student> sourceList = personList;
return sourceList.toArray(destinationArray);
}

public static Students getInstance(){
return instance;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/curriculeon/Teacher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.curriculeon;

public interface Teacher {
void teach(Learner learner, Double numberOfHours);
void lecture(Learner[] learners, double numberOfHours);
}
98 changes: 98 additions & 0 deletions src/test/java/com/github/curriculeon/TestConstructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.github.curriculeon;

import org.junit.Assert;
import org.junit.Test;

public class TestConstructor {
//create a testImplementation method that asserts that an instructor is an instance of teacher

@Test
public void testImplementation() {

//given
Instructor instructor = new Instructor(null, null);

//when
boolean outcome = instructor instanceof Teacher;


//then
Assert.assertTrue(outcome);

// (all in one line)Assert.assertTrue(new Instructor(null, null)instanceof Teacher);
}

@Test
public void testInheritance() {

//given
Instructor instructor = new Instructor(null, null);

//when
boolean outcome = instructor instanceof Person;


//then
Assert.assertTrue(outcome);
}

@Test
public void testTeach(){

//given
Instructor instructor=new Instructor(null, null);
Learner learner=new Student(null, null);
Double numberOfHoursToTeach=134.0;
Double preStudyTime=learner.getTotalStudyTime();
//total study time before giving the lecture
Double expected=preStudyTime + numberOfHoursToTeach;

//when
//when an Instructor invokes teach method, respective student's totalStudyTime instance variable incremented by specified numberOfHours
//instructor is able to teach some learner for some hours, here teacher is teaching for some numberOfHoursToTeach
instructor.teach(learner, numberOfHoursToTeach);
Double actual=learner.getTotalStudyTime();



//then
//exected is equal to actual such that actual is equal to some TotalStudyTime
Assert.assertEquals(expected, actual);


}

@Test
public void testLecture() {
//lecture method ensures when an instructor invokes lecture method, respective array of students totalStudyTime instance variables is incremented by numberOfHours/students.length

//given
Teacher teacher=new Instructor(null, null);
Learner[] learners=new Learner[]{
new Student(0L,"leon"),
new Student(1L, "Christopher"),
new Student(2L, "Leon 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);
//Assert.assertEquals(numberOfHoursToTeach, totalStudyTime);

}


}
}
19 changes: 19 additions & 0 deletions src/test/java/com/github/curriculeon/TestInstructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.curriculeon;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;


public class TestInstructors {
@Test
public void test() {
Instructors instructors = Instructors.getInstance();
List<String> expectedNames = Arrays.asList("Leon Hunter", "Haseeb Muhammad");
for(Instructor instructor : instructors) {
Assert.assertTrue(expectedNames.contains(instructor.getName()));
}
}
}
Loading