Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. package tarea07grafica;
  2.  
  3. import java.io.Serializable;
  4.  
  5.  
  6.  
  7. public class medicos implements Serializable{
  8.  
  9. private static final long serialVersionUID = 1L;
  10.  
  11. //declaramos los atributos
  12. private String NIF;
  13. private String nombre;
  14. private String apellidos;
  15. private int telefono;
  16. private String sexo;
  17. private boolean guardias;
  18. private String especialidad;
  19.  
  20. //creamos el constructor por defecto
  21. public medicos(){
  22.  
  23. }
  24.  
  25. //creamos el constructor
  26. public medicos(String NIF, String nombre, String apellidos, int telefono, String sexo, boolean guardias, String especialidad) {
  27. this.NIF = NIF;
  28. this.nombre = nombre;
  29. this.apellidos = apellidos;
  30. this.telefono = telefono;
  31. this.sexo = sexo;
  32. this.guardias = guardias;
  33. this.especialidad = especialidad;
  34. }
  35.  
  36.  
  37. //creamos los getter y setter
  38. public String getNIF() {
  39. return NIF;
  40. }
  41.  
  42. public void setNIF(String NIF) {
  43. this.NIF = NIF;
  44. }
  45.  
  46. public String getNombre() {
  47. return nombre;
  48. }
  49.  
  50. public void setNombre(String nombre) {
  51. this.nombre = nombre;
  52. }
  53.  
  54. public String getApellidos() {
  55. return apellidos;
  56. }
  57.  
  58. public void setApellidos(String apellidos) {
  59. this.apellidos = apellidos;
  60. }
  61.  
  62. public int getTelefono() {
  63. return telefono;
  64. }
  65.  
  66. public void setTelefono(int telefono) {
  67. this.telefono = telefono;
  68. }
  69.  
  70. public String getSexo() {
  71. return sexo;
  72. }
  73.  
  74. public void setSexo(String sexo) {
  75. this.sexo = sexo;
  76. }
  77.  
  78. public boolean isGuardias() {
  79. return guardias;
  80. }
  81.  
  82. public void setGuardias(boolean guardias) {
  83. this.guardias = guardias;
  84. }
  85.  
  86. public String getEspecialidad() {
  87. return especialidad;
  88. }
  89.  
  90. public void setEspecialidad(String especialidad) {
  91. this.especialidad = especialidad;
  92. }
  93.  
  94.  
  95. //creamos el metodo toString
  96. @Override
  97. public String toString() {
  98. return "medicos= " + "NIF: " + NIF + ", nombre: " + nombre + ", apellidos: " + apellidos + ", telefono: " + telefono + ", sexo: " + sexo + ", guardias: " + guardias + ", especialidad: " + especialidad;
  99. }
  100.  
  101. public class Tarea07Grafica {
  102.  
  103.  
  104. private void leerArchivo(){
  105.  
  106. medicos m;
  107. String NIF, nombre, apellidos, telefono, sexo, guardias, especialidad;
  108.  
  109. try {
  110. FileInputStream archivo = new FileInputStream("medicos.txt");//abrimos el flujo de entrada
  111. ObjectInputStream lectura = new ObjectInputStream(archivo);//usamos el ObjectInputStream para leer objetos del archivo
  112. //recorremos el archivo
  113. while(true){
  114. m = (medicos) lectura.readObject();
  115. NIF = m.getNIF();
  116. nombre = m.getNombre();
  117. apellidos = m.getApellidos();
  118. telefono = String.valueOf(m.getTelefono());
  119. sexo = String.valueOf(m.getSexo());
  120. guardias = String.valueOf(m.isGuardias());
  121. especialidad = String.valueOf(m.getEspecialidad());
  122.  
  123. }
  124. //capturamos las excepciones
  125. }catch (EOFException ex){
  126. System.err.println("Error " + ex);
  127. }catch (FileNotFoundException ex) {
  128. System.err.println("Error, " + ex);
  129. } catch (IOException | ClassNotFoundException ex) {
  130. System.err.println("Error, " + ex);
  131. }
  132. }
  133.  
  134. public void añadirDatos(){
  135.  
  136. try {
  137. FileOutputStream archivo = new FileOutputStream ("medicos.txt", true);//abrimos el flujo de salida
  138. try (ObjectOutputStream escritura = new ObjectOutputStream(archivo)) {//usamos el ObjectOutputStream para escribir objetos en el archivo
  139. medicos m = new medicos();//creamos el objeto
  140. escritura.writeObject(m); //escribimos objetos en el archivo
  141. escritura.close();//cerramos el archivo
  142. }
  143. //capturamos las excepciones
  144. } catch (FileNotFoundException ex) {
  145. System.err.println("Error," + ex);
  146. } catch (IOException ex) {
  147. System.err.println("Error, " + ex);
  148. }
  149.  
  150. }
  151.  
  152. public static void main(String[] args){
  153.  
  154. Tarea07Grafica archivo = new Tarea07Grafica();
  155. Interfaz Interfaz = new Interfaz();
  156. Interfaz.setLocationRelativeTo(null);
  157. Interfaz.setTitle("Ventana Principal");
  158. Interfaz.setVisible(true);
  159.  
  160.  
  161. }
  162. }
  163.  
  164. private void btnMostrarActionPerformed(java.awt.event.ActionEvent evt) {
  165.  
  166.  
  167. File archivo = new File("medicos.dat");//inicializamos el objeto de tipo File
  168.  
  169. medicos m = null;//inicializamos a null el objeto creado
  170.  
  171. try {
  172. FileInputStream fis = new FileInputStream(archivo); //abrimos el flujo de entrada de datos
  173.  
  174. if (archivo != null){ //ponemos el condicional para que compruebe si el fichero esta vacio o no
  175. while (fis.available()>0){
  176. ObjectInputStream entrada = new ObjectInputStream(fis); //establecemos la entrada de datos en el objeto
  177. m = (medicos) entrada.readObject(); //leemos los datos del objeto
  178. System.out.println(m.toString()); //imprimimos por pantalla los resultados
  179. }
  180. fis.close(); //cerramos el flujo de entrada
  181. }
  182. //capturamos las excepciones
  183. }catch (IOException e){
  184. System.out.println("Final del archivo");
  185.  
  186. } catch (ClassNotFoundException ex) {
  187. System.err.println("Error " + ex);
  188. }
  189. }
  190.  
  191. private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {
  192.  
  193. //primero validamos los datos
  194. String datos = "";
  195.  
  196. if (txtNIF.getText().equals("")){
  197. JOptionPane.showMessageDialog(null, "Ingrese el NIF");
  198. return;
  199. }
  200.  
  201. if (txtNombre.getText().equals("")){
  202. JOptionPane.showMessageDialog(null, "Ingrese el nombre");
  203. return;
  204. }
  205.  
  206. if (txtApellidos.getText().equals("")){
  207. JOptionPane.showMessageDialog(null, "Ingrese los apellidos");
  208. return;
  209. }
  210.  
  211. if (txtTelefono.getText().equals("")){
  212. JOptionPane.showMessageDialog(null, "Ingrese el telefono");
  213. return;
  214. }
  215.  
  216. //grabamos los datos
  217. datos = txtNIF.getText() + txtNombre.getText() + txtApellidos.getText() + txtTelefono.getText() + " " + buttonGroup1.getSelection();
  218. if (rbtnMujer.isSelected())
  219. datos += " Mujer";
  220. else
  221. datos += " Hombre";
  222. cbxGuardias.isSelected();
  223. JcbxEspecialidad.setSelectedItem(datos + "n");
  224.  
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement