Skip to content
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
19 changes: 19 additions & 0 deletions src/main/java/com/github/curriculeon/Instructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.curriculeon; //package is a grouping of related classes and interfaced; prevents naming conflicts

public class Instructor extends Person implements Teacher{ //Instructor is child of Person
public Instructor(Long id, String name) { //Long id and String name are the 2 params
super(id, name); //super is used to call a nonparameterless constructor in Parent (Person) class
}
@Override //new version of method in child class
public void teach(Learner learner, Double numberOfHours) {
learner.learn(numberOfHours);
}
@Override //new version of method in child class
public void lecture(Learner[] learners, Double numberOfHours) {
double numberOfHoursPerLearner = numberOfHours / learners.length;
for (int i = 0; i < learners.length; i++) {
Learner learner = learners[i];
learner.learn(numberofHoursPerLearner);
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/github/curriculeon/Learner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.curriculeon;

public interface Learner {
//Learner interface and all methods are implemented by Person class
void learn(Double numberOfHours);
Double getTotalStudyTime();
}
22 changes: 21 additions & 1 deletion src/main/java/com/github/curriculeon/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.github.curriculeon;

public class Person {
public class Person { //Person is parent class
final long id; //final cannot change
private String name;

public Person() {
this.id = null;
}

Person(long id, String name){
this.id = id;
this.name = name;
}
public long getId(){
return id;
}
public String getName(){ //get method/getter used because name is private
return name;
}
public void setName(String name){ //set method/setter used because name is private
this.name = name;
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/github/curriculeon/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.curriculeon;

public class Student extends Person implements Learner { //Student is child of Person class and accesses public methods via extends
private double totalStudyTime;

public Student(Long id, String name) {
super(id, name);//super is used to call a nonparameterless constructor in the parent class Person
}

@Override//allows new version of method in child class
public void learn(Double numberOfHours) {
this.totalStudyTime = totalStudyTime + numberOfHours;
}

@Override//allows new version of method in child class
public Double getTotalStudyTime() {
return totalStudyTime;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/curriculeon/Teacher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.curriculeon;

public interface Teacher {//this interface is implemented by Person class
void teach(Learner learner, Double numberOfHours);
void lecture(Learner[] learners, Double numberOfHours);
}
33 changes: 33 additions & 0 deletions src/test/java/com/github/curriculeon/TestPerson.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package com.github.curriculeon;

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

public class TestPerson {
public void testConstructor() {
// given
Long expectedId = 0L;
String expectedName = "Some name";

// when
Person person = new Person(expectedId, expectedName);
String actualName = person.getName();
Long actualId = person.getId();

// then
Assert.assertEquals(expectedId,actualId);
Assert.assertEquals(expectedName,actualName);
}

@Test
public void testSetName() {
// given
Person person = new Person(-1,null);
String expectedName = "Some name";
Assert.assertNotEquals(expectedName, person.getName());

// when
person.setName(expectedName);
String actualName = person.getName();

// then
Assert.assertEquals(expectedName,actualName);
}
}