2

I have a problem with the setters and arrays in java, I can't set it right.

This are the getters and setters.

public Movimiento[] getMov() {
    return mov;
}

public void setMov(Movimiento[] mov) {
    this.mov = mov;
}

This are the attributes of Movimiento(Movement)

private double monto;
private int tipo;
private String operacion;

this is Cuenta

public class Cuenta {
    final static int MAXC = 5;
    final static int MAXmov = 10;
    private double monto = 0;
    private Movimiento mov[] = new Movimiento [MAXmov];
    private int ncuenta[] = new int [MAXC];
    private Cliente clientes[] = new Cliente[MAXC];

    public Cuenta(Cliente[] clientes) {
        this.clientes = clientes;
    }

    public double getMonto() {
        return monto;
    }


    public void setMonto(double monto) {
        this.monto = monto;
    }

    public Movimiento[] getMov() {
        return mov;
    }

    public void setMov(Movimiento[] mov) {
        this.mov = mov;
    }

Maybe I am doing something wrong, I tried to set it like this and it (and other ways) does not work.

I want to set an object to the position[0]

cuentas[ncuenta].setMov(new Movimiento(monto,tipo,operacion))[0];

The method setMove(Movimiento[]) int the type Cuenta is not applicable for the arguments (Movimiento) I am stuck at this and I cant find a solution for it.

cgsdfc
  • 538
  • 4
  • 19
  • Don't put the language in the title, add it as a *tag*, like I just done for you. Many people here filter questions on tags, and if you don't specify the correct tags some who might be able to help you maybe not even bother to read your question. – Some programmer dude Jun 06 '18 at 01:49
  • what is `cuentas` declared as ? – Scary Wombat Jun 06 '18 at 02:00
  • 1
    what really you wanna do because you miss with alot for definitions – mooga Jun 06 '18 at 02:35
  • `cuentas[ncuenta].setMov(new Movimiento(monto,tipo,operacion))` **does not** return an array object. So `cuentas[ncuenta].setMov(new Movimiento(monto,tipo,operacion))[0]` would be erroneous.. – Romeo Sierra Jun 06 '18 at 02:41

3 Answers3

0

I understand what you are trying to do. You want to set a single Movimiento object to a specific position in your Movimiento[] mov array and retrieve it, but currently what you have in setMov() just updates the mov variable with a new array object.

Update your setMov() like this:

public void setMov(Movimiento mov, int position) {
    this.mov[position] = mov;
}

and your getMov() should also have a position specified to retrieve a single Movimiento Object from your mov array

public Movimiento getMov(int position) {
    return this.mov[position];
}

now you can set and retrieve items easily.

cuentas[ncuenta].setMov(new Movimento(mono,tipo, operacion), position);
cuentas[ncuenta].getMov(position);
0

Two major problems.

  1. cuentas[ncuenta].setMov(new Movimiento(monto,tipo,operacion)) is fundamentally wrong, because you are passing an instance of Movimiento to setMov(), which is expecting an instance of Movimiento[].

  2. cuentas[ncuenta].setMov(new Movimiento(monto,tipo,operacion)) won't return an array reference. Because setMov() is void.

Seems like you have confused the concept of setters. Remember setMov() will only set an array reference to a variable. You can't set the indices of the array with that.

How to correct it? We can't really tell anything on that because, neither the scenario, nor your problem is clear. For instance you have not mentioned what this cuentas[ncuenta] is about. Assuming that cuentas[ncuenta] refers to an instance of Cuenta and you need to assign a value to an index of an array, I would suggest something like follows.

cuentas[ncuenta].setMov(new Movimiento[10]); // A new array of type Movimiento
cuentas[ncuenta].getMov()[0] = new Movimiento(monto,tipo,operacion); // Extract the reference of 0th index of the array and assign the value needed.

NOTE AGAIN before actually trying this out, read about getters and setters in Java in this SO answer.

Romeo Sierra
  • 1,666
  • 1
  • 17
  • 35
-1

You must receive an array of Movimento[], but you didn't initialize an Array of Movimiento