Java. Error calling a class

5

In a university assignment I'm having problems trying one of the classes because it will not let me call her. It does not let me use neither Banco.listarDescybuerto(); nor his.listarDescubierto();

public class Banco {

    Cliente[] cliente;
    Empleado empleado = new Empleado();

    public static void main(String[] args) {

        Empleado empleado = new Empleado();
        Cliente[] cliente;
        cliente = new Cliente[50];

        int longitudArray = 50;

        for (int i = 0; i < (longitudArray); i++) {
            cliente[i] = new Cliente();
        }

        // Crea con valores aleatorios los vvalores del límite descubierto y el
        // saldo
        for (int i = 0; i < (longitudArray); i++) {
            int aleatorioSaldo = (int) Math.floor(Math.random() * (0 - (5000 + 1)) + (5000));
            int aleatorioDescubierto = (int) Math.floor(Math.random() * (0 - (300 + 1)) + (300));
            cliente[i].setSaldo(aleatorioSaldo);
            cliente[i].setLimiteDecubierto(aleatorioDescubierto);
        }

        // Lector del array que tiene el saldo de los clientes
        System.out.println("El saldo de los clientes es: ");
        for (int i = 0; i < longitudArray; i++) {
            System.out.println(i + ". " + cliente[i].getSaldo() + " ");
        }

        // Lector del array del límite descubierto de los clientes
        System.out.println(" ");
        System.out.println("El limite descubierto de los clientes es: ");
        for (int i = 0; i < longitudArray; i++) {
            System.out.println(i + ". " + cliente[i].getLimiteDecubierto() + " ");
        }

        // Prueba de la función de transferir
        System.out.println(" ");
        empleado.transferir(cliente[1], cliente[2], 30);
        System.out.println("El saldo actual del emisor es: " + cliente[1].getSaldo());
        System.out.println("El saldo actual del recepetor es: " + cliente[2].getSaldo());

        Banco.listarDescubierto(); // AQUI ESTÁ EL PROBLEMA
    }

    public Banco(Cliente[] cliente, Empleado empleado) {
        super();
        this.cliente = cliente;
        this.empleado = empleado;
    }

    public Cliente[] getCliente() {
        return cliente;
    }

    public void setCliente(Cliente[] cliente) {
        this.cliente = cliente;
    }

    public Empleado getEmpleado() {
        return empleado;
    }

    public void setEmpleado(Empleado empleado) {
        this.empleado = empleado;
    }

    public void listarDescubierto() {
        for (int i = 0; i < (cliente.length); i++) {
            if (cliente[i].descubierto()) {
                System.out.println(
                        "El saldo esta descubierto, el saldo de " + cliente[i] + " es " + cliente[i].getSaldo());
            }

        }
    }
}
    
asked by Alejandro 18.05.2017 в 18:14
source

3 answers

4

To be able to access this method, you must create an instance of the main class Bank and what is not a static method and then access the method.

Banco sol = new Banco ();
sol.listarDescubierto();

The way it presents will work as long as the method is static , that is

public static void listarDescubierto() {  ... }
/* Para acceder la forma */
listarDescubierto();
/* O */
Banco.listarDescubierto();
    
answered by 18.05.2017 в 18:18
2

You have to define as static the method so that it can be accessed in the way you want ( Banco.listarDescubierto(); ):

  public static void listarDescubierto() {
        for (int i = 0; i < (cliente.length); i++) {
            if (cliente[i].descubierto()) {
                System.out.println(
                        "El saldo esta descubierto, el saldo de " + cliente[i] + " es " + cliente[i].getSaldo());
            }

        }
    }

Otherwise, if the method is not defined as static, you have to make an instance to access the method:

 Banco banco = new Banco ();
 banco.listarDescubierto();
    
answered by 18.05.2017 в 18:29
1

You must create the bank object instance as follows Banco banco = new Banco();

In object-oriented programming, an object is an instance of a class.

In your code, you would replace Banco.listarDescubierto(); with banco.listarDescubierto();

Since if you have to be like your example, you return the static listarDescubierto() method:

 public static void listarDescubierto() {
        for (int i = 0; i < (cliente.length); i++) {
            if (cliente[i].descubierto()) {
                System.out.println(
                        "El saldo esta descubierto, el saldo de " + cliente[i] + " es " + cliente[i].getSaldo());
            }

        }
    }

Although in practice it would not be the best.

    
answered by 18.05.2017 в 22:47