package conversion; import java.util.HashMap; import java.util.Map; /** * La classe Conversion permet de convertir une somme en Euro. * * Version 7 */ public class Conversion { /* * Cette version règle le problème de l'efficacité de la recherche d'une * devise grâce à l'utilisation d'une table de hachage. */ private static final Map DEVISES = new HashMap(); static { String[] noms = { "Franc", "Mark", "Dollard", "Euro" }; double[] taux = { 6.55957, 1.95583, 1.2713, 1. }; boolean[] variables = { false, false, true, true }; for (int i = 0; i < noms.length; ++i) { try { ajouterDevise(noms[i], taux[i], variables[i]); } catch (DeviseExistanteException e) { throw new Error(e.getMessage()); } } } public static Devise devise(String nom) throws DeviseInconnueException { Devise d = DEVISES.get(nom); if (d == null) { throw new DeviseInconnueException(nom); } return d; } public static boolean deviseTraitee(String nom) { return DEVISES.containsKey(nom); } private static void ajouterDevise(String nom, double tauxDeChange, boolean variable) throws DeviseExistanteException { Devise d = DEVISES.get(nom); if (d == null) DEVISES.put(nom, variable ? new DeviseVariable(nom, tauxDeChange) : new DeviseConstante(nom, tauxDeChange)); else throw new DeviseExistanteException(d); } public static void ajouterDeviseConstante(String nom, double tauxDeChange) throws DeviseExistanteException { ajouterDevise(nom, tauxDeChange, false); } public static void ajouterDeviseVariable(String nom, double tauxDeChange) throws DeviseExistanteException { ajouterDevise(nom, tauxDeChange, true); } public static Devise[] devisesTraitees() { return DEVISES.values().toArray(new DeviseAbstraite[DEVISES.size()]); } }