How to create an Object Arraylist and iterate over it?

Asked

Viewed 637 times

0

I need to create a ArrayList of this object, however within its own method, and have a method to return the array with the 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...");
            }

        }

    }
}

JAVA BANK.

/**
 * 
 */
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);
    }




}
  • 1

    Why would a bank have a list of banks as an instance property? And what is the intention of the method criarBanco inside Banco which creates a Banco and inserts it into a method variable ArrayList<Banco> that soon is discarded? I could not make sense of your code, especially the class Banco

  • @Jeffersonquesado, sorry I’m new to Java. I just wanted to encapsulate these methods to make them safe without the risk of being changed. But what I really want to do is this: "I may have several banks, and in these banks may have several accounts, savings or current accounts in each of them". How I do this using Arraylist and using all good practices?

  • 1

    Create a method that returns an item through its position and iterate over it in main. Create another that returns the list size too to be able to iterate correctly. To do this in the specific class, in my view, is a violation of the principle of sole responsibility.

  • @Articuno, I just answered above, could help me regarding the code about what I said?

  • Just a little curiosity, why do you have all the texts of your program in CAPS ? It is a program for people with visual disabilities ?

  • My tip remains the same, for all cases cited (list banks and list accounts). But one thing I wouldn’t do is put the list within the specific classes, because a Bank class should represent only a bank and not a list, the same for the Account class. The main or other main class is who should take care of it.

  • @Articuno, got it! Only here’s the thing, if I put the new Arraylist in the main beauty, I know how to do. But I wanted to make things safer and best-practice. I would like to use Arraylist creating methods, such as: Insert into the given Arraylist by method, take the given Arraylist by method. And this I do not know, this is my doubt. Could help me?

  • Define "good practice". Because it doesn’t make much sense to me.

  • @Thiagosaad Arraylist itself presents the methods you are looking for. What you would need to do is create the same methods in the class you want, and call the Arraylist method in this new method. For example a private void addBanco(Banco banco) will perform bancos.add(banco) inside your body. That’s what you need?

Show 4 more comments

1 answer

-1

I’ll answer to you in stages:

  1. Get your main menu out of the while, because it will repeat every moment inside your code. This pollutes the user interface.
  2. In your menu is not mentioned that -1 is the output of your program.
  3. To iterate your content ArrayList, can use the lambda concept (Java 8 - examples in this link: https://www.mkyong.com/java8/java-8-foreach-examples/).
    An example:

    arrBancos.forEach(ab-> System.out.println(ab.code + " - " + ab.nome));
    
  4. No 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 within the ArrayList.
    In your code main allows the option to enter the values of the name and account so that you can pass to your method and add to your list.

After added, you can iterate using lambda, as described in item 3. I hope I’ve helped, if you need can post your question.

  • 1

    It would be interesting for you to edit the answer by displaying the code with the suggested changes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.