0

Buen día.

Hice un Array de Objetos, y quisiera saber cómo podría optimizarlo.

Mi código es el siguiente

Gson gson = new Gson();
    String idPerfil = "R65";
    String idWorkPlace = "0073";
    String workPlace = null;
    String response = null;
    WorkPlace territories = new WorkPlace();
    WorkPlace zones = new WorkPlace();
    WorkPlace offices = new WorkPlace();
    WorkPlace jobs = new WorkPlace();
    WorkPlace employees = new WorkPlace();
    WorkPlace customers = new WorkPlace();


    if(perfilOficina.contains(idPerfil)){
        WorkPlaceDetail workPlaceDetail = srvBranchesV00.getBrachesWorkplace(idWorkPlace);
        ArrayList<WorkPlace> workp = new ArrayList<WorkPlace>();

        territories.setId("Territoriales");
        territories.setName("");
        zones.setId("Zonas");
        zones.setName("");
        offices.setId("Oficinas");
        offices.setName(workPlace);
        jobs.setId("Cargos");
        jobs.setName("");
        employees.setId("Funcionarios");
        employees.setName("");
        customers.setId("Clientes");
        customers.setName("");          
        workp.add(territories);
        workp.add(zones);
        workp.add(offices);
        workp.add(jobs);
        workp.add(employees);
        workp.add(customers);

        response = gson.toJson(workp);
0

1 respuesta 1

1

Podrías crear un método para rellenar los datos de cada objeto, de tal forma que tendrías :

Para el objeto Territorio, Zones, Office, Employees,Costumers y Jobs te valdría con esto :

//asumiendo que son datos String

void setDatos(String id,String name){
     setId("Territoriales");
     setName("");
}

Para el objeto Workp :


void addDatos(Territories territories,Zones zones,Offices offices, Jobs jobs, Employees employees, Costumers costumers){

        add(territories);
        add(zones);
        add(offices);
        add(jobs);
        add(employees);
        add(customers);
}

Quedando tu código mucho más limpio:

Gson gson = new Gson();
    String idPerfil = "R65";
    String idWorkPlace = "0073";
    String workPlace = null;
    String response = null;
    WorkPlace territories = new WorkPlace();
    WorkPlace zones = new WorkPlace();
    WorkPlace offices = new WorkPlace();
    WorkPlace jobs = new WorkPlace();
    WorkPlace employees = new WorkPlace();
    WorkPlace customers = new WorkPlace();


    if(perfilOficina.contains(idPerfil)){
        WorkPlaceDetail workPlaceDetail = srvBranchesV00.getBrachesWorkplace(idWorkPlace);
        ArrayList<WorkPlace> workp = new ArrayList<WorkPlace>();


        territories.setDatos("Territoriales","");
        zones.setDatos("Zonas","");
        offices.setDatos("Oficinas",workPlace);
        jobs.setDatos("Cargos","");
        employees.setDatos("Funcionarios","");
        customers.setDatos("Clientes","");

        workp.addDAtos(territories,zones,offices,jobs,employees,customers);

        response = gson.toJson(workp);

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.