package W04H01_AdvancedComparable;

public class PersonV2 implements Comparable<PersonV2> {

    private final String firstName;
    private final String lastName;

    PersonV2(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    String getFirstName() {
        return firstName;
    }

    String getLastName() {
        return lastName;
    }

    @Override
    public int compareTo(PersonV2 other) {
        return this.toStringForComparable().compareTo(other.toStringForComparable());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        return (this.compareTo(this.getClass().cast(obj)) == 0);
        // Equivale a (sin tener que poner el nombre de la classe de this):
        // return (this.compareTo((PersonV2) obj) == 0);
    }

    @Override
    public int hashCode() {
        return this.toStringForComparable().hashCode();
    }

    public String toStringForComparable() {
        return (lastName.toLowerCase() + "\t" + firstName.toLowerCase());
    }
    // '\t' = 9, '\n' = '10' ' ' = 32, ',' = 44
    // Si toStringForComparable() esta bien implementado los 3 metodos
    // anteriores funcionaran en cualquie classe sin cambiar nada. :-)
    
    @Override
    public String toString() {
        return (lastName + ", " + firstName);
    }
}
