Skip to content

maybe i did it right? #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions interfaces-1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/interfaces/ConcretePeople.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.interfaces;

public class ConcretePeople extends People<Person> {

public Person[] toArray() {
Person[] personArray = new Person[this.count()];
for (int i = 0; i < this.count(); i++) {
personArray[i] = this.personList.get(i);
}
return personArray;
}
}
30 changes: 30 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Educator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.zipcoder.interfaces;

public enum Educator implements Teacher {
INSTRUCTOR0(Long.valueOf(1), "instructor0"), INSTRUCTOR1(Long.valueOf(2), "instructor1"), INSTRUCTOR2(Long.valueOf(3), "instructor2"), INSTRUCTOR3(Long.valueOf(4), "instructor3"), INSTRUCTOR4(Long.valueOf(5), "instructor4");

private Instructor instructor;
private Double timeWorked = 0.0;

Educator (Long id, String name) {
this.instructor = new Instructor(id, name);
}

public void teach(Learner learner, Double numberOfHours) {
timeWorked += numberOfHours;
learner.learn(numberOfHours);
}

public void lecture(Learner[] learners, Double numberOfHours) {
Double numberOfHoursPerLearner = numberOfHours / learners.length;
for (Learner learner : learners) {
teach(learner, numberOfHoursPerLearner);
}
}

public Double getTimeWorked() {
return timeWorked;
}


}
19 changes: 19 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Instructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.zipcoder.interfaces;

public class Instructor extends Person implements Teacher {

public Instructor(Long id, String name) {
super(id, name);

}
public void teach(Learner learner, Double numberOfHours) {
learner.learn(numberOfHours);
}

public void lecture(Learner[] learners, Double numberOfHours) {
Double numberOfHoursPerLearner = numberOfHours / learners.length;
for (Learner learner: learners) {
learner.learn(numberOfHoursPerLearner);
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.zipcoder.interfaces;

public class Instructors extends People<Instructor> {
private static final Instructors INSTANCE;

private Instructors() {}

public static Instructors getInstance() {
if (INSTANCE == null) {
INSTANCE.add(new Instructor(Long.valueOf(3), "Wes"));
}
return INSTANCE;
}

static {
INSTANCE = new Instructors();
for (int i = 0; i < 5; i++) {
INSTANCE.add(new Instructor(Long.valueOf(i+1), "instructor"+i));
}

}

public Instructor[] toArray() {
return personList.toArray(new Instructor[0]);
}
}
8 changes: 8 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Learner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.zipcoder.interfaces;

public interface Learner {
void learn(Double numberOfHours);

Double getTotalStudyTime();

}
67 changes: 67 additions & 0 deletions src/main/java/io/zipcoder/interfaces/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.zipcoder.interfaces;

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

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

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

public E findById(Long id) {
E target = null;
for (E person: this.personList) {
if (person.getId().equals(id)) {
target = person;
break;
}
}
return target;
}

public Boolean contains(E person) {
Boolean exists = false;
for (E eachPerson: this.personList) {
if (eachPerson.getName().equals(person.getName())
&& eachPerson.getId().equals(person.getId())) {
exists = true;
break;
}
}
return exists;
}

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

public void remove(Long id) {
E personToRemove = this.findById(id);
this.remove(personToRemove);
}

public void removeAll() {
this.personList = new ArrayList<E>();
}

public Integer count() {
return this.personList.size();
}

public abstract E[] toArray();
// Person[] personArray = new Person[this.count()];
// for (int i = 0; i < this.count(); i++) {
// personArray[i] = this.personList.get(i);
// }
// return personArray;
// }

public Iterator<E> iterator() {
return personList.iterator();
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package io.zipcoder.interfaces;

public class Person {
private final long id;
private String name;

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

public Long getId() {
return this.id;
}

public String getName() {
return this.name;
}

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

}
18 changes: 18 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.zipcoder.interfaces;

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

public Student(Long id, String name) {
super(id, name);
this.totalStudyTime = 0.0;
}

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

public Double getTotalStudyTime() {
return this.totalStudyTime;
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Students.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.interfaces;

public class Students extends People<Student> {
private static final Students INSTANCE;

private Students() {}

static Students getInstance() {
if (INSTANCE == null) {
INSTANCE.add(new Student(3L, "Wes"));
}
return INSTANCE;
}

static {
INSTANCE = new Students();
for (long i = 0; i < 35; i++) {
INSTANCE.add(new Student(i+1L, "student"+i));
}
}

public Student[] toArray() {
return personList.toArray(new Student[0]);
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Teacher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.zipcoder.interfaces;

public interface Teacher {
void teach(Learner learner, Double numberOfHours);
void lecture(Learner[] learners, Double numberOfHours);
}
38 changes: 38 additions & 0 deletions src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.zipcoder.interfaces;

import java.util.LinkedHashMap;

public class ZipCodeWilmington {
private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington();

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

public static ZipCodeWilmington getInstance() {
return INSTANCE;
}

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

public static void hostLecture (Long id, Double numberOfHours) {
Teacher teacher = instructors.findById(id);

teacher.lecture(students.toArray(), numberOfHours);
}

public static LinkedHashMap<Student, Double> getStudyMap() {
LinkedHashMap<Student, Double> map = new LinkedHashMap<Student, Double>();
for (Student student: students) {
map.put(student, student.getTotalStudyTime());
}
return map;
}

// static {
// INSTANCE = new ZipCodeWilmington();
// students = Students.getInstance();
// instructors = Instructors.getInstance();
// }
}
39 changes: 39 additions & 0 deletions src/test/java/io/zipcoder/interfaces/TestEducator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.zipcoder.interfaces;

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

import static org.junit.Assert.*;

public class TestEducator {

@Test
public void testTeach() {
Student stu = new Student(Long.valueOf(56), "Stu");
Educator.INSTRUCTOR1.teach(stu, 5000.0);
Assert.assertEquals(stu.getTotalStudyTime(), Educator.INSTRUCTOR1.getTimeWorked(), 2);
}

@Test
public void testLecture() {
Student stu1 = new Student(Long.valueOf(344), "Stu");
Student stu3 = new Student(Long.valueOf(5623), "Stu");
Student stu2 = new Student(Long.valueOf(56123), "Stu");
Student[] stus = {stu1, stu2, stu3};
Educator.INSTRUCTOR2.lecture(stus, 6000.0);
Double expected = 2000.0;
for (Student stu : stus) {
Double actual = stu.getTotalStudyTime();
Assert.assertEquals(expected, actual, 2);
}
}

@Test
public void testGetTimeWorked() {
Student stu = new Student(Long.valueOf(333), "Stu");
Educator.INSTRUCTOR0.teach(stu, 5000.0);
Double actual = Educator.INSTRUCTOR0.getTimeWorked();
Double expected = 5000.0;
Assert.assertEquals(expected, actual, 2);
}
}
42 changes: 42 additions & 0 deletions src/test/java/io/zipcoder/interfaces/TestInstructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.zipcoder.interfaces;

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

public class TestInstructor {
@Test
public void testImplementation() {
Instructor instructor = new Instructor(Long.valueOf(3), "github");
Assert.assertTrue(instructor instanceof Teacher);
}

@Test
public void testInheritance() {
Instructor instructor = new Instructor(Long.valueOf(3), "github");
Assert.assertTrue(instructor instanceof Person);
}

@Test
public void testTeach() {
Instructor instructor = new Instructor(Long.valueOf(3), "github");
Student student1 = new Student(Long.valueOf(5), "Wes");
instructor.teach(student1, 45.4);
Double actual = student1.getTotalStudyTime();
Double expected = 45.4;
Assert.assertEquals(expected, actual, 2);
}

@Test
public void testLecture() {
Instructor instructor = new Instructor(Long.valueOf(3), "github");
Student student1 = new Student(Long.valueOf(5), "Wes");
Student student2 = new Student(Long.valueOf(8), "Table");
Student student3 = new Student(Long.valueOf(33), "Book");
Student[] students = {student1, student2, student3};

instructor.lecture(students, 48.0);
for (Student student: students) {
Assert.assertEquals(student.getTotalStudyTime(), 16.0, 2);
}
}
}
Loading