CompteCourant.java
001 package fr.isae.bank;
002 
003 /**
004  * Un compte courant est un compte simple avec un historique des operations
005  * effectuees.
006  *
007  @author <a href="mailto:cregut@enseeiht.fr">Xavier Cregut</a>
008  @author <a href="mailto:garion@isae.fr">Christophe Garion</a>
009  @version 1.0
010  */
011 public class CompteCourant extends CompteSimple {
012 
013     private Historique historique;
014     private int longLigneReleve;
015     private static String titulaireRel = "Titulaire : ";
016 
017     /**
018      * Cree un nouveau <code>CompteCourant</code> avec un montant initial.
019      *
020      @param titulaire le titulaire du compte
021      @param depotInitial le montant initial
022      */
023     public CompteCourant(Personne titulaire, double depotInitial) {
024         super(titulaire, 0);
025         this.historique = new Historique();
026         this.crediter("depot initial", depotInitial);
027         this.longLigneReleve = Math.max((titulaireRel + titulaire).length(),
028                                         this.historique.getLongMaxIntitule() +
029                                         this.historique.getLongMaxMontantC() +
030                                         this.historique.getLongMaxMontantD() 9);
031     }
032 
033     /**
034      * Cree un nouveau <code>CompteCourant</code> dont le solde est nul.
035      *
036      @param titulaire le titulaire du comtpe
037      */
038     public CompteCourant(Personne titulaire) {
039         this(titulaire, 0);
040     }
041 
042     /**
043      * L'historique associe au compte.
044      *
045      @return une instance de <code>Historique</code> correspondant
046      *         a l'historique du compte
047      */
048     Historique getHistorique() {
049         return this.historique;
050     }
051 
052     /**
053      <code>crediter</code> credite le compte du montant fourni (en euros) et
054      * enregistre l'operation. Il n'y a pas d'intitule.
055      *
056      @param montant un <code>double</code> qui est le montant a crediter
057      */
058     @Override public void crediter(double montant) {
059         this.crediter("", montant);
060     }
061 
062     /**
063      <code>debiter</code> debite le compte du montant fourni (en euros) et
064      * enregistre l'operation. Il n'y a pas d'intitule.
065      *
066      @param montant un <code>double</code> qui est le montant a debiter
067      */
068     @Override public void debiter(double montant) {
069         this.debiter("", montant);
070     }
071 
072     /**
073      <code>crediter</code> credite le compte du montant fourni (en euros) et
074      * enregistre l'operation. Elle est declaree final car elle est utilisee dans
075      *  le constructeur.
076      *
077      @param intitule une instance de <code>String</code> qui est l'intitule
078      *                 de l'operation
079      @param montant un <code>double</code> qui est le montant a crediter
080      */
081     public final void crediter(String intitule, double montant) {
082         super.crediter(montant);
083         this.historique.enregistrer(intitule, montant);
084     }
085 
086     /**
087      <code>debiter</code> debite le compte du montant fourni (en euros) et
088      * enregistre l'operation.
089      *
090      @param intitule une instance de <code>String</code> qui est l'intitule
091      *                 de l'operation
092      @param montant un <code>double</code> qui est le montant a crediter
093      */
094     public void debiter(String intitule, double montant) {
095         super.debiter(montant);
096         this.historique.enregistrer(intitule, -montant);
097     }
098 
099     /**
100      <code>editerReleve</code> edite un releve du compte.
101      *
102      */
103     public void editerReleve() {
104         double montant = 0;
105         String fc = "s";
106         String fd = "s";
107 
108         this.longLigneReleve = Math.max(this.longLigneReleve,
109                                         this.historique.getLongMaxIntitule() +
110                                         this.historique.getLongMaxMontantC() +
111                                         this.historique.getLongMaxMontantD() 10);
112 
113         int longMaxIntitule = this.historique.getLongMaxIntitule();
114         int longMaxMontantC = this.historique.getLongMaxMontantC();
115         int longMaxMontantD = this.historique.getLongMaxMontantD();
116 
117         // on affiche l'entete
118         this.printEnteteReleve(longMaxIntitule,
119                                longMaxMontantC,
120                                longMaxMontantD);
121 
122         // on affiche l'historique des operations
123         for (String intitule : this.historique) {
124             montant = this.historique.get(intitule);
125             fc = (montant >= ".2f" "s");
126             fd = (montant < ".2f" "s");
127 
128             System.out.printf("| %1$-" + longMaxIntitule + "s" +
129                               " | %2$" + longMaxMontantC + fc +
130                               " | %3$" + longMaxMontantD + fd +
131                               " |\n",
132                               intitule,
133                               (montant >= ? montant : ""),
134                               (montant < ? -montant : "")
135                               );
136         }
137 
138         // on affiche le solde du compte
139         this.printPiedReleve(longMaxIntitule,
140                              longMaxMontantC,
141                              longMaxMontantD);
142     }
143 
144     /**
145      <code>editerReleveCredits</code> edite un releve des credits du compte.
146      *
147      */
148     public void editerReleveCredits() {
149         double montant = 0;
150 
151         this.longLigneReleve = Math.max(this.longLigneReleve,
152                                         this.historique.getLongMaxIntitule() +
153                                         this.historique.getLongMaxMontantC() +
154                                         this.historique.getLongMaxMontantD() 10);
155 
156         int longMaxIntitule = this.historique.getLongMaxIntitule();
157         int longMaxMontantC = this.historique.getLongMaxMontantC();
158         int longMaxMontantD = this.historique.getLongMaxMontantD();
159 
160         // on affiche l'entete
161         this.printEnteteReleve(longMaxIntitule,
162                                longMaxMontantC,
163                                longMaxMontantD);
164 
165         // on affiche l'historique des operations de credits
166         for (String intitule : this.historique) {
167             montant = this.historique.get(intitule);
168             if (montant > 0) {
169             System.out.printf("| %1$-" + longMaxIntitule + "s" +
170                               " | %2$" + longMaxMontantC + ".2f" +
171                               " | %3$" + longMaxMontantD + "s" +
172                               " |\n",
173                               intitule,
174                               montant,
175                               ""
176                               );
177             }
178         }
179 
180         // on affiche le solde du compte
181         this.printPiedReleve(longMaxIntitule,
182                              longMaxMontantC,
183                              longMaxMontantD);
184     }
185 
186     /**
187      <code>editerReleveDebits</code> edite un releve des debits du compte.
188      *
189      */
190     public void editerReleveDebits() {
191         double montant = 0;
192 
193         this.longLigneReleve = Math.max(this.longLigneReleve,
194                                         this.historique.getLongMaxIntitule() +
195                                         this.historique.getLongMaxMontantC() +
196                                         this.historique.getLongMaxMontantD() 10);
197 
198         int longMaxIntitule = this.historique.getLongMaxIntitule();
199         int longMaxMontantC = this.historique.getLongMaxMontantC();
200         int longMaxMontantD = this.historique.getLongMaxMontantD();
201 
202         // on affiche l'entete
203         this.printEnteteReleve(longMaxIntitule,
204                                longMaxMontantC,
205                                longMaxMontantD);
206 
207         // on affiche l'historique des operations de debit
208         for (String intitule : this.historique) {
209             montant = this.historique.get(intitule);
210             if (montant < 0) {
211             System.out.printf("| %1$-" + longMaxIntitule + "s" +
212                               " | %2$" + longMaxMontantC + "s" +
213                               " | %3$" + longMaxMontantD + ".2f" +
214                               " |\n",
215                               intitule,
216                               "",
217                               montant
218                               );
219             }
220         }
221 
222         // on affiche le solde du compte
223         this.printPiedReleve(longMaxIntitule,
224                              longMaxMontantC,
225                              longMaxMontantD);
226     }
227 
228     private void printEnteteReleve(int longMaxIntitule,
229                                    int longMaxMontantC,
230                                    int longMaxMontantD) {
231         System.out.println(new String(new char[this.longLigneReleve]).replace("\0""-"));
232         System.out.printf(titulaireRel + "%1$" +
233                           (this.longLigneReleve - titulaireRel.length()) "s\n",
234                           getTitulaire());
235         System.out.println("+" +
236                            new String(new char[this.longLigneReleve - 2]).replace("\0""-"+
237                            "+");
238         System.out.printf("| " +
239                           new String(new char[longMaxIntitule]).replace("\0"" "+
240                           " | %1$" + longMaxMontantC + "s" +
241                           " | %2$" + longMaxMontantD + "s" +
242                           " |\n",
243                           "credit",
244                           "debit");
245 
246         System.out.println("|-" +
247                            new String(new char[longMaxIntitule]).replace("\0""-"+
248                            "-+-" +
249                            new String(new char[longMaxMontantC]).replace("\0""-"+
250                            "-+-" +
251                            new String(new char[longMaxMontantD]).replace("\0""-"+
252                            "-|"
253                            );
254     }
255 
256     private void printPiedReleve(int longMaxIntitule,
257                                  int longMaxMontantC,
258                                  int longMaxMontantD) {
259         double d = this.getSolde();
260         String fc = (d >= ".2f" "s");
261         String fd = (d < ".2f" "s");
262 
263         System.out.println("|-" +
264                            new String(new char[longMaxIntitule]).replace("\0""-"+
265                            "-+-" +
266                            new String(new char[longMaxMontantC]).replace("\0""-"+
267                            "-+-" +
268                            new String(new char[longMaxMontantD]).replace("\0""-"+
269                            "-|"
270                            );
271         System.out.printf("| %1$" + longMaxIntitule + "s" +
272                           " | %2$" + longMaxMontantC + fc +
273                           " | %3$" + longMaxMontantD + fd +
274                           " |\n",
275                           "solde",
276                           (d >= ? d : ""),
277                           (d < ? d : "")
278                           );
279         System.out.println("+" +
280                            new String(new char[this.longLigneReleve - 2]).replace("\0""-"+
281                            "+");
282     }
283 }