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()); } } } public static Devise devise(String nom) throws DeviseInconnueException { for (Devise d : DEVISES) { if (d.nom().equals(nom)) { return d; } } throw new DeviseInconnueException(nom); } public static boolean deviseTraitee(String nom) { try { devise(nom); return true; } catch (DeviseInconnueException e) { return false; } } private static void ajouterDevise(String nom, double tauxDeChange, boolean variable) throws DeviseExistanteException { try { Devise d = devise(nom); throw new DeviseExistanteException(d); } catch (DeviseInconnueException e) { DEVISES.add(variable ? new DeviseVariable(nom, tauxDeChange) : new DeviseConstante(nom, tauxDeChange)); } } 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()]); } }