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

lab #82

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

lab #82

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
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
<groupId>io.zipcoder</groupId>
<artifactId>interfaces-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>interfaces-1</name>
<url>http://maven.apache.org</url>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Instructor.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.zipcoder.interfaces;

public class Instructors extends People<Instructor> {

static final Instructors INSTANCE;

private Instructors() {
}

static {
INSTANCE = new Instructors();
}

public static Instructors getInstance() {
return Instructors.INSTANCE;
}
}
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 {

public void learn (double numberOfHours);

public Double getTotalStudyTime ();
}
59 changes: 59 additions & 0 deletions src/main/java/io/zipcoder/interfaces/People.java
Original file line number Diff line number Diff line change
@@ -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 People<E extends Person>implements Iterable<E>{

List<E> 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<E> iterator() {
return (Iterator<E>) personList.iterator();
}

}
25 changes: 25 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Student.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Students.java
Original file line number Diff line number Diff line change
@@ -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<Student> 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]);
}
}
8 changes: 8 additions & 0 deletions src/main/java/io/zipcoder/interfaces/Teacher.java
Original file line number Diff line number Diff line change
@@ -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);
}
28 changes: 28 additions & 0 deletions src/main/java/io/zipcoder/interfaces/ZipCodeWilmington.java
Original file line number Diff line number Diff line change
@@ -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<Student, Double> getStudyMap () {
Map<Student, Double> result = new HashMap<>();
for (Student student : students) {
result.put(student, student.getTotalStudyTime());
}
return result;
}
}
49 changes: 49 additions & 0 deletions src/test/java/io/zipcoder/interfaces/TestInstructor.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
20 changes: 20 additions & 0 deletions src/test/java/io/zipcoder/interfaces/TestInstructors.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
50 changes: 50 additions & 0 deletions src/test/java/io/zipcoder/interfaces/TestPeople.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions src/test/java/io/zipcoder/interfaces/TestPerson.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading