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;
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;
25 import businessLogic.BLFacade;
26 import domain.RegularUser;
28 import javax.swing.ImageIcon;
29 import java.awt.SystemColor;
31 public class RegisterGUI extends JFrame {
32 private JLabel lblNombre;
33 private JLabel lblApellidos;
34 private JLabel lblFechaNacimiento;
35 private JLabel lblEmail;
36 private JLabel lblCuentaBancaria;
37 private JLabel lblTelfono;
38 private JLabel lblDireccin;
39 private JTextField textUser;
40 private JTextField textName;
41 private JTextField textLastName;
42 private JTextField textBirth;
43 private JTextField textEmail;
44 private JTextField textBank;
45 private JTextField textPhoneNumber;
46 private JTextField textAddress;
47 private JPasswordField textPass;
48 private static BLFacade facade = LoginGUI.getBusinessLogic();
49 private static User user;
50 JButton buttonRegistrarse = new JButton("Registrarse");
51 private JButton btnAtras;
52 private JLabel lblInfoNombre;
53 private JLabel lblInfoNombre_1;
54 private JLabel lblIcono2;
55 private JLabel lblInfoDir;
57 public RegisterGUI() {
58 getContentPane().setBackground(Color.WHITE);
61 } catch (Exception e) {
66 public static void setBusinessLogic(BLFacade pfacade) {
70 public static BLFacade getBusinessLogic() {
74 public void initialize() throws ParseException {
76 getContentPane().setLayout(null);
77 setBounds(100, 100, 763, 450);
78 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
80 JLabel lblUser = new JLabel("Usuario:");
81 lblUser.setBounds(5, 55, 100, 14);
82 lblUser.setFont(new Font("Tahoma", Font.PLAIN, 13));
83 lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
84 getContentPane().add(lblUser);
86 JLabel lblUser_1 = new JLabel("Contraseña:");
87 lblUser_1.setBounds(5, 84, 100, 14);
88 lblUser_1.setHorizontalAlignment(SwingConstants.RIGHT);
89 lblUser_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
90 getContentPane().add(lblUser_1);
92 lblNombre = new JLabel("Nombre:");
93 lblNombre.setBounds(285, 56, 100, 14);
94 lblNombre.setHorizontalAlignment(SwingConstants.RIGHT);
95 lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 13));
96 getContentPane().add(lblNombre);
98 lblApellidos = new JLabel("Apellidos:");
99 lblApellidos.setBounds(285, 96, 100, 14);
100 lblApellidos.setHorizontalAlignment(SwingConstants.RIGHT);
101 lblApellidos.setFont(new Font("Tahoma", Font.PLAIN, 13));
102 getContentPane().add(lblApellidos);
104 lblFechaNacimiento = new JLabel("Fecha nacimiento:");
105 lblFechaNacimiento.setBounds(268, 133, 117, 14);
106 lblFechaNacimiento.setHorizontalAlignment(SwingConstants.RIGHT);
107 lblFechaNacimiento.setFont(new Font("Tahoma", Font.PLAIN, 13));
108 getContentPane().add(lblFechaNacimiento);
110 lblEmail = new JLabel("Email:");
111 lblEmail.setBounds(285, 173, 100, 14);
112 lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);
113 lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 13));
114 getContentPane().add(lblEmail);
116 lblCuentaBancaria = new JLabel("Cuenta bancaria:");
117 lblCuentaBancaria.setBounds(285, 213, 100, 14);
118 lblCuentaBancaria.setHorizontalAlignment(SwingConstants.RIGHT);
119 lblCuentaBancaria.setFont(new Font("Tahoma", Font.PLAIN, 13));
120 getContentPane().add(lblCuentaBancaria);
122 lblTelfono = new JLabel("Teléfono:");
123 lblTelfono.setBounds(285, 252, 100, 14);
124 lblTelfono.setHorizontalAlignment(SwingConstants.RIGHT);
125 lblTelfono.setFont(new Font("Tahoma", Font.PLAIN, 13));
126 getContentPane().add(lblTelfono);
128 lblDireccin = new JLabel("Dirección:");
129 lblDireccin.setBounds(285, 292, 100, 14);
130 lblDireccin.setHorizontalAlignment(SwingConstants.RIGHT);
131 lblDireccin.setFont(new Font("Tahoma", Font.PLAIN, 13));
132 getContentPane().add(lblDireccin);
134 textUser = new JTextField();
135 textUser.setBounds(115, 51, 131, 20);
136 textUser.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
137 textUser.setColumns(10);
138 getContentPane().add(textUser);
140 textName = new JTextField();
141 textName.setBounds(395, 55, 131, 20);
142 textName.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
143 textName.setColumns(10);
144 getContentPane().add(textName);
146 textLastName = new JTextField();
147 textLastName.setBounds(395, 95, 131, 20);
148 textLastName.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
149 textLastName.setColumns(10);
150 getContentPane().add(textLastName);
152 textBirth = new JTextField();
153 textBirth.setBounds(395, 132, 131, 20);
154 textBirth.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
155 textBirth.setColumns(10);
156 getContentPane().add(textBirth);
158 textEmail = new JTextField();
159 textEmail.setBounds(395, 172, 159, 20);
160 textEmail.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
161 textEmail.setColumns(10);
162 getContentPane().add(textEmail);
164 textBank = new JTextField();
165 textBank.setBounds(395, 212, 153, 20);
166 textBank.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
167 textBank.setColumns(10);
168 getContentPane().add(textBank);
170 textPhoneNumber = new JTextField();
171 textPhoneNumber.setBounds(395, 251, 131, 20);
172 textPhoneNumber.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
173 textPhoneNumber.setColumns(10);
174 getContentPane().add(textPhoneNumber);
176 textAddress = new JTextField();
177 textAddress.setBounds(395, 291, 227, 20);
178 textAddress.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
179 textAddress.setColumns(10);
180 getContentPane().add(textAddress);
182 textPass = new JPasswordField();
183 textPass.setBounds(115, 82, 131, 19);
184 textPass.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
185 textPass.setColumns(10);
186 getContentPane().add(textPass);
187 buttonRegistrarse.setForeground(Color.WHITE);
188 buttonRegistrarse.setBackground(SystemColor.textHighlight);
189 buttonRegistrarse.setFont(new Font("Dialog", Font.BOLD, 14));
191 buttonRegistrarse.setBounds(440, 348, 140, 23);
192 getContentPane().add(buttonRegistrarse);
194 JLabel lblNewLabel = new JLabel("dd/mm/aaaa");
195 lblNewLabel.setForeground(Color.GRAY);
196 lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
197 lblNewLabel.setBounds(530, 132, 79, 16);
198 getContentPane().add(lblNewLabel);
200 JLabel lblEsxxXxxxXxxx = new JLabel("ESXX XXXX XXXX XXXX ");
201 lblEsxxXxxxXxxx.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
202 lblEsxxXxxxXxxx.setForeground(Color.GRAY);
203 lblEsxxXxxxXxxx.setBounds(553, 212, 146, 16);
204 getContentPane().add(lblEsxxXxxxXxxx);
206 JLabel lblXxxxxxxxx = new JLabel("(+34) XXXXXXXXX");
207 lblXxxxxxxxx.setForeground(Color.GRAY);
208 lblXxxxxxxxx.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
209 lblXxxxxxxxx.setBounds(530, 251, 146, 16);
210 getContentPane().add(lblXxxxxxxxx);
212 JLabel lblAlMenos = new JLabel("Al menos:");
213 lblAlMenos.setForeground(Color.GRAY);
214 lblAlMenos.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
216 lblAlMenos.setBounds(81, 113, 146, 16);
217 getContentPane().add(lblAlMenos);
219 JLabel lblCaracteres = new JLabel("8 caracteres");
220 lblCaracteres.setForeground(Color.GRAY);
221 lblCaracteres.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 10));
223 lblCaracteres.setBounds(101, 133, 146, 16);
224 getContentPane().add(lblCaracteres);
226 JLabel lblUnaMayscula = new JLabel("Una mayúscula");
227 lblUnaMayscula.setForeground(Color.GRAY);
228 lblUnaMayscula.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 10));
230 lblUnaMayscula.setBounds(101, 152, 146, 16);
231 getContentPane().add(lblUnaMayscula);
233 JLabel lblUnaMin = new JLabel("Una minúscula");
234 lblUnaMin.setForeground(Color.GRAY);
235 lblUnaMin.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 10));
237 // lblUnaMin.setFont(new Font("Dialog", Font.BOLD, 10));
238 lblUnaMin.setBounds(102, 171, 146, 16);
239 getContentPane().add(lblUnaMin);
241 JLabel lblUnNmero = new JLabel("Un número");
242 lblUnNmero.setForeground(Color.GRAY);
243 lblUnNmero.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 10));
245 // lblUnNmero.setFont(new Font("Dialog", Font.BOLD, 10));
246 lblUnNmero.setBounds(101, 190, 146, 16);
247 getContentPane().add(lblUnNmero);
249 JLabel lblUnCaracterEspecial = new JLabel("Un caracter especial");
250 lblUnCaracterEspecial.setForeground(Color.GRAY);
251 lblUnCaracterEspecial.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 10));
253 // lblUnCaracterEspecial.setFont(new Font("Dialog", Font.BOLD, 10));
254 lblUnCaracterEspecial.setBounds(101, 208, 146, 16);
255 getContentPane().add(lblUnCaracterEspecial);
257 JLabel lblSinEspaciosEn = new JLabel("Sin espacios en blanco");
258 lblSinEspaciosEn.setForeground(Color.GRAY);
259 lblSinEspaciosEn.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 10));
261 // lblSinEspaciosEn.setFont(new Font("Dialog", Font.BOLD, 10));
262 lblSinEspaciosEn.setBounds(101, 229, 146, 16);
263 getContentPane().add(lblSinEspaciosEn);
265 JLabel lblgmailcom = new JLabel("debe ser @gmail.com");
266 lblgmailcom.setForeground(Color.GRAY);
267 lblgmailcom.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
269 // lblgmailcom.setFont(new Font("Dialog", Font.BOLD, 12));
270 lblgmailcom.setBounds(559, 174, 146, 16);
271 getContentPane().add(lblgmailcom);
273 btnAtras = new JButton("Atrás");
274 btnAtras.setBackground(SystemColor.controlShadow);
275 btnAtras.setForeground(Color.WHITE);
276 btnAtras.setFont(new Font("Dialog", Font.BOLD, 14));
277 btnAtras.addActionListener(new ActionListener() {
279 public void actionPerformed(ActionEvent e) {
281 // Frame reg = new LoginGUI();
282 // reg.setAlwaysOnTop(true);
283 // reg.setVisible(true);
288 btnAtras.setBounds(54, 348, 79, 23);
289 getContentPane().add(btnAtras);
291 JLabel lblIcono = new JLabel(" ");
292 lblIcono.setIcon(new ImageIcon("C:\\Users\\Bryan\\Desktop\\Workspace\\ISBets21BRYMAUJONUNA\\info.png"));
293 lblIcono.setBounds(54, 106, 30, 30);
294 getContentPane().add(lblIcono);
296 lblInfoNombre = new JLabel("Sin números");
297 lblInfoNombre.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
298 lblInfoNombre.setForeground(Color.GRAY);
299 lblInfoNombre.setBounds(530, 55, 95, 16);
300 getContentPane().add(lblInfoNombre);
302 lblInfoNombre_1 = new JLabel("Sin números");
303 lblInfoNombre_1.setForeground(Color.GRAY);
304 lblInfoNombre_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
305 lblInfoNombre_1.setBounds(530, 95, 95, 16);
306 getContentPane().add(lblInfoNombre_1);
308 ImageIcon icon = new ImageIcon("imagenes/info.png");
309 lblIcono2 = new JLabel(icon);
310 lblIcono2.setBounds(640, 284, 30, 30);
311 getContentPane().add(lblIcono2);
314 lblInfoDir = new JLabel("Opcional");
315 lblInfoDir.setBounds(665, 291, 55, 16);
316 lblInfoDir.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
317 lblInfoDir.setForeground(Color.GRAY);
319 getContentPane().add(lblInfoDir);
320 buttonRegistrarse.addActionListener(new ActionListener() {
321 @SuppressWarnings("deprecation")
323 public void actionPerformed(ActionEvent e) {
326 boolean control = true;
327 if (textUser.getText().equals("")) {
328 JOptionPane.showMessageDialog(getContentPane(), "Introduce un nombre de usuario",
329 "Error", JOptionPane.ERROR_MESSAGE);
334 if (validoNombre(textName.getText()) == false) {
335 textName.setText("");
337 JOptionPane.showMessageDialog(getContentPane(), "Nombre con solo letras", "Error",
338 JOptionPane.ERROR_MESSAGE);
343 if (validoNombre(textLastName.getText()) == false) {
344 textLastName.setText("");
346 JOptionPane.showMessageDialog(getContentPane(), "Apellidos con solo letras", "Error",
347 JOptionPane.ERROR_MESSAGE);
352 if (validofecha(textBirth.getText()) == false) {
353 JOptionPane.showMessageDialog(getContentPane(), "Fecha incorrecta", "Error",
354 JOptionPane.ERROR_MESSAGE);
359 if (validoEmail(textEmail.getText()) == false) {
360 JOptionPane.showMessageDialog(getContentPane(), "Email incorrecto", "Error",
361 JOptionPane.ERROR_MESSAGE);
366 if (validoContraseña(textPass.getText()) == false) {
367 JOptionPane.showMessageDialog(getContentPane(), "Contraseña incorrecta", "Error",
368 JOptionPane.ERROR_MESSAGE);
372 if (textBank.getText().equals("")) {
376 else if (validoCuentaB(textBank.getText()) == false) {
377 JOptionPane.showMessageDialog(getContentPane(), "Cuenta Bancaria incorrecta", "Error",
378 JOptionPane.ERROR_MESSAGE);
382 if (validoNumero(textPhoneNumber.getText()) == false) {
383 JOptionPane.showMessageDialog(getContentPane(), "Numero de teléfono incorrecto", "Error",
384 JOptionPane.ERROR_MESSAGE);
390 facade.registrar(textUser.getText(), textPass.getText(), textName.getText(),
391 textLastName.getText(), textBirth.getText(), textEmail.getText(), textBank.getText(),
392 Integer.parseInt(textPhoneNumber.getText()), textAddress.getText(), 0);
393 JOptionPane.showMessageDialog(getContentPane(), "Registrado correctamente el usuario: "
394 + textUser.getText() + "\n\nSerá redirigido al apartado para loguearse...");
396 JOptionPane.showMessageDialog(getContentPane(),
397 "ENHORABUENA!!!!!\nTe regalamos un bono de bienvenida de 5€ para que apuestes");
398 facade.aplicarBonoBienvenida(textUser.getText());
402 } catch (exceptions.UserAlreadyExistException ex) {
404 JOptionPane.showMessageDialog(getContentPane(), ex.getMessage(), "Error",
405 JOptionPane.ERROR_MESSAGE);
408 catch (Exception o) {
419 /* *************************************************************** */
420 /* FUNCIONES AUXILIARES DE COMPROBACIÓN DE CAMPOS PARA EL REGISTRO */
421 /* *************************************************************** */
423 // se verifica que el nombre sea solo formado por letras o espacios en blanco,
424 // tambien usado para apellidos
425 private boolean validoNombre(String nombre) {
426 int longitud = nombre.toCharArray().length;
428 for (int i = 0; i < longitud; i++) {
429 if (!Character.isLetter(nombre.toCharArray()[i]) && !Character.isWhitespace(nombre.toCharArray()[i])) {
437 // se verifica que la fecha sea valida
438 private boolean validofecha(String pfecha) {
441 String[] parts = pfecha.split("\\/"); // se divide el string pfecha en partes separadas por un /
442 if (parts.length != 3) { // se verifica que se el array surgido tiene tres elementos XX/XX/XXXX, sino
447 int month; // mes [1,...,12]
448 int dayOfMonth; // día [1,...,31]
449 dayOfMonth = Integer.parseInt(parts[0]); // se parsea a int la primera parte XX/xx/xxxx, de string a int
450 month = Integer.parseInt(parts[1]); // se parsea a int la segunda parte xx/XX/xxxx, de string a int
451 year = Integer.parseInt(parts[2]); // se parsea a int la tercera parte xx/xx/XXXX, de string a int
452 if (year < 1900) { // se verifica que el año sea mayor a 1900
453 throw new IllegalArgumentException("Año inválido.");
456 Calendar calendar = Calendar.getInstance(); // se combierte la fecha en un Calendar, que automaticamente
457 calendar.setLenient(false); // si al intentarlo no puede, lanza excepcion como fecha
458 calendar.set(Calendar.YEAR, year); // no valida
459 calendar.set(Calendar.MONTH, month - 1); // [0,...,11] (la clase Calendar trata asi los meses 0-11)
460 calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
461 Date date = calendar.getTime(); // date es la fecha en formato Calendar XXXX/XX/XX
462 Date date2 = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()); // fecha
466 if (date.after(date2)) { // se verifica que la fecha introducida no es posterior a la actual
467 System.out.println("FECHA POSTERIOR A HOY");
468 throw new IllegalArgumentException("Año inválido.");
471 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // se transforma la fecha formato Calendar a
473 System.out.println(sdf.format(date)); // dd/MM/yyyy //es decir, de XXXX/XX/XX a XX/XX/XXXX
474 System.out.println("Fecha validada");
475 System.out.println("La fecha actual es: " + LocalDate.now());
479 } catch (Exception p) {
480 System.out.println("FECHA NO VALIDA");
486 private boolean validoEmail(String email) {
487 Pattern pattern = Pattern.compile("^[a-zA-Z0-9]+((\\_|\\.)[a-zA-Z0-9]+)?@gmail\\.com$");
488 Matcher mat = pattern.matcher(email);
490 System.out.println("Porfin metes un email correscto estupida");
493 System.out.println("Amiga que email de meirda es ese");
499 public static boolean validoContraseña(String contraseña) {
500 Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&?+=])(?=\\S+$).{8,}$");
501 Matcher mat = pattern.matcher(contraseña);
503 System.out.println("Porfin metes una contraseña correcta estupida");
506 System.out.println("Amiga que contraseña de mierda es esa");
512 // acepta cuentas bancarias del tipo: ES23 3434 2342 3423
513 private boolean validoCuentaB(String cuentab) {
514 Pattern pattern = Pattern.compile("^[A-Z]{2}[0-9]{2}\\s[0-9]{4}\\s[0-9]{4}\\s[0-9]{4}$");
515 Matcher mat = pattern.matcher(cuentab);
517 System.out.println("El formato de la cuenta bancaria cumple el formato");
519 System.out.println("El formato de la cuenta bancaria es incorrecto");
525 // +34(opcional) 123456789
526 private boolean validoNumero(String numero) {
527 Pattern pattern = Pattern.compile("^(\\+[0-9]+\\s)?[0-9]{9}$");
528 Matcher mat = pattern.matcher(numero);
530 System.out.println("El formato del numero de telefono cumple el formato");
532 System.out.println("El formato del numero de telofono es incorrecto");