Add an ArrayList of persons to the Person class.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 11 Jan 2019 13:42:30 +0000 (14:42 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 11 Jan 2019 13:42:30 +0000 (14:42 +0100)
Fix all Person constructor and attributes constructors.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
14 files changed:
src/Eye.java
src/EyeView.java
src/Firstname.java
src/FirstnameView.java
src/Main.java
src/MainWindowsView.java
src/Origin.java
src/OriginView.java
src/Person.java
src/PersonView.java
src/Size.java
src/SizeView.java
src/Weight.java
src/WeightView.java

index ed2231b7aaa81da839726fea50b760f917f178f3..bf83b8a0410ac43b17604fdae45f82f8935aa655 100644 (file)
@@ -36,7 +36,7 @@ public class Eye implements Comparable<Eye> {
                 this.color = Color.getColor(color);
 
         } else {
-            throw new IllegalArgumentException("Color must be " + colorsList);
+            throw new IllegalArgumentException("Color must be" + new Eye().toString());
         }
     }
 
@@ -66,4 +66,14 @@ public class Eye implements Comparable<Eye> {
         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();
     }
+
+    @Override
+    public String toString() {
+        StringBuilder stringBuilder = new StringBuilder();
+        for (String c : colorsList) {
+            stringBuilder.append(" ");
+            stringBuilder.append(c);
+        }
+        return stringBuilder.toString();
+    }
 }
index aa54c8b8d76bdf0109e9fc294d8a21d63feacf63..ad0c2fa81fd22f50313e121369e9e1330885ca1f 100644 (file)
@@ -5,8 +5,11 @@ public class EyeView extends JComponent {
     private JLabel label;
     private JList colorsList;
 
-    EyeView() {
-        setEyeObj(new Eye());
+    /**
+     * @param eyeObj
+     */
+    EyeView(Eye eyeObj) {
+        setEyeObj(eyeObj);
         this.label = new JLabel();
         this.label.setText("Eyes color");
         this.colorsList = new JList(getEyeObj().getColorsList());
@@ -16,13 +19,6 @@ public class EyeView extends JComponent {
         add(colorsList);
     }
 
-    /**
-     * @param eyeObj
-     */
-    EyeView(Eye eyeObj) {
-        setEyeObj(eyeObj);
-    }
-
     /**
      * @return
      */
index 27362c2d6f203f53c207641f98cce619dd1d0b6d..e97ff8199f5410e6cdf522805e6849d4fa1a2b8f 100644 (file)
@@ -1,6 +1,13 @@
 public class Firstname implements Comparable<Firstname> {
     private String firstname;
 
+    Firstname() {
+    }
+
+    Firstname(String firstname) {
+        setFirstname(firstname);
+    }
+
     public String getFirstname() {
         return firstname;
     }
index 3ff29770357320cdeaff4b132cbc0208c6878874..8634dc71ac715b2b868398aae1331d2ccf1d48e2 100644 (file)
@@ -5,13 +5,12 @@ public class FirstnameView extends JComponent {
     private JTextField textField;
     private JLabel label;
 
-    FirstnameView(int length) {
+    FirstnameView(int length, Firstname firstnameObj) {
+        setFirstnameObj(firstnameObj);
         this.label = new JLabel();
         this.label.setText("Firstname");
         this.textField = new JTextField(length);
-        if (firstnameObj != null) {
-            this.textField.setText(this.firstnameObj.getFirstname());
-        }
+        this.textField.setText(firstnameObj.getFirstname());
         add(label);
         add(textField);
     }
index 77979dae22387c4523a9cb43dc9b50e57dfc7744..5bac73ebab3fa905a7284370d082a79ba12e49a1 100644 (file)
@@ -1,3 +1,5 @@
+import java.util.ArrayList;
+
 public class Main {
 
     public static void main(String[] args) {
@@ -5,16 +7,24 @@ public class Main {
         //creating and showing this application's GUI.
         String programName = "Person Comparator";
 
-        Eye blackEye = new Eye("black");
-        Eye greenEye = new Eye("green");
-        Eye blueEye = new Eye("blue");
-        Eye brownEye = new Eye("brown");
+        ArrayList<Person> personArrayList = new ArrayList<>();
+        Person person1 = new Person("Alan", "USA", 180, 95, "black");
+        personArrayList.add(person1);
+        Person person2 = new Person("Brice", "France", 190, 82, "brown");
+        personArrayList.add(person2);
+        Person person3 = new Person("Alexandre", "France", 175, 73, "green");
+        personArrayList.add(person3);
+        Person person4 = new Person("Sophia", "Brasil", 155, 57, "blue");
+        personArrayList.add(person4);
 
-        //Person person = new Person();
+        Person emptyPerson = new Person();
+        emptyPerson.setPersonArrayList(personArrayList);
+        PersonView emptyPersonView = new PersonView(emptyPerson);
 
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
-                new MainWindowsView(programName).showGUI();
+                MainWindowsView mainWindows = new MainWindowsView(programName, emptyPersonView);
+                mainWindows.showGUI();
             }
         });
     }
index 729a9ad20382654cc21213f283a6f455e7d9f0c8..dbe2eedaf9beaee2f2f75da5c24374cbfa21121e 100644 (file)
@@ -6,7 +6,7 @@ import java.util.ArrayList;
 
 public class MainWindowsView extends JFrame {
 
-    MainWindowsView(String title) {
+    MainWindowsView(String title, JPanel panel) {
         //Create and set up the window.
         setTitle(title);
         setSize(300, 300);
@@ -26,9 +26,6 @@ public class MainWindowsView extends JFrame {
         });
         fileMenu.add(exit);
 
-        //Main pane
-        JPanel panel = new PersonView();
-
         //Get all Swing/AWT primitive components in the views and add them to the panel.
         ArrayList<Component> components = new ArrayList<>();
         for (int i = 0; i < panel.getComponentCount(); i++) {
index 30efb81c140862718dbe20c013629886a25f58a9..6e89fb93e36900638f8ea51dd4728d48d3e047e2 100644 (file)
@@ -2,9 +2,13 @@ public class Origin implements Comparable<Origin> {
     private String continent;
     private String country;
 
-    Origin(String continent, String country) {
+    Origin() {
+    }
+
+    Origin(String country) {
         setCountry(country);
-        setContinent(continent);
+        //TODO: properly set the continent from the country.
+        setContinent("Europe");
     }
 
     public String getContinent() {
index 2402e5fea6232f49326437c443ec1c4b1bb3a611..695a90cc53b7e41e9d53c50d4c770a91c36fb62d 100644 (file)
@@ -4,16 +4,13 @@ public class OriginView extends JComponent {
     private Origin originObj;
     private JLabel label;
 
-    OriginView() {
+    OriginView(Origin originObj) {
+        setOriginObj(originObj);
         this.label = new JLabel();
         this.label.setText("Origin");
         add(label);
     }
 
-    OriginView(Origin originObj) {
-        setOriginObj(originObj);
-    }
-
     public Origin getOriginObj() {
         return originObj;
     }
index 3e6788ffac59805d10321c8671f96d2e8c6bf6cb..c0483f8eb85b25cc6f25508ba021a5e266a1d203 100644 (file)
@@ -10,12 +10,22 @@ public class Person implements Comparable<Person>, Comparator<Person> {
     //List of persons in the database to compare to.
     private ArrayList<Person> personArrayList;
 
-    Person(Firstname firstname, Origin origin, Size size, Weight weight, Eye eye) {
-        setFirstname(firstname);
-        setOrigin(origin);
-        setPersonSize(size);
-        setWeight(weight);
-        setEye(eye);
+    Person() {
+        setFirstname(new Firstname());
+        setOrigin(new Origin());
+        setPersonSize(new Size());
+        setWeight(new Weight());
+        setEye(new Eye());
+        setPersonArrayList(new ArrayList<>());
+    }
+
+    Person(String firstname, String country, Integer size, Integer weight, String colorEye) {
+        setFirstname(new Firstname(firstname));
+        setOrigin(new Origin(country));
+        setPersonSize(new Size(size));
+        setWeight(new Weight(weight));
+        setEye(new Eye(colorEye));
+        setPersonArrayList(new ArrayList<>());
     }
 
     public void setFirstname(Firstname firstname) {
index 76ccf5add249ff3fa272ef80d3ad2f4e5093b6c8..53816af3db23ea902dda6fd5b99fe45f5dd3c756 100644 (file)
@@ -8,12 +8,13 @@ public class PersonView extends JPanel {
     private WeightView weightView;
     private EyeView eyeView;
 
-    PersonView() {
-        setFirstnameView(new FirstnameView(10));
-        setOriginView(new OriginView());
-        setSizeView(new SizeView());
-        setWeightView(new WeightView());
-        setEyeView(new EyeView());
+    PersonView(Person personObj) {
+        setPersonObj(personObj);
+        setFirstnameView(new FirstnameView(10, this.personObj.getFirstname()));
+        setOriginView(new OriginView(this.personObj.getOrigin()));
+        setSizeView(new SizeView(this.personObj.getPersonSize()));
+        setWeightView(new WeightView(this.personObj.getWeight()));
+        setEyeView(new EyeView(this.personObj.getEye()));
 
         add(firstnameView);
         add(originView);
@@ -22,14 +23,6 @@ public class PersonView extends JPanel {
         add(eyeView);
     }
 
-   /* PersonView(FirstnameView firstnameView, OriginView originView, SizeView sizeView, WeightView weightView, EyeView eyeView) {
-        setFirstnameView(firstnameView);
-        setOriginView(originView);
-        setSizeView(sizeView);
-        setWeightView(weightView);
-        setEyeView(eyeView);
-    }*/
-
     public Person getPersonObj() {
         return personObj;
     }
index c2392073b34662affdc474f619a1ab27519e2a98..e665140b9f83c020a1f23ea1dc973ea45ccdbd22 100644 (file)
@@ -3,6 +3,9 @@ public class Size implements Comparable<Size> {
     private int min = 20;
     private int size;
 
+    Size() {
+    }
+
     Size(int size) {
         setSize(size);
     }
@@ -15,12 +18,12 @@ public class Size implements Comparable<Size> {
         if (validateSize(size)) {
             this.size = size;
         } else {
-            throw new IllegalArgumentException("Size must be between" + this.min + " and " + this.max);
+            throw new IllegalArgumentException("Size must be between " + this.min + " and " + this.max);
         }
     }
 
     private boolean validateSize(int size) {
-        return (size > max || size < min);
+        return (size >= min && size <= max);
     }
 
     @Override
index d426cd9dd6932c83f63d2d3c9c37b54546ac37a7..18e5016661c23d699b6c945bd99babce3a33e37d 100644 (file)
@@ -4,16 +4,13 @@ public class SizeView extends JComponent {
     private Size sizeObj;
     private JLabel label;
 
-    SizeView() {
+    SizeView(Size sizeObj) {
+        setSizeObj(sizeObj);
         this.label = new JLabel();
         this.label.setText("Size");
         add(label);
     }
 
-    SizeView(Size sizeObj) {
-        setSizeObj(sizeObj);
-    }
-
     public Size getSizeObj() {
         return sizeObj;
     }
index e79d1025a9e35977344ab5003e129df1aa88ff03..47547ba4f524711c42a3bc1c3e561fec0be48853 100644 (file)
@@ -1,6 +1,9 @@
 public class Weight implements Comparable<Weight> {
     private int weight;
 
+    Weight() {
+    }
+
     Weight(int weight) {
         setWeight(weight);
     }
index 239c437556dededfbbaf3839a0dba0b5c528d89b..1576b62e2e836d77e3433b3c49e32e092ab79bc2 100644 (file)
@@ -4,16 +4,13 @@ public class WeightView extends JComponent {
     private Weight weightObj;
     private JLabel label;
 
-    WeightView() {
+    WeightView(Weight weightObj) {
+        setWeightObj(weightObj);
         this.label = new JLabel();
         this.label.setText("Weight");
         add(label);
     }
 
-    WeightView(Weight weightObj) {
-        setWeightObj(weightObj);
-    }
-
     public Weight getWeightObj() {
         return weightObj;
     }