O exercicio pede:
a) Um método que verifique a igualdade semântica entre duas pessoas (as pessoas são
iguais se possuem o mesmo nome e a mesma mãe);
b) Um método que verifique se duas pessoas são irmãs;
c) Um método que verifique se uma pessoa é antecessora da pessoa que recebeu a
mensagem (é seu pai ou sua mãe, ou antecessor do pai ou antecessor da mãe).
Eu estava tentando fazer desse jeito
package teste1;
import java.io.*;
import java.util.*;
public class Pessoa {
Scanner in = new Scanner(System.in);
File arquivo = new File("arquivo.txt");
//Atributos
private String nome;
private String mae;
private String pai;
//Metodos getters e setters
Pessoa() {
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getMae() {
return mae;
}
public void setMae(String mae) {
this.mae = mae;
}
public String getPai() {
return pai;
}
public void setPai(String pai) {
this.pai = pai;
}
public Pessoa(String nome) {
this.nome = nome;
this.mae = null;
this.pai = null;
}
public Pessoa(String nome, String mae, String pai) {
this.nome = nome;
this.mae = mae;
this.pai = pai;
}
void igualdadeSemantica() {
try {
System.out.println("Entre com o nome:");
setNome(in.nextLine());
System.out.println("Entre com o nome da mãe: ");
setMae(in.nextLine());
System.out.println("Entre com o nome do pai");
setPai(in.nextLine());
if (arquivo.exists() == false) {
arquivo.createNewFile();
}
FileWriter fw = new FileWriter(arquivo, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(getNome().toUpperCase()+";"+getMae().toUpperCase()+";"+getPai().toUpperCase());
bw.newLine();
bw.close();
fw.close();
}catch (IOException ex) {
}
//-------------------------------------------------------------------------------------------------------------------------
try{
FileReader fr = new FileReader(arquivo);
BufferedReader br = new BufferedReader(fr);
String linha = br.readLine();
ArrayList<String> salvar = new ArrayList();
String nome1 = getNome().toUpperCase();
String nome2 = getMae().toUpperCase();
while (linha != null) {
if (linha.contains(nome1) && linha.contains(nome2) ) {
System.out.println("Nome já cadastrado");
} else {
salvar.add(linha);
}
linha = br.readLine();
}
br.close();
fr.close();
FileWriter fw2 = new FileWriter(arquivo, true);
fw2.close();
FileWriter ft = new FileWriter(arquivo);
BufferedWriter bt = new BufferedWriter(ft);
for (int i = 0; i < salvar.size(); i++) {
bt.write(salvar.get(i));
bt.newLine();
}
bt.close();
ft.close();
} catch (IOException ex) {
}
}
}
No While que eu estou tentando tentando ver, mas não está dando certo, se eu não colocar o while, o programa guada de boa, ou dependendo a logica que eu colocar tbm, tipo do lugar de contains collocar o equals, mas mesmo assim eu não estou entendendo a logica, sou novo em java . Se alguém poder me ajudar eu agradeceria