From 5687dc10a68b52d28b63151d8c16a45775ef2e77 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 15 Jan 2019 12:24:06 +0100 Subject: [PATCH] Implement the person comparator : MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Add a distance attribute in the Person class; * Add helper function to compute the distance from a reference person; * Display the person (text only for now) in the GUI. Signed-off-by: Jérôme Benoit --- src/Eye.java | 5 +-- src/Firstname.java | 7 ++-- src/Main.java | 13 +++--- src/Origin.java | 7 ++-- src/Person.java | 44 +++++++------------- src/PersonView.java | 41 ++++++++++-------- src/Size.java | 16 +++---- src/SizeView.java | 2 +- src/{LevenshteinDistance.java => Utils.java} | 23 +++++++++- src/Weight.java | 10 ++--- 10 files changed, 86 insertions(+), 82 deletions(-) rename src/{LevenshteinDistance.java => Utils.java} (54%) diff --git a/src/Eye.java b/src/Eye.java index edaea1b..822e6f8 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -1,7 +1,7 @@ import java.awt.Color; import java.util.Arrays; -public class Eye implements Comparable { +public class Eye { private String strColor; private Color color; private String[] colorsList = {"black", "blue", "brown", "green"}; @@ -81,8 +81,7 @@ public class Eye implements Comparable { * @param eye * @return */ - @Override - public int compareTo(Eye eye) { + public int distanceTo(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(); diff --git a/src/Firstname.java b/src/Firstname.java index e97ff81..f5b543e 100644 --- a/src/Firstname.java +++ b/src/Firstname.java @@ -1,4 +1,4 @@ -public class Firstname implements Comparable { +public class Firstname { private String firstname; Firstname() { @@ -16,8 +16,7 @@ public class Firstname implements Comparable { this.firstname = firstname; } - @Override - public int compareTo(Firstname firstname) { - return LevenshteinDistance.computeLevenshteinDistance(this.getFirstname(), firstname.getFirstname()); + public int distanceTo(Firstname firstname) { + return Utils.computeLevenshteinDistance(this.getFirstname(), firstname.getFirstname()); } } \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 221d09c..f7714fd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -20,7 +20,7 @@ public class Main { personArrayList.add(person6); Person person7 = new Person("Bob", "United States of America", 162, 75, "green"); personArrayList.add(person7); - Person person8 = new Person("Jonh", "United Kingdom", 176, 102, "brown"); + Person person8 = new Person("John", "United Kingdom", 176, 102, "brown"); personArrayList.add(person8); Person person9 = new Person("Kithe", "Germany", 166, 69, "black"); personArrayList.add(person9); @@ -36,9 +36,9 @@ public class Main { personArrayList.add(person14); Person person15 = new Person("Abdel", "Mali", 168, 52, "black"); personArrayList.add(person15); - Person person16 = new Person("Sherrazade", "Mali", 148, 48, "black"); + Person person16 = new Person("Shéhérazade", "Mali", 148, 48, "black"); personArrayList.add(person16); - Person person17 = new Person("Aifa", "Mali", 149, 49, "black"); + Person person17 = new Person("Aïfa", "Mali", 149, 49, "black"); personArrayList.add(person17); Person person18 = new Person("Roberto", "Brasil", 168, 56, "brown"); personArrayList.add(person18); @@ -64,9 +64,9 @@ public class Main { personArrayList.add(person28); Person person29 = new Person("Ange", "France", 175, 62, "green"); personArrayList.add(person29); - Person person30 = new Person("Aurelie", "France", 163, 59, "green"); + Person person30 = new Person("Aurélie", "France", 163, 59, "green"); personArrayList.add(person30); - Person person31 = new Person("Audrey", "United Kingdom", 163, 69, "blue"); + Person person31 = new Person("Audrey", "United Kingdom", 169, 69, "green"); personArrayList.add(person31); Person person32 = new Person("Anthony", "United Kingdom", 176, 69, "black"); personArrayList.add(person32); @@ -74,8 +74,7 @@ public class Main { personArrayList.add(person33); Person emptyPerson = new Person(); - emptyPerson.setPersonArrayList(personArrayList); - PersonView emptyPersonView = new PersonView(emptyPerson); + PersonView emptyPersonView = new PersonView(emptyPerson, personArrayList); //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. diff --git a/src/Origin.java b/src/Origin.java index 7d3e855..c02dd28 100644 --- a/src/Origin.java +++ b/src/Origin.java @@ -1,7 +1,7 @@ import java.util.ArrayList; import java.util.Arrays; -public class Origin implements Comparable { +public class Origin { private String continent = "Unknown"; private String country; private String[] continents = {"Africa", "Americas", "Asia", "Europe", "Oceania"}; @@ -9,7 +9,7 @@ public class Origin implements Comparable { {"Mali"}, /* Africa */ {"United States of America", "Brasil", "Mexico"}, /* Americas */ {"India"}, /* Asia */ - {"France", "Germany", "United Kingdom", "Belgium", "Norway"}, /* Europe */ + {"France", "Italie", "Germany", "United Kingdom", "Belgium", "Norway"}, /* Europe */ {"Australia"} /* Oceania */ }; @@ -62,8 +62,7 @@ public class Origin implements Comparable { return "Unknown"; } - @Override - public int compareTo(Origin origin) { + public int distanceTo(Origin origin) { return 0; } } diff --git a/src/Person.java b/src/Person.java index 4eabb1b..5d9d812 100644 --- a/src/Person.java +++ b/src/Person.java @@ -1,15 +1,12 @@ -import java.util.ArrayList; import java.util.Comparator; -import java.util.ListIterator; -public class Person implements Comparable, Comparator { +public class Person implements Comparator { private Firstname firstname; private Origin origin; private Size size; private Weight weight; private Eye eye; - //List of persons in the database to compare to. - private ArrayList personArrayList; + private Integer distanceFromReference; Person() { setFirstname(new Firstname()); @@ -17,7 +14,7 @@ public class Person implements Comparable, Comparator { setPersonSize(new Size()); setWeight(new Weight()); setEye(new Eye()); - setPersonArrayList(new ArrayList<>()); + setDistanceFromReference(0); } Person(String firstname, String country, Integer size, Integer weight, String colorEye) { @@ -26,7 +23,7 @@ public class Person implements Comparable, Comparator { setPersonSize(new Size(size)); setWeight(new Weight(weight)); setEye(new Eye(colorEye)); - setPersonArrayList(new ArrayList<>()); + setDistanceFromReference(0); } public void setFirstname(Firstname firstname) { @@ -69,25 +66,12 @@ public class Person implements Comparable, Comparator { return eye; } - public void setPersonArrayList(ArrayList personArrayList) { - this.personArrayList = personArrayList; + public void setDistanceFromReference(Integer distanceFromReference) { + this.distanceFromReference = distanceFromReference; } - public ArrayList getPersonArrayList() { - 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); + public Integer getDistanceFromReference() { + return distanceFromReference; } @Override @@ -98,18 +82,18 @@ public class Person implements Comparable, Comparator { ", size=" + size.getSize() + ", weight=" + weight.getWeight() + ", eye=" + eye.getStrColor() + "(" + this.getEye().getColor().getRed() + "," + this.getEye().getColor().getGreen() + "," + this.getEye().getColor().getBlue() + ")" + + ", distanceFromReference=" + this.getDistanceFromReference() + '}'; } - @Override - public int compareTo(Person person) { - return this.firstname.compareTo(person.getFirstname()) + this.origin.compareTo(person.getOrigin()) - + this.size.compareTo(person.getPersonSize()) + this.weight.compareTo(person.getWeight()) - + this.eye.compareTo(person.getEye()); + public Integer distanceTo(Person person) { + return this.getFirstname().distanceTo(person.getFirstname()) + this.getOrigin().distanceTo(person.getOrigin()) + + this.getPersonSize().distanceTo(person.getPersonSize()) + this.getWeight().distanceTo(person.getWeight()) + + this.getEye().distanceTo(person.getEye()); } @Override public int compare(Person p1, Person p2) { - return p1.compareTo(p2); + return p1.getDistanceFromReference().compareTo(p2.getDistanceFromReference()); } } diff --git a/src/PersonView.java b/src/PersonView.java index a98c1e7..8cee3e2 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -8,6 +8,7 @@ public class PersonView extends JPanel implements ActionListener { private int width = 400; private int height = 600; private Person personObj; + private ArrayList personArrayList; private FirstnameView firstnameView; private OriginView originView; private SizeView sizeView; @@ -18,13 +19,14 @@ public class PersonView extends JPanel implements ActionListener { /*private ImageIcon originImage; private ImageIcon personImage;*/ - PersonView(Person personObj) { + PersonView(Person personObj, ArrayList personArrayList) { setPersonObj(personObj); setFirstnameView(new FirstnameView(15, 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())); + setPersonArrayList(personArrayList); this.compareButton.addActionListener(this); JLabel personLabel = new JLabel("Closest person found"); @@ -104,6 +106,13 @@ public class PersonView extends JPanel implements ActionListener { this.weightView = weightView; } + /** + * @return + */ + public EyeView getEyeView() { + return eyeView; + } + /** * @param eyeView */ @@ -111,11 +120,12 @@ public class PersonView extends JPanel implements ActionListener { this.eyeView = eyeView; } - /** - * @return - */ - public EyeView getEyeView() { - return eyeView; + public ArrayList getPersonArrayList() { + return personArrayList; + } + + public void setPersonArrayList(ArrayList personArrayList) { + this.personArrayList = personArrayList; } @Override @@ -139,18 +149,15 @@ public class PersonView extends JPanel implements ActionListener { 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); + ArrayList personArrayList = this.getPersonArrayList(); + //Utils.displayArrayList(personArrayList); + Utils.computeDistanceFrom(getPersonObj(), 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); + //Utils.displayArrayList(personArrayList); + Person closestPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) + 1); + JLabel closestPersonLabel = new JLabel(); + closestPersonLabel.setText(closestPerson.toString()); + this.personPanel.add(closestPersonLabel); } } } diff --git a/src/Size.java b/src/Size.java index a61f24c..eb8003f 100644 --- a/src/Size.java +++ b/src/Size.java @@ -1,6 +1,7 @@ -public class Size implements Comparable { +public class Size { private int max = 240; private int min = 40; + private int defaultSize = 160; private int size; Size() { @@ -18,6 +19,10 @@ public class Size implements Comparable { return max; } + public int getDefaultSize() { + return defaultSize; + } + public int getSize() { return size; } @@ -34,13 +39,8 @@ public class Size implements Comparable { return (size >= getMin() && size <= getMax()); } - @Override - public int compareTo(Size size) { + public int distanceTo(Size size) { int distance = size.getSize() - this.getSize(); - if (distance >= 0) - return distance; - else - return -distance; - + return Math.abs(distance); } } diff --git a/src/SizeView.java b/src/SizeView.java index bdb2c7e..fd17e7a 100644 --- a/src/SizeView.java +++ b/src/SizeView.java @@ -9,7 +9,7 @@ public class SizeView extends JPanel { setSizeObj(sizeObj); this.label = new JLabel(); this.label.setText("Size (meters)"); - SpinnerNumberModel numberModel = new SpinnerNumberModel((this.getSizeObj().getMax() - this.getSizeObj().getMin()) / 2, + SpinnerNumberModel numberModel = new SpinnerNumberModel(this.getSizeObj().getDefaultSize(), this.getSizeObj().getMin(), this.getSizeObj().getMax(), 1); diff --git a/src/LevenshteinDistance.java b/src/Utils.java similarity index 54% rename from src/LevenshteinDistance.java rename to src/Utils.java index 3706772..b551cb1 100644 --- a/src/LevenshteinDistance.java +++ b/src/Utils.java @@ -1,5 +1,26 @@ +import java.util.ArrayList; +import java.util.ListIterator; + +public class Utils { + public static void displayArrayList(ArrayList arrayList) { + ListIterator iter = arrayList.listIterator(); + Integer i = 0; + while (iter.hasNext()) { + System.out.println(i.toString() + " " + iter.next()); + i++; + } + } + + public static void computeDistanceFrom(Person personFrom, ArrayList personArrayList) { + if (!personArrayList.contains(personFrom)) + personArrayList.add(personFrom); + ListIterator iter = personArrayList.listIterator(); + while (iter.hasNext()) { + Person personCursor = iter.next(); + personCursor.setDistanceFromReference(personFrom.distanceTo(personCursor)); + } + } -public class LevenshteinDistance { /** * @param a * @param b diff --git a/src/Weight.java b/src/Weight.java index 4b67611..82fa712 100644 --- a/src/Weight.java +++ b/src/Weight.java @@ -1,4 +1,4 @@ -public class Weight implements Comparable { +public class Weight { private int min = 2; private int max = 600; private int defaultWeight = 72; @@ -48,12 +48,8 @@ public class Weight implements Comparable { return intArray; } - @Override - public int compareTo(Weight weight) { + public int distanceTo(Weight weight) { int distance = weight.getWeight() - this.getWeight(); - if (distance >= 0) - return distance; - else - return -distance; + return Math.abs(distance); } } -- 2.34.1