Java Object Array [closed]

3

I have a doubt regarding the arrangement of objects, in the attached image I have the declaration of the arrangement, then I charge with the set method, but when it comes to returning the loaded arrangement it does not return any value to me, I already made the constructors and the properties of each attribute of the person class, and by means of the set, assign the values that each space of the array will have but I do not know what to do for the array to show me the values, exactly the values of a position that I indicate by means of a validation.

Some advice I should correct in my code or change.

 public Persona[] ConsultarSaldo(Persona[] arreglo){

    for (int i = 0; i < arreglo.length; i++) {
        if (arreglo[i] != null){
        Persona objtemporal = new Persona();
        objtemporal = arreglo[i];


            double SaldoActual = objtemporal.getSaldo();
            objtemporal.setSaldo(SaldoActual);
            arreglo[i] = objtemporal;

        }        
    }
        return arreglo; 

What I need is to preload the arrangement and when I call some method I return the saved value, but it is not returning it, if the exercise is to assign from the beginning the values of the class person to 10 clients per medium of an arrangement, and that's what I tried to do through the set, but when for example I ask to know how much balance the person in the account returns 0, as if he had nothing, in the same way with the other attributes, I obviously use a method where the get calls those values that are supposed to be already loaded in the array

When requesting the balance you are not returning the balance of any of the 10 positions of the array

    
asked by Angelo Castro 31.07.2016 в 11:17
source

1 answer

2

Update

  

To obtain all the balances of the array received from people

public double[] saldos(Personas[] personas) {
    if (personas != null) {
        double[] saldos = new double[personas.length];
        for (int i = 0; i < personas.length; i+=1) {
            saldos[i] = personas[i].getSaldo();
        }
        return saldos;
    }
    return null;
}

As I understand it, you want to make a set(arrayPersona, posicion, valoresPersona) method that modifies your people array and then another method that shows all the contents of that array.

Changes to be made

  • Refactor class Person
    • Use builder design pattern (simple).
    • Include class methods (static) to perform the functions of adding, changing, and displaying, always receiving the People array to which we refer.
  

Code

public class Persona {

    // ESTÁTICOS

    public static boolean add(Persona[] personas, Persona persona) {
        for (int i = 0; i < personas.length; i++) {
            if (personas[i] == null) {
                personas[i] = persona;
                return true;
            }
        }
        return false;
    }

    public static void show(Persona[] personas) {
        for (int i = 0; i < personas.length; i++) {
            System.out.println(personas);
        }
    }

    public static void set(Persona[] personas, int posicion, Persona persona) {
        if (personas != null && persona != null) {
            personas[posicion] = persona; // Estaría bien comprobar la posición
        }
    }

    // CAMPOS

    private String cuenta, identificacion, nombre;
    private int clave;
    private double saldo;
    private boolean bloqueado;

    // MÉTODOS


    public Persona() {
    }

    public String getCuenta() {
        return cuenta;
    }

    public Persona setCuenta(String cuenta) {
        this.cuenta = cuenta;
        return this;
    }

    public String getIdentificacion() {
        return identificacion;
    }

    public Persona setIdentificacion(String identificacion) {
        this.identificacion = identificacion;
        return this;
    }

    public String getNombre() {
        return nombre;
    }

    public Persona setNombre(String nombre) {
        this.nombre = nombre;
        return this;
    }

    public int getClave() {
        return clave;
    }

    public Persona setClave(int clave) {
        this.clave = clave;
        return this;
    }

    public double getSaldo() {
        return saldo;
    }

    public Persona setSaldo(double saldo) {
        this.saldo = saldo;
        return this;
    }

    public boolean isBloqueado() {
        return bloqueado;
    }

    public Persona setBloqueado(boolean bloqueado) {
        this.bloqueado = bloqueado;
        return this;
    }

    @Override
    public String toString() {
        return "Persona{" +
                "cuenta='" + cuenta + '\'' +
                ", identificacion='" + identificacion + '\'' +
                ", nombre='" + nombre + '\'' +
                ", clave=" + clave +
                ", saldo=" + saldo +
                ", bloqueado=" + bloqueado +
                '}';
    }
}

public class MainPersonas {

    public static void main(String[] args) {

        Persona[] personas = new Persona[10];

        Persona.add(personas, new Persona()
                .setCuenta("1")
                .setIdentificacion("1-1371-0735")
                .setNombre("Angelo")
                .setClave(1234)
                .setSaldo(25000.00)
                .setBloqueado(false)
        );

        Persona.set(personas, 0, new Persona()
                .setCuenta("2")
                .setIdentificacion("1-1111-1111")
                .setNombre("Carlos")
                .setClave(1111)
                .setSaldo(1000.00)
                .setBloqueado(false)
        );

        Persona.show(personas);

    }
}
    
answered by 31.07.2016 в 11:43