001 package fr.isae.bank;
002
003 import java.util.ArrayList;
004 import java.util.HashMap;
005 import java.util.Iterator;
006
007 /**
008 * <code>Historique</code> gere un historique des debits et credits avec
009 * leur montant et l'intitule de l'operation. On peut retrouver un montant
010 * d'une operation avec l'intitule de l'operation.
011 *
012 * <p> Comme <code>Historique</code> realise <code>Iterable</code>, on peut
013 * utiliser une boucle <code>for</code> pour parcourir les intitules
014 * disponibles dans un historique : <br/>
015 *
016 * <code>
017 * for (String intitule : historique) { ... }
018 * </code>
019 *
020 * @author <a href="mailto:cregut@enseeiht.fr">Xavier Cregut</a>
021 * @author <a href="mailto:garion@isae.fr">Christophe Garion</a>
022 * @version 1.0
023 */
024 public class Historique implements Iterable<String> {
025
026 private HashMap<String, Double> operations;
027 private int longMaxIntitule;
028 private int longMaxMontantC;
029 private int longMaxMontantD;
030 private ArrayList<String> intitules;
031 private int numTransAnonyme;
032
033 /**
034 * Construire un historique vide.
035 *
036 */
037 public Historique() {
038 this.operations = new HashMap<String, Double>();
039 this.longMaxIntitule = 1;
040 this.longMaxMontantC = 7;
041 this.longMaxMontantD = 7;
042 this.intitules = new ArrayList<String>();
043 this.numTransAnonyme = 1;
044 }
045
046 /**
047 * Le nombre d'operations enregistrees dans l'historique.
048 *
049 * @return le nombre d'operations enregistrees
050 */
051 public int getNbValeurs() {
052 return this.operations.size();
053 }
054
055 /**
056 * Enregistrer une nouvelle information dans l'historique. Si
057 * l'intitule fourni est une chaine de caracteres vide, on utilise comme intitule
058 * "operation n" avec n un entier incremente a chaque fois.
059 *
060 * @param intitule l'intitule de l'operation
061 * @param montant le montant de l'operation. Si le montant est
062 * negatif, c'est un debit
063 */
064 public void enregistrer(String intitule, double montant) {
065 String intAux = null;
066
067 if (intitule.equals("")) {
068 intAux = "operation " + this.numTransAnonyme;
069 this.numTransAnonyme++;
070 } else {
071 intAux = intitule;
072 }
073
074 this.operations.put(intAux, montant);
075 this.intitules.add(intAux);
076
077 this.longMaxIntitule = Math.max(this.longMaxIntitule,
078 intAux.length());
079
080 if (montant > 0) {
081 this.longMaxMontantC = Math.max(this.longMaxMontantC,
082 Double.toString(montant).length());
083 } else {
084 this.longMaxMontantD = Math.max(this.longMaxMontantD,
085 Double.toString(montant).length() - 1);
086 }
087 }
088
089 /**
090 * La longueur maximale des intitules contenus dans l'historique.
091 * Peut servir lors de l'affichage des releves.
092 *
093 * @return la longueur max. des intitules de l'historique
094 */
095 public int getLongMaxIntitule() {
096 return this.longMaxIntitule;
097 }
098
099 /**
100 * La longueur maximale (en terme d'affichage) des credit contenus dans
101 * l'historique. Peut servir lors de l'affichage des releves.
102 *
103 * @return la longueur max. des credits de l'historique
104 */
105 public int getLongMaxMontantC() {
106 return this.longMaxMontantC;
107 }
108
109 /**
110 * La longueur maximale (en terme d'affichage) des devuts contenus dans
111 * l'historique. Peut servir lors de l'affichage des releves.
112 *
113 * @return la longueur max. des debits de l'historique
114 */
115 public int getLongMaxMontantD() {
116 return this.longMaxMontantD;
117 }
118
119 /**
120 * Renvoie le montant d'une operation a partir de son intitule.
121 *
122 * @param intitule l'intitule de l'operation dont on veut le montant
123 * @return le montant de l'operation sous forme d'une instance de
124 * <code>Double</code>
125 */
126 public Double get(String intitule) {
127 return this.operations.get(intitule);
128 }
129
130 /**
131 * Un iterateur sur l'ensemble des intitules des operations.
132 *
133 * @return une instance de <code>Iterator<String></code> permettant
134 * de parcourir l'ensemble des intitules des operations
135 */
136 public Iterator<String> iterator() {
137 return this.intitules.iterator();
138 }
139 }
|