Olá, estou com dúvida no método remover da Classe ListaAssociado abaixo, pois um Associado só pode ser removido se não tiver empréstimos e não estou conseguindo fazer isto. Coloquei a Classe ListaEmprestimo também para análise, agradeço qualquer ajuda…
package biblioteca;
import java.awt.TextArea;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ListaAssociado {
private ArrayList<Associado> itens = new ArrayList<Associado>();
private ListaEmprestimo emprestimos;
public ListaAssociado() {
}
public ListaAssociado(ListaEmprestimo emprestimos) {
this.emprestimos = emprestimos;
}
public ArrayList<Associado> getItens() {
return itens;
}
public void setAssociado(ArrayList<Associado> itens) {
this.itens = itens;
}
public void cadastrar() {
Associado novo = new Associado(emprestimos);
String matricula = JOptionPane.showInputDialog("Digite a Matricula:");
if (matricula.equalsIgnoreCase(novo.getMatricula())) {
JOptionPane.showMessageDialog(null, "Matrícula Já Existe");
return;
}
novo.setMatricula(matricula);
novo.cadastrar();
getItens().add(novo);
}
public Associado consultarAssociado(String matricula) {
for (Associado associado : getItens()) {
if (matricula.equalsIgnoreCase(associado.getMatricula())) {
return associado;
}
}
return null;
}
public Associado consultarEmprestimo(String matricula) {
for (Emprestimo emprestimo : emprestimos.getEmprestimo()) {
for (Associado associado : getItens()) {
if (matricula.equalsIgnoreCase(emprestimo.getMatricula())) {
return associado;
}
}
}
return null;
}
public void remover() {
String matricula = JOptionPane.showInputDialog("Digite Matrícula:");
Associado ass = consultarEmprestimo(matricula);
if (ass == null) {
if (JOptionPane.showConfirmDialog(null,
"Deseja Remover este Associado:\n" + ass.toString(),
"Remover", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE, null) == JOptionPane.OK_OPTION) {
getItens().remove(ass);
}
} else {
if (ass != null) {
JOptionPane.showMessageDialog(null, "Associado Possui Empréstimo!");
}
}
}
public Associado selecionar() {
StringBuilder saida = new StringBuilder();
saida.append("Informe a Matrícula:\n");
for (Associado item : getItens()) {
saida.append(item.getMatricula()).append("-").append(item.getNome()).append("\n");
}
if (saida.toString().isEmpty()) {
return null;
} else {
TextArea janela = new TextArea(saida.toString());
janela.setEditable(false);
String mat = JOptionPane.showInputDialog(janela).toString();
Associado associado = consultarAssociado(mat);
return associado;
}
}
Na Classe ListaEmprestimo:
package biblioteca;
import java.awt.TextArea;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ListaEmprestimo {
private ArrayList<Emprestimo> emprestimos = new ArrayList<Emprestimo>();
private ListaLivro livros;
private ListaAssociado associados;
public ListaEmprestimo(){
}
public ListaLivro getLivros() {
return livros;
}
public void setLivros(ListaLivro livros) {
this.livros = livros;
}
public ListaAssociado getAssociados() {
return associados;
}
public void setAssociados(ListaAssociado associados) {
this.associados = associados;
}
public ListaEmprestimo(ListaLivro livros,ListaAssociado associados) {
this.livros = livros;
this.associados = associados;
}
public ArrayList<Emprestimo> getEmprestimo() {
return emprestimos;
}
public void setEmprestimo(ArrayList<Emprestimo> emprestimos) {
this.emprestimos = emprestimos;
}
public Emprestimo consultarEmprestimo(String matricula) {
for (Emprestimo emprestimo : getEmprestimo()) {
if (matricula.equalsIgnoreCase(emprestimo.getMatricula())) {
return emprestimo;
}
}
return null;
}
public void cadastrar() {
Emprestimo novo = new Emprestimo();
String matricula = JOptionPane.showInputDialog("Digite a Matrícula:");
if (matricula.equalsIgnoreCase(novo.getMatricula())) {
JOptionPane.showMessageDialog(null, "Matrícula Já Existe");
return;
}
novo.setMatricula(matricula);
Associado associado = getAssociados().selecionar();
if(associado != null) novo.getAssociado().add(associado);
do{
Livro livro = getLivros().selecionar();
if(livro != null) novo.getLivro().add(livro);
} while(JOptionPane.showConfirmDialog(null,
"Deseja Escolher Mais Um Livro Para o Associado?\n",
"Empréstimo", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null) == JOptionPane.YES_OPTION);
emprestimos.add(novo);
}