Advertisement
Guest User

usuario

a guest
Feb 25th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. /**
  2. * Proyecto: Juego de la vida.
  3. * Implementa el concepto de Usuario según el modelo1.1
  4. * En esta versión sólo se ha aplicado un diseño OO básico.
  5. * @since: prototipo 0.1.0
  6. * @source: Usuario.java
  7. * @version: 0.1.2 - 2020/01/7
  8. * @author: TIP
  9. */
  10.  
  11. package modelo;
  12.  
  13. import util.Fecha;
  14. import util.Formato;
  15.  
  16. public class Usuario extends Persona implements Identificable {
  17.  
  18. public enum RolUsuario {
  19. NORMAL,
  20. ADMIN,
  21. INVITADO;
  22. }
  23.  
  24. private String id;
  25. private Fecha fechaAlta;
  26. private ClaveAcceso claveAcceso;
  27. private RolUsuario rol;
  28.  
  29.  
  30. /**
  31. * Constructor convencional. Utiliza métodos set...()
  32. * @param fechaAlta
  33. * @param claveAcceso
  34. * @param rol
  35. */
  36. public Usuario(Nif nif, String nombre, String apellidos,
  37. DireccionPostal domicilio, Correo correo, Fecha fechaNacimiento,
  38. Fecha fechaAlta, ClaveAcceso claveAcceso, RolUsuario rol) {
  39. // setNif(nif);
  40. // setNombre(nombre);
  41. // setApellidos(apellidos);
  42. // setDomicilio(domicilio);
  43. // setCorreo(correo);
  44. // setFechaNacimiento(fechaNacimiento);
  45. super(nif,nombre, apellidos, domicilio, correo, fechaNacimiento);
  46. setFechaAlta(fechaAlta);
  47. setClaveAcceso(claveAcceso);
  48. setRol(rol);
  49. generarId();
  50. }
  51.  
  52. private void generarId() {
  53. assert this.nif != null;
  54. assert this.nombre != null;
  55. assert this.apellidos != null;
  56. String[] apellidos = this.apellidos.split("[ ]");
  57. this.id = ""+ this.nombre.charAt(0)
  58. + apellidos[0].charAt(0) + apellidos[1].charAt(0)
  59. + this.nif.getTexto().substring(7);
  60. this.id = this.id.toUpperCase();
  61. }
  62.  
  63. public void VariarId() {
  64. String alfabetoNif = Formato.LETRAS_NIF;
  65. String alfabetoNifDesplazado = alfabetoNif.substring(1) + alfabetoNif.charAt(0);
  66. this.id = this.id.substring(0, 4)
  67. + alfabetoNifDesplazado.charAt(alfabetoNif.indexOf(id.charAt(4)));
  68. }
  69.  
  70. public Usuario() {
  71. this(new Nif(),
  72. "Invitado",
  73. "Invitado Invitado",
  74. new DireccionPostal(),
  75. new Correo(),
  76. new Fecha().addYears(-16),
  77. new Fecha(),
  78. new ClaveAcceso(),
  79. RolUsuario.INVITADO);
  80. }
  81.  
  82. /**
  83. * Constructor copia.
  84. * @param usr
  85. */
  86. public Usuario(Usuario usuario) {
  87. this(new Nif(usuario.nif),
  88. new String(usuario.nombre),
  89. new String(usuario.apellidos),
  90. new DireccionPostal(usuario.domicilio),
  91. new Correo(usuario.correo),
  92. new Fecha(usuario.fechaNacimiento),
  93. new Fecha(usuario.fechaAlta),
  94. new ClaveAcceso(usuario.claveAcceso),
  95. usuario.rol
  96. );
  97. }
  98.  
  99. @Override
  100. public String getId() {
  101. return id;
  102. }
  103.  
  104.  
  105. public Fecha getFechaAlta() {
  106. return fechaAlta;
  107. }
  108.  
  109. public void setFechaAlta(Fecha fechaAlta) {
  110. assert fechaAlta != null;
  111. if (fechaAltaValida(fechaAlta)) {
  112. this.fechaAlta = fechaAlta;
  113. }
  114. // Todavía no se gestionan errores de usuario.
  115. if (this.fechaAlta == null) { // Tiempo de construcción.
  116. this.fechaAlta = new Usuario().fechaAlta; // Defecto.
  117. }
  118. }
  119.  
  120. /**
  121. * Comprueba validez de una fecha de alta.
  122. * @param fechaAlta.
  123. * @return true si cumple.
  124. */
  125. private boolean fechaAltaValida(Fecha fechaAlta) {
  126. return !fechaAlta.after(new Fecha()); // Que no sea futura.
  127. }
  128.  
  129. public ClaveAcceso getClaveAcceso() {
  130. return claveAcceso;
  131. }
  132.  
  133. public void setClaveAcceso(ClaveAcceso claveAcceso) {
  134. assert claveAcceso != null;
  135. this.claveAcceso = claveAcceso;
  136. }
  137.  
  138. public RolUsuario getRol() {
  139. return rol;
  140. }
  141.  
  142. public void setRol(RolUsuario rol) {
  143. assert rol != null;
  144. this.rol = rol;
  145. }
  146.  
  147. /**
  148. * Redefine el método heredado de la clase Objecto.
  149. * @return el texto formateado del estado -valores de atributos- de objeto de la clase Usuario.
  150. */
  151. @Override
  152. public String toString() {
  153. return super.toString() + String.format(
  154. "%-16s %s\n"
  155. + "%-16s %s\n"
  156. + "%-16s %s\n"
  157. + "%-16s %s\n",
  158. "id:", this.id,
  159. "fechaAlta:", (this.fechaAlta.getYear()) + "."
  160. + (this.fechaAlta.getMes()) + "."
  161. + this.fechaAlta.getDia(),
  162. "claveAcceso:", this.claveAcceso,
  163. "rol:", this.rol
  164. );
  165. }
  166.  
  167.  
  168.  
  169.  
  170. @Override
  171. public int hashCode() {
  172. final int prime = 31;
  173. int result = super.hashCode();
  174. result = prime * result + ((claveAcceso == null) ? 0 : claveAcceso.hashCode());
  175. result = prime * result + ((fechaAlta == null) ? 0 : fechaAlta.hashCode());
  176. result = prime * result + ((id == null) ? 0 : id.hashCode());
  177. result = prime * result + ((rol == null) ? 0 : rol.hashCode());
  178. return result;
  179. }
  180.  
  181. @Override
  182. public boolean equals(Object obj) {
  183. if (this == obj)
  184. return true;
  185. if (!super.equals(obj))
  186. return false;
  187. if (getClass() != obj.getClass())
  188. return false;
  189. Usuario other = (Usuario) obj;
  190. if (claveAcceso == null) {
  191. if (other.claveAcceso != null)
  192. return false;
  193. } else if (!claveAcceso.equals(other.claveAcceso))
  194. return false;
  195. if (fechaAlta == null) {
  196. if (other.fechaAlta != null)
  197. return false;
  198. } else if (!fechaAlta.equals(other.fechaAlta))
  199. return false;
  200. if (id == null) {
  201. if (other.id != null)
  202. return false;
  203. } else if (!id.equals(other.id))
  204. return false;
  205. if (rol != other.rol)
  206. return false;
  207. return true;
  208. }
  209.  
  210. @Override
  211. public Usuario clone() {
  212. return new Usuario(this);
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement