fin de arbre suite
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,87 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Authentification {
|
||||
// Dictionnaire pour stocker les identifiants et mots de passe
|
||||
private HashMap<String, String> users;
|
||||
|
||||
public Authentification() {
|
||||
users = new HashMap<>();
|
||||
}
|
||||
|
||||
// Ajout d'un utilisateur
|
||||
public void addUser(String username, String password) {
|
||||
if (users.containsKey(username)) {
|
||||
System.out.println("Utilisateur \"" + username + "\" existe déjà.");
|
||||
} else {
|
||||
users.put(username, password);
|
||||
System.out.println("Utilisateur \"" + username + "\" ajouté");
|
||||
}
|
||||
}
|
||||
|
||||
// Suppression d'un utilisateur
|
||||
public void removeUser(String username) {
|
||||
if (users.remove(username) != null) {
|
||||
System.out.println("Utilisateur \"" + username + "\" retiré");
|
||||
} else {
|
||||
System.out.println("Utilisateur \"" + username + "\" non trouvé.");
|
||||
}
|
||||
}
|
||||
|
||||
// Authentification d'un utilisateur
|
||||
public void authenticate(String username, String password) {
|
||||
if (users.containsKey(username) && users.get(username).equals(password)) {
|
||||
System.out.println("Utilisateur \"" + username + "\" reconnu");
|
||||
} else {
|
||||
System.out.println("Utilisateur \"" + username + "\" non reconnu");
|
||||
}
|
||||
}
|
||||
|
||||
// Main pour interagir avec le programme
|
||||
public static void main(String[] args) {
|
||||
Authentification auth = new Authentification();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
System.out.print("> ");
|
||||
String command = scanner.nextLine();
|
||||
String[] parts = command.split(" ");
|
||||
|
||||
if (parts.length == 0) continue;
|
||||
|
||||
switch (parts[0].toLowerCase()) {
|
||||
case "add":
|
||||
if (parts.length == 3) {
|
||||
auth.addUser(parts[1], parts[2]);
|
||||
} else {
|
||||
System.out.println("Usage : add <username> <password>");
|
||||
}
|
||||
break;
|
||||
|
||||
case "del":
|
||||
if (parts.length == 2) {
|
||||
auth.removeUser(parts[1]);
|
||||
} else {
|
||||
System.out.println("Usage : del <username>");
|
||||
}
|
||||
break;
|
||||
|
||||
case "auth":
|
||||
if (parts.length == 3) {
|
||||
auth.authenticate(parts[1], parts[2]);
|
||||
} else {
|
||||
System.out.println("Usage : auth <username> <password>");
|
||||
}
|
||||
break;
|
||||
|
||||
case "quit":
|
||||
System.out.println("Au revoir");
|
||||
scanner.close();
|
||||
return;
|
||||
|
||||
default:
|
||||
System.out.println("Commande non reconnue.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,140 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Authentification {
|
||||
|
||||
private Node root;
|
||||
|
||||
// Ajouter un utilisateur
|
||||
public void addUser(String username, String password) {
|
||||
root = addUserRec(root, username, password);
|
||||
System.out.println("Utilisateur \"" + username + "\" ajouté");
|
||||
}
|
||||
|
||||
private Node addUserRec(Node node, String username, String password) {
|
||||
if (node == null) {
|
||||
return new Node(username, password);
|
||||
}
|
||||
if (username.compareTo(node.username) < 0) {
|
||||
node.left = addUserRec(node.left, username, password);
|
||||
} else if (username.compareTo(node.username) > 0) {
|
||||
node.right = addUserRec(node.right, username, password);
|
||||
} else {
|
||||
node.password = password; // Mise à jour du mot de passe si utilisateur existe
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// Supprimer un utilisateur
|
||||
public void removeUser(String username) {
|
||||
root = removeUserRec(root, username);
|
||||
}
|
||||
|
||||
private Node removeUserRec(Node node, String username) {
|
||||
if (node == null) {
|
||||
System.out.println("Utilisateur \"" + username + "\" non trouvé.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (username.compareTo(node.username) < 0) {
|
||||
node.left = removeUserRec(node.left, username);
|
||||
} else if (username.compareTo(node.username) > 0) {
|
||||
node.right = removeUserRec(node.right, username);
|
||||
} else {
|
||||
// Cas où l'utilisateur est trouvé
|
||||
if (node.left == null){
|
||||
System.out.println("Utilisateur \"" + username + "\" retiré");
|
||||
return node.right;
|
||||
}
|
||||
if (node.right == null){
|
||||
System.out.println("Utilisateur \"" + username + "\" retiré");
|
||||
return node.left;
|
||||
}
|
||||
|
||||
// Remplacer par le successeur in-order
|
||||
Node minNode = findMin(node.right);
|
||||
node.username = minNode.username;
|
||||
node.password = minNode.password;
|
||||
node.right = removeUserRec(node.right, minNode.username);
|
||||
}
|
||||
System.out.println("Utilisateur \"" + username + "\" retiré");
|
||||
return node;
|
||||
}
|
||||
|
||||
private Node findMin(Node node) {
|
||||
while (node.left != null) {
|
||||
node = node.left;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// Authentification d’un utilisateur
|
||||
public void authenticate(String username, String password) {
|
||||
Node user = findUser(root, username);
|
||||
if (user != null && user.password.equals(password)) {
|
||||
System.out.println("Utilisateur \"" + username + "\" reconnu");
|
||||
} else {
|
||||
System.out.println("Utilisateur \"" + username + "\" non reconnu");
|
||||
}
|
||||
}
|
||||
|
||||
private Node findUser(Node node, String username) {
|
||||
if (node == null){
|
||||
return null;
|
||||
}
|
||||
if (username.equals(node.username)){
|
||||
return node;
|
||||
}
|
||||
if (username.compareTo(node.username) < 0) {
|
||||
return findUser(node.left, username);
|
||||
}
|
||||
return findUser(node.right, username);
|
||||
}
|
||||
|
||||
// Main pour le test
|
||||
public static void main(String[] args) {
|
||||
Authentification auth = new Authentification();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
System.out.print("> ");
|
||||
String command = scanner.nextLine();
|
||||
String[] parts = command.split(" ");
|
||||
|
||||
if (parts.length == 0) continue;
|
||||
|
||||
switch (parts[0].toLowerCase()) {
|
||||
case "add":
|
||||
if (parts.length == 3) {
|
||||
auth.addUser(parts[1], parts[2]);
|
||||
} else {
|
||||
System.out.println("Usage : add <username> <password>");
|
||||
}
|
||||
break;
|
||||
|
||||
case "del":
|
||||
if (parts.length == 2) {
|
||||
auth.removeUser(parts[1]);
|
||||
} else {
|
||||
System.out.println("Usage : del <username>");
|
||||
}
|
||||
break;
|
||||
|
||||
case "auth":
|
||||
if (parts.length == 3) {
|
||||
auth.authenticate(parts[1], parts[2]);
|
||||
} else {
|
||||
System.out.println("Usage : auth <username> <password>");
|
||||
}
|
||||
break;
|
||||
|
||||
case "quit":
|
||||
System.out.println("Au revoir");
|
||||
scanner.close();
|
||||
return;
|
||||
|
||||
default:
|
||||
System.out.println("Commande non reconnue.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre_suite/Authentification/auth_sans_API/Node.class
Normal file
BIN
DEV3.2/arbre_suite/Authentification/auth_sans_API/Node.class
Normal file
Binary file not shown.
11
DEV3.2/arbre_suite/Authentification/auth_sans_API/Node.java
Normal file
11
DEV3.2/arbre_suite/Authentification/auth_sans_API/Node.java
Normal file
@@ -0,0 +1,11 @@
|
||||
// Classe interne pour représenter un nœud de l'arbre
|
||||
public class Node {
|
||||
String username;
|
||||
String password;
|
||||
Node left, right;
|
||||
|
||||
Node(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user