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

import java.util.HashMap;
import java.util.Map;

public class Classroom {

private static final Classroom INSTANCE = new Classroom();
Students students=Students.getInstance();
Instructors instructors=Instructors.getInstance();

private Classroom() {

}


public static Classroom getInstance() {
return INSTANCE;
}


public void hostLecture(Teacher teacher, double numberOfHours){
teacher.lecture( students.toArray(),numberOfHours);

}
public void hostLecture(long id, double numberOfHours){
Instructor instructor=instructors.findById(id);
instructor.lecture( students.toArray(),numberOfHours);
}

public Map<Student,Double> getStudyMap(){
Map<Student,Double> studentMap=new HashMap<>();

for(Student student1 : students.toArray()) {
Double studyTime =student1.getTotalStudyTime();
studentMap.put(student1,studyTime);
}
return studentMap;
}


}


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

}

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

import java.util.List;

public class Instructors extends People<Instructor> {

private static final Instructors INSTANCE = new Instructors();

private Instructors() {
super();
this.add(new Instructor(01L, "Leon"));
this.add(new Instructor(02L, "Haseeb"));
}

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


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

public interface Learner {

public void learn(double numberOfHours);

public double getTotalStudyTime();


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

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

abstract public class People <SomeType extends Person>implements Iterable{
List<SomeType> personList;

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

public People(List<SomeType> personList) {

this.personList = personList;
}

public void add(SomeType person) {

personList.add(person);
}

public SomeType findById(long id) {
for (SomeType person : personList) {
if (person.getId() == id) {
return person;
}
}
return null;
}

public Boolean contains(SomeType person) {
return personList.contains(person);

}

public void remove(SomeType person) {
if (personList.contains(person)) {
personList.remove(person);
}

}

public void removeById(long id) {
SomeType person = findById(id);
personList.remove(person);

}

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

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

abstract public SomeType[] toArray();


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

public class Person {
private Long id;
private String name;

public Person(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

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

public Person(){
this.id = null;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/github/curriculeon/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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) {
totalStudyTime+=numberOfHours;

}

@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 Students() {
super();
this.add(new Student(01L, "Anjali"));
this.add(new Student(02L, "Deepti"));
}

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


public static Students getInstance() {
return INSTANCE;
}


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

import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;
import java.util.Set;

public class ClassroomTest extends TestCase {
@Test
public void testHostLecture() {
//given
Classroom classroom = Classroom.getInstance();
Teacher teacher = Instructors.getInstance().findById(1L);
Integer numberOfStudents = Students.getInstance().count();
Double numberOfHoursToLecture = numberOfStudents.doubleValue();
Double expectedHoursLearned = numberOfHoursToLecture / numberOfStudents;
Map<Student, Double> preStudyMap = classroom.getStudyMap();


//when
classroom.hostLecture(teacher, numberOfHoursToLecture);
Map<Student, Double> postStudyMap = classroom.getStudyMap();
Set<Student> keySet = postStudyMap.keySet();
for (Student student : keySet) {
Double preStudyTime = preStudyMap.get(student);
Double expectedStudyTime = preStudyTime + expectedHoursLearned;
Double actualStudyTime = postStudyMap.get(student);

// then
Assert.assertEquals(expectedStudyTime, actualStudyTime);

}


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

import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;

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

public class InstructorsTest extends TestCase {


@Test
public void testGetInstance() {
Instructors instructor = Instructors.getInstance();
String[] instructorNameArray = {"Leon", "Haseeb"};
List<String> listInstructor = Arrays.asList(instructorNameArray);

Person[] instructorArray = instructor.toArray();
for (int i = 0; i < instructorArray.length; i++) {
Person person = instructorArray[i];
String personName = person.getName();
boolean hasPersonWithName = listInstructor.contains(personName);
Assert.assertTrue(hasPersonWithName);

}
}

}
Loading