Ayuda con mi codigo! Software de consulta
Publicado por Mario (2 intervenciones) el 03/07/2019 02:55:07
Buenas, la verdad que soy nuevo en esto de la programacion y me gusta mucho pero por desgracia aqui donde vivo no hay donde pueda estudiar asi que voy leyendo y aprendiendo como puedo ,y ahora me tire a hacer mi primer proyectito a ver que salia pero me encontre con un problema que me tiene trabado , ojala alguien me pueda dar una mano, el software es sencillo y hace lo siguiente, simplemente registrar y almacenar socios y hasta ahi todo bien pero ahora estoy teniendo problemas para volver a cargar los socios , porque no encuentro la manera de que todo vuelva a su lugar,me gustaria 1 de 2 cosas : 1 que el programa cargue automaticamente los datos guardados al abrir o usar el boton de carga para cargar los datos , me gustaria mas la primera enrealidad , dejo mi codigo y espero que alguien me pueda ayudar y tambien me gustaria cualquier critica constructiva sobre el codigo en general ! todos los consejos son bien recibidos, si hubiera ando que se pueda simplificar o hacer de otra forma mejor es bienvenido.
aqui dejo la clase
y adjunto una imagen

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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package ambusol;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
public class Vent1 extends JFrame {
private static final long serialVersionUID = 1L;
private JButton exitButton;
private JComboBox cmbLista;
private JTextArea textArea1;
private JPanel rootPanel;
private JTextField txtNomOs;
private JTextField txtPlanOs;
private JTextField txtNumOs;
private JTextField txtApe;
private JTextField txtNomb;
private JTextField txtDni;
private JTextField txtDir;
private JTextField txtTel;
private JTextField txtCel;
private JTextField txtEmail;
private JComboBox cmbPlan;
private JButton add;
private JButton borrarButton;
private JButton guardarButton;
private JButton cargarButton;
private JTable table1;
public static Map<String,Paciente> pacientes = new LinkedHashMap<>();
public static Map<String,Paciente> pacientes2 = new LinkedHashMap<>();
public Vent1(){
add(rootPanel);
setSize(800, 500);
//Se Recupera los datos del archivo pacientes.txt
//Areas de texto desactivadas al incio
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
//Interacciones areas de texto
txtNomb.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(true);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtApe.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(true);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtNomOs.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(true);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtNumOs.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(true);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtPlanOs.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(true);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtDni.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(true);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtDir.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(true);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtTel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(true);
txtCel.setEnabled(false);
txtEmail.setEnabled(false);
}
});
txtCel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(true);
txtEmail.setEnabled(false);
}
});
txtEmail.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtNomb.setEnabled(false);
txtApe.setEnabled(false);
txtNomOs.setEnabled(false);
txtNumOs.setEnabled(false);
txtPlanOs.setEnabled(false);
txtDni.setEnabled(false);
txtDir.setEnabled(false);
txtTel.setEnabled(false);
txtCel.setEnabled(false);
txtEmail.setEnabled(true);
}
});
//------BOTONES------
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String nombre=txtNomb.getText();
String apellido=txtApe.getText();
String nomOs=txtNomOs.getText();
String numOs=txtNumOs.getText();
String planOS=txtPlanOs.getText();
String dni=txtDni.getText();
String dir=txtDir.getText();
String tel=txtTel.getText();
String cel=txtCel.getText();
String email=txtEmail.getText();
if(txtNomb.getText().length()==0){
JOptionPane.showMessageDialog(null, "Para completar el registro todas las casillas obligatorias* estar completas");
}
else if (txtApe.getText().length()==0){
JOptionPane.showMessageDialog(null, "Para completar el registro todas las casillas obligatorias* estar completas");
}
else if (txtDir.getText().length()==0){
JOptionPane.showMessageDialog(null, "Para completar el registro todas las casillas obligatorias* estar completas");
}
else if (txtTel.getText().length()==0){
JOptionPane.showMessageDialog(null, "Para completar el registro todas las casillas obligatorias* estar completas");
}
else{ Paciente pac=new Paciente(nombre, apellido, nomOs, numOs, planOS, dni, dir, tel, cel, email);
int idPac=pac.getIdPac();
pacientes.put(idPac+" "+nombre+" "+apellido, pac);
cmbLista.addItem(idPac+" "+nombre+" "+apellido);
cmbLista.updateUI();
JOptionPane.showMessageDialog(null, "¡Socio agregado con exito!");
//Resetea los valores
txtNomb.setText("");
txtApe.setText("");
txtNomOs.setText("");
txtNumOs.setText("");
txtPlanOs.setText("");
txtDni.setText("");
txtTel.setText("");
txtTel.setText("");
txtCel.setText("");
txtEmail.setText("");
}
}
});
borrarButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object selectItem = cmbLista.getSelectedItem();
pacientes.remove(selectItem.toString());
cmbLista.removeItem(selectItem);
textArea1.updateUI();
JOptionPane.showMessageDialog(null, "¡Socio borrado con exito!");
}
});
guardarButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ObjectOutputStream escribiendoFichero = new ObjectOutputStream(
new FileOutputStream("pacientes.dat") );
escribiendoFichero.writeObject(pacientes);
escribiendoFichero.close();
JOptionPane.showMessageDialog(null, "Guardando Datos de pacientes");
JOptionPane.showMessageDialog(null, "Guardado completado con exito");
}catch (Exception b){
JOptionPane.showMessageDialog(null, "Algo Fallo, no se puedo guardar el registro");
}
/* try{
FileOutputStream out=new FileOutputStream("pacientes.txt");
ObjectOutputStream pac=new ObjectOutputStream(out);
pac.writeObject(pacientes);
pac.flush();
JOptionPane.showMessageDialog(null, "Socio guardado con exito");
}catch (Exception b){
JOptionPane.showMessageDialog(null, "Algo Fallo, no se puedo guardar el registro");
}*/
}
});
cargarButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
ObjectInputStream leerF= new ObjectInputStream( new FileInputStream("pacientes.dat"));
pacientes2=(Map<String,Paciente>)(leerF.readObject());
leerF.close();
JOptionPane.showMessageDialog(null, "Cargado con Exito");
}
catch (Exception b){
JOptionPane.showMessageDialog(null, "Algo Fallo, no se puedo cargar el registro");
}
}
});
}
aqui dejo la clase
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
package ambusol;
import java.io.Serializable;
public class Paciente implements Serializable {
private String nombre;
private String apellido;
private String nomOs;
private String numOs;
private String planOS;
private String dni;
private String dir;
private String tel;
private String cel;
private String email;
private int idPac;
private static int contPac;
private static int fReg;
private static int fSal;
public Paciente(String nombreArg, String apellidoArg, String nomOsArg, String numOsArg,String planOSArg, String dniArg, String dirArg, String telArg, String celArg, String emailArg)
{
this.nombre=nombreArg;
this.apellido=apellidoArg;
this.nomOs=nomOsArg;
this.numOs=numOsArg;
this.planOS=planOSArg;
this.dni=dniArg;
this.dir=dirArg;
this.tel=telArg;
this.cel=celArg;
this.email=emailArg;
this.idPac=contPac++;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getNomOs() {
return nomOs;
}
public void setNomOs(String nomOs) {
this.nomOs = nomOs;
}
public String getNumOs() {
return numOs;
}
public void setNumOs(String numOs) {
this.numOs = numOs;
}
public String getPlanOS() {
return planOS;
}
public void setPlanOS(String planOS) {
this.planOS = planOS;
}
public String getDni() {
return dni;
}
public void setDni(String dni) {
this.dni = dni;
}
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getCel() {
return cel;
}
public void setCel(String cel) {
this.cel = cel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getIdPac() {
return idPac;
}
public void setIdPac(int idPac) {
this.idPac = idPac;
}
public static int getContPac() {
return contPac;
}
public static void setContPac(int contPac) {
Paciente.contPac = contPac;
}
}
y adjunto una imagen
Valora esta pregunta


0