Android + DEV 4.4
This commit is contained in:
61
DEV 3.2/TP08/Authentification/Authentification.java
Normal file
61
DEV 3.2/TP08/Authentification/Authentification.java
Normal file
@@ -0,0 +1,61 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class Authentification {
|
||||
public static void main(String[] args) {
|
||||
Map<String, String> loginMap = new TreeMap<>();
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
try {
|
||||
while (true) {
|
||||
String command = br.readLine();
|
||||
String[] cargs = command.split(" ");
|
||||
|
||||
switch (cargs[0].toLowerCase()) {
|
||||
case "add":
|
||||
if (loginMap.containsKey(cargs[1])) {
|
||||
System.err.println("Utilisateur \"" + cargs[1] + "\" déjà existant.");
|
||||
} else {
|
||||
loginMap.put(cargs[1], cargs[2]);
|
||||
System.out.println("Utilisateur \"" + cargs[1] + "\" ajouté.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "auth":
|
||||
if (loginMap.containsKey(cargs[1])) {
|
||||
if (loginMap.get(cargs[1]).compareTo(cargs[2]) == 0) {
|
||||
System.out.println("Utilisateur \"" + cargs[1] + "\" reconnu.");
|
||||
} else {
|
||||
System.err.println("Utilisateur \"" + cargs[1] + "\" non reconnu.");
|
||||
}
|
||||
} else {
|
||||
System.err.println("Utilisateur \"" + cargs[1] + "\" non reconnu.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "del":
|
||||
if (loginMap.containsKey(cargs[1])) {
|
||||
loginMap.remove(cargs[1]);
|
||||
System.out.println("Utilisateur \"" + cargs[1] + "\" retiré.");
|
||||
} else {
|
||||
System.err.println("Utilisateur \"" + cargs[1] + "\" non reconnu.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "quit":
|
||||
System.out.println("Au revoir");
|
||||
return;
|
||||
|
||||
default:
|
||||
System.out.println("Commande inconnue");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
DEV 3.2/TP08/Authentification/StringComparator.java
Normal file
11
DEV 3.2/TP08/Authentification/StringComparator.java
Normal file
@@ -0,0 +1,11 @@
|
||||
public class StringComparator implements TreeComparator<String> {
|
||||
|
||||
@Override
|
||||
public int compare(String a, String b) {
|
||||
int comparing = a.compareTo(b);
|
||||
|
||||
if (comparing == 0) return 0;
|
||||
return comparing /= Math.abs(comparing);
|
||||
}
|
||||
|
||||
}
|
||||
4
DEV 3.2/TP08/Authentification/TreeComparator.java
Normal file
4
DEV 3.2/TP08/Authentification/TreeComparator.java
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
public interface TreeComparator<T> {
|
||||
public int compare(T a, T b);
|
||||
}
|
||||
96
DEV 3.2/TP08/Authentification/TreeMap.java
Normal file
96
DEV 3.2/TP08/Authentification/TreeMap.java
Normal file
@@ -0,0 +1,96 @@
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class TreeMap<K,V> implements Map<K,V> {
|
||||
|
||||
private TreeNode<K,V> root;
|
||||
private TreeComparator<K> comparator;
|
||||
|
||||
public TreeMap() {
|
||||
|
||||
}
|
||||
|
||||
public TreeComparator<K> getComparator() {
|
||||
return comparator;
|
||||
}
|
||||
|
||||
public void setComparator(TreeComparator<K> comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
root = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
if (root == null) return false;
|
||||
else return root.containsKey((K)key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
if (root == null) return null;
|
||||
return root.get((K)key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) {
|
||||
if (root == null) {
|
||||
root = new TreeNode<K,V>(this, key, value);
|
||||
return null;
|
||||
}
|
||||
|
||||
return root.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends K, ? extends V> m) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(Object key) {
|
||||
if (root == null) return null;
|
||||
else if (comparator.compare(root.getKey(), (K)key) == 0) {
|
||||
V value = root.getValue();
|
||||
root = null;
|
||||
return value;
|
||||
} else {
|
||||
return root.remove((K)key);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
105
DEV 3.2/TP08/Authentification/TreeNode.java
Normal file
105
DEV 3.2/TP08/Authentification/TreeNode.java
Normal file
@@ -0,0 +1,105 @@
|
||||
public class TreeNode<K,V> {
|
||||
|
||||
private K key;
|
||||
private V value;
|
||||
|
||||
private TreeMap<K, V> tree;
|
||||
private TreeNode<K, V> sub;
|
||||
private TreeNode<K, V> top;
|
||||
|
||||
public TreeNode(TreeMap<K, V> tree, K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
int comparison = tree.getComparator().compare(this.key, key);
|
||||
|
||||
if (comparison == -1) {
|
||||
if (sub == null) sub = new TreeNode<K,V>(tree, key, value);
|
||||
else sub.put(key, value);
|
||||
return null;
|
||||
} else if (comparison == 0) {
|
||||
V oldValue = this.value;
|
||||
this.value = value;
|
||||
return oldValue;
|
||||
} else {
|
||||
if (top == null) top = new TreeNode<K,V>(tree, key, value);
|
||||
else top.put(key, value);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
int comparison = tree.getComparator().compare(this.key, key);
|
||||
|
||||
if (comparison == -1) {
|
||||
if (sub == null) return null;
|
||||
else return sub.get(key);
|
||||
} else if (comparison == 0) {
|
||||
return value;
|
||||
} else {
|
||||
if (top == null) return null;
|
||||
else return top.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsKey(K key) {
|
||||
int comparison = tree.getComparator().compare(this.key, key);
|
||||
|
||||
if (comparison == -1) {
|
||||
if (sub == null) return false;
|
||||
else return sub.containsKey(key);
|
||||
} else if (comparison == 0) {
|
||||
return true;
|
||||
} else {
|
||||
if (top == null) return false;
|
||||
else return top.containsKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
public TreeNode<K, V> getSub() {
|
||||
return sub;
|
||||
}
|
||||
|
||||
public TreeNode<K, V> getTop() {
|
||||
return top;
|
||||
}
|
||||
|
||||
public V remove(K key) {
|
||||
int comparison = tree.getComparator().compare(this.key, key);
|
||||
|
||||
if (comparison == -1) {
|
||||
if (sub == null) return null;
|
||||
else {
|
||||
if (tree.getComparator().compare(key, sub.getKey()) == 0) {
|
||||
V value = sub.getValue();
|
||||
sub = null;
|
||||
|
||||
return value;
|
||||
} else {
|
||||
return sub.remove(key);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (top == null) return null;
|
||||
else {
|
||||
if (tree.getComparator().compare(key, top.getKey()) == 0) {
|
||||
V value = top.getValue();
|
||||
top = null;
|
||||
return value;
|
||||
} else {
|
||||
return top.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user