diff --git a/TP3_GUI/src/gui/ButtonEditor.java b/TP3_GUI/src/gui/ButtonEditor.java index 949e620..38b3c6e 100644 --- a/TP3_GUI/src/gui/ButtonEditor.java +++ b/TP3_GUI/src/gui/ButtonEditor.java @@ -20,6 +20,36 @@ public class ButtonEditor extends DefaultCellEditor { private boolean clicked; private int row; + /** + * Procura na lista principal a Pessoa correspondente aos valores da linha + * atualmente editada na tabela. + */ + private Pessoa findPessoa(JTable table, int modelRow) { + String nome = table.getModel().getValueAt(modelRow, 0).toString(); + String generoStr = table.getModel().getValueAt(modelRow, 1).toString(); + char genero; + if (generoStr.toLowerCase().startsWith("masculino")) { + genero = 'M'; + } else if (generoStr.toLowerCase().startsWith("feminino")) { + genero = 'F'; + } else { + genero = '?'; + } + String data = table.getModel().getValueAt(modelRow, 2).toString(); + String tele = table.getModel().getValueAt(modelRow, 3).toString(); + + for (Pessoa pessoa : MainFrame.gestor.getLista()) { + boolean nomeEq = pessoa.getNome().equals(nome); + boolean generoEq = pessoa.getGenero() == genero; + boolean dataEq = java.util.Objects.equals(pessoa.getDataNascimento(), data); + boolean teleEq = java.util.Objects.equals(pessoa.getNumeroTelemovel(), tele); + if (nomeEq && generoEq && dataEq && teleEq) { + return pessoa; + } + } + return null; + } + public ButtonEditor(JCheckBox checkBox, JTable table) { super(checkBox); button = new JButton(); @@ -38,15 +68,7 @@ public ButtonEditor(JCheckBox checkBox, JTable table) { JOptionPane.QUESTION_MESSAGE, null, opcoes, opcoes[0]); if (escolha == 0) { - Pessoa p = null; - int i = 0; - for (Pessoa pessoa : MainFrame.gestor.getLista()) { - if (i == modelRow) { - p = pessoa; - break; - } - i++; - } + Pessoa p = findPessoa(table, modelRow); if (p == null) return; // Segurança: não encontrado @@ -85,15 +107,7 @@ public ButtonEditor(JCheckBox checkBox, JTable table) { } else if (escolha == 1) { - Pessoa aRemover = null; - int i = 0; - for (Pessoa pessoa : MainFrame.gestor.getLista()) { - if (i == modelRow) { - aRemover = pessoa; - break; - } - i++; - } + Pessoa aRemover = findPessoa(table, modelRow); if (aRemover != null) { boolean removido = MainFrame.gestor.getLista().remover(aRemover);