summaryrefslogtreecommitdiff
path: root/src/section-8
diff options
context:
space:
mode:
Diffstat (limited to 'src/section-8')
-rw-r--r--src/section-8/Candidate.java75
-rw-r--r--src/section-8/CandidateTest.java62
-rw-r--r--src/section-8/Communication.java40
-rw-r--r--src/section-8/CommunicationTest.java43
4 files changed, 220 insertions, 0 deletions
diff --git a/src/section-8/Candidate.java b/src/section-8/Candidate.java
new file mode 100644
index 0000000..7df9535
--- /dev/null
+++ b/src/section-8/Candidate.java
@@ -0,0 +1,75 @@
+package ca.mokhan.assignment1;
+
+import java.util.ArrayList;
+
+public class Candidate extends AddressBook {
+ private double grade = 0.0;
+ private Communication communication;
+ private boolean isInnovative;
+ private double regulatoryCapability;
+
+ public Candidate(
+ String firstName,
+ String lastName,
+ double grade,
+ String communication,
+ boolean isInnovative,
+ double regulatoryCapability) {
+ super(firstName, "", lastName);
+ this.grade = grade;
+ this.setCommunication(communication);
+ this.isInnovative = isInnovative;
+ this.regulatoryCapability = regulatoryCapability;
+ }
+
+ public boolean isInnovative() {
+ return this.isInnovative;
+ }
+
+ public double getGrade() {
+ return this.grade;
+ }
+
+ public double getRegulation() {
+ return this.regulatoryCapability;
+ }
+
+ public String getCommunication() {
+ return this.communication.toString();
+ }
+
+ public void setCommunication(String communication) {
+ this.communication = Communication.findBy(communication);
+ }
+
+ public void setGrade(double grade) {
+ this.grade = grade;
+ }
+
+ public void setInnovation(boolean innovation) {
+ this.isInnovative = innovation;
+ }
+
+ public void setRegulation(double regulatoryCapability) {
+ this.regulatoryCapability = regulatoryCapability;
+ }
+
+ public boolean isEligible() {
+ if (this.grade >= 85.0) {
+ return this.communication.isAtLeast(Communication.Average) || this.isInnovative();
+ } else {
+ return (this.regulatoryCapability >= 0.5
+ && this.communication.isAtLeast(Communication.Average));
+ }
+ }
+
+ public static ArrayList<Candidate> getEligibleCandidates(Candidate[] candidates) {
+ ArrayList<Candidate> eligible = new ArrayList<Candidate>();
+
+ for (Candidate candidate : candidates) {
+ if (candidate.isEligible()) eligible.add(candidate);
+ }
+
+ return eligible;
+ }
+}
diff --git a/src/section-8/CandidateTest.java b/src/section-8/CandidateTest.java
new file mode 100644
index 0000000..6d2ca5e
--- /dev/null
+++ b/src/section-8/CandidateTest.java
@@ -0,0 +1,62 @@
+package ca.mokhan.assignment1;
+
+import java.util.ArrayList;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class CandidateTest extends TestCase {
+ private Candidate[] candidates = {
+ new Candidate("Elena", "Brandon", 82.30, "poor", true, 0.5),
+ new Candidate("Thomas", "Molson", 85.10, "poor", false, 1.0),
+ new Candidate("Hamilton", "Winn", 77.77, "average", false, 0.8),
+ new Candidate("Suzie", "Sarandin", 69.93, "average", false, 0.0),
+ new Candidate("Philip", "Winne", 93.03, "average", true, 1.0),
+ new Candidate("Alex", "Trebok", 88.61, "poor", true, 0.7),
+ new Candidate("Emma", "Pivoto", 55.99, "excellent", false, 0.8),
+ new Candidate("John", "Lenthen", 87.49, "excellent", true, 0.9),
+ new Candidate("James", "Lean", 88.00, "excellent", false, 0.5),
+ new Candidate("Jane", "Ostin", 91.20, "average", true, 0.6),
+ new Candidate("Emily", "Car", 66.79, "excellent", false, 0.3),
+ new Candidate("Daniel", "Hamshire", 76.65, "average", true, 0.2),
+ new Candidate("Neda", "Bazdar", 55.89, "excellent", true, 0.5),
+ new Candidate("Aaron", "Smith", 90.01, "excellent", false, 0.3),
+ new Candidate("Kate", "Hen", 87.9, "poor", false, 0.8)
+ };
+
+ public CandidateTest(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(CandidateTest.class);
+ }
+
+ public void testGetEligibleCandidates() {
+ ArrayList<Candidate> eligibleCandidates = Candidate.getEligibleCandidates(this.candidates);
+ for (String expected :
+ new String[] {
+ "Hamilton", "Philip", "Alex", "Emma", "John", "James", "Jane", "Neda", "Aaron"
+ }) {
+ assertNotNull(
+ eligibleCandidates.stream()
+ .filter(x -> expected.equals(x.getFirstName()))
+ .findAny()
+ .orElse(null));
+ }
+ }
+
+ public void testIsElligibleWithGreaterThanRequiredGrade() {
+ assertTrue(new Candidate("Tsuyoshi", "Garrett", 85.0, "excellent", false, 0.0).isEligible());
+ }
+
+ public void testIsElligibleWithLessThanRequiredGrade() {
+ assertFalse(new Candidate("Tsuyoshi", "Garrett", 84.9, "average", false, 0.0).isEligible());
+ assertTrue(new Candidate("Tsuyoshi", "Garrett", 84.9, "average", false, 0.5).isEligible());
+ }
+
+ public void testIsElligibleWithPoorCommunication() {
+ assertTrue(new Candidate("Tsuyoshi", "Garrett", 85.0, "poor", true, 0.0).isEligible());
+ assertFalse(new Candidate("Tsuyoshi", "Garrett", 85.0, "poor", false, 0.0).isEligible());
+ }
+}
diff --git a/src/section-8/Communication.java b/src/section-8/Communication.java
new file mode 100644
index 0000000..2e4c2a8
--- /dev/null
+++ b/src/section-8/Communication.java
@@ -0,0 +1,40 @@
+package ca.mokhan.assignment1;
+
+public class Communication implements Comparable<Communication> {
+ private String name;
+ private Integer ranking;
+
+ public Communication(String name, Integer ranking) {
+ this.name = name;
+ this.ranking = ranking;
+ }
+
+ public int compareTo(Communication other) {
+ return this.ranking.compareTo(other.ranking);
+ }
+
+ public boolean isAtLeast(Communication other) {
+ return this.compareTo(other) >= 0;
+ }
+
+ @Override
+ public String toString() {
+ return this.name;
+ }
+
+ public static final Communication Poor = new Communication("poor", 0);
+ public static final Communication Average = new Communication("average", 1);
+ public static final Communication Excellent = new Communication("excellent", 2);
+
+ public static Communication findBy(String name) {
+ switch (name) {
+ case "poor":
+ return Communication.Poor;
+ case "average":
+ return Communication.Average;
+ case "excellent":
+ return Communication.Excellent;
+ }
+ throw new IllegalArgumentException("Unknown communication type");
+ }
+}
diff --git a/src/section-8/CommunicationTest.java b/src/section-8/CommunicationTest.java
new file mode 100644
index 0000000..675492f
--- /dev/null
+++ b/src/section-8/CommunicationTest.java
@@ -0,0 +1,43 @@
+package ca.mokhan.assignment1;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class CommunicationTest extends TestCase {
+ public CommunicationTest(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(CommunicationTest.class);
+ }
+
+ public void testCompareTo() {
+ assertEquals(-1, Communication.Poor.compareTo(Communication.Average));
+ assertEquals(-1, Communication.Poor.compareTo(Communication.Excellent));
+ assertEquals(0, Communication.Poor.compareTo(Communication.Poor));
+
+ assertEquals(-1, Communication.Average.compareTo(Communication.Excellent));
+ assertEquals(1, Communication.Average.compareTo(Communication.Poor));
+ assertEquals(0, Communication.Average.compareTo(Communication.Average));
+
+ assertEquals(1, Communication.Excellent.compareTo(Communication.Average));
+ assertEquals(1, Communication.Excellent.compareTo(Communication.Poor));
+ assertEquals(0, Communication.Excellent.compareTo(Communication.Excellent));
+ }
+
+ public void testIsAtLeast() {
+ assertFalse(Communication.Poor.isAtLeast(Communication.Average));
+ assertFalse(Communication.Poor.isAtLeast(Communication.Excellent));
+ assertTrue(Communication.Poor.isAtLeast(Communication.Poor));
+
+ assertFalse(Communication.Average.isAtLeast(Communication.Excellent));
+ assertTrue(Communication.Average.isAtLeast(Communication.Poor));
+ assertTrue(Communication.Average.isAtLeast(Communication.Average));
+
+ assertTrue(Communication.Excellent.isAtLeast(Communication.Average));
+ assertTrue(Communication.Excellent.isAtLeast(Communication.Poor));
+ assertTrue(Communication.Excellent.isAtLeast(Communication.Excellent));
+ }
+}