forked from Eliiss/ProyectoFinal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsuario.java
306 lines (254 loc) · 11 KB
/
Usuario.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// si te sobra tiempo verifica si el dni y el cif son reales o no
public class Usuario {
private static final String EXPRESION_CORREO = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})*$";
//el + indica que debe haber al menos una y * es cero o varios
private static final String EXPRESION_NOMBRE = "^([A-Z]{1}[a-z]+[ ]*){1,2}";
// las llaves son cuantas repeticiones
private static final String EXPRESION_DNI = "^[0-9]{8}[A-Z]$";
private static final String EXPRESION_TELEFONO = "^\\+34[69]\\d{8}$";
/*número de teléfono que empieza con el prefijode España),
seguido de un "6" o un "9", y ocho dígitos más*/
private static final String EXPRESION_CLAVE = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{8,}$";
/* clave con 8 caracteres con al menos un numero, un caracter especial y una letra
varios grupos lookhead que simplemente evaluan una condicion
(?=.*[A-Za-z]) evalua si hay caracteres letras
(?=.*\d) evalua si hay caracteres digitos
(?=.*[@$!%*#?&]) evalua si hay caracteres especiales
[A-Za-z\\d@$!%*#?&]{8,} longitud minima de 8 caracteres
*/
private static final String EXPRESION_WEB = "^https://[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(:[0-9]+)?(\\/[A-Za-z0-9-]+)*(\\/[A-Za-z0-9-]+\\.[A-Za-z]+)?$";
/*
Comienza con "https://",
serie de caracteres alfanuméricos y guiones, que pueden estar seguidos por una o más repeticiones
Número de puerto opcional , serie de directorios opcionales, seguidos de un nombre de archivo opcional con una extensión de archivo.
*/
private static final String EXPRESION_CIF= "^[A-HJNP-SUVW]{1}[0-9]{7}[0-9A-J]{1}$";
private TipoUsuario tipoUsuario;
private String correo;
private String nombre;
private String DNI;
private TarjetaCredito tarjetaCredito;
private Direccion direccion;
private String numeroTelefono;
private String clave;
private String website;
private String CIF;
// PREVENIMOS ALGUNOS ERRORES DEL USUARIO MEDIANTE UN CONSTRUCTOR
public Usuario( TipoUsuario tipoUsuario , String correo, String nombre, String DNI, Direccion direccion, String clave, TarjetaCredito tarjetaCredito, String numeroTelefono, String website, String CIF) {
if (tipoUsuario == null || correo == null|| clave == null) {
throw new IllegalArgumentException("Faltan campos por especificar");
}
switch (tipoUsuario){
case ADMINISTRADOR :
if (!esCorreoValido(correo)) {
throw new IllegalArgumentException("El correo electrónico proporcionado es inválido");
}
if (! "admin".equals(clave)){
throw new IllegalArgumentException("La clave proporcionada es inválida");
}
break;
case CLIENTE_PARTICULAR :
if ( DNI == null || direccion == null || numeroTelefono == null || tarjetaCredito == null || nombre == null) {
throw new IllegalArgumentException("Faltan campos por especificar");
}
if (!esCorreoValido(correo)) {
throw new IllegalArgumentException("El correo electrónico proporcionado es inválido");
}
if (!esDNIValido(DNI)) {
throw new IllegalArgumentException("El DNI proporcionado es inválido");
}
if (!esNombreValido(nombre)) {
throw new IllegalArgumentException("El nombre proporcionado es inválido");
}
if (!esNumeroTelefono(numeroTelefono)) {
throw new IllegalArgumentException("El número proporcionado es inválido");
}
if (!esClaveCliente(clave)) {
throw new IllegalArgumentException("La clave proporcionada es inválida");
}
break;
case EMPRESA:
if ( CIF == null || direccion == null || numeroTelefono == null || tarjetaCredito == null || website == null|| nombre == null) {
throw new IllegalArgumentException("Faltan campos por especificar");
}
if (!esCorreoValido(correo)) {
throw new IllegalArgumentException("El correo electrónico proporcionado es inválido");
}
if (!esNumeroTelefono(numeroTelefono)) {
throw new IllegalArgumentException("El número proporcionado es inválido");
}
if (!esClaveCliente(clave)) {
throw new IllegalArgumentException("La clave proporcionada es inválida");
}
if (!esWebSite(website)) {
throw new IllegalArgumentException("No se encuentra la página web");
}
if (!esCIFValido(CIF)) {
throw new IllegalArgumentException("El CIF proporcionado es invalido");
}
break;
default:
throw new IllegalArgumentException(" Usuario es desconocido");
}
this.tipoUsuario = tipoUsuario;
this.correo = correo;
this.nombre = nombre;
this.DNI = DNI;
this.direccion = direccion;
this.tarjetaCredito = tarjetaCredito;
this.numeroTelefono = numeroTelefono;
this.clave = clave;
this.website = website;
this.CIF = CIF;
}
public TipoUsuario getTipoUsuario() {
return tipoUsuario;
}
public TipoUsuario setTipoUsuario(TipoUsuario tipoUsuario) {
return this.tipoUsuario=tipoUsuario;
}
public String getCorreo() {
return correo;
}
private boolean esCorreoValido(String email){
Pattern patron= Pattern.compile( EXPRESION_CORREO);
Matcher match = patron.matcher(email);
return match.find(); // comprobamos unicamente si el formato del correo es correcto
}
public void setCorreo(String correo) {
if (esCorreoValido(correo)){
this.correo = correo;
}
}
public String getNombre() {
return nombre;
}
public static boolean esNombreValido(String nombre){
Pattern patron= Pattern.compile( EXPRESION_NOMBRE);
Matcher match = patron.matcher(nombre);
return match.find(); // De nuevo, solo validamos el formato no si es el nombre es coherente
}
public void setNombre(String nombre) {
if (esNombreValido(nombre)){
this.nombre=nombre;
}
}
public String getCIF() {
return CIF;
}
private static boolean esCIFValido(String CIF){
Pattern patron = Pattern.compile(EXPRESION_CIF);
Matcher match = patron.matcher(CIF);
return match.find();
}
public void setCIF(String CIF) {
if (esCIFValido(CIF)){
this.CIF = CIF;
}
}
public String getDNI() {
return DNI;
}
public static boolean esDNIValido(String DNI){
Pattern patron= Pattern.compile( EXPRESION_DNI);
Matcher match = patron.matcher(DNI);
return match.find(); // NO CALCULA SI EL FORMATO ES UN DNI POSIBLE SOLO COMPRUEBA EL FORMATO / ESTRUCTURA
}
public void setDNI(String DNI) {
if (esDNIValido(DNI)){
this.DNI = DNI;
}
}
public TarjetaCredito getTarjetaCredito() {
return tarjetaCredito;
}
public void setTarjetaCredito(TarjetaCredito tarjetaCredito) {
this.tarjetaCredito = tarjetaCredito;
}
public Direccion getDireccion() {
return direccion;
}
public void setDireccion(Direccion direccion) {
this.direccion = direccion;
}
public String getNumeroTelefono() {
return numeroTelefono;
}
private boolean esNumeroTelefono( String numeroTelefono){
Pattern patron = Pattern.compile(EXPRESION_TELEFONO);
Matcher match = patron.matcher(numeroTelefono);
return match.find();
}
public void setNumeroTelefono(String numeroTelefono) {
if (esNumeroTelefono(numeroTelefono)){
this.numeroTelefono = numeroTelefono;
}
}
public String getClave() {
return clave;
}
private boolean esClaveCliente(String clave){
Pattern patron = Pattern.compile(EXPRESION_CLAVE);
Matcher match = patron.matcher(clave);
return match.find();
}
public void setClave(String clave) {
if (esClaveCliente (clave)){
this.clave = clave;
}
}
public String getWebsite() {
return website;
}
private boolean esWebSite(String website){
Pattern patron = Pattern.compile(EXPRESION_WEB);
Matcher matcher = patron.matcher(website);
return matcher.find();
}
public void setWebsite(String website) {
if (esWebSite(website)){
this.website = website;
}
}
public void registrarse(TipoUsuario tipoUsuario, String correo, String clave, String nombre, Direccion direccion, TarjetaCredito tarjetaCredito, String numeroTelefono, String CIF) {
switch(tipoUsuario){
case CLIENTE_PARTICULAR:
this.correo = correo;
this.clave = clave;
this.nombre = nombre;
this.direccion = direccion;
this.tarjetaCredito = tarjetaCredito;
this.numeroTelefono = numeroTelefono;
case EMPRESA:
this.correo = correo;
this.clave = clave;
this.nombre = nombre;
this.direccion = direccion;
this.tarjetaCredito = tarjetaCredito;
this.numeroTelefono = numeroTelefono;
this.CIF = CIF;
case ADMINISTRADOR:
this.clave=clave;
this.correo=correo;
}
}
public boolean iniciarSesion(String correo, String clave) {
return (this.correo.equals(correo) && this.clave.equals(clave));
}
public void actualizarDatos(Direccion direccion, TarjetaCredito tarjetaCredito, String numeroTelefono) {
this.direccion = direccion;
this.tarjetaCredito = tarjetaCredito;
this.numeroTelefono = numeroTelefono;
}
public void eliminarCuenta() {
this.tipoUsuario = null;
this.correo = null;
this.clave = null;
this.nombre = null;
this.direccion = null;
this.tarjetaCredito = null;
this.numeroTelefono = null;
}
}