Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tarea07grafica;
- import java.io.Serializable;
- public class medicos implements Serializable{
- private static final long serialVersionUID = 1L;
- //declaramos los atributos
- private String NIF;
- private String nombre;
- private String apellidos;
- private int telefono;
- private String sexo;
- private boolean guardias;
- private String especialidad;
- //creamos el constructor por defecto
- public medicos(){
- }
- //creamos el constructor
- public medicos(String NIF, String nombre, String apellidos, int telefono, String sexo, boolean guardias, String especialidad) {
- this.NIF = NIF;
- this.nombre = nombre;
- this.apellidos = apellidos;
- this.telefono = telefono;
- this.sexo = sexo;
- this.guardias = guardias;
- this.especialidad = especialidad;
- }
- //creamos los getter y setter
- public String getNIF() {
- return NIF;
- }
- public void setNIF(String NIF) {
- this.NIF = NIF;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public String getApellidos() {
- return apellidos;
- }
- public void setApellidos(String apellidos) {
- this.apellidos = apellidos;
- }
- public int getTelefono() {
- return telefono;
- }
- public void setTelefono(int telefono) {
- this.telefono = telefono;
- }
- public String getSexo() {
- return sexo;
- }
- public void setSexo(String sexo) {
- this.sexo = sexo;
- }
- public boolean isGuardias() {
- return guardias;
- }
- public void setGuardias(boolean guardias) {
- this.guardias = guardias;
- }
- public String getEspecialidad() {
- return especialidad;
- }
- public void setEspecialidad(String especialidad) {
- this.especialidad = especialidad;
- }
- //creamos el metodo toString
- @Override
- public String toString() {
- return "medicos= " + "NIF: " + NIF + ", nombre: " + nombre + ", apellidos: " + apellidos + ", telefono: " + telefono + ", sexo: " + sexo + ", guardias: " + guardias + ", especialidad: " + especialidad;
- }
- public class Tarea07Grafica {
- private void leerArchivo(){
- medicos m;
- String NIF, nombre, apellidos, telefono, sexo, guardias, especialidad;
- try {
- FileInputStream archivo = new FileInputStream("medicos.txt");//abrimos el flujo de entrada
- ObjectInputStream lectura = new ObjectInputStream(archivo);//usamos el ObjectInputStream para leer objetos del archivo
- //recorremos el archivo
- while(true){
- m = (medicos) lectura.readObject();
- NIF = m.getNIF();
- nombre = m.getNombre();
- apellidos = m.getApellidos();
- telefono = String.valueOf(m.getTelefono());
- sexo = String.valueOf(m.getSexo());
- guardias = String.valueOf(m.isGuardias());
- especialidad = String.valueOf(m.getEspecialidad());
- }
- //capturamos las excepciones
- }catch (EOFException ex){
- System.err.println("Error " + ex);
- }catch (FileNotFoundException ex) {
- System.err.println("Error, " + ex);
- } catch (IOException | ClassNotFoundException ex) {
- System.err.println("Error, " + ex);
- }
- }
- public void añadirDatos(){
- try {
- FileOutputStream archivo = new FileOutputStream ("medicos.txt", true);//abrimos el flujo de salida
- try (ObjectOutputStream escritura = new ObjectOutputStream(archivo)) {//usamos el ObjectOutputStream para escribir objetos en el archivo
- medicos m = new medicos();//creamos el objeto
- escritura.writeObject(m); //escribimos objetos en el archivo
- escritura.close();//cerramos el archivo
- }
- //capturamos las excepciones
- } catch (FileNotFoundException ex) {
- System.err.println("Error," + ex);
- } catch (IOException ex) {
- System.err.println("Error, " + ex);
- }
- }
- public static void main(String[] args){
- Tarea07Grafica archivo = new Tarea07Grafica();
- Interfaz Interfaz = new Interfaz();
- Interfaz.setLocationRelativeTo(null);
- Interfaz.setTitle("Ventana Principal");
- Interfaz.setVisible(true);
- }
- }
- private void btnMostrarActionPerformed(java.awt.event.ActionEvent evt) {
- File archivo = new File("medicos.dat");//inicializamos el objeto de tipo File
- medicos m = null;//inicializamos a null el objeto creado
- try {
- FileInputStream fis = new FileInputStream(archivo); //abrimos el flujo de entrada de datos
- if (archivo != null){ //ponemos el condicional para que compruebe si el fichero esta vacio o no
- while (fis.available()>0){
- ObjectInputStream entrada = new ObjectInputStream(fis); //establecemos la entrada de datos en el objeto
- m = (medicos) entrada.readObject(); //leemos los datos del objeto
- System.out.println(m.toString()); //imprimimos por pantalla los resultados
- }
- fis.close(); //cerramos el flujo de entrada
- }
- //capturamos las excepciones
- }catch (IOException e){
- System.out.println("Final del archivo");
- } catch (ClassNotFoundException ex) {
- System.err.println("Error " + ex);
- }
- }
- private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {
- //primero validamos los datos
- String datos = "";
- if (txtNIF.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Ingrese el NIF");
- return;
- }
- if (txtNombre.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Ingrese el nombre");
- return;
- }
- if (txtApellidos.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Ingrese los apellidos");
- return;
- }
- if (txtTelefono.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Ingrese el telefono");
- return;
- }
- //grabamos los datos
- datos = txtNIF.getText() + txtNombre.getText() + txtApellidos.getText() + txtTelefono.getText() + " " + buttonGroup1.getSelection();
- if (rbtnMujer.isSelected())
- datos += " Mujer";
- else
- datos += " Hombre";
- cbxGuardias.isSelected();
- JcbxEspecialidad.setSelectedItem(datos + "n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement