-
-
Save parzibyte/44dea22384d742a0f59d2b05e61e8671 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Usuario { | |
private String nombre; | |
private int edad; | |
private String DNI; | |
public Usuario() { | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public int getEdad() { | |
return edad; | |
} | |
public void setEdad(int edad) { | |
this.edad = edad; | |
} | |
public String getDNI() { | |
return DNI; | |
} | |
public boolean setDNI(String DNI) { | |
// Solo puede medir 9 (sin guión) o 10 (con guión) | |
if (DNI.length() != 9 && DNI.length() != 10) { | |
return false; | |
} | |
// Si mide 10, debe tener un guión en la posición 8 | |
if (DNI.length() == 10) { | |
// Extraerlo y regresar false si no es un guión | |
String posibleGuion = String.valueOf(DNI.charAt(8)); | |
if (!posibleGuion.equals("-")) { | |
return false; | |
} | |
} | |
// Hasta ahora sabemos que mide 9 o 10 y que el guión es válido (esté presente o no) | |
String primerosOcho = DNI.substring(0, 8); | |
String ultimo = String.valueOf(DNI.charAt(DNI.length() - 1)); | |
// Comprobar que los primeros 8 sean numéricos | |
if (!primerosOcho.matches("[0-9]+")) { | |
return false; | |
} | |
// Comprobar que el último sea una letra | |
if (!ultimo.matches("[A-Z]+")) { | |
return false; | |
} | |
// Si llegamos hasta aquí y no regresamos arriba, entonces el DNI es válido | |
this.DNI = DNI; | |
return true; | |
} | |
@Override | |
public String toString() { | |
return "Usuario{" + | |
"nombre='" + nombre + '\'' + | |
", edad=" + edad + | |
", DNI='" + DNI + '\'' + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment