*/
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());
}
public void setEyeObj(Eye eyeObj) {
this.eyeObj = eyeObj;
}
+
+ public JList getColorsList() {
+ return colorsList;
+ }
}
public void setFirstnameObj(Firstname firstnameObj) {
this.firstnameObj = firstnameObj;
}
+
+ public JTextField getTextField() {
+ return textField;
+ }
}
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<Person> personArrayList = new ArrayList<>();
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);
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);
this.originObj = originObj;
}
+ public JComboBox<String> getComboBox() {
+ return comboBox;
+ }
+
@Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
import java.util.ArrayList;
import java.util.Comparator;
+import java.util.ListIterator;
public class Person implements Comparable<Person>, Comparator<Person> {
private Firstname firstname;
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())
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;
@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<Person> 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);
+ }
}
}
public void setSizeObj(Size sizeObj) {
this.sizeObj = sizeObj;
}
+
+ public JSpinner getSpinner() {
+ return spinner;
+ }
}
public void setWeightObj(Weight weightObj) {
this.weightObj = weightObj;
}
+
+ public JComboBox<Integer> getComboBox() {
+ return comboBox;
+ }
}