From 9b3bfcdd95253a04ed12a676ee29a9d777c1e496 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 14 Jan 2019 16:01:06 +0100 Subject: [PATCH] Start the implementation of inputs gathering and the comparator code (still buggy, need work). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Eye.java | 10 +++++++--- src/EyeView.java | 4 ++++ src/FirstnameView.java | 4 ++++ src/Main.java | 6 +++--- src/OriginView.java | 4 ++++ src/Person.java | 25 +++++++++++++++++++++++++ src/PersonView.java | 34 +++++++++++++++++++++++++++++++++- src/SizeView.java | 4 ++++ src/WeightView.java | 4 ++++ 9 files changed, 88 insertions(+), 7 deletions(-) diff --git a/src/Eye.java b/src/Eye.java index 5961244..edaea1b 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -47,10 +47,14 @@ public class Eye implements Comparable { */ 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()); } diff --git a/src/EyeView.java b/src/EyeView.java index 8e75698..2f892e4 100644 --- a/src/EyeView.java +++ b/src/EyeView.java @@ -34,4 +34,8 @@ public class EyeView extends JPanel { public void setEyeObj(Eye eyeObj) { this.eyeObj = eyeObj; } + + public JList getColorsList() { + return colorsList; + } } diff --git a/src/FirstnameView.java b/src/FirstnameView.java index 1fb6fee..cd253e5 100644 --- a/src/FirstnameView.java +++ b/src/FirstnameView.java @@ -22,4 +22,8 @@ public class FirstnameView extends JPanel { public void setFirstnameObj(Firstname firstnameObj) { this.firstnameObj = firstnameObj; } + + public JTextField getTextField() { + return textField; + } } diff --git a/src/Main.java b/src/Main.java index 5a89468..221d09c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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 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); diff --git a/src/OriginView.java b/src/OriginView.java index c347bf9..cef4040 100644 --- a/src/OriginView.java +++ b/src/OriginView.java @@ -30,6 +30,10 @@ public class OriginView extends JPanel implements ItemListener { this.originObj = originObj; } + public JComboBox getComboBox() { + return comboBox; + } + @Override public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { diff --git a/src/Person.java b/src/Person.java index c0483f8..4eabb1b 100644 --- a/src/Person.java +++ b/src/Person.java @@ -1,5 +1,6 @@ import java.util.ArrayList; import java.util.Comparator; +import java.util.ListIterator; public class Person implements Comparable, Comparator { private Firstname firstname; @@ -76,6 +77,30 @@ public class Person implements Comparable, Comparator { 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()) diff --git a/src/PersonView.java b/src/PersonView.java index e703ef3..a98c1e7 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -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 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); + } } } diff --git a/src/SizeView.java b/src/SizeView.java index e9e16f6..bdb2c7e 100644 --- a/src/SizeView.java +++ b/src/SizeView.java @@ -27,4 +27,8 @@ public class SizeView extends JPanel { public void setSizeObj(Size sizeObj) { this.sizeObj = sizeObj; } + + public JSpinner getSpinner() { + return spinner; + } } diff --git a/src/WeightView.java b/src/WeightView.java index fd6d68c..2c41f5e 100644 --- a/src/WeightView.java +++ b/src/WeightView.java @@ -25,4 +25,8 @@ public class WeightView extends JPanel { public void setWeightObj(Weight weightObj) { this.weightObj = weightObj; } + + public JComboBox getComboBox() { + return comboBox; + } } -- 2.34.1