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;
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;
50 public RegisterGUI() {
53 } catch (Exception e) {
58 public static void setBusinessLogic(BLFacade pfacade) {
62 public static BLFacade getBusinessLogic() {
66 public void initialize() throws ParseException {
68 getContentPane().setLayout(null);
69 setBounds(100, 100, 713, 450);
70 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
180 buttonRegistrarse.setBounds(256, 348, 140, 23);
181 getContentPane().add(buttonRegistrarse);
183 JLabel lblNewLabel = new JLabel("dd/mm/aaaa");
184 lblNewLabel.setForeground(Color.GRAY);
185 lblNewLabel.setBounds(530, 132, 79, 16);
186 getContentPane().add(lblNewLabel);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
248 btnAtras = new JButton("Atrás");
249 btnAtras.addActionListener(new ActionListener() {
251 public void actionPerformed(ActionEvent e) {
253 Frame reg = new LoginGUI();
254 reg.setAlwaysOnTop(true);
255 reg.setVisible(true);
260 btnAtras.setBounds(54, 348, 79, 23);
261 getContentPane().add(btnAtras);
262 buttonRegistrarse.addActionListener(new ActionListener() {
263 @SuppressWarnings("deprecation")
265 public void actionPerformed(ActionEvent e) {
268 boolean control = true;
269 if (textUser.getText().equals("")) {
270 JOptionPane.showMessageDialog(getContentPane(), "Introduzca un nombre de usuario", "Error",
271 JOptionPane.ERROR_MESSAGE);
276 if (validoNombre(textName.getText()) == false) {
277 textName.setText("");
279 JOptionPane.showMessageDialog(getContentPane(), "Nombre con solo letras", "Error",
280 JOptionPane.ERROR_MESSAGE);
285 if (validoNombre(textLastName.getText()) == false) {
286 textLastName.setText("");
288 JOptionPane.showMessageDialog(getContentPane(), "Apellidos con solo letras", "Error",
289 JOptionPane.ERROR_MESSAGE);
294 if (validofecha(textBirth.getText()) == false) {
295 JOptionPane.showMessageDialog(getContentPane(), "Fecha incorrecta", "Error",
296 JOptionPane.ERROR_MESSAGE);
301 if (validoEmail(textEmail.getText()) == false) {
302 JOptionPane.showMessageDialog(getContentPane(), "Email incorrecto", "Error",
303 JOptionPane.ERROR_MESSAGE);
308 if (validoContraseña(textPass.getText()) == false) {
309 JOptionPane.showMessageDialog(getContentPane(), "Contraseña incorrecta", "Error",
310 JOptionPane.ERROR_MESSAGE);
314 if (validoCuentaB(textBank.getText()) == false) {
315 JOptionPane.showMessageDialog(getContentPane(), "Cuena Bancaria incorrecta", "Error",
316 JOptionPane.ERROR_MESSAGE);
320 if (validoNumero(textPhoneNumber.getText()) == false) {
321 JOptionPane.showMessageDialog(getContentPane(), "Numero de teléfono incorrecto", "Error",
322 JOptionPane.ERROR_MESSAGE);
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...");
334 Frame reg2 = new LoginGUI();
335 reg2.setAlwaysOnTop(true);
336 reg2.setVisible(true);
340 } catch (exceptions.UserAlreadyExistException ex) {
342 JOptionPane.showMessageDialog(getContentPane(), ex.getMessage(), "Error",
343 JOptionPane.ERROR_MESSAGE);
346 catch (Exception o) {
357 /* *************************************************************** */
358 /* FUNCIONES AUXILIARES DE COMPROBACIÓN DE CAMPOS PARA EL REGISTRO */
359 /* *************************************************************** */
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;
366 for (int i = 0; i < longitud; i++) {
367 if (!Character.isLetter(nombre.toCharArray()[i]) && !Character.isWhitespace(nombre.toCharArray()[i])) {
375 // se verifica que la fecha sea valida
376 private boolean validofecha(String pfecha) {
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
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.");
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
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.");
409 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // se transforma la fecha formato Calendar a
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());
417 } catch (Exception p) {
418 System.out.println("FECHA NO VALIDA");
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);
428 System.out.println("El email introducido cumple el formato");
431 System.out.println("El email introducido no cumple el formato");
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);
441 System.out.println("Porfin metes una contraseña correcta estupida");
444 System.out.println("Amiga que contraseña de mierda es esa");
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);
455 System.out.println("El formato de la cuenta bancaria cumple el formato");
457 System.out.println("El formato de la cuenta bancaria es incorrecto");
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);
468 System.out.println("El formato del numero de telefono cumple el formato");
470 System.out.println("El formato del numero de telofono es incorrecto");