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

Quick Search    Search Deep

Source code: list/ListReservations.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.Reservation;
14  import datas.Title;
15  
16  /**
17   * Class ListReservation
18   */
19  public class ListReservations {
20      private static ListReservations listReservations;
21      private Vector theReservations;
22      private final String FICHIER = "listReservations.data";
23      
24      /**
25       * Create the list
26       * @see Object#Object()
27       */
28      private ListReservations() {
29          this.theReservations = new Vector();
30      }
31      
32      /**
33       * Method getHandle return the unique object listReservation.
34       * @return ListReservation the unique listReservation object
35       */
36      public static ListReservations getHandle() {
37          if (listReservations == null) {
38              listReservations = new ListReservations();
39          }
40          return listReservations;
41      }
42      
43      /**
44       * Method addNewReservation add a new reservation.
45       * @param reservation the reservation to add
46       * @return boolean true if the reservation was added
47       *
48       * @pre reservation != null // Reservation must be set
49       */
50      public boolean addNewReservation(Reservation reservation) {
51          boolean ret = false;
52          //Vérifie que le title de la réservation existe
53          if (ListTitles
54          .getHandle()
55          .findTheTitle(reservation.getTitle().getName(), null, null)
56          != null) {
57              //Si il existe on l'ajoute
58              ret = this.theReservations.add(reservation);
59          }
60          return ret;
61      }
62      
63      /**
64       * Method findTheReservation find the reservation of the title.
65       * @param title the title to search
66       * @return Reservation the reservation of this title
67       *       or null if the reservation cannot been found
68       *
69       * @pre title != null //title must be set
70       */
71      public Reservation findTheReservation(Title title) {
72          Reservation ret = null;
73          Reservation temp;
74          //Récupère toutes les réservation
75          Enumeration enum = this.theReservations.elements();
76          
77          while (enum.hasMoreElements()) {
78              temp = (Reservation) enum.nextElement();
79              if (temp.getTitle().equals(title)) {
80                  ret = temp;
81              }
82          }
83          return ret;
84      }
85      
86      /**
87       * Method findTheBorrowerReservation find all the reservation of the borrower.
88       * @param borrower the borrower to search reservation for
89       * @return Vector the list of reservation
90       *
91       * @pre borrower != null //borrower must be set
92       */
93      public Vector findTheBorrowerReservation(Borrower borrower) {
94          Vector ret = new Vector();
95          Reservation temp;
96          Enumeration enum = this.theReservations.elements();
97          while (enum.hasMoreElements()) {
98              temp = (Reservation) enum.nextElement();
99              if (temp.getTheBorrowers().indexOf(borrower) != -1) {
100                 ret.add(temp);
101             }
102         }
103         return ret;
104     }
105     
106     /**
107      * Method deleteTheReservation delete the reservation.
108      * @param reservation reservation to delete
109      * @return boolean true if the reservation was deleted
110      *
111      * @pre reservation != null //Reservation must be set
112      */
113     public boolean deleteTheReservation(Reservation reservation) {
114         boolean ret = false;
115         
116         ret = this.theReservations.remove(reservation);
117         
118         return ret;
119     }
120     
121     /**
122      * Method readListFromDisk read the data of the listReservations from the disk.
123      * @return boolean true if the data have been read
124      */
125     public boolean readListFromDisk() {
126         BufferedReader in = null;
127         
128         this.theReservations = new Vector();
129         
130         String filePath = System.getProperty("user.dir");
131         
132         boolean ret = true;
133         try {
134             String line;
135             
136             in = new BufferedReader(new FileReader(filePath+"/"+FICHIER));
137             while ((line = in.readLine()) != null) {
138                 
139                 this.addNewReservation(new Reservation(line));
140             }
141         } catch (IOException e) {
142             ret = false;
143         } catch (NoSuchElementException e) {
144             ret = false;
145         } finally {
146             try {
147                 if (in != null) {
148                     
149                     in.close();
150                 }
151             } catch (IOException e) {
152                 ret = false;
153             }
154         }
155         return ret;
156     }
157     
158     /**
159      * Method writeListToDisk write the data of the listReservations to the disk.
160      * @return boolean true if and only if the data have been write
161      */
162     public boolean writeListToDisk() {
163         boolean ret = true;
164         int theReservationLenght = this.theReservations.size();
165         FileOutputStream out = null;
166         
167         String filePath = System.getProperty("user.dir");
168         
169         try {
170             
171             out = new FileOutputStream(new File(filePath+"/"+FICHIER));
172             for (int i = 0; i < theReservationLenght; i++) {
173                 
174                 StringBuffer sb = new StringBuffer();
175                 sb.append(((Reservation) this.theReservations.get(i)).toString());
176                 sb.append('\n');
177                 
178                 out.write(sb.toString().getBytes());
179             }
180             
181             out.flush();
182         } catch (IOException e) {
183             
184             System.out.println(e.getLocalizedMessage());
185             ret = false;
186         } finally {
187             try {
188                 if (out != null) {
189                     
190                     out.close();
191                 }
192             } catch (IOException e) {
193                 ret = false;
194             }
195         }
196         return ret;
197     }
198 }
199 /**
200  * Class ListReservation
201  *//**
202   * Method addNewReservation add a new reservation.
203   * @param reservation the reservation to add
204   * @return boolean true if the reservation was added
205   * @pre reservation != null
206   */