diff --git a/.gitignore b/.gitignore index c9bfc295..3b60d3fa 100644 --- a/.gitignore +++ b/.gitignore @@ -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 + diff --git a/pom.xml b/pom.xml index f0effe12..63b545ff 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,8 @@ UTF-8 + 1.8 + 1.8 @@ -22,4 +24,6 @@ test + + diff --git a/src/main/java/interfaces/Learner.java b/src/main/java/interfaces/Learner.java new file mode 100644 index 00000000..32c7d382 --- /dev/null +++ b/src/main/java/interfaces/Learner.java @@ -0,0 +1,7 @@ +package interfaces; + +public interface Learner { + + void learn(double numberOfHours); + Double getTotalStudyTime(); +} diff --git a/src/main/java/interfaces/Teacher.java b/src/main/java/interfaces/Teacher.java new file mode 100644 index 00000000..04b40951 --- /dev/null +++ b/src/main/java/interfaces/Teacher.java @@ -0,0 +1,8 @@ +package interfaces; + +public interface Teacher { + + void teach (Learner learner, double numberOfHours); + void lecture (Learner[] learners, double numberOfHours); + +} diff --git a/src/main/java/io/zipcoder/interfaces/Educator.java b/src/main/java/io/zipcoder/interfaces/Educator.java new file mode 100644 index 00000000..adcd7bd5 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Educator.java @@ -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; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Instructor.java b/src/main/java/io/zipcoder/interfaces/Instructor.java new file mode 100644 index 00000000..e0feff6e --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -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); + } + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Instructors.java b/src/main/java/io/zipcoder/interfaces/Instructors.java new file mode 100644 index 00000000..b2782c32 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,28 @@ +package io.zipcoder.interfaces; + + +public final class Instructors extends People { + + 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")); + } +} diff --git a/src/main/java/io/zipcoder/interfaces/People.java b/src/main/java/io/zipcoder/interfaces/People.java new file mode 100644 index 00000000..59c7d523 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -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 implements Iterable{ + + List 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 iterator() { + return this.personList.iterator(); + } + +} diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..9d48632a 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -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; + } } + diff --git a/src/main/java/io/zipcoder/interfaces/Student.java b/src/main/java/io/zipcoder/interfaces/Student.java new file mode 100644 index 00000000..f558ff89 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -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; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Students.java b/src/main/java/io/zipcoder/interfaces/Students.java new file mode 100644 index 00000000..166f24d3 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,29 @@ +package io.zipcoder.interfaces; + + +public final class Students extends People { + + 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")); + } +} diff --git a/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java new file mode 100644 index 00000000..736cfaec --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -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 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 getStudyMap(){ + studyMap = new HashMap(); + for (Student eachStudent : students){ + studyMap.put(eachStudent,eachStudent.getTotalStudyTime()); + } + return studyMap; + } +} diff --git a/src/test/java/io/zipcoder/interfaces/EducatorTest.java b/src/test/java/io/zipcoder/interfaces/EducatorTest.java new file mode 100644 index 00000000..1c281a8e --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/EducatorTest.java @@ -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); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/InstructorTest.java b/src/test/java/io/zipcoder/interfaces/InstructorTest.java new file mode 100644 index 00000000..924d8a13 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/InstructorTest.java @@ -0,0 +1,56 @@ +package io.zipcoder.interfaces; + +import interfaces.Teacher; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class InstructorTest { + Instructor testInstructor; + + @Test + public void testImplementation(){ + testInstructor = new Instructor(32L,"John"); + + Assert.assertTrue(testInstructor instanceof Teacher); + } + + @Test + public void testInheritance(){ + testInstructor = new Instructor(32L,"John"); + + Assert.assertTrue(testInstructor instanceof Person); + } + + @Test + public void teach() { + testInstructor = new Instructor(32L,"John"); + Student testStudent = new Student(44L,"Joe"); + + testInstructor.teach(testStudent,24); + + double expected = 24; + double actual = testStudent.getTotalStudyTime(); + + Assert.assertEquals(expected,actual,0.0); + } + + @Test + public void lecture() { + testInstructor = new Instructor(32L,"John"); + Student student1 = new Student(44L,"Joe"); + Student student2 = new Student(43L,"Sam"); + Student student3 = new Student(41L,"Jack"); + + Student[] students = {student1,student2,student3}; + + testInstructor.lecture(students,9); + + double expected = 3; + double actual = student1.getTotalStudyTime(); + + Assert.assertEquals(expected,actual,0.0); + + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/InstructorsTest.java b/src/test/java/io/zipcoder/interfaces/InstructorsTest.java new file mode 100644 index 00000000..03e5ff8f --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/InstructorsTest.java @@ -0,0 +1,24 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; +import java.util.List; + + +public class InstructorsTest { + + @Test + public void getInstance() { + Instructors testInstructors = Instructors.getInstance(); + + + for (Instructor eachInst : testInstructors){ + System.out.println(eachInst.getName() + " - " + eachInst.getId()); + } + + Integer expected = 2; + Integer actual = testInstructors.count(); + + Assert.assertEquals(expected,actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/PeopleTest.java b/src/test/java/io/zipcoder/interfaces/PeopleTest.java new file mode 100644 index 00000000..14eafabe --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/PeopleTest.java @@ -0,0 +1,57 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PeopleTest { + People testPerson; + + // this can't be instatiated anymore + + @Test + public void testAdd() { +// testPerson = new People(); +// Person person = new Person(32L,"Joe"); +// Integer actual; +// +// Integer expected = 0; +// actual = testPerson.count(); +// +// Assert.assertEquals(expected,actual); +// +// testPerson.add(person); +// expected = 1; +// actual = testPerson.count(); +// +// Assert.assertEquals(expected,actual); + } + + @Test + public void testFindById() { +// testPerson = new People(); +// Person person = new Person(32L,"Joe"); +// +// testPerson.add(person); +// +// Assert.assertEquals(person,testPerson.findById(32L)); +// Assert.assertNotEquals(person,testPerson.findById(31L)); + } + + @Test + public void testRemove() { +// testPerson = new People(); +// Person person = new Person(32L,"Joe"); +// Person person1 = new Person(30L,"Jack"); +// +// testPerson.add(person); +// testPerson.add(person1); +// +// Integer expected = 1; +// testPerson.remove(32L); +// Integer actual = testPerson.count(); +// +// Assert.assertEquals(expected,actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/PersonTest.java b/src/test/java/io/zipcoder/interfaces/PersonTest.java new file mode 100644 index 00000000..d7c06929 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/PersonTest.java @@ -0,0 +1,47 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PersonTest { + + Person testPerson; + + + + @Test + public void getId() { + Long expected = 32L; + String name = "Joe"; + testPerson = new Person(expected,name); + + Long actual = testPerson.getId(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void getName() { + Long id = 32L; + String expected = "Joe"; + testPerson = new Person(id,expected); + + String actual = testPerson.getName(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void setName() { + Long id = 32L; + String expected = "Joe"; + testPerson = new Person(id,""); + + testPerson.setName("Joe"); + String actual = testPerson.getName(); + + Assert.assertEquals(expected,actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/StudentTest.java b/src/test/java/io/zipcoder/interfaces/StudentTest.java new file mode 100644 index 00000000..55a61d96 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/StudentTest.java @@ -0,0 +1,35 @@ +package io.zipcoder.interfaces; + +import interfaces.Learner; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StudentTest { + Student testStudent; + + @Test + public void testLearn() { + testStudent = new Student(34L,"Joe"); + + testStudent.learn(25d); + + double expected = 25.0; + double actual = testStudent.getTotalStudyTime(); + + Assert.assertEquals(expected,actual,0.0); + } + + @Test + public void testImplementation(){ + testStudent = new Student(34L, "Jack"); + Assert.assertTrue(testStudent instanceof Learner); + } + + @Test + public void testInheritance(){ + testStudent = new Student(34L, "Jack"); + Assert.assertTrue(testStudent instanceof Person); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/StudentsTest.java b/src/test/java/io/zipcoder/interfaces/StudentsTest.java new file mode 100644 index 00000000..9c5d182a --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/StudentsTest.java @@ -0,0 +1,20 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class StudentsTest { + + @Test + public void getInstance() { + Students testStudents = Students.getInstance(); + + for (Student eachS : testStudents){ + System.out.println(eachS.getName() + " - " + eachS.getId()); + } + + Integer expected = 3; + Integer actual = testStudents.count(); + Assert.assertEquals(expected,actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java deleted file mode 100644 index d64cd2f0..00000000 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ /dev/null @@ -1,5 +0,0 @@ -package io.zipcoder.interfaces; - -public class TestPerson { - -} diff --git a/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java new file mode 100644 index 00000000..7822572c --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/ZipCodeWilmingtonTest.java @@ -0,0 +1,41 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class ZipCodeWilmingtonTest { + + @Test + public void testhostLectureById() { + Students cohort = Students.getInstance(); + Instructors teachers = Instructors.getInstance(); + + ZipCodeWilmington.hostLecture(30L,90); + + Double expected = 30.0; + for (Student eachStudent : cohort){ + Double actual = eachStudent.getTotalStudyTime(); + Assert.assertEquals(expected,actual,0.0); + } + + ZipCodeWilmington.hostLecture(30L,-90); + + } + + @Test + public void testHostLectureByInstructor() { + + Students cohort = Students.getInstance(); + Instructors teachers = Instructors.getInstance(); + + ZipCodeWilmington.hostLecture(teachers.findById(30L),90); + + Double expected = 30.0; + + for (Student eachStudent : cohort){ + Double actual = eachStudent.getTotalStudyTime(); + Assert.assertEquals(expected,actual,0.0); + } + ZipCodeWilmington.hostLecture(30L,-90); + } +} \ No newline at end of file