Versión del proyecto de Apuestas tras la iteración 1
[ISBets21MAUBRY] / eclipse-workspace / ISBets21BRYMAUJONUNA / src / main / java / gui / RegisterGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Frame;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.time.LocalDate;
11 import java.time.ZoneId;
12 import java.util.Calendar;
13 import java.util.Date;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import javax.swing.JButton;
18 import javax.swing.JFrame;
19 import javax.swing.JLabel;
20 import javax.swing.JOptionPane;
21 import javax.swing.JPasswordField;
22 import javax.swing.JTextField;
23 import javax.swing.SwingConstants;
24
25 import businessLogic.BLFacade;
26 import domain.User;
27
28 public class RegisterGUI extends JFrame {
29         private JLabel lblNombre;
30         private JLabel lblApellidos;
31         private JLabel lblFechaNacimiento;
32         private JLabel lblEmail;
33         private JLabel lblCuentaBancaria;
34         private JLabel lblTelfono;
35         private JLabel lblDireccin;
36         private JTextField textUser;
37         private JTextField textName;
38         private JTextField textLastName;
39         private JTextField textBirth;
40         private JTextField textEmail;
41         private JTextField textBank;
42         private JTextField textPhoneNumber;
43         private JTextField textAddress;
44         private JPasswordField textPass;
45         private static BLFacade facade = LoginGUI.getBusinessLogic();
46         private static User user;
47         JButton buttonRegistrarse = new JButton("Registrarse");
48         private JButton btnAtras;
49
50         public RegisterGUI() {
51                 try {
52                         initialize();
53                 } catch (Exception e) {
54                         e.printStackTrace();
55                 }
56         }
57
58         public static void setBusinessLogic(BLFacade pfacade) {
59                 facade = pfacade;
60         }
61
62         public static BLFacade getBusinessLogic() {
63                 return facade;
64         }
65
66         public void initialize() throws ParseException {
67                 setTitle("Registro");
68                 getContentPane().setLayout(null);
69                 setBounds(100, 100, 713, 450);
70                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71
72                 JLabel lblUser = new JLabel("Usuario:");
73                 lblUser.setBounds(5, 55, 100, 14);
74                 lblUser.setFont(new Font("Tahoma", Font.PLAIN, 13));
75                 lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
76                 getContentPane().add(lblUser);
77
78                 JLabel lblUser_1 = new JLabel("Contraseña:");
79                 lblUser_1.setBounds(5, 84, 100, 14);
80                 lblUser_1.setHorizontalAlignment(SwingConstants.RIGHT);
81                 lblUser_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
82                 getContentPane().add(lblUser_1);
83
84                 lblNombre = new JLabel("Nombre:");
85                 lblNombre.setBounds(285, 56, 100, 14);
86                 lblNombre.setHorizontalAlignment(SwingConstants.RIGHT);
87                 lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 13));
88                 getContentPane().add(lblNombre);
89
90                 lblApellidos = new JLabel("Apellidos:");
91                 lblApellidos.setBounds(285, 96, 100, 14);
92                 lblApellidos.setHorizontalAlignment(SwingConstants.RIGHT);
93                 lblApellidos.setFont(new Font("Tahoma", Font.PLAIN, 13));
94                 getContentPane().add(lblApellidos);
95
96                 lblFechaNacimiento = new JLabel("Fecha nacimiento:");
97                 lblFechaNacimiento.setBounds(268, 133, 117, 14);
98                 lblFechaNacimiento.setHorizontalAlignment(SwingConstants.RIGHT);
99                 lblFechaNacimiento.setFont(new Font("Tahoma", Font.PLAIN, 13));
100                 getContentPane().add(lblFechaNacimiento);
101
102                 lblEmail = new JLabel("Email:");
103                 lblEmail.setBounds(285, 173, 100, 14);
104                 lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);
105                 lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 13));
106                 getContentPane().add(lblEmail);
107
108                 lblCuentaBancaria = new JLabel("Cuenta bancaria:");
109                 lblCuentaBancaria.setBounds(285, 213, 100, 14);
110                 lblCuentaBancaria.setHorizontalAlignment(SwingConstants.RIGHT);
111                 lblCuentaBancaria.setFont(new Font("Tahoma", Font.PLAIN, 13));
112                 getContentPane().add(lblCuentaBancaria);
113
114                 lblTelfono = new JLabel("Teléfono:");
115                 lblTelfono.setBounds(285, 252, 100, 14);
116                 lblTelfono.setHorizontalAlignment(SwingConstants.RIGHT);
117                 lblTelfono.setFont(new Font("Tahoma", Font.PLAIN, 13));
118                 getContentPane().add(lblTelfono);
119
120                 lblDireccin = new JLabel("Dirección:");
121                 lblDireccin.setBounds(285, 292, 100, 14);
122                 lblDireccin.setHorizontalAlignment(SwingConstants.RIGHT);
123                 lblDireccin.setFont(new Font("Tahoma", Font.PLAIN, 13));
124                 getContentPane().add(lblDireccin);
125
126                 textUser = new JTextField();
127                 textUser.setBounds(115, 51, 131, 20);
128                 textUser.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
129                 textUser.setColumns(10);
130                 getContentPane().add(textUser);
131
132                 textName = new JTextField();
133                 textName.setBounds(395, 55, 131, 20);
134                 textName.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
135                 textName.setColumns(10);
136                 getContentPane().add(textName);
137
138                 textLastName = new JTextField();
139                 textLastName.setBounds(395, 95, 131, 20);
140                 textLastName.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
141                 textLastName.setColumns(10);
142                 getContentPane().add(textLastName);
143
144                 textBirth = new JTextField();
145                 textBirth.setBounds(395, 132, 131, 20);
146                 textBirth.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
147                 textBirth.setColumns(10);
148                 getContentPane().add(textBirth);
149
150                 textEmail = new JTextField();
151                 textEmail.setBounds(395, 172, 159, 20);
152                 textEmail.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
153                 textEmail.setColumns(10);
154                 getContentPane().add(textEmail);
155
156                 textBank = new JTextField();
157                 textBank.setBounds(395, 212, 153, 20);
158                 textBank.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
159                 textBank.setColumns(10);
160                 getContentPane().add(textBank);
161
162                 textPhoneNumber = new JTextField();
163                 textPhoneNumber.setBounds(395, 251, 131, 20);
164                 textPhoneNumber.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
165                 textPhoneNumber.setColumns(10);
166                 getContentPane().add(textPhoneNumber);
167
168                 textAddress = new JTextField();
169                 textAddress.setBounds(395, 291, 227, 20);
170                 textAddress.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
171                 textAddress.setColumns(10);
172                 getContentPane().add(textAddress);
173
174                 textPass = new JPasswordField();
175                 textPass.setBounds(115, 82, 131, 19);
176                 textPass.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
177                 textPass.setColumns(10);
178                 getContentPane().add(textPass);
179
180                 buttonRegistrarse.setBounds(256, 348, 140, 23);
181                 getContentPane().add(buttonRegistrarse);
182
183                 JLabel lblNewLabel = new JLabel("dd/mm/aaaa");
184                 lblNewLabel.setForeground(Color.GRAY);
185                 lblNewLabel.setBounds(530, 132, 79, 16);
186                 getContentPane().add(lblNewLabel);
187
188                 JLabel lblEsxxXxxxXxxx = new JLabel("ESXX XXXX XXXX XXXX ");
189                 lblEsxxXxxxXxxx.setFont(new Font("Dialog", Font.BOLD, 12));
190                 lblEsxxXxxxXxxx.setForeground(Color.GRAY);
191                 lblEsxxXxxxXxxx.setBounds(553, 212, 146, 16);
192                 getContentPane().add(lblEsxxXxxxXxxx);
193
194                 JLabel lblXxxxxxxxx = new JLabel("(+34) XXXXXXXXX");
195                 lblXxxxxxxxx.setForeground(Color.GRAY);
196                 lblXxxxxxxxx.setFont(new Font("Dialog", Font.BOLD, 12));
197                 lblXxxxxxxxx.setBounds(530, 251, 146, 16);
198                 getContentPane().add(lblXxxxxxxxx);
199
200                 JLabel lblAlMenos = new JLabel("Al menos:");
201                 lblAlMenos.setForeground(Color.GRAY);
202                 lblAlMenos.setFont(new Font("Dialog", Font.BOLD, 12));
203                 lblAlMenos.setBounds(125, 113, 146, 16);
204                 getContentPane().add(lblAlMenos);
205
206                 JLabel lblCaracteres = new JLabel("8 caracteres");
207                 lblCaracteres.setForeground(Color.GRAY);
208                 lblCaracteres.setFont(new Font("Dialog", Font.BOLD, 10));
209                 lblCaracteres.setBounds(145, 133, 146, 16);
210                 getContentPane().add(lblCaracteres);
211
212                 JLabel lblUnaMayscula = new JLabel("Una mayúscula");
213                 lblUnaMayscula.setForeground(Color.GRAY);
214                 lblUnaMayscula.setFont(new Font("Dialog", Font.BOLD, 10));
215                 lblUnaMayscula.setBounds(145, 152, 146, 16);
216                 getContentPane().add(lblUnaMayscula);
217
218                 JLabel lblUnaMin = new JLabel("Una minúscula");
219                 lblUnaMin.setForeground(Color.GRAY);
220                 lblUnaMin.setFont(new Font("Dialog", Font.BOLD, 10));
221                 lblUnaMin.setBounds(146, 171, 146, 16);
222                 getContentPane().add(lblUnaMin);
223
224                 JLabel lblUnNmero = new JLabel("Un número");
225                 lblUnNmero.setForeground(Color.GRAY);
226                 lblUnNmero.setFont(new Font("Dialog", Font.BOLD, 10));
227                 lblUnNmero.setBounds(145, 190, 146, 16);
228                 getContentPane().add(lblUnNmero);
229
230                 JLabel lblUnCaracterEspecial = new JLabel("Un caracter especial");
231                 lblUnCaracterEspecial.setForeground(Color.GRAY);
232                 lblUnCaracterEspecial.setFont(new Font("Dialog", Font.BOLD, 10));
233                 lblUnCaracterEspecial.setBounds(145, 208, 146, 16);
234                 getContentPane().add(lblUnCaracterEspecial);
235
236                 JLabel lblSinEspaciosEn = new JLabel("Sin espacios en blanco");
237                 lblSinEspaciosEn.setForeground(Color.GRAY);
238                 lblSinEspaciosEn.setFont(new Font("Dialog", Font.BOLD, 10));
239                 lblSinEspaciosEn.setBounds(145, 229, 146, 16);
240                 getContentPane().add(lblSinEspaciosEn);
241
242                 JLabel lblgmailcom = new JLabel("@gmail.com");
243                 lblgmailcom.setForeground(Color.GRAY);
244                 lblgmailcom.setFont(new Font("Dialog", Font.BOLD, 12));
245                 lblgmailcom.setBounds(566, 172, 146, 16);
246                 getContentPane().add(lblgmailcom);
247
248                 btnAtras = new JButton("Atrás");
249                 btnAtras.addActionListener(new ActionListener() {
250                         @Override
251                         public void actionPerformed(ActionEvent e) {
252
253                                 Frame reg = new LoginGUI();
254                                 reg.setAlwaysOnTop(true);
255                                 reg.setVisible(true);
256                                 dispose();
257
258                         }
259                 });
260                 btnAtras.setBounds(54, 348, 79, 23);
261                 getContentPane().add(btnAtras);
262                 buttonRegistrarse.addActionListener(new ActionListener() {
263                         @SuppressWarnings("deprecation")
264                         @Override
265                         public void actionPerformed(ActionEvent e) {
266
267                                 try {
268                                         boolean control = true;
269                                         if (textUser.getText().equals("")) {
270                                                 JOptionPane.showMessageDialog(getContentPane(), "Introduzca un nombre de usuario", "Error",
271                                                                 JOptionPane.ERROR_MESSAGE);
272                                                 control = false;
273
274                                         }
275
276                                         if (validoNombre(textName.getText()) == false) {
277                                                 textName.setText("");
278
279                                                 JOptionPane.showMessageDialog(getContentPane(), "Nombre con solo letras", "Error",
280                                                                 JOptionPane.ERROR_MESSAGE);
281                                                 control = false;
282
283                                         }
284
285                                         if (validoNombre(textLastName.getText()) == false) {
286                                                 textLastName.setText("");
287
288                                                 JOptionPane.showMessageDialog(getContentPane(), "Apellidos con solo letras", "Error",
289                                                                 JOptionPane.ERROR_MESSAGE);
290                                                 control = false;
291
292                                         }
293
294                                         if (validofecha(textBirth.getText()) == false) {
295                                                 JOptionPane.showMessageDialog(getContentPane(), "Fecha incorrecta", "Error",
296                                                                 JOptionPane.ERROR_MESSAGE);
297                                                 control = false;
298
299                                         }
300
301                                         if (validoEmail(textEmail.getText()) == false) {
302                                                 JOptionPane.showMessageDialog(getContentPane(), "Email incorrecto", "Error",
303                                                                 JOptionPane.ERROR_MESSAGE);
304                                                 control = false;
305
306                                         }
307
308                                         if (validoContraseña(textPass.getText()) == false) {
309                                                 JOptionPane.showMessageDialog(getContentPane(), "Contraseña incorrecta", "Error",
310                                                                 JOptionPane.ERROR_MESSAGE);
311                                                 control = false;
312
313                                         }
314                                         if (validoCuentaB(textBank.getText()) == false) {
315                                                 JOptionPane.showMessageDialog(getContentPane(), "Cuena Bancaria incorrecta", "Error",
316                                                                 JOptionPane.ERROR_MESSAGE);
317                                                 control = false;
318
319                                         }
320                                         if (validoNumero(textPhoneNumber.getText()) == false) {
321                                                 JOptionPane.showMessageDialog(getContentPane(), "Numero de teléfono incorrecto", "Error",
322                                                                 JOptionPane.ERROR_MESSAGE);
323                                                 control = false;
324
325                                         }
326
327                                         if (control) {
328                                                 facade.registrar(textUser.getText(), textPass.getText(), textName.getText(),
329                                                                 textLastName.getText(), textBirth.getText(), textEmail.getText(), textBank.getText(),
330                                                                 Integer.parseInt(textPhoneNumber.getText()), textAddress.getText());
331                                                 JOptionPane.showMessageDialog(getContentPane(), "Registrado correctamente " + textUser.getText()
332                                                                 + "\nSerá redirigido al apartado para loguearse...");
333
334                                                 Frame reg2 = new LoginGUI();
335                                                 reg2.setAlwaysOnTop(true);
336                                                 reg2.setVisible(true);
337                                                 dispose();
338                                         }
339
340                                 } catch (exceptions.UserAlreadyExistException ex) {
341
342                                         JOptionPane.showMessageDialog(getContentPane(), ex.getMessage(), "Error",
343                                                         JOptionPane.ERROR_MESSAGE);
344                                 }
345
346                                 catch (Exception o) {
347                                         o.printStackTrace();
348
349                                 }
350
351                         }
352
353                 });
354
355         }
356
357         /* *************************************************************** */
358         /* FUNCIONES AUXILIARES DE COMPROBACIÓN DE CAMPOS PARA EL REGISTRO */
359         /* *************************************************************** */
360
361         // se verifica que el nombre sea solo formado por letras o espacios en blanco,
362         // tambien usado para apellidos
363         private boolean validoNombre(String nombre) {
364                 int longitud = nombre.toCharArray().length;
365
366                 for (int i = 0; i < longitud; i++) {
367                         if (!Character.isLetter(nombre.toCharArray()[i]) && !Character.isWhitespace(nombre.toCharArray()[i])) {
368                                 return false;
369                         }
370                 }
371
372                 return true;
373         }
374
375         // se verifica que la fecha sea valida
376         private boolean validofecha(String pfecha) {
377                 try {
378
379                         String[] parts = pfecha.split("\\/"); // se divide el string pfecha en partes separadas por un /
380                         if (parts.length != 3) { // se verifica que se el array surgido tiene tres elementos XX/XX/XXXX, sino
381                                                                                 // devuelve false
382                                 return false;
383                         } else {
384                                 int year; // año
385                                 int month; // mes [1,...,12]
386                                 int dayOfMonth; // día [1,...,31]
387                                 dayOfMonth = Integer.parseInt(parts[0]); // se parsea a int la primera parte XX/xx/xxxx, de string a int
388                                 month = Integer.parseInt(parts[1]); // se parsea a int la segunda parte xx/XX/xxxx, de string a int
389                                 year = Integer.parseInt(parts[2]); // se parsea a int la tercera parte xx/xx/XXXX, de string a int
390                                 if (year < 1900) { // se verifica que el año sea mayor a 1900
391                                         throw new IllegalArgumentException("Año inválido.");
392                                 }
393
394                                 Calendar calendar = Calendar.getInstance(); // se combierte la fecha en un Calendar, que automaticamente
395                                 calendar.setLenient(false); // si al intentarlo no puede, lanza excepcion como fecha
396                                 calendar.set(Calendar.YEAR, year); // no valida
397                                 calendar.set(Calendar.MONTH, month - 1); // [0,...,11] (la clase Calendar trata asi los meses 0-11)
398                                 calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
399                                 Date date = calendar.getTime(); // date es la fecha en formato Calendar XXXX/XX/XX
400                                 Date date2 = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()); // fecha
401                                                                                                                                                                                                                         // actual en
402                                                                                                                                                                                                                         // formato
403                                                                                                                                                                                                                         // Date
404                                 if (date.after(date2)) { // se verifica que la fecha introducida no es posterior a la actual
405                                         System.out.println("FECHA POSTERIOR A HOY");
406                                         throw new IllegalArgumentException("Año inválido.");
407
408                                 }
409                                 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // se transforma la fecha formato Calendar a
410                                                                                                                                                         // formato Date
411                                 System.out.println(sdf.format(date)); // dd/MM/yyyy //es decir, de XXXX/XX/XX a XX/XX/XXXX
412                                 System.out.println("Fecha validada");
413                                 System.out.println("La fecha actual es: " + LocalDate.now());
414                                 return true;
415                         }
416
417                 } catch (Exception p) {
418                         System.out.println("FECHA NO VALIDA");
419                         return false;
420                 }
421
422         }
423
424         private boolean validoEmail(String email) {
425                 Pattern pattern = Pattern.compile("^[a-zA-Z0-9]+((\\_|\\.)[a-zA-Z0-9]+)?@gmail\\.com$");
426                 Matcher mat = pattern.matcher(email);
427                 if (mat.matches()) {
428                         System.out.println("El email introducido cumple el formato");
429
430                 } else {
431                         System.out.println("El email introducido no cumple el formato");
432                         return false;
433                 }
434                 return true;
435         }
436
437         private boolean validoContraseña(String contraseña) {
438                 Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&?+=])(?=\\S+$).{8,}$");
439                 Matcher mat = pattern.matcher(contraseña);
440                 if (mat.matches()) {
441                         System.out.println("Porfin metes una contraseña correcta estupida");
442
443                 } else {
444                         System.out.println("Amiga que contraseña de mierda es esa");
445                         return false;
446                 }
447                 return true;
448         }
449
450         // acepta cuentas bancarias del tipo: ES23 3434 2342 3423
451         private boolean validoCuentaB(String cuentab) {
452                 Pattern pattern = Pattern.compile("^[A-Z]{2}[0-9]{2}\\s[0-9]{4}\\s[0-9]{4}\\s[0-9]{4}$");
453                 Matcher mat = pattern.matcher(cuentab);
454                 if (mat.matches()) {
455                         System.out.println("El formato de la cuenta bancaria cumple el formato");
456                 } else {
457                         System.out.println("El formato de la cuenta bancaria es incorrecto");
458                         return false;
459                 }
460                 return true;
461         }
462
463         // +34(opcional) 123456789
464         private boolean validoNumero(String numero) {
465                 Pattern pattern = Pattern.compile("^(\\+[0-9]+\\s)?[0-9]{9}$");
466                 Matcher mat = pattern.matcher(numero);
467                 if (mat.matches()) {
468                         System.out.println("El formato del numero de telefono cumple el formato");
469                 } else {
470                         System.out.println("El formato del numero de telofono es incorrecto");
471                         return false;
472                 }
473                 return true;
474         }
475 }