Problems with serialization

0

I am carrying out a project of a parking lot that registers clients, the vehicle of each of them, the number of hours that will last, I calculate the price depending on the vehicle, etc ... Until there is everything right, I have to go increasing by giving an example the total sum of what the parking lot received for each vehicle, this I am doing in a class (Reports) with static attributes, and the window of JFrame shows them as it should be, the problem is that when you close the program and reopen it, the Reports class has all the default values that would be 0 and obviously the JFrame window also shows everything in 0.

PD: The program reads the serializable when it starts and saves it when it is closed.

Class Reports

import java.io.Serializable;
public class Informes implements Serializable{  

private static double sdinerot = 0, sdineroTA = 0, sdineroTB = 0, sdineroTC = 0, sdinerocarro = 0,
        sdinerocamioneta = 0, sdinerobus = 0, sdineromoto = 0, pClienteA = 0, pClienteB = 0, pClienteC = 0;
private static int tCA = 0, tCB = 0, tCC = 0;  


public double getSdinerot() {
    return sdinerot;
}

public void setSdinerot(double total) {
    sdinerot = sdinerot + total;

}

public double getSdineroTA() {
    return sdineroTA;
}

public void setSdineroTA(double sdineroTA) {
    this.sdineroTA = this.sdineroTA + sdineroTA;


}

public double getSdineroTB() {
    return sdineroTB;
}

public void setSdineroTB(double sdineroTB) {
    this.sdineroTB = this.sdineroTB + sdineroTB;


}

public double getSdineroTC() {
    return sdineroTC;
}

public void setSdineroTC(double sdineroTC) {
    this.sdineroTC = this.sdineroTC + sdineroTC;


}

public int gettCA() {
    return tCA;
}

public void setcCA() {
    tCA++;


}

public int gettCB() {
    return tCB;
}

public void setcCB() {
    tCB++;


}

public int gettCC() {
    return tCC;
}

public void setcCC() {
    tCC++;


}

public double getSdinerocarro() {
    return sdinerocarro;
}

public void setSdinerocarro(double sdinerocarro) {
    this.sdinerocarro = this.sdinerocarro + sdinerocarro;


}

public double getSdinerocamioneta() {
    return sdinerocamioneta;
}

public void setSdinerocamioneta(double sdinerocamioneta) {
    this.sdinerocamioneta = this.sdinerocamioneta + sdinerocamioneta;


}

public double getSdinerobus() {
    return sdinerobus;
}

public void setSdinerobus(double sdinerobus) {
    this.sdinerobus = this.sdinerobus + sdinerobus;


}

public double getSdineromoto() {
    return sdineromoto;
}

public void setSdineromoto(double sdineromoto) {
    this.sdineromoto = this.sdineromoto + sdineromoto;


}

public void setPromedioClienteA() {
    if (tCA == 0) {
        pClienteA = 0;
    } else {
        pClienteA = (sdineroTA / tCA);
    }

}

public void setPromedioClienteB() {
    if (tCB == 0) {
        pClienteB = 0;
    } else {
        pClienteB = (sdineroTB / tCB);
    }


}

public void setPromedioClienteC() {
    if (tCC == 0) {
        pClienteC = 0;
    } else {
        pClienteC = (sdineroTC / tCC);
    }


}

public static double getPromedioClienteA() {
    return pClienteA;
}

public static double getPromedioClienteB() {
    return pClienteB;
}

public static double getPromedioClienteC() {
    return pClienteC;
}

}

Class where I keep the serialized

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

public class DatosGenerales {

public static Vector<Clientes> vector = new Vector<Clientes>();

public static void guardarSerializadoClientes() {
    try {
        File miFile = new File("DatosClientes.txt");
        ObjectOutputStream datos = new ObjectOutputStream(new FileOutputStream(miFile));
        datos.writeObject(DatosGenerales.vector);
        datos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void leerSerializadoClientes() {
    ObjectInputStream datos;
    File miFile = new File("DatosClientes.txt");
    try {
        datos = new ObjectInputStream(new FileInputStream(miFile));
        DatosGenerales.vector = (Vector) datos.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void guardarSerializadoInformes() {
    Informes informes = new Informes();
    try {
        File miFile = new File("Informes.txt");
        ObjectOutputStream datos = new ObjectOutputStream(new FileOutputStream(miFile));
        datos.writeObject(informes);
        datos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void leerSerializadoInformes() {
    Informes informes = new Informes();

    ObjectInputStream datos;
    File miFile = new File("Informes.txt");
    try {
        datos = new ObjectInputStream(new FileInputStream(miFile));
        informes = (Informes) datos.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

Customer Class

public class Clientes implements Serializable {

private String nombre = "", apellido = "", placa = "", sexo = "";
private double identificacion = 0, telefono = 0, total = 0;
private int horas = 0, tipovehiculo = 0, tipocliente = 0, edad = 0;

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 getPlaca() {
    return placa;
}

public void setPlaca(String placa) {
    this.placa = placa;
}

public double getIdentificacion() {
    return identificacion;
}

public void setIdentificacion(double identificacion) {
    this.identificacion = identificacion;
}

public double getTelefono() {
    return telefono;
}

public void setTelefono(double telefono) {
    this.telefono = telefono;
}

public double getTotal() {
    return total;
}

public int getHoras() {
    return horas;
}

public void setHoras(int horas) {
    this.horas = horas;
}

public int getTipovehiculo() {
    return tipovehiculo;
}

public void setTipovehiculo(int tipovehiculo) {
    this.tipovehiculo = tipovehiculo;
}

public int getTipocliente() {
    return tipocliente;
}

public void setTipocliente(int tipocliente) {
    this.tipocliente = tipocliente;
}


public int getEdad() {
    return edad;
}

public void setEdad(int edad) {
    this.edad = edad;
}

public String getSexo() {
    return sexo;
}

public void setSexo(String sexo) {
    this.sexo = sexo;
}

//Calcular el total segun el tipo de vehiculo
public void setCalcular() {

    Vehiculos vehiculos = new Vehiculos();
    Informes informes = new Informes();
    switch (tipovehiculo) {
        case 1:
            vehiculos.setCarro(tipocliente, horas,identificacion);
            total = vehiculos.getTotal(); //Establece el total del cliente
            informes.setSdinerocarro(total);//Suma promedio total para los carros
            break;
        case 2:
            vehiculos.setCamioneta(tipocliente, horas,identificacion);
            total = vehiculos.getTotal();//Establece el total del cliente
            informes.setSdinerocamioneta(total);//Suma promedio total para las camionetas
            break;
        case 3:
            vehiculos.setBus(tipocliente, horas,identificacion);
            total = vehiculos.getTotal();//Establece el total del cliente
            informes.setSdinerobus(total);//Suma promedio total para los buses
            break;
        case 4:
            vehiculos.setMoto(tipocliente, horas,identificacion);
            total = vehiculos.getTotal();//Establece el total del cliente
            informes.setSdineromoto(total);//Suma promedio total para las motos
            break;
    }

    informes.setSdinerot(total); //Sumar los totales de todos los clientes       

}

}

Car Class

public class Vehiculos {

private double total = 0;
Informes informes = new Informes();

public void setCarro(int tipocliente, int horas, double id) {
    switch (tipocliente) {  //Calcular el total segun el tipo de cliente
        case 1:
            total = ((3500 * horas) - ((3500 * horas) * 0.25));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int s = getVerificar(id);
            if (s == 0) {//Si el cliente no existe
                informes.setcCA();
            }

            informes.setSdineroTA(total);
            informes.setPromedioClienteA();
            break;
        case 2:
            total = ((3500 * horas) - ((3500 * horas) * 0.15));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int j = getVerificar(id);
            if (j == 0) {//Si el cliente no existe
                informes.setcCB();
            }

            informes.setSdineroTB(total);
            informes.setPromedioClienteB();
            break;
        case 3:
            total = ((3500 * horas));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int k = getVerificar(id);
            if (k == 0) {//Si el cliente no existe
                informes.setcCC();
            }

            informes.setSdineroTC(total);
            informes.setPromedioClienteC();
            break;
    }

}

public void setCamioneta(int tipocliente, int horas, double id) {
    switch (tipocliente) {   //Calcular el total segun el tipo de cliente
        case 1:
            total = ((5000 * horas) - ((5000 * horas) * 0.25));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int s = getVerificar(id);
            if (s == 0) {//Si el cliente no existe
                informes.setcCA();
            }

            informes.setSdineroTA(total);
            informes.setPromedioClienteA();
            break;
        case 2:
            total = ((5000 * horas) - ((5000 * horas) * 0.15));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int j = getVerificar(id);
            if (j == 0) {//Si el cliente no existe
                informes.setcCB();
            }

            informes.setSdineroTB(total);
            informes.setPromedioClienteB();
            break;
        case 3:
            total = ((5000 * horas));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int k = getVerificar(id);
            if (k == 0) {//Si el cliente no existe
                informes.setcCC();
            }

            informes.setSdineroTC(total);
            informes.setPromedioClienteC();
            break;
    }
}

public void setBus(int tipocliente, int horas, double id) {
    switch (tipocliente) {  //Calcular el total segun el tipo de cliente
        case 1:
            total = ((6500 * horas) - ((6500 * horas) * 0.25));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int s = getVerificar(id);
            if (s == 0) {//Si el cliente no existe
                informes.setcCA();
            }

            informes.setSdineroTA(total);
            informes.setPromedioClienteA();
            break;
        case 2:
            total = ((6500 * horas) - ((6500 * horas) * 0.15));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int j = getVerificar(id);
            if (j == 0) {//Si el cliente no existe
                informes.setcCB();
            }

            informes.setSdineroTB(total);
            informes.setPromedioClienteB();
            break;
        case 3:
            total = ((6500 * horas));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int k = getVerificar(id);
            if (k == 0) {//Si el cliente no existe
                informes.setcCC();
            }

            informes.setSdineroTC(total);
            informes.setPromedioClienteC();
            break;
    }
}

public void setMoto(int tipocliente, int horas, double id) {
    switch (tipocliente) {  //Calcular el total segun el tipo de cliente
        case 1:
            total = ((3000 * horas) - ((3000 * horas) * 0.25));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int s = getVerificar(id);
            if (s == 0) {//Si el cliente no existe
                informes.setcCA();
            }

            informes.setSdineroTA(total);
            informes.setPromedioClienteA();
            break;
        case 2:
            total = ((3000 * horas) - ((3000 * horas) * 0.15));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int j = getVerificar(id);
            if (j == 0) {//Si el cliente no existe
                informes.setcCB();
            }

            informes.setSdineroTB(total);
            informes.setPromedioClienteB();
            break;
        case 3:
            total = ((3000 * horas));

            //Verificamos si el cliente existe para no tener que sumar el tipo de cliente
            int k = getVerificar(id);
            if (k == 0) {//Si el cliente no existe
                informes.setcCC();
            }

            informes.setSdineroTC(total);
            informes.setPromedioClienteC();
            break;
    }
}

public double getTotal() {
    return total;
}

private int getVerificar(double id) {
    int a = 0;
    for (int i = 0; i < DatosGenerales.vector.size(); i++) {
        Clientes c = DatosGenerales.vector.get(i);
        if (c.getIdentificacion() == id) {
            a = 1;
            return 1;
        }
    }
    if (a == 0) {
        return 0;
    }
    return 0;
}

}

    
asked by Steven Camargo 19.11.2017 в 05:01
source

1 answer

1

In your Reports class, static variables are not going to be serialized with their value if they had changed. They will be serialized with the value of the constructor or predefined, as you have it.

The java serialization scheme does not serialize static objects, nor those defined as transient.

If you want to serialize the values of your variables of your Report class, you need to define them as class variables, that is, "not static".

    
answered by 19.11.2017 в 17:32