Structure the code to respect MVC design pattern.
[Persons_Comparator.git] / src / Eye.java
index b02dbfcac2602262801fe521b123cc8234e4f253..7d5d80401879470faa13c37560b705f4d1fedd63 100644 (file)
@@ -1,9 +1,12 @@
 import java.awt.Color;
+import java.util.List;
+import java.util.Arrays;
 
-public class Eye {
+public class Eye implements Comparable<Eye> {
     private Color color;
+    private List<String> colorList = Arrays.asList("black", "green", "blue", "brown");
 
-    Eye(Color color) {
+    Eye(String color) {
         setColor(color);
     }
 
@@ -11,7 +14,29 @@ public class Eye {
         return color;
     }
 
-    public void setColor(Color color) {
-        this.color = color;
+    public void setColor(String color) {
+        if (validateColor(color)) {
+            if (color.equals("brown"))
+                this.color = new Color(88, 41, 0);
+            else
+                this.color = Color.getColor(color);
+
+        } else {
+            throw new IllegalArgumentException("Color must be " + colorList.toString());
+        }
+    }
+
+    private boolean validateColor(String color) {
+        return colorList.contains(color);
+    }
+
+    @Override
+    public int compareTo(Eye eye) {
+        double r_diff = this.getColor().getRed() - eye.getColor().getRed();
+        double g_diff = this.getColor().getGreen() - eye.getColor().getGreen();
+        double b_diff = this.getColor().getBlue() - eye.getColor().getBlue();
+        // See https://en.wikipedia.org/wiki/Color_difference
+        Double distance = Math.sqrt(2 * Math.pow(r_diff, 2) + 4 * Math.pow(g_diff, 2) + 3 * Math.pow(b_diff, 2));
+        return distance.intValue();
     }
 }