88 lines
2.8 KiB
Java
88 lines
2.8 KiB
Java
|
|
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.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|