From: Jérôme Benoit Date: Fri, 11 Jan 2019 13:42:30 +0000 (+0100) Subject: Add an ArrayList of persons to the Person class. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b974e749af3a4b2df3737b2d361233c704dcc4d8;p=Persons_Comparator.git Add an ArrayList of persons to the Person class. Fix all Person constructor and attributes constructors. Signed-off-by: Jérôme Benoit --- diff --git a/src/Eye.java b/src/Eye.java index ed2231b..bf83b8a 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -36,7 +36,7 @@ public class Eye implements Comparable { 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 { 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(); + } } diff --git a/src/EyeView.java b/src/EyeView.java index aa54c8b..ad0c2fa 100644 --- a/src/EyeView.java +++ b/src/EyeView.java @@ -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 */ diff --git a/src/Firstname.java b/src/Firstname.java index 27362c2..e97ff81 100644 --- a/src/Firstname.java +++ b/src/Firstname.java @@ -1,6 +1,13 @@ public class Firstname implements Comparable { private String firstname; + Firstname() { + } + + Firstname(String firstname) { + setFirstname(firstname); + } + public String getFirstname() { return firstname; } diff --git a/src/FirstnameView.java b/src/FirstnameView.java index 3ff2977..8634dc7 100644 --- a/src/FirstnameView.java +++ b/src/FirstnameView.java @@ -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); } diff --git a/src/Main.java b/src/Main.java index 77979da..5bac73e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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 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(); } }); } diff --git a/src/MainWindowsView.java b/src/MainWindowsView.java index 729a9ad..dbe2eed 100644 --- a/src/MainWindowsView.java +++ b/src/MainWindowsView.java @@ -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 components = new ArrayList<>(); for (int i = 0; i < panel.getComponentCount(); i++) { diff --git a/src/Origin.java b/src/Origin.java index 30efb81..6e89fb9 100644 --- a/src/Origin.java +++ b/src/Origin.java @@ -2,9 +2,13 @@ public class Origin implements Comparable { 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() { diff --git a/src/OriginView.java b/src/OriginView.java index 2402e5f..695a90c 100644 --- a/src/OriginView.java +++ b/src/OriginView.java @@ -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; } diff --git a/src/Person.java b/src/Person.java index 3e6788f..c0483f8 100644 --- a/src/Person.java +++ b/src/Person.java @@ -10,12 +10,22 @@ public class Person implements Comparable, Comparator { //List of persons in the database to compare to. private ArrayList 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) { diff --git a/src/PersonView.java b/src/PersonView.java index 76ccf5a..53816af 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -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; } diff --git a/src/Size.java b/src/Size.java index c239207..e665140 100644 --- a/src/Size.java +++ b/src/Size.java @@ -3,6 +3,9 @@ public class Size implements Comparable { private int min = 20; private int size; + Size() { + } + Size(int size) { setSize(size); } @@ -15,12 +18,12 @@ public class Size implements Comparable { 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 diff --git a/src/SizeView.java b/src/SizeView.java index d426cd9..18e5016 100644 --- a/src/SizeView.java +++ b/src/SizeView.java @@ -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; } diff --git a/src/Weight.java b/src/Weight.java index e79d102..47547ba 100644 --- a/src/Weight.java +++ b/src/Weight.java @@ -1,6 +1,9 @@ public class Weight implements Comparable { private int weight; + Weight() { + } + Weight(int weight) { setWeight(weight); } diff --git a/src/WeightView.java b/src/WeightView.java index 239c437..1576b62 100644 --- a/src/WeightView.java +++ b/src/WeightView.java @@ -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; }