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);
}
}
Why would a bank have a list of banks as an instance property? And what is the intention of the method
criarBanco
insideBanco
which creates aBanco
and inserts it into a method variableArrayList<Banco>
that soon is discarded? I could not make sense of your code, especially the classBanco
– Jefferson Quesado
@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?
– Thiago Saad
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.
– user28595
@Articuno, I just answered above, could help me regarding the code about what I said?
– Thiago Saad
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 ?
– Isac
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.
– user28595
@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?
– Thiago Saad
Define "good practice". Because it doesn’t make much sense to me.
– user28595
@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 performbancos.add(banco)
inside your body. That’s what you need?– Gustavo Cinque