import java.awt.Color;
import java.util.Arrays;
-public class Eye implements Comparable<Eye> {
+public class Eye {
private String strColor;
private Color color;
private String[] colorsList = {"black", "blue", "brown", "green"};
* @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();
-public class Firstname implements Comparable<Firstname> {
+public class Firstname {
private String firstname;
Firstname() {
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
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);
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);
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);
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.
import java.util.ArrayList;
import java.util.Arrays;
-public class Origin implements Comparable<Origin> {
+public class Origin {
private String continent = "Unknown";
private String country;
private String[] continents = {"Africa", "Americas", "Asia", "Europe", "Oceania"};
{"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 */
};
return "Unknown";
}
- @Override
- public int compareTo(Origin origin) {
+ public int distanceTo(Origin origin) {
return 0;
}
}
-import java.util.ArrayList;
import java.util.Comparator;
-import java.util.ListIterator;
-public class Person implements Comparable<Person>, Comparator<Person> {
+public class Person implements Comparator<Person> {
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<Person> personArrayList;
+ private Integer distanceFromReference;
Person() {
setFirstname(new Firstname());
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) {
setPersonSize(new Size(size));
setWeight(new Weight(weight));
setEye(new Eye(colorEye));
- setPersonArrayList(new ArrayList<>());
+ setDistanceFromReference(0);
}
public void setFirstname(Firstname firstname) {
return eye;
}
- public void setPersonArrayList(ArrayList<Person> personArrayList) {
- this.personArrayList = personArrayList;
+ public void setDistanceFromReference(Integer distanceFromReference) {
+ this.distanceFromReference = distanceFromReference;
}
- public ArrayList<Person> 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
", 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());
}
}
private int width = 400;
private int height = 600;
private Person personObj;
+ private ArrayList<Person> personArrayList;
private FirstnameView firstnameView;
private OriginView originView;
private SizeView sizeView;
/*private ImageIcon originImage;
private ImageIcon personImage;*/
- PersonView(Person personObj) {
+ PersonView(Person personObj, ArrayList<Person> 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");
this.weightView = weightView;
}
+ /**
+ * @return
+ */
+ public EyeView getEyeView() {
+ return eyeView;
+ }
+
/**
* @param eyeView
*/
this.eyeView = eyeView;
}
- /**
- * @return
- */
- public EyeView getEyeView() {
- return eyeView;
+ public ArrayList<Person> getPersonArrayList() {
+ return personArrayList;
+ }
+
+ public void setPersonArrayList(ArrayList<Person> personArrayList) {
+ this.personArrayList = personArrayList;
}
@Override
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);
+ ArrayList<Person> 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);
}
}
}
-public class Size implements Comparable<Size> {
+public class Size {
private int max = 240;
private int min = 40;
+ private int defaultSize = 160;
private int size;
Size() {
return max;
}
+ public int getDefaultSize() {
+ return defaultSize;
+ }
+
public int getSize() {
return size;
}
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);
}
}
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);
+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<Person> personArrayList) {
+ if (!personArrayList.contains(personFrom))
+ personArrayList.add(personFrom);
+ ListIterator<Person> iter = personArrayList.listIterator();
+ while (iter.hasNext()) {
+ Person personCursor = iter.next();
+ personCursor.setDistanceFromReference(personFrom.distanceTo(personCursor));
+ }
+ }
-public class LevenshteinDistance {
/**
* @param a
* @param b
-public class Weight implements Comparable<Weight> {
+public class Weight {
private int min = 2;
private int max = 600;
private int defaultWeight = 72;
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);
}
}