package conversion; import java.util.ArrayList; import java.util.List; /** * La classe Conversion permet de convertir une somme en Euro. * * Version 6 */ public class Conversion { /* * Cette version rgle le problme de l'existance de devises taux fixe. * * Il reste toujours un problme d'efficacit de la mthode devise(String) ; * */ private static final List DEVISES = new ArrayList(); 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()); } } } private static Devise chercherDevise(String nom) { for (Devise d : DEVISES) { if (d.nom().equals(nom)) { return d; } } return null; } public static Devise devise(String nom) throws DeviseInconnueException { Devise d = chercherDevise(nom); if (d == null) { throw new DeviseInconnueException(nom); } return d; } public static boolean deviseTraitee(String nom) { return (chercherDevise(nom) != null); } private static void ajouterDevise(String nom, double tauxDeChange, boolean variable) throws DeviseExistanteException { Devise d = chercherDevise(nom); if (d == null) DEVISES.add(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.toArray(new DeviseAbstraite[DEVISES.size()]); } }