Start the implementation of inputs gathering and the comparator code (still buggy...
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 14 Jan 2019 15:01:06 +0000 (16:01 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 14 Jan 2019 15:01:06 +0000 (16:01 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Eye.java
src/EyeView.java
src/FirstnameView.java
src/Main.java
src/OriginView.java
src/Person.java
src/PersonView.java
src/SizeView.java
src/WeightView.java

index 59612440812c342dd6dd929b91936de79363d337..edaea1bb467c07f00a510e155804a25c7b952114 100644 (file)
@@ -47,10 +47,14 @@ public class Eye implements Comparable<Eye> {
      */
     public void setColor(String color) {
         if (validateColor(color)) {
-            if (color.equals("brown"))
+            if (color.equals("black"))
+                this.color = Color.black;
+            else if (color.equals("blue"))
+                this.color = Color.blue;
+            else if (color.equals("brown"))
                 this.color = new Color(88, 41, 0);
-            else
-                this.color = Color.getColor(color);
+            else if (color.equals("green"))
+                this.color = Color.green;
         } else {
             throw new IllegalArgumentException("Color must be" + this.colorListToString());
         }
index 8e7569855b31e566dfb38ddf02ddc0e3e9667758..2f892e4b99e47239078137243c3554caaa181363 100644 (file)
@@ -34,4 +34,8 @@ public class EyeView extends JPanel {
     public void setEyeObj(Eye eyeObj) {
         this.eyeObj = eyeObj;
     }
+
+    public JList getColorsList() {
+        return colorsList;
+    }
 }
index 1fb6feeaa2577961b1025cee6c65b31beba8c85a..cd253e502651f9b11035ba65ab7ed0b21eb83bdd 100644 (file)
@@ -22,4 +22,8 @@ public class FirstnameView extends JPanel {
     public void setFirstnameObj(Firstname firstnameObj) {
         this.firstnameObj = firstnameObj;
     }
+
+    public JTextField getTextField() {
+        return textField;
+    }
 }
index 5a894684ea8c281381fed54cfb78f7409315e791..221d09c15b67fcdcb2e507412a40b98ec0b26aa6 100644 (file)
@@ -3,8 +3,6 @@ import java.util.ArrayList;
 public class Main {
 
     public static void main(String[] args) {
-        //Schedule a job for the event-dispatching thread:
-        //creating and showing this application's GUI.
         String programName = "Person Comparator";
 
         ArrayList<Person> personArrayList = new ArrayList<>();
@@ -16,7 +14,7 @@ public class Main {
         personArrayList.add(person3);
         Person person4 = new Person("Sophia", "Brasil", 155, 57, "blue");
         personArrayList.add(person4);
-        Person person5 = new Person("Sylvain", "Italie", 181, 75, "brow");
+        Person person5 = new Person("Sylvain", "Italie", 181, 75, "brown");
         personArrayList.add(person5);
         Person person6 = new Person("Merlin", "United States of America", 210, 88, "blue");
         personArrayList.add(person6);
@@ -79,6 +77,8 @@ public class Main {
         emptyPerson.setPersonArrayList(personArrayList);
         PersonView emptyPersonView = new PersonView(emptyPerson);
 
+        //Schedule a job for the event-dispatching thread:
+        //creating and showing this application's GUI.
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 MainWindowsView mainWindows = new MainWindowsView(programName, emptyPersonView);
index c347bf93c1d24f33c0bda8967435d16fefc43335..cef40402ad8f302f6add239ddf8957ca647f77c4 100644 (file)
@@ -30,6 +30,10 @@ public class OriginView extends JPanel implements ItemListener {
         this.originObj = originObj;
     }
 
+    public JComboBox<String> getComboBox() {
+        return comboBox;
+    }
+
     @Override
     public void itemStateChanged(ItemEvent event) {
         if (event.getStateChange() == ItemEvent.SELECTED) {
index c0483f8eb85b25cc6f25508ba021a5e266a1d203..4eabb1b9b651a662d6994799bbab666b78cae90f 100644 (file)
@@ -1,5 +1,6 @@
 import java.util.ArrayList;
 import java.util.Comparator;
+import java.util.ListIterator;
 
 public class Person implements Comparable<Person>, Comparator<Person> {
     private Firstname firstname;
@@ -76,6 +77,30 @@ public class Person implements Comparable<Person>, Comparator<Person> {
         return personArrayList;
     }
 
+    public void displayArrayList(ArrayList arrayList) {
+        ListIterator iter = arrayList.listIterator();
+        Integer i = 0;
+        while (iter.hasNext()) {
+            System.out.println(i.toString() + " " + iter.next());
+            i++;
+        }
+    }
+
+    public void displayPersonArrayList() {
+        this.displayArrayList(this.personArrayList);
+    }
+
+    @Override
+    public String toString() {
+        return "Person{" +
+                "firstname=" + firstname.getFirstname() +
+                ", origin=" + origin.getCountry() +
+                ", size=" + size.getSize() +
+                ", weight=" + weight.getWeight() +
+                ", eye=" + eye.getStrColor() + "(" + this.getEye().getColor().getRed() + "," + this.getEye().getColor().getGreen() + "," + this.getEye().getColor().getBlue() + ")" +
+                '}';
+    }
+
     @Override
     public int compareTo(Person person) {
         return this.firstname.compareTo(person.getFirstname()) + this.origin.compareTo(person.getOrigin())
index e703ef3c60b75d5020b87a0dc056ed9e32e0f400..a98c1e7d89796ce7d1078d0b1c202252b261a104 100644 (file)
@@ -2,6 +2,7 @@ import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.util.ArrayList;
 
 public class PersonView extends JPanel implements ActionListener {
     private int width = 400;
@@ -119,6 +120,37 @@ public class PersonView extends JPanel implements ActionListener {
 
     @Override
     public void actionPerformed(ActionEvent actionEvent) {
-
+        //TODO: one can implement a smarter way of getting all inputs values inside the main panel.
+        String firstname = this.getFirstnameView().getTextField().getText();
+        String country = null;
+        if (this.getOriginView().getComboBox().getSelectedIndex() != -1)
+            country = (String) this.getOriginView().getComboBox().getSelectedObjects()[0];
+        Integer size = (Integer) this.getSizeView().getSpinner().getValue();
+        Integer weight = (Integer) this.getWeightView().getComboBox().getSelectedItem();
+        String eye_color = (String) this.getEyeView().getColorsList().getSelectedValue();
+        if (firstname.equals("") || country == null || eye_color == null) {
+            JOptionPane.showMessageDialog(this,
+                    "Some required fields are missing.",
+                    "Error",
+                    JOptionPane.ERROR_MESSAGE);
+        } else {
+            this.getPersonObj().setFirstname(new Firstname(firstname));
+            this.getPersonObj().setOrigin(new Origin(country));
+            this.getPersonObj().setPersonSize(new Size(size));
+            this.getPersonObj().setWeight(new Weight(weight));
+            this.getPersonObj().setEye(new Eye(eye_color));
+            //this.getPersonObj().displayPersonArrayList();
+            ArrayList<Person> personArrayList = this.getPersonObj().getPersonArrayList();
+            personArrayList.add(this.getPersonObj());
+            this.getPersonObj().displayArrayList(personArrayList);
+            personArrayList.sort(getPersonObj());
+            this.getPersonObj().displayArrayList(personArrayList);
+            Integer previousPersonIndex = personArrayList.indexOf(this.getPersonObj()) - 1;
+            Integer nextPersonIndex = personArrayList.indexOf(this.getPersonObj()) + 1;
+            //Person previousPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) - 1);
+            //Person nextPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) + 1);
+            System.out.println(previousPersonIndex);
+            System.out.println(nextPersonIndex);
+        }
     }
 }
index e9e16f62812d2276591c26b67bdf53ad5ceb0b56..bdb2c7e8fa2bd6dbe5889fc6eda1ca54c1b14647 100644 (file)
@@ -27,4 +27,8 @@ public class SizeView extends JPanel {
     public void setSizeObj(Size sizeObj) {
         this.sizeObj = sizeObj;
     }
+
+    public JSpinner getSpinner() {
+        return spinner;
+    }
 }
index fd6d68cb40d0e974ca32b226fb3d80cb6611a9a7..2c41f5ec889c2a73430fb3e1f90a391923a390b5 100644 (file)
@@ -25,4 +25,8 @@ public class WeightView extends JPanel {
     public void setWeightObj(Weight weightObj) {
         this.weightObj = weightObj;
     }
+
+    public JComboBox<Integer> getComboBox() {
+        return comboBox;
+    }
 }