import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class Archivo {
private String nombre;
public Archivo(String nombre_archivo) {
this.nombre = nombre_archivo;
}
public List<Titular> cargarArchivo() {
List<Titular> lista = new ArrayList<Titular>();
try {
File tmo = new File(nombre);
if (tmo.exists()) {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(nombre));
Object tm;
while ((tm = in.readObject()) != null) {
Titular tmp = (Titular) tm;
lista.add(tmp);
}
}
} catch (Exception ex) {
// Logger.getLogger(Archivo.class.getName()).log(Level.SEVERE, null, ex);
}
return lista;
}
public void save(Titular t) {
ObjectInputStream in = null;
List<Titular> lista = new ArrayList<Titular>();
try {
lista.add(t);
File exi = new File(nombre);
if (exi.exists()) {
in = new ObjectInputStream(new FileInputStream(nombre));
Object tm;
while ((tm = in.readObject()) != null) {
Titular tmp = (Titular) tm;
lista.add(tmp);
}
in.close();
}
} catch (Exception ex) {
// Logger.getLogger(Archivo.class.getName()).log(Level.SEVERE, null, ex);
}
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(nombre));
for (Titular titul : lista.toArray(new Titular[0])) {
out.writeObject(titul);
}
out.close();
} catch (IOException ex) {
// Logger.getLogger(Archivo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
import java.io.Serializable;
public class Titular implements Serializable {
private String NOMBRE_TITULAR;
private String NOMBRES_FAMILIARES="";
private int tipo;
private static int []monto={1000000, 800000, 500000};
public Titular(String nombre){
NOMBRE_TITULAR=nombre;
}
public String getTitular(){
return NOMBRE_TITULAR;
}
public void agregaFamiliar(String familiar){
NOMBRES_FAMILIARES+=familiar+";";
}
public int getNumeroDeFamiliares(){
return NOMBRES_FAMILIARES.split(";").length;
}
public void setTipo(int t){
tipo=t;
}
public int getTipo(){
return tipo;
}
public int getMontoaPagar(){
return (getNumeroDeFamiliares()+1)*monto[tipo];
}
}
import java.util.List;
import javax.swing.JOptionPane;
public class Principal {
private static String pideDato(String t) {
return JOptionPane.showInputDialog(t);
}
public static void main(String[] args) {
Archivo a = new Archivo("Seguros.obj");
List<Titular> lista = a.cargarArchivo();
int opcion;
do {
opcion = Integer.parseInt(JOptionPane.showInputDialog("Compañía de seguros Segumet\n"
+ "1.- Agregar Titular.\n"
+ "2.- Mostrar Menu con opciones.\n"
+ "3.- Salir."));
switch (opcion) {
case 1:
String nombre_titular = pideDato("Nombre del titular");
Titular t = new Titular(nombre_titular);
boolean agregar_familiar = true;
do {
int respuesta = JOptionPane.showConfirmDialog(null, "¿Agregar familiar?", "", JOptionPane.YES_NO_OPTION);
if (respuesta == JOptionPane.OK_OPTION) {
String familiar_ = pideDato("Escribe el nombre del familiar.");
t.agregaFamiliar(familiar_);
} else {
agregar_familiar = false;
}
} while (agregar_familiar);
int opcion_tipo = Integer.parseInt(JOptionPane.showInputDialog("Tipo de pago\n"
+ "0.- 1.000.000\n"
+ "1.- 800.000\n"
+ "2.- 500.000"));
t.setTipo(opcion_tipo);
lista.add(t);
a.save(t);
lista = a.cargarArchivo();
break;
case 2:
int opcion2;
do {
opcion2 = Integer.parseInt(JOptionPane.showInputDialog("Introduce una opcion\n"
+ "1.- Dado un tipo de seguro, indicar cuantos titulares tienen ese tipo de seguro.\n"
+ "2.- Calcular el promedio de familiares asegurados por los titulares\n"
+ "3.- Generar un reporte con aquellos titulares cuya cantidad de familiares sea mayor o igual al promedio de familiares calculado en el punto anterior\n"
+ "4.- Generar una lista con el nombre del titular y el monto a pagar según el tipo de seguro, el cual se calcula multiplicando el monto por la cantidad de familiares + 1, que corresponde al titular.\n"
+ "5.- Generar un arreglo con el titular que paga más y el que paga menos.\n"
+ "6.- Regresar"));
switch (opcion2) {
case 1:
int opcion3 = Integer.parseInt(JOptionPane.showInputDialog("Elige el tipo de seguro\n"
+ "0.- Total\n"
+ "1.- Medio\n"
+ "2.- Bajo"));
int numero = 0;
for (Titular tm : lista.toArray(new Titular[0])) {
if (tm.getTipo() == opcion3) {
numero++;
}
}
JOptionPane.showMessageDialog(null, "Hay " + numero + " Titulares con ese tipo de seguro");
break;
case 2:
int nuemero_familiares = 0;
for (Titular tm : lista.toArray(new Titular[0])) {
nuemero_familiares =nuemero_familiares+ tm.getNumeroDeFamiliares();
}
float promedio = (float) nuemero_familiares / lista.size();
JOptionPane.showMessageDialog(null, "El promedio de familiares es de: " + promedio);
break;
case 3:
int nuemero_familiare = 0;
for (Titular tm : lista.toArray(new Titular[0])) {
nuemero_familiare += tm.getNumeroDeFamiliares();
}
float promedi = (float) nuemero_familiare / lista.size();
String nombres_titul = "";
for (Titular tm : lista.toArray(new Titular[0])) {
if (tm.getNumeroDeFamiliares() >= promedi) {
nombres_titul += tm.getTitular() + "\n";
}
}
JOptionPane.showMessageDialog(null, "Nombres de titulares.\n " + nombres_titul);
break;
case 4:
String datos = "";
for (Titular tm : lista.toArray(new Titular[0])) {
datos += tm.getTitular() + "\t pagará: " + tm.getMontoaPagar() + "\n";
}
JOptionPane.showMessageDialog(null, datos);
break;
case 5:
if (lista.size() > 0) {
Titular menor = lista.get(0);
Titular mayor = lista.get(0);
for (int i = 1; i < lista.size(); i++) {
if (lista.get(i).getMontoaPagar() > mayor.getMontoaPagar()) {
mayor = lista.get(i);
}
if (lista.get(i).getMontoaPagar() < menor.getMontoaPagar()) {
menor = lista.get(i);
}
}
JOptionPane.showMessageDialog(null, "Mayor: " + mayor.getTitular() + " \n"
+ "Menor: " + menor.getTitular());
} else {
JOptionPane.showMessageDialog(null, "No hay ningun titular");
}
break;
}
} while (opcion2 != 6);
break;
}
} while (opcion != 3);
}
}
Este es un simple código que hace uso de Programación Orientada a Objetos y usa archivos para guardar los datos.
Lo hice hace tiempo creo te despejará dudas ^^ saludos y espero te sea de utilidad