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
17 changes: 17 additions & 0 deletions src/main/java/com/github/curriculeon/Classroom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.curriculeon;

public class Classroom {
private Students students = Students.getInstance();
private Instructors instructors = Instructors.getInstance();

public void hostLecture(Teacher teacher, Double numberOfHours){
teacher.lecture((Learner[])students.toArray(), numberOfHours);
}

public void hostLecture(long id, Double numberOfHours) {
Person person = instructors.findById(id);
Instructor instructor = (Instructor)person;
instructor.lecture((Learner[])students.toArray(), numberOfHours);
}

}
23 changes: 23 additions & 0 deletions src/main/java/com/github/curriculeon/Instructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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){
Double numberOfHoursPerLearner = numberOfHours / learners.length;
for (int i = 0; i < learners.length; i++) {
Learner learner = learners[i];
learner.learn(numberOfHoursPerLearner);

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

public final class Instructors extends People {
private static final Instructors INSTANCE = new Instructors();

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

public static Instructors getInstance() {
return INSTANCE;
}

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

public interface Learner {
public void learn(Double numberOfHours);

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

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

public class People implements Iterable<Person> {
List<Person> personList = new ArrayList<>();

public void add (Person person) {

this.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) {

return personList.contains(person);
}

public void remove(Person person) {

personList.remove(person);
}

public void remove(long id) {
for(Person person : personList) {
if (person.getId() == id) {
personList.remove(person);
}
}
}

public void removeAll() {

personList.clear();
}

public int count() {

return personList.size();
}

public Person[] toArray() {
int arrayLength = personList.size();
Person[] newArray = new Person[arrayLength];
return personList.toArray(newArray);
}

public Iterator<Person> 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 {
private final Long id;
private String name;

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

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;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/github/curriculeon/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.curriculeon;

public class Student extends Person implements Learner {
private Double totalStudyTime = 2.0;

public Student(Long id, String name) {
super(id, name);
}

@Override
public void learn(Double numberOfHours){
this.totalStudyTime = totalStudyTime + numberOfHours;
}

@Override
public Double getTotalStudyTime() {

return totalStudyTime;
}

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

public final class Students extends People {
private static final Students INSTANCE = new Students();

private Students(){
super();
this.add(new Student(0L, "Student A"));
this.add(new Student(1L, "Student B"));
this.add(new Student(2L, "Student C"));

}

public static Students getInstance(){

return INSTANCE;
}


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

public interface Teacher {

void teach(Learner learner, Double numberOfHours);

void lecture(Learner[] learners, Double numberOfHours);

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

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

public class TestInstructor {
@Test
public void testImplementation(){
//given
Instructor instructor = new Instructor(null, null);

//when
boolean outcome = instructor instanceof Teacher;

//then
Assert.assertTrue(outcome);
}


@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();
Double expected = preStudyTime + numberOfHoursToTeach;

//when
instructor.teach(learner, numberOfHoursToTeach);
Double actual = learner.getTotalStudyTime();

//then
Assert.assertEquals(expected, actual);
}

@Test
public void testLecture() {
//given
Teacher teacher = new Instructor(null, null);
Student baseStudent = new Student(null, null);
Learner[] learners = new Learner[]{
new Student(0L, "John"),
new Student(1L, "Q"),
new Student(2L, "Public")
};
Double numberOfHours = 128.0;
Double expected = numberOfHours/learners.length + baseStudent.getTotalStudyTime();

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

}
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/github/curriculeon/TestInstructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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() {
//given
Instructors instructors = Instructors.getInstance();
List<String> instructorNameList = Arrays.asList("Leon", "Haseeb");

//when
Person[] instructorArray = instructors.toArray();
for(int i = 0; i < instructorArray.length; i++) {
Person person = instructorArray[i];
String personName = person.getName();
boolean hasPersonWithName = instructorNameList.contains(personName);

//then
Assert.assertTrue(hasPersonWithName);
}
}
}
55 changes: 55 additions & 0 deletions src/test/java/com/github/curriculeon/TestPeople.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.curriculeon;

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

public class TestPeople {

@Test
public void testAdd() {
//given
People peopleList = new People();
Person person1 = new Person();

//when
peopleList.add(person1);

//then
Assert.assertTrue(peopleList.contains(person1));
}

@Test
public void testRemove() {
//given
People peopleList = new People();
Person person1 = new Person();
peopleList.add(person1);
Boolean expected = peopleList.contains(person1);

//when
peopleList.remove(person1);

//then
Assert.assertFalse(peopleList.contains(person1));
}

@Test
public void testFindById() {
//given
People peopleList = new People();
Person person1 = new Person(575L, "Joe");
peopleList.add(person1);
Boolean expected = peopleList.contains(person1);

// when
peopleList.findById(575L);

//then
Assert.assertTrue(peopleList.contains(peopleList.findById(575L)));


}



}
Loading