diff --git a/pom.xml b/pom.xml index f0effe12..22cffbe3 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,19 @@ io.zipcoder interfaces-1 0.0.1-SNAPSHOT - jar + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + jar interfaces-1 http://maven.apache.org 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..9fb4bb82 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructor.java @@ -0,0 +1,15 @@ +package io.zipcoder.interfaces; + +public class Instructor extends Person implements Teacher { + + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + } + + public void lecture(Learner[] learners, double numberOfHours) { + for (int i = 0; i < learners.length; i++) { + Double numOfHoursPerLearner = numberOfHours/ learners.length; + learners[i].learn(numOfHoursPerLearner); + } + } +} 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..18a5fe17 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Instructors.java @@ -0,0 +1,17 @@ +package io.zipcoder.interfaces; + +public class Instructors extends People { + + static final Instructors INSTANCE; + + private Instructors() { + } + + static { + INSTANCE = new Instructors(); + } + + public static Instructors getInstance() { + return Instructors.INSTANCE; + } +} diff --git a/src/main/java/io/zipcoder/interfaces/Learner.java b/src/main/java/io/zipcoder/interfaces/Learner.java new file mode 100644 index 00000000..e9b5b99c --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Learner.java @@ -0,0 +1,8 @@ +package io.zipcoder.interfaces; + +public interface Learner { + + public void learn (double numberOfHours); + + public Double getTotalStudyTime (); +} 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..4353accb --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/People.java @@ -0,0 +1,59 @@ +package io.zipcoder.interfaces; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.function.Consumer; + +public class Peopleimplements Iterable{ + + List personList; + + public People () { + this.personList = new ArrayList<>(); + } + + public void add (E person) { + personList.add(person); + } + + public E findById (long id) { + for (E person : personList) { + if (person.getId() == id) { + return person; + } + } + return null; + } + + public Boolean containsPerson (E person) { + return personList.contains(person); + } + + public void removePerson (E person) { + personList.remove(person); + } + + public void removePerson (long id) { + personList.remove(findById(id)); + } + + public void removeAll () { + personList.removeAll(personList); + } + + public Integer count () { + return personList.size(); + } + + public Array[] toArray () { + return (Array[]) personList.toArray(); + } + + public Iterator iterator() { + return (Iterator) personList.iterator(); + } + +} diff --git a/src/main/java/io/zipcoder/interfaces/Person.java b/src/main/java/io/zipcoder/interfaces/Person.java index fc6a3ffe..9d69aa7e 100644 --- a/src/main/java/io/zipcoder/interfaces/Person.java +++ b/src/main/java/io/zipcoder/interfaces/Person.java @@ -1,5 +1,30 @@ package io.zipcoder.interfaces; +import sun.util.resources.ga.LocaleNames_ga; + public class Person { + final long id; + String name; + + public Person () { + this.id = System.nanoTime(); + this.name = ""; + } + + public Person (long id, String name) { + this.id = id; + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public long getId() { + return id; + } } 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..ae7df72b --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Student.java @@ -0,0 +1,16 @@ +package io.zipcoder.interfaces; + +public class Student extends Person implements Learner{ + + double totalStudyTime; + + public void learn(double numberOfHours) { + for (int i = 0; i < numberOfHours; i++) { + totalStudyTime++; + } + } + + public Double getTotalStudyTime() { + return 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..530cb3d4 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Students.java @@ -0,0 +1,27 @@ +package io.zipcoder.interfaces; + +import com.sun.xml.internal.bind.v2.model.core.ID; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + + public class Students extends People implements Serializable { + // Part 7.1 + static final Students INSTANCE; + + private Students(){ + } + + static { + INSTANCE = new Students(); + } + + public static Students getInstance() { + return Students.INSTANCE; + } + + public Student[] getArray () { + return super.personList.toArray(new Student[0]); + } + } diff --git a/src/main/java/io/zipcoder/interfaces/Teacher.java b/src/main/java/io/zipcoder/interfaces/Teacher.java new file mode 100644 index 00000000..89270d0f --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/Teacher.java @@ -0,0 +1,8 @@ +package io.zipcoder.interfaces; + +public interface Teacher { + + public void teach (Learner learner, double numberOfHours); + + public void lecture (Learner[] learners, double numberOfHours); +} 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..73745959 --- /dev/null +++ b/src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java @@ -0,0 +1,28 @@ +package io.zipcoder.interfaces; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public enum ZipCodeWilmington { + INSTANCE; + final Students students = Students.getInstance(); + final Instructors instructors = Instructors.getInstance(); + + public void hostLecture (Teacher teacher, double numberOfHours) { + teacher.lecture(students.personList.toArray(new Learner[0]), numberOfHours); + } + + public void hostLecture (long id, double numberOfHours) { + Teacher teacher = instructors.findById(id); + hostLecture(teacher, numberOfHours); + } + + public Map getStudyMap () { + Map result = new HashMap<>(); + for (Student student : students) { + result.put(student, student.getTotalStudyTime()); + } + return result; + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructor.java b/src/test/java/io/zipcoder/interfaces/TestInstructor.java new file mode 100644 index 00000000..2a9f3329 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructor.java @@ -0,0 +1,49 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + + @Test + public void implementationTest () { + Instructor instructor = new Instructor(); + + Assert.assertTrue(instructor instanceof Teacher); + } + + @Test + public void inheritanceTest () { + Instructor instructor = new Instructor(); + + Assert.assertTrue(instructor instanceof Person); + } + + @Test + public void teachTest () { + Instructor instructor = new Instructor(); + Student student = new Student(); + double expected = 10; + + instructor.teach(student, 10); + + double actual = student.getTotalStudyTime(); + Assert.assertEquals(expected, actual, 0); + } + + @Test + public void lectureTest () { + Instructor instructor = new Instructor(); + Student student = new Student(); + Student student1 = new Student(); + Learner[] learners = new Learner[]{student, student1}; + double expected = 5; + + instructor.lecture(learners, 10); + double actual = student.getTotalStudyTime(); + double actual1 = student1.getTotalStudyTime(); + + Assert.assertEquals(expected, actual, 0); + Assert.assertEquals(expected, actual1, 0); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestInstructors.java b/src/test/java/io/zipcoder/interfaces/TestInstructors.java new file mode 100644 index 00000000..2709c10d --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestInstructors.java @@ -0,0 +1,20 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + + @Test + public void testingSingleton () { + Instructor leon = new Instructor(); + Instructor dolio = new Instructor(); + Integer expected = 2; + + Instructors.getInstance().add(leon); + Instructors.getInstance().add(dolio); + Integer actual = Instructors.getInstance().count(); + + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestPeople.java b/src/test/java/io/zipcoder/interfaces/TestPeople.java new file mode 100644 index 00000000..64557d19 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestPeople.java @@ -0,0 +1,50 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + + @Test + public void testAdd () { + Person person = new Person(); + Person person1 = new Person(); + Integer expected = 2; + + People people = new People(); + people.add(person); + people.add(person1); + Integer actual = people.personList.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testRemove () { + Person person = new Person(); + Person person1 = new Person(); + Integer expected = 1; + + People people = new People(); + people.add(person); + people.add(person1); + people.removePerson(person); + Integer actual = people.personList.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testFindById () { + Person person = new Person(2, ""); + Person person1 = new Person(23, ""); + Person expected = person; + + People people = new People(); + people.add(person); + people.add(person1); + Person actual = people.findById(2); + + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestPerson.java b/src/test/java/io/zipcoder/interfaces/TestPerson.java index d64cd2f0..1453ff71 100644 --- a/src/test/java/io/zipcoder/interfaces/TestPerson.java +++ b/src/test/java/io/zipcoder/interfaces/TestPerson.java @@ -1,5 +1,31 @@ package io.zipcoder.interfaces; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void constructorTest() { + long expectedId = 10; + String expectedName = "Calvin"; + + Person person = new Person(10, "Calvin"); + long actualId = person.getId(); + String actualName = person.getName(); + + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void setNameTest () { + String expected = "Alfred"; + + Person person = new Person(); + person.setName("Alfred"); + String actual = person.getName(); + + Assert.assertEquals(expected, actual); + } } diff --git a/src/test/java/io/zipcoder/interfaces/TestStudent.java b/src/test/java/io/zipcoder/interfaces/TestStudent.java new file mode 100644 index 00000000..d16eaae7 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudent.java @@ -0,0 +1,32 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + + @Test + public void testImplementation () { + Student student = new Student(); + + Assert.assertTrue(student instanceof Learner); + } + + @Test + public void testInheritance () { + Student student = new Student(); + + Assert.assertTrue(student instanceof Person); + } + + @Test + public void testLearn () { + Student student = new Student(); + double expected = 10; + + student.learn(10); + double actual = student.getTotalStudyTime(); + + Assert.assertEquals(expected, actual, 0); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestStudents.java b/src/test/java/io/zipcoder/interfaces/TestStudents.java new file mode 100644 index 00000000..e2ad99ad --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestStudents.java @@ -0,0 +1,20 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents { + + @Test + public void testingSingleton () { + Student student1 = new Student(); + Student student2 = new Student(); + Integer expected = 2; + + Students.getInstance().add(student1); + Students.getInstance().add(student2); + Integer actual = Students.getInstance().count(); + + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java new file mode 100644 index 00000000..e83734b7 --- /dev/null +++ b/src/test/java/io/zipcoder/interfaces/TestZipCodeWilmington.java @@ -0,0 +1,28 @@ +package io.zipcoder.interfaces; + +import org.junit.Assert; +import org.junit.Test; + +public class TestZipCodeWilmington { + + @Test + public void testHostLecture () { + Student eric = new Student(); + Student cindy = new Student(); + Student petunya = new Student(); + Student tom = new Student(); + Instructor jawn = new Instructor(); + + Double exepected = 25.0; + Students.getInstance().add(eric); + Students.getInstance().add(cindy); + Students.getInstance().add(petunya); + Students.getInstance().add(tom); + Instructors.getInstance().add(jawn); + + ZipCodeWilmington.INSTANCE.hostLecture(jawn.getId(), 100.0); + Double actual = ZipCodeWilmington.INSTANCE.getStudyMap().get(eric); + + Assert.assertEquals(exepected, actual); + } +}