Files
DEV/DEV3.2/TP08/02_Authentification/ABR.java
Simoes Lukas b2ebfe848e ANDROID WE
2026-01-22 14:28:35 +01:00

112 lines
2.4 KiB
Java

public class ABR {
private String valeur;
private ABR filsGauche;
private ABR filsDroit;
private int compteur = 0;
public ABR() {
this.valeur = null;
this.filsDroit = null;
this.filsGauche = null;
}
public ABR(String valeur) {
this.valeur = valeur;
}
public boolean authentifier(String id, String password) {
// Si le noeud courant est vide, aller dans le fils gauche
if (this.valeur == null) {
if (this.filsGauche != null) {
return this.filsGauche.authentifier(id, password);
}
return false; // rien dans l'arbre
}
// Si on trouve l'id au noeud courant
if (this.valeur.equals(id)) {
if (this.filsDroit != null && this.filsDroit.valeur.equals(password)) {
return true;
}
return false;
}
// Sinon, continuer à gauche
if (this.filsGauche != null) {
return this.filsGauche.authentifier(id, password);
}
return false;
}
public void supprimer(String id) {
if (this.filsGauche != null) {
if (this.filsGauche.valeur.equals(id)) {
if (this.filsGauche.filsGauche != null) {
this.filsGauche = this.filsGauche.filsGauche;
} else {
this.filsGauche = null;
}
System.out.println("Suppression effectuée.");
} else {
this.filsGauche.supprimer(id);
}
}
}
public void ajouterUtilisateur(String id, String password) {
if (this.filsGauche != null && this.filsGauche.equals(id)) {
System.out.println("Valeur déjà présente.");
return;
}
if (this.filsGauche != null) {
this.filsGauche.ajouterUtilisateur(id, password);
} else {
this.filsGauche = new ABR(id);
this.filsGauche.filsDroit = new ABR(password);
}
}
public boolean estPresent(String id) {
if (this.filsGauche == null) {
return false;
}
if (this.compteur == 0) {
this.compteur++;
this.filsGauche.estPresent(id);
}
if (this.compteur != 0 && this.valeur == null) {
return false;
}
if (this.valeur.equals(id)) {
return true;
} else {
return this.filsGauche.estPresent(id);
}
}
public String toString() {
String aRenvoyer = "";
if (this.filsDroit == null && this.filsGauche == null) {
return "";
} else {
if (this.filsGauche == null) {
return "";
} else {
aRenvoyer += "Identifiant : " + this.filsGauche.valeur + " / Mot de passe : " + this.filsGauche.filsDroit.valeur + "\n";
return aRenvoyer + this.filsGauche.toString();
}
}
}
}