How to create an Object ArrayList and iterate over it?

0

I need to create a ArrayList of this object, but within its own method, and have a method to return the array with Iterator .

I'm trying to do it this way, but nothing happens.

MAIN.JAVA

package banco;

import java.util.Scanner;

public class main {

    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        int choose = 0, contador = 0;

        while(contador != -1) {
            System.out.println("(1) - CRIE UMA CONTA \n"
                     + "(2) - VISUALIZAR SALDO DA CONTA CORRENTE \n"
                     + "(3) - VISUALIZAR SALDO DA POUPANCA \n"
                     + "(4) - RETIRAR DINHEIRO DA CONTA CORRENTE \n"
                     + "(5) - RETIRAR DINHEIRO DA CONTA POUPANCA \n"
                     + "(6) - APLICAR DINHEIRO NA CONTA CORRENTE \n"
                     + "(7) - APLICAR DINHEIRO NA CONTA POUPANCA \n");
            choose = entrada.nextInt();

            switch(choose) {
            case 1:
                System.out.println("OPÇÃO ESCOLHIDA (1)");
                Banco nomeBanco = new Banco("UVA", "1234");
                nomeBanco.criarBanco();


            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            default:
                System.out.println("NÃO EXISTE ESTA OPÇÃO, TENTE NOVAMENTE...");
            }

        }

    }
}

BANK.JAVA

/**
 * 
 */
package banco;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author saadt
 *
 */

public class Banco {
    private String nome;
    private String code;
    private ArrayList<Banco> bancos;
    Conta conta;

    public Banco(String nome, String code) {
        this.nome = nome;
        this.code = code;
        this.bancos = new ArrayList<Banco>();
    }

    public void criarBanco(){
        ArrayList<Banco> arrBancos = new ArrayList<Banco>();
        Banco novoBanco = new Banco("UVA", "1234");
        arrBancos.add(novoBanco);

        System.out.println(arrBancos);
    }




}
    
asked by anonymous 02.03.2018 / 02:09

1 answer

-1

I'll respond to you in stages:

  • Take your main menu from within while , as it will repeat at any time within your code. This pollutes the user interface.
  • In your menu it is not mentioned that -1 is the output of your program.
  • To iterate the contents of your ArrayList , you can use the lambda concept (Java 8 - examples in this link: link ). Home An example:

    arrBancos.forEach(ab-> System.out.println(ab.code + " - " + ab.nome));
    
  • You do not need to declare two ArraysList in your Bank class. You can declare it along with the other attributes, and in the method you can receive the attributes of the accounts as parameters and insert them into ArrayList . Home In your code main allow the option to enter the values of the name and account so you can move to your method and add your own list.
  • After added, you can iterate using lambda, as I described in item 3 . I hope I have helped if you need can post your question.

        
    02.03.2018 / 14:42