Fix all Person constructor and attributes constructors.
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
this.color = Color.getColor(color);
} else {
- throw new IllegalArgumentException("Color must be " + colorsList);
+ throw new IllegalArgumentException("Color must be" + new Eye().toString());
}
}
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();
+ }
}
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());
add(colorsList);
}
- /**
- * @param eyeObj
- */
- EyeView(Eye eyeObj) {
- setEyeObj(eyeObj);
- }
-
/**
* @return
*/
public class Firstname implements Comparable<Firstname> {
private String firstname;
+ Firstname() {
+ }
+
+ Firstname(String firstname) {
+ setFirstname(firstname);
+ }
+
public String getFirstname() {
return firstname;
}
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);
}
+import java.util.ArrayList;
+
public class Main {
public static void main(String[] args) {
//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<Person> 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();
}
});
}
public class MainWindowsView extends JFrame {
- MainWindowsView(String title) {
+ MainWindowsView(String title, JPanel panel) {
//Create and set up the window.
setTitle(title);
setSize(300, 300);
});
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<Component> components = new ArrayList<>();
for (int i = 0; i < panel.getComponentCount(); i++) {
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() {
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;
}
//List of persons in the database to compare to.
private ArrayList<Person> 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) {
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);
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;
}
private int min = 20;
private int size;
+ Size() {
+ }
+
Size(int size) {
setSize(size);
}
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
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;
}
public class Weight implements Comparable<Weight> {
private int weight;
+ Weight() {
+ }
+
Weight(int weight) {
setWeight(weight);
}
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;
}