Skip to content
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

Finished lab #75

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
20 changes: 19 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
/target/
.DS_Store
.classpath
#.project
.project
.settings


# User-specific stuff:
*.iml
.idea/**
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -22,4 +24,6 @@
<scope>test</scope>
</dependency>
</dependencies>


</project>
7 changes: 7 additions & 0 deletions src/main/java/interfaces/Learner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package interfaces;

public interface Learner {

void learn(double numberOfHours);
Double getTotalStudyTime();
}
8 changes: 8 additions & 0 deletions src/main/java/interfaces/Teacher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package interfaces;

public interface Teacher {

void teach (Learner learner, double numberOfHours);
void lecture (Learner[] learners, double numberOfHours);

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

import interfaces.Learner;
import interfaces.Teacher;

public enum Educator implements Teacher {

DOLIO (new Instructor(20L,"Wil"),0.0),
ROBERTO (new Instructor(22L,"Jack"),0.0);

private final Instructor instructor;
private double timeWorked;


Educator(Instructor instructor, double timeWorked) {
this.instructor = instructor;
this.timeWorked = timeWorked;
}

public double getTimeWorked() {
return this.timeWorked;
}

@Override
public void teach(Learner learner, double numberOfHours) {
this.instructor.teach(learner,numberOfHours);
this.timeWorked += numberOfHours;
}

@Override
public void lecture(Learner[] learners, double numberOfHours) {
this.instructor.lecture(learners,numberOfHours);
this.timeWorked += numberOfHours;
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Instructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.zipcoder.interfaces;

import interfaces.Learner;
import interfaces.Teacher;

public class Instructor extends Person implements Teacher {


//----------- constructor ---------------------------
public Instructor(Long id, String name) {
super(id, name);
}


//----------- interfaces ---------------------------

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

public void lecture(Learner[] learners, double numberOfHours) {
double numberOfHoursPerLearner = numberOfHours / learners.length;

for(Learner eachLearner : learners){
eachLearner.learn(numberOfHoursPerLearner);
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.zipcoder.interfaces;


public final class Instructors extends People<Instructor> {

private static final Instructors INSTANCE;// = new Instructors();


public static Instructors getInstance(){
return INSTANCE;
}

public Instructor[] toArray() {
Instructor[] instructorsArray = new Instructor[this.INSTANCE.count()];
int i = 0;
for (Instructor eachInstructor : this.INSTANCE){
instructorsArray[i++] = eachInstructor;
}
return instructorsArray;
}

// the below is how you instantiate it
static {
INSTANCE = new Instructors();
INSTANCE.add(new Instructor(30L,"Dolio"));
INSTANCE.add(new Instructor(31L,"Roberto"));
}
}
54 changes: 54 additions & 0 deletions src/main/java/io/zipcoder/interfaces/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.zipcoder.interfaces;

import javax.swing.*;
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[] personArray;


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

public E findById(Long id){

for (E eachPerson : this.personList){
if (eachPerson.getId() == id) return eachPerson;
}
return null;
}

public Boolean contains(E person){
return this.personList.contains(person);
}

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

public void remove(Long id){
this.personList.remove(findById(id));
}

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

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

public abstract E[] toArray();

public Iterator<E> iterator() {
return this.personList.iterator();
}

}
20 changes: 20 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,24 @@

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;
}
}

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

import interfaces.Learner;

public class Student extends Person implements Learner {

double totalStudyTime;

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

//--------------------------------------------



//---------- interfaces -----------------------
public void learn(double numberOfHours) {
this.totalStudyTime += numberOfHours;
}


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


public final class Students extends People<Student> {

private static final Students INSTANCE;// = new Students();

public static Students getInstance(){
return INSTANCE;
}


public Student[] toArray() {
Student[] studentArray = new Student[this.INSTANCE.count()];
int i = 0;
for (Student eachStudent : this.INSTANCE){
studentArray[i++] = eachStudent;
}
return studentArray;
}

// the below is how you instantiate it
static {
INSTANCE = new Students();
INSTANCE.add(new Student(30L,"John"));
INSTANCE.add(new Student(31L, "V"));
INSTANCE.add(new Student(32L,"X"));
}
}
37 changes: 37 additions & 0 deletions src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.zipcoder.interfaces;
import interfaces.Teacher;

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

public final class ZipCodeWilmington {

private static final ZipCodeWilmington INSTANCE = new ZipCodeWilmington();

private static Students students = Students.getInstance();
private static Instructors instructors = Instructors.getInstance();
private static Map<Student, Double> studyMap;


public static ZipCodeWilmington getInstance(){
return INSTANCE;
}


// static so it can be accessed directly
public static void hostLecture(Teacher teacher, double numberOfHours){
teacher.lecture(students.toArray(),numberOfHours);
}

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

public static Map<Student,Double> getStudyMap(){
studyMap = new HashMap<Student, Double>();
for (Student eachStudent : students){
studyMap.put(eachStudent,eachStudent.getTotalStudyTime());
}
return studyMap;
}
}
55 changes: 55 additions & 0 deletions src/test/java/io/zipcoder/interfaces/EducatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.zipcoder.interfaces;

import interfaces.Teacher;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class EducatorTest {

@Test
public void testImplementation(){
Assert.assertTrue(Educator.DOLIO instanceof Teacher);
}

@Test
public void getTimeWorked() {
Student student1 = new Student(20L,"Joe");
Student student2 = new Student(22L,"Jack");
Student[] students = {student1,student2};

Educator.DOLIO.lecture(students,20);
Double expected = 20.0;
Double actual = Educator.DOLIO.getTimeWorked();

Assert.assertEquals(expected,actual,0.0);
}

@Test
public void teach() {
//each student
Student student1 = new Student(20L,"Joe");
Educator.ROBERTO.teach(student1,10);

double expected = 10.0;
double actual = student1.getTotalStudyTime();

Assert.assertEquals(expected,actual,0.0);
}

@Test
public void lecture() {
// divide per nr of students
Student student1 = new Student(20L,"Joe");
Student student2 = new Student(21L,"Jack");
Student[] students = {student1,student2};

Educator.ROBERTO.lecture(students,90);

double expected = 45;
double actual = student1.getTotalStudyTime();

Assert.assertEquals(expected,actual,0.0);
}
}
Loading