I'm making an application so that through a menu, it allows you to:
The fact is that more or less the first two options I have made, but with errors:
-
In option 1, I need to say that if I say I do not want to enter more data, the menu will appear directly again.
-
In option 2, I can not show the object created in the first option.
-
Option 3 I do not know directly how to do it, it's about entering an attribute of the object and finding that object. I know that the beginning should be like option 2, since it is about accessing and reading, but showing the object I no longer know. I would appreciate some guidance especially with the methods, I'm pretty stuck.
The code is as follows:
public class Tarea06Pacientes {
/**
* @param args the command line arguments
*/
//Clase Scanner:
static Scanner entrada = new Scanner(System.in);
static File fichero;
static FileOutputStream fos;
static ObjectOutputStream oos;
static FileInputStream fis;
static ObjectInputStream ois;
static Object obj;
static boolean menu = false;
static int opciones;
public static void main(String[] args) {
while (menu == false) {
//Cramos el menu:
opciones = Integer.parseInt(JOptionPane.showInputDialog("Menu:\n1. Añadir paciente.\n" +
"2. Listar pacientes.\n" +
"3. Buscar pacientes.\n" +
"4. Borrar el fichero de datos.\n" +
"5. Salir"));
switch (opciones) {
case 1:
anadir();
break;
case 2:
listar();
break;
case 3:
}
}
}
public static void anadir() {
try {
//Creamos el objeto de la clase Pacientes:
Paciente paciente1 = new Paciente();
//Creamos el fichero
fichero = new File("C:\FP\PROG\u6\pacientes.dat");
//Creamos las clases necesarias:
fos = new FileOutputStream(fichero);
oos = new ObjectOutputStream(fos);
boolean pregunta = false;
while (pregunta == false) {
System.out.println("Introduzca nombre del paciente: ");
String nPaciente = entrada.nextLine();
paciente1.setNombre(nPaciente);
oos.writeObject(paciente1);
System.out.println("Introduzca apellidos del paciente: ");
String aPaciente = entrada.nextLine();
paciente1.setApellidos(aPaciente);
oos.writeObject(paciente1);
System.out.println("Introduzca dienta del paciente: ");
String dPaciente = entrada.nextLine();
paciente1.setNombre(dPaciente);
oos.writeObject(paciente1);
System.out.println("Introduzca numero de cama del paciente: ");
String cPaciente = entrada.nextLine();
paciente1.setNombre(cPaciente);
oos.writeObject(paciente1);
System.out.println("¿Desea seguir introduciendo datos de pacientes?");
String respuesta = entrada.nextLine();
if (respuesta.equals("S") || respuesta.equals("s")) {
pregunta = false;
} else {
menu = true;
pregunta = true;
}
}
fos.close();
} catch (Exception e) {
System.out.println("¡El fichero no existe!");
}
}
public static void listar() {
try {
fichero = new File("C:\FP\PROG\u6\pacientes.dat");
fis = new FileInputStream(fichero);
ois = new ObjectInputStream(fis);
obj = ois.readObject();
System.out.println(obj);
fis.close();
menu = true;
} catch (Exception e) {
}
}
public static void buscar(String cama) {
try {
fichero = new File("C:\FP\PROG\u6\pacientes.dat");
fis = new FileInputStream(fichero);
ois = new ObjectInputStream(fis);
obj = ois.readObject();
System.out.println("Indtroduzca el número de la cama: ");
cama = entrada.nextLine();
int numC = cama.length();
for (int i = 0; numC < 6; i++) {
System.out.println(obj);
}
ois.close();
menu = false;
} catch (Exception e) {
}
}
}