Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: list/ListBorrowers.java


1   package list;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileOutputStream;
6   import java.io.FileReader;
7   import java.io.IOException;
8   import java.util.Enumeration;
9   import java.util.NoSuchElementException;
10  import java.util.Vector;
11  
12  import datas.Borrower;
13  import datas.Loan;
14  import datas.Reservation;
15  
16  /**
17   * Class ListBorrowers
18   */
19  public class ListBorrowers {
20      
21      private final String FICHIER = "listBorrowers.data";
22      private Vector theBorrowers;
23      private static ListBorrowers myListBorrowers;
24      
25      //Constructeur
26      /**
27       * Create the list
28       * @see Object#Object()
29       */
30      private ListBorrowers() {
31          theBorrowers = new Vector();
32      }
33      
34      /**
35       * Method getHandle return the unique object listBorrowers.
36       * @return ListBorrowers the unique ListBorrowers object
37       */
38      public static ListBorrowers getHandle() {
39          if (myListBorrowers == null) {
40              myListBorrowers = new ListBorrowers();
41          }
42          return myListBorrowers;
43      }
44      
45      /**
46       * Method getTheBorrowers return the vector of borrowers.
47       * @return Vector the vector that represent all the borrower
48       */
49      public Vector getTheBorrowers() {
50          return theBorrowers;
51      }
52      
53      /**
54       * Method findTheBorrower find a borrower with his lastname.
55       * @param lastname the lastname of the borrower
56       * @return Borrower the borrower who have the same lastname;
57       *       null if the borrower cannot be found
58       *
59       * @pre (lastname != null)&&(lastname.getLenght()!=0) //lastname must be set
60       */
61      public Borrower findTheBorrower(String lastname) {
62          Borrower ret = null;
63          Borrower temp;
64          Enumeration elements = theBorrowers.elements();
65          while (elements.hasMoreElements()) {
66              temp = (Borrower) elements.nextElement();
67              if (temp.getLastname().equalsIgnoreCase(lastname)) {
68                  ret = temp;
69              }
70          }
71          return ret;
72      }
73      
74      /**
75       * Method addNewBorrower add a new borrower in the list.
76       * @param theBorrower the borrower to add
77       * @return boolean true if and only if theBorrower have been added to the list
78       *
79       * @pre theBorrower != null //theBorrower must be set
80       */
81      public boolean addNewBorrower(Borrower theBorrower) {
82          boolean ret = false;
83          //Vérifie que le borrower n'est pas déjà dans la liste
84          if (theBorrowers.indexOf(theBorrower) == -1) {
85              //Il n'y est pas déjà on l'ajoute
86              ret = theBorrowers.add(theBorrower);
87          }
88          return ret;
89      }
90      
91      /**
92       * Method deleteTheBorrower delete the borrower from the list.
93       * @param theBorrower the borrower to delete
94       * @return boolean true if and only if theBorrower have been deleted from the list
95       *
96       * @pre theBorrower != null //theBorrower must be set
97       */
98      public boolean deleteTheBorrower(Borrower theBorrower) {
99          
100         boolean ret = false;
101         //Vérifie que le borrower n'as pas loué de livre
102         if (ListLoans.getHandle().findTheBorrowerLoan(theBorrower).isEmpty()) {
103             //Vérifie que le borrower n'as pas de réservations en cours
104             if (!ListReservations
105             .getHandle()
106             .findTheBorrowerReservation(theBorrower)
107             .isEmpty()) {
108                 //Il a des réservations en cours on les effacent
109                 //Recherche de toutes les réservations ou le borrower figure
110                 Enumeration enumeration =
111                 ListReservations.getHandle().findTheBorrowerReservation(theBorrower).elements();
112                 while (enumeration.hasMoreElements()) {
113                     Reservation reservation = (Reservation) enumeration.nextElement();
114                     //Efface les réservations
115                     ListReservations.getHandle().deleteTheReservation(reservation);
116                 }
117             }
118             //On efface le borrower de la liste des borrowers
119             ret = theBorrowers.remove(theBorrower);
120         }
121         return ret;
122     }
123     
124     /**
125      * Method updateBorrower update a borrower.
126      * @param oldLastname the old lastname
127      * @param newLastname the new lastname
128      * @param firstname the new firstname
129      * @param address the new address
130      * @param city the new city
131      * @param zipCode the new zipCode
132      * @param state the new state
133      * @return boolean true if and only if the borrower have been updated
134      *
135      * @pre ((lastname != null) && (lastname.length() != 0)) // lastname must be set
136      * @pre ((firstname != null) && (firstname.length() != 0)) // firstname must be set
137      * @pre ((address != null) && (address.length() != 0)) // address must be set
138      * @pre ((city != null) && (city.length() != 0)) // city must be set
139      * @pre ((zipcode != null) && (zipcode.length() != 0)) // zipcode must be set
140      * @pre ((state != null) && (state.length() != 0)) // state must be set
141      */
142     public boolean updateBorrower(
143     String oldLastname,
144     String newLastname,
145     String firstname,
146     String address,
147     String city,
148     String zipCode,
149     String state) {
150         
151         boolean ret = true;
152         //Récupère l'ancien borrower
153         Borrower oldBorrower = this.findTheBorrower(oldLastname);
154         //Crée le nouveau borrower
155         Borrower newBorrower =
156         new Borrower(newLastname, firstname, address, city, zipCode, state);
157         //Récupère l'objet listReservation
158         ListReservations listReservations = ListReservations.getHandle();
159         //Récupère l'objet listLoans
160         ListLoans listLoans = ListLoans.getHandle();
161         Reservation reservation;
162         
163         //Si le borrower que l'on veut modifier exite
164         if (oldBorrower != null) {
165             //On récupère toutes les réservations effectué par ce borrower
166             Enumeration enumeration =
167             listReservations.findTheBorrowerReservation(oldBorrower).elements();
168             while (enumeration.hasMoreElements()) {
169                 reservation = (Reservation) enumeration.nextElement();
170                 //On change les valeurs du borrower de ces réservations
171                 ret = ret && reservation.setBorrower(oldBorrower, newBorrower);
172             }
173             //On récupère tous les loan effectués par ce borrower
174             enumeration = listLoans.findTheBorrowerLoan(oldBorrower).elements();
175             while (enumeration.hasMoreElements()) {
176                 Loan element = (Loan) enumeration.nextElement();
177                 //On chamge les valeurs du borrower de ces loan
178                 ret = ret && element.setTheBorrower(newBorrower);
179             }
180             //On enlève le borrower actuel de la liste
181             ret = ret && this.theBorrowers.remove(oldBorrower);
182             //On ajoute le nouveau
183             ret = ret && this.addNewBorrower(newBorrower);
184             
185         } else {
186             ret = false;
187         }
188         return ret;
189     }
190     /**
191      * Method readListFromDisk read the data of the listBorrower from the disk.
192      * @return boolean true if the data have been read
193      */
194     public boolean readListFromDisk() {
195         BufferedReader in = null;
196         boolean ret = true;
197         
198         //Récupère le répertoire courant
199         String filePath = System.getProperty("user.dir");
200         //Remise à zéro de la liste
201         theBorrowers = new Vector();
202         
203         try {
204             String line;
205             //Ouvre le fichier en lecture
206             in = new BufferedReader(new FileReader(filePath + "/" + FICHIER));
207             while ((line = in.readLine()) != null) {
208                 //pour chaque ligne on ajoute le borrower correspondant
209                 this.addNewBorrower(new Borrower(line));
210             }
211         } catch (IOException e) {
212             ret = false;
213         } catch (NoSuchElementException e) {
214             ret = false;
215         } finally {
216             try {
217                 if (in != null) {
218                     //On ferme le fichier
219                     in.close();
220                 }
221             } catch (IOException e) {}
222         }
223         return ret;
224     }
225     
226     /**
227      * Method writeListToDisk write the data of the listBorrower to the disk.
228      * @return boolean true if and only if the data have been write
229      */
230     public boolean writeListToDisk() {
231         boolean ret = true;
232         FileOutputStream out = null;
233         int theBorrowerLenght = theBorrowers.size();
234         //Récupère le répertoire courant
235         String filePath = System.getProperty("user.dir");
236         
237         try {
238             //Ouvre le flux de sortie
239             out = new FileOutputStream(new File(filePath + "/" + FICHIER));
240             //Tant qu'il y a des borrower dans la liste
241             for (int i = 0; i < theBorrowerLenght; i++) {
242                 //On passe par un string buffer pour des raisons de performances
243                 StringBuffer sb = new StringBuffer();
244                 //On récupère le borrower correspondant
245                 sb.append(((Borrower) theBorrowers.get(i)).toString());
246                 sb.append("\n");
247                 //Ecriture des infos sur le disque
248                 out.write(sb.toString().getBytes());
249             }
250             //On vide le tampon
251             out.flush();
252         } catch (IOException e) {
253             //Si il y a eu une erreur on l'affiche
254             System.out.println(e.getLocalizedMessage());
255             ret = false;
256         } finally {
257             try {
258                 if (out != null) {
259                     //On ferme le fichier
260                     out.close();
261                 }
262             } catch (IOException e) {
263                 ret = false;
264             }
265         }
266         return ret;
267     }
268 }