viernes, 5 de julio de 2013

Clase Persona

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datos.dominio;

import java.util.Objects;

/**
 *
 * @author DAS
 */
public class Persona {

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 23 * hash + Objects.hashCode(this.id);
        hash = 23 * hash + Objects.hashCode(this.nombre);
        hash = 23 * hash + Objects.hashCode(this.primerApellido);
        hash = 23 * hash + Objects.hashCode(this.segundoApellido);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Persona other = (Persona) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        if (!Objects.equals(this.nombre, other.nombre)) {
            return false;
        }
        if (!Objects.equals(this.primerApellido, other.primerApellido)) {
            return false;
        }
        if (!Objects.equals(this.segundoApellido, other.segundoApellido)) {
            return false;
        }
        return true;
    }

    private Integer id = -1;
    String nombre = "Sin nombre";
    private String primerApellido = null;
    private String segundoApellido = null;

    public Persona() {
        nombre = "";
        primerApellido = "";
        segundoApellido = "";
    }

    public Persona(String nom, String pa, String sa) {
        try {
            //Validando en nombre      

          setNombre(nom);
            //Primer paterno
            if (pa == null || !pa.trim().isEmpty()
                    && pa.matches("[a-zA-Z áéíóíñÑ]+")
                    && pa.length() <= 60) {

                primerApellido = pa;
            } else {
                System.err.println("El texto: " + pa + ""
                        + " no es valido para el primer apellido");
            }

            //Segundo paterno
            if (sa == null || !sa.trim().isEmpty()
                    && sa.matches("[a-zA-Z áéíóíñÑ]+")
                    && sa.length() <= 60) {

                segundoApellido = sa;
            } else {
                System.err.println("El texto: " + sa + ""
                        + " no es valido para el segundo apellido");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            nombre = "Sin nombre";
            primerApellido = null;
            segundoApellido = null;
            throw ex;
        }

    }

    public String getNombre() {
        return nombre;
    }

    
    public void setNombre(String nom) {
        if (nom != null && !nom.trim().isEmpty()
                && nom.matches("[a-zA-Z áéíóíñÑ]+")
                && nom.length() <= 60) {
            //System.out.println("ok");
            nombre = nom;
        } else {
            System.err.println("El texto: " + nom + ""
                    + "No es valido para un nombre");
        }
    }

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return the primerApellido
     */
    public String getPrimerApellido() {
        return primerApellido;
    }

    /**
     * @param primerApellido the primerApellido to set
     */
    public void setPrimerApellido(String primerApellido) {
        this.primerApellido = primerApellido;
    }

    /**
     * @return the segundoApellido
     */
    public String getSegundoApellido() {
        return segundoApellido;
    }

    /**
     * @param segundoApellido the segundoApellido to set
     */
    public void setSegundoApellido(String segundoApellido) {
        this.segundoApellido = segundoApellido;
    }

    @Override
    public String toString() {
        return "Mi toString:" + nombre();
    }

    public String nombre() {

        return nombre + " " + getPrimerApellido() + " " + getSegundoApellido();
    }

    public static void main(String[] args) {
        try {
            // Persona p = new Persona("Nombre", null, null);
            Persona p = new Persona();
            Persona p2 = new Persona("Nombre", null, null);
        } catch (Exception ex) {


            System.out.println("No se pudo crear el objeto persona");
        }
    }
}

No hay comentarios:

Publicar un comentario