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

Quick Search    Search Deep

Source code: list/ListLoans.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.Item;
14  import datas.Loan;
15  import datas.Title;
16  
17  /**
18   * Class ListLoans
19   */
20  public class ListLoans {
21      
22      private final String FICHIER = "listLoans.data";
23      private Vector theLoans;
24      private static ListLoans myListLoans;
25      
26      /**
27       * Create a listLoan
28       * @see Object#Object()
29       */
30      private ListLoans() {
31          theLoans = new Vector();
32      }
33      
34      /**
35       *Method getHandle return the unique object listLoans
36       * @return ListLoans the unique listLoans object
37       */
38      public static ListLoans getHandle() {
39          if (myListLoans == null) {
40              myListLoans = new ListLoans();
41          }
42          return myListLoans;
43      }
44      
45      /**
46       * Method findTheLoan find the loan of the specified item.
47       * @param theItem the item to search
48       * @return Loan the result loan or null if the item havent't been loan
49       *
50       * @pre theItem != null // the item must be set
51       */
52      public Loan findTheLoan(Item theItem) {
53          Loan ret = null;
54          Loan temp;
55          //Récupère la liste des loans
56          Enumeration elements = theLoans.elements();
57          while (elements.hasMoreElements()) {
58              temp = (Loan) elements.nextElement();
59              //recherche l'item
60              if (temp.getItem().getTitle().getName().equalsIgnoreCase(theItem.getTitle().getName())
61              && temp.getItem().getItemId() == theItem.getItemId()) {
62                  //Reourne l'item trouvé
63                  ret = temp;
64              }
65          }
66          return ret;
67      }
68      
69      /**
70       * Method addNewLoan add a new loan of the specified item.
71       * @param theBorrower the Borrower who loan the item
72       * @param theItem the item that have been loan
73       * @return boolean true if and only if the loan was added
74       *
75       * @pre theBorrower!=null // theBorrower must be set
76       * @pre theItem != null //theItem must be set
77       */
78      public boolean addNewLoan(Borrower theBorrower, Item theItem) {
79          boolean ret = false;
80          if (ListBorrowers
81          .getHandle()
82          .findTheBorrower(theBorrower.getLastname())
83          .equals(theBorrower)
84          && ListItems
85          .getHandle()
86          .findTheItem(theItem.getTitle().getName(), theItem.getItemId())
87          .equals(theItem)) {
88              
89              ret = true;
90              Vector theBorrowerLoan = this.findTheBorrowerLoan(theBorrower);
91              if (theBorrowerLoan.size() >= 3) {
92                  ret = false;
93              }
94              
95              Enumeration enum = theBorrowerLoan.elements();
96              while (enum.hasMoreElements()) {
97                  Loan loan = (Loan) enum.nextElement();
98                  if (loan.getItem().getTitle().getName().equals(theItem.getTitle().getName())) {
99                      
100                     ret = false;
101                 }
102             }
103             
104             
105             if (ret) {
106                 
107                 theLoans.add(new Loan(theBorrower, theItem));
108             }
109         }
110         return ret;
111     }
112     
113     
114     /**
115      * Method addNewLoan add the loan.
116      * @param theLoan loan to add to the vector of loan
117      *
118      * @pre theLoan != null //The loan must be set
119      */
120     public void addNewLoan(Loan theLoan) {
121         theLoans.add(theLoan);
122     }
123     
124     /**
125      * Method deleteTheLoan delete the specified loan.
126      * @param theLoan the loan to delete
127      * @return boolean true if the loan have been deleted
128      *
129      * @pre theLoan!=null //theLoan must be set
130      */
131     public boolean deleteTheLoan(Loan theLoan) {
132         //On enlève la location de l'item
133         theLoan.getItem().becomeBorrowed(false);
134         return theLoans.remove(theLoan);
135     }
136     
137     /**
138      * Method deleteTheLoan delete the loan associated with the specified
139      * title and borrower lastname.
140      * @param title the title of the item that have been loan
141      * @param lastname the lastname of the borrower who loan the item
142      * @return boolean true if the loan have been deleted
143      *
144      * @pre title != null //title must be set
145      * @pre (lastname!=null)&&(lastname.length()!=0) //lastname must be set
146      */
147     public boolean deleteTheLoan(Title title, String lastname) {
148         boolean ret=false;
149         //Recherche des item correspondant au title
150         Enumeration enum =
151         ListItems.getHandle().findTheItemCopyOfTitle(title).elements();
152         while (enum.hasMoreElements()) {
153             Item item = (Item) enum.nextElement();
154             //Recherche le loan associé à cet item
155             Loan loan = this.findTheLoan(item);
156             //Vérifie qu'il est bien loué
157             if (loan != null) {
158                 //Vérifie que le borrower est celui que l'on veut virer
159                 if (loan.getBorrower().getLastname().equalsIgnoreCase(lastname)) {
160                     //Tout vas bien on l'efface
161                     ret = this.deleteTheLoan(loan);
162                 }
163             }
164         }
165         return ret;
166     }
167     
168     /**
169      * Method findTheBorrowerLoan find the loan associated to the borrower.
170      * @param borrower the borrower to search
171      * @return Vector all the loan of the borrower
172      *
173      * @pre borrower != null //borrower must be set
174      */
175     public Vector findTheBorrowerLoan(Borrower borrower) {
176         Vector ret = new Vector();
177         Loan loan;
178         //On récupère tous les loan
179         Enumeration elements = theLoans.elements();
180         while (elements.hasMoreElements()) {
181             loan = (Loan) elements.nextElement();
182             //On vérifie si c'est le bon borrower
183             if (loan.getBorrower().getLastname().equalsIgnoreCase(borrower.getLastname())) {
184                 //On ajoute le loan au vecteur du résultat
185                 ret.add(loan);
186             }
187         }
188         return ret;
189     }
190     
191     /**
192      * Method readListFromDisk read the data of the listLoans from the disk.
193      * @return boolean true if the data have been read
194      */
195     public boolean readListFromDisk() {
196         boolean ret = true;
197         BufferedReader in = null;
198         Loan theLoan;
199         //On remet à zéro les loans
200         theLoans = new Vector();
201         //On récupère le répertoire de travail de l'utilisateur
202         String filePath = System.getProperty("user.dir");
203         
204         try {
205             String line;
206             //On ouvre le fichier en lecture
207             in = new BufferedReader(new FileReader(filePath + "/" + FICHIER));
208             while ((line = in.readLine()) != null) {
209                 //Pour chaque ligne on recrée le loan
210                 theLoan = new Loan(line);
211                 //Et on l'ajoute au vecteur
212                 this.addNewLoan(theLoan);
213             }
214         } catch (IOException e) {
215             ret = false;
216         } catch (NoSuchElementException e) {
217             ret = false;
218         } finally {
219             try {
220                 if (in != null) {
221                     //Fermeture du fichier
222                     in.close();
223                 }
224             } catch (IOException e) {}
225         }
226         return ret;
227     }
228     
229     /**
230      * Method writeListToDisk write the data of the listLoans to the disk.
231      * @return boolean true if and only if the data have been write
232      */
233     public boolean writeListToDisk() {
234         boolean ret = true;
235         int theLoansLenght = this.theLoans.size();
236         FileOutputStream out = null;
237         //On récupère le répertoire de travail de l'utilisateur
238         String filePath = System.getProperty("user.dir");
239         
240         try {
241             //On ouvre le fichier en écriture
242             out = new FileOutputStream(new File(filePath + "/" + FICHIER));
243             for (int i = 0; i < theLoansLenght; i++) {
244                 //Passage par un string buffer pour des questions de performance
245                 StringBuffer sb = new StringBuffer();
246                 sb.append(((Loan) this.theLoans.get(i)).toString());
247                 sb.append('\n');
248                 //On écrit les données sur le disque
249                 out.write(sb.toString().getBytes());
250             }
251             //On vide le tampon
252             out.flush();
253         } catch (IOException e) {
254             //Si il y a une erreur d'écriture on affiche un message
255             System.out.println(e.getLocalizedMessage());
256             ret = false;
257         } finally {
258             try {
259                 if (out != null) {
260                     //On ferme le fichier
261                     out.close();
262                 }
263             } catch (IOException e) {
264                 ret = false;
265             }
266         }
267         return ret;
268     }
269 }