· 9 years ago · Oct 05, 2016, 04:42 AM
1package escuela;
2import javax.swing.Box;
3import javax.swing.JLabel;
4import javax.swing.JOptionPane;
5import javax.swing.JPanel;
6import javax.swing.*;
7
8import org.apache.commons.lang3.StringUtils;
9
10public class Estudiante {
11
12public static void main(String[] args) {
13RegistroEstudiante estudiante = new RegistroEstudiante();
14
15 estudiante.inicio();
16}
17
18
19}
20
21package escuela;
22import javax.swing.JOptionPane;
23import org.apache.commons.lang3.StringUtils;
24
25public class RegistroEstudiante{
26
27private int matricula = 0; //Sólo 6 dÃgitos
28private byte edad = 0; //No menor de 0
29private float altura = 0.0F;
30private String nombre = null, menu = null, ape_paterno = null, ape_materno = null, correo = null, genero = null, estadoCivil;
31private int length = 0;
32private boolean error = false;
33
34public void inicio() {
35 String[] menuPrincipal = { "Ingresar", "Mostrar", "Salir" };
36 String[] genero = { "Masculino", "Femenino" };
37 String[] estadoCivilMenu = { "Soltero/a", "Comprometido/a", "Casado/a", "Divorciado/a", "Viudo/a" };
38 Correos Correos = new Correos();
39 do {
40 try {
41 setMenu((String)JOptionPane.showInputDialog(null, "¿Qué deseas hacer?", "UNACAR", JOptionPane.QUESTION_MESSAGE, null, menuPrincipal, menuPrincipal[0]));
42
43 switch(getMenu()) {
44 case "Ingresar":
45 do{
46 setError(false);
47 try {
48 setMatricula(Integer.parseInt((String)JOptionPane.showInputDialog(null, "Introduce MatrÃcula [6]", "Agregando estudiante", JOptionPane.QUESTION_MESSAGE, null, null, null)));
49 } catch (java.lang.NumberFormatException e) {
50 JOptionPane.showMessageDialog(null, "Introdujiste un caractér inválido", "Error", JOptionPane.ERROR_MESSAGE, null);
51 setError(true);
52 }
53 length = (int)(Math.log10(getMatricula())+1); // Saca cantidad de caracteres
54 if ( (length != 6) && (error == false) )
55 {
56 JOptionPane.showMessageDialog(null, "La MatrÃcula DEBE ser de 6 dÃgitos", "Error", JOptionPane.ERROR_MESSAGE, null);
57 setMatricula(0);
58 }
59 } while (length != 6);
60
61 //SET NOMBRE
62 do{
63 setError(false);
64 setNombre((String)JOptionPane.showInputDialog(null, "Introduce nombre del alumno", "Agregando estudiante", JOptionPane.QUESTION_MESSAGE, null, null, null));
65 if(StringUtils.isAlpha(getNombre()) == false)
66 {
67 JOptionPane.showMessageDialog(null, "El nombre debe contener únicamente letras [A-Za-z]", "Error", JOptionPane.ERROR_MESSAGE, null);
68 setNombre(null);
69 setError(true);
70 }
71
72 }while(isError() == true);
73
74 //SET APELLIDO PATERNO
75 do{
76 setError(false);
77 setApe_paterno((String)JOptionPane.showInputDialog(null, "Introduce apellido paterno de " + getNombre(), "Agregando estudiante", JOptionPane.QUESTION_MESSAGE, null, null, null));
78 if(StringUtils.isAlpha(getApe_paterno()) == false)
79 {
80 JOptionPane.showMessageDialog(null, "El apellido debe contener únicamente letras [A-Za-z]", "Error", JOptionPane.ERROR_MESSAGE, null);
81 setApe_paterno(null);
82 setError(true);
83 }
84
85 }while(isError() == true);
86
87 //SET APellido MATERNO FTW
88
89 do{
90 setError(false);
91 setApe_materno((String)JOptionPane.showInputDialog(null, "Introduce apellido materno de " + getNombre(), "Agregando estudiante", JOptionPane.QUESTION_MESSAGE, null, null, null));
92 if(StringUtils.isAlpha(getApe_materno()) == false)
93 {
94 JOptionPane.showMessageDialog(null, "El nombre debe contener únicamente letras [A-Za-z]", "Error", JOptionPane.ERROR_MESSAGE, null);
95 setApe_materno(null);
96 setError(true);
97 }
98
99 }while(isError() == true);
100
101 // SELECCIONA SEXO
102
103 if(JOptionPane.showOptionDialog(null, "Selecciona género de " + getNombre(), "Agregando estudiante", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, genero, genero[0]) == 0 ){
104 setGenero("Masculino");
105 }else{
106 setGenero("Femenino");
107 }
108
109 // SET ESTADO CIVIL COMPA
110
111 setEstadoCivil((String)JOptionPane.showInputDialog(null, "Estado Civil de " + getNombre(), "UNACAR", JOptionPane.QUESTION_MESSAGE, null, estadoCivilMenu, estadoCivilMenu[0]));
112
113 // SET EDAD
114 do{
115 setError(false);
116 try {
117 setEdad(Byte.parseByte((String)JOptionPane.showInputDialog(null, "Introduce la edad del alumno", "Agregando estudiante", JOptionPane.QUESTION_MESSAGE, null, null, null)));
118 } catch (java.lang.NumberFormatException e) {
119 JOptionPane.showMessageDialog(null, "Aquà no se aceptan números...", "Error", JOptionPane.ERROR_MESSAGE, null);
120 setError(true);
121 }
122 if(getEdad() < 0)
123 {
124 JOptionPane.showMessageDialog(null, getNombre() + " no puede tener 0 años o menos, corrige... ", "Error", JOptionPane.ERROR_MESSAGE, null);
125 setEdad((byte) 0);
126 setError(true);
127 }
128
129 }while(isError() == true);
130
131
132 //SET ALTURA
133
134 do{
135 setError(false);
136 try {
137 setAltura(Float.parseFloat((String)JOptionPane.showInputDialog(null, "Introduce la altura del alumno", "Agregando estudiante", JOptionPane.QUESTION_MESSAGE, null, null, null)));
138 } catch (java.lang.NumberFormatException e) {
139 JOptionPane.showMessageDialog(null, "Aquà no se aceptan letras...", "Error", JOptionPane.ERROR_MESSAGE, null);
140 setError(true);
141 }
142
143 if(getAltura() < 0)
144 {
145 JOptionPane.showMessageDialog(null, getNombre() + " no puede medir 0 centimetros o menos, corrige...", "Error", JOptionPane.ERROR_MESSAGE, null);
146 setAltura(0.0F);
147 setError(true);
148 }
149
150 }while(isError() == true);
151
152 // SetMail
153 escuela.Correos.main();
154
155 do {
156 try {
157 // setCorreo(correos.getCorreoConcatenado());
158 System.out.println("En RegistroEstudiante.java " + correo);
159 Thread.sleep(1000);
160
161 } catch (InterruptedException e) {
162 // TODO Auto-generated catch block
163 e.printStackTrace();
164 }
165 } while (correo == null);
166 break;
167
168 case "Mostrar":
169 break;
170
171
172 } // END SWITCH
173 }// END TRY
174 catch (java.lang.NullPointerException e) {
175 switch(JOptionPane.showConfirmDialog(null, "¿Seguro que deseas salir?", "Salir o no salir?.. He ahà la cuestión...", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null)) {
176 case 0:
177 return;
178
179 case 1:
180 break;
181 }
182 }
183 } while (getMenu() != "Salir");
184
185}
186
187public boolean sonLetras(String name) {
188 return name.matches("[a-zA-Z]+");
189}
190
191public int getMatricula() {
192 return matricula;
193}
194
195public void setMatricula(int matricula) {
196 this.matricula = matricula;
197}
198
199public byte getEdad() {
200 return edad;
201}
202
203public void setEdad(byte edad) {
204 if(edad < 0)
205 JOptionPane.showMessageDialog(null, "No se aceptan valores menores a 0", "Error", JOptionPane.WARNING_MESSAGE, null);
206 this.edad = edad;
207}
208
209public float getAltura() {
210 return altura;
211}
212
213public void setAltura(float altura) {
214 this.altura = altura;
215}
216
217public String getNombre() {
218 return nombre;
219}
220
221public void setNombre(String nombre) {
222 this.nombre = nombre;
223}
224
225public String getApe_paterno() {
226 return ape_paterno;
227}
228
229public void setApe_paterno(String ape_paterno) {
230 this.ape_paterno = ape_paterno;
231}
232
233public String getApe_materno() {
234 return ape_materno;
235}
236
237public void setApe_materno(String ape_materno) {
238 this.ape_materno = ape_materno;
239}
240
241public String getCorreo() {
242 return correo;
243}
244
245public void setCorreo(String correo) {
246 this.correo = correo;
247}
248
249
250
251public int getLength() {
252 return length;
253}
254
255
256
257public void setLength(int length) {
258 this.length = length;
259}
260
261
262
263public boolean isError() {
264 return error;
265}
266
267
268
269public void setError(boolean error) {
270 this.error = error;
271}
272
273
274public String getGenero() {
275 return genero;
276}
277
278
279public void setGenero(String genero) {
280 this.genero = genero;
281}
282
283
284public String getEstadoCivil() {
285 return estadoCivil;
286}
287
288
289public void setEstadoCivil(String estadoCivil) {
290 this.estadoCivil = estadoCivil;
291}
292
293public String getMenu() {
294 return menu;
295}
296
297public void setMenu(String menu) {
298 this.menu = menu;
299}
300
301}
302
303package escuela;
304
305import java.awt.Component;
306import java.awt.EventQueue;
307import java.awt.Font;
308import java.awt.event.ActionEvent;
309import java.awt.event.ActionListener;
310import java.awt.event.MouseAdapter;
311import java.awt.event.MouseEvent;
312
313import javax.swing.DefaultComboBoxModel;
314import javax.swing.JButton;
315import javax.swing.JComboBox;
316import javax.swing.JFrame;
317import javax.swing.JLabel;
318import javax.swing.JPopupMenu;
319import javax.swing.JTextField;
320
321import org.eclipse.wb.swing.FocusTraversalOnArray;
322import java.awt.Window.Type;
323
324public class Correos {
325
326private JFrame frmAgregandoEstudiante;
327private JTextField txtUsuario;
328private String correoConcatenado = null;
329
330/**
331 * Launch the application.
332 */
333public static void main() {
334 EventQueue.invokeLater(new Runnable() {
335 public void run() {
336 try {
337 Correos window = new Correos();
338 window.frmAgregandoEstudiante.setVisible(true);
339 } catch (Exception e) {
340 e.printStackTrace();
341 }
342 }
343 });
344}
345
346/**
347 * Create the application.
348 */
349public Correos() {
350 initialize();
351}
352
353/**
354 * Initialize the contents of the frame.
355 */
356private void initialize() {
357 frmAgregandoEstudiante = new JFrame();
358 frmAgregandoEstudiante.setAlwaysOnTop(true);
359 frmAgregandoEstudiante.setResizable(false);
360 frmAgregandoEstudiante.setTitle("Agregando estudiante");
361 frmAgregandoEstudiante.setBounds(100, 100, 272, 164);
362 frmAgregandoEstudiante.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
363 frmAgregandoEstudiante.getContentPane().setLayout(null);
364
365 txtUsuario = new JTextField();
366 txtUsuario.setToolTipText("Introduce la primera parte de tu correo");
367 txtUsuario.setBounds(10, 48, 104, 30);
368 frmAgregandoEstudiante.getContentPane().add(txtUsuario);
369 txtUsuario.setColumns(10);
370
371 JComboBox comboBox = new JComboBox();
372 comboBox.setModel(new DefaultComboBoxModel(new String[] {"@gmail.com", "@outlook.com", "@unacar.mx", "@live.com", "@yahoo.com", "@hotmail.com"}));
373 comboBox.setBounds(124, 48, 119, 30);
374 frmAgregandoEstudiante.getContentPane().add(comboBox);
375
376 JButton btnAgregar = new JButton("Agregar");
377 btnAgregar.addActionListener(new ActionListener() {
378 public void actionPerformed(ActionEvent arg0) {
379 String user,mail;
380 RegistroEstudiante re = new RegistroEstudiante();
381
382 user = txtUsuario.getText();
383 mail = (String) comboBox.getSelectedItem();
384 setCorreoConcatenado(user.concat(mail));
385 re.setCorreo(getCorreoConcatenado());
386 System.out.println("re.setcorreo " + re.getCorreo());
387
388
389 }
390 });
391 btnAgregar.setBounds(77, 89, 89, 23);
392 frmAgregandoEstudiante.getContentPane().add(btnAgregar);
393
394 JLabel lblNewLabel = new JLabel("Agrega el correo del estudiante");
395 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
396 lblNewLabel.setBounds(10, 11, 207, 27);
397 frmAgregandoEstudiante.getContentPane().add(lblNewLabel);
398 frmAgregandoEstudiante.getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{txtUsuario, comboBox, btnAgregar}));
399}
400private static void addPopup(Component component, final JPopupMenu popup) {
401 component.addMouseListener(new MouseAdapter() {
402 public void mousePressed(MouseEvent e) {
403 if (e.isPopupTrigger()) {
404 showMenu(e);
405 }
406 }
407 public void mouseReleased(MouseEvent e) {
408 if (e.isPopupTrigger()) {
409 showMenu(e);
410 }
411 }
412 private void showMenu(MouseEvent e) {
413 popup.show(e.getComponent(), e.getX(), e.getY());
414 }
415 });
416}
417
418public String getCorreoConcatenado() {
419 return correoConcatenado;
420}
421
422public void setCorreoConcatenado(String correoConcatenado) {
423 this.correoConcatenado = correoConcatenado;
424}
425}