I created a State class with name, acronym and capital and a Country with name, capital and states - an ArrayList of type State.
I want to create a method insertState (State e) that before adding the state into a Country, checks if it has not already been added before and displays a message. I thought of it this way:
public void adicionaEstado(Estado e){
for(int i=0; i < estados.size(); i++){
if (estados.get(i).getNome().equals(e.getNome())){
System.out.println("Esse estado já foi adicionado ao país anteriormente.");
}
else{
estados.add(e);
System.out.println("Estado adicionado.");
}
}
}
Although no error is made, you are not adding the elements. Also, I would like to know what the signature of the method would be for me to be able to return these messages.