1   package kalenteri;
2   
3   import java.io.BufferedReader;
4   import java.io.FileNotFoundException;
5   import java.io.FileOutputStream;
6   import java.io.FileReader;
7   import java.io.IOException;
8   import java.io.PrintStream;
9   import java.util.Collection;
10  import java.util.ArrayList;
11  import java.util.Iterator;
12  
13  import fi.jyu.mit.ohj2.Mjonot;
14  
15  
16  /**
17   * Harjoitukset-luokka.
18   * Ylläpitää listaa Harjoitus-olioista.
19   * @author Timo Koski
20   *
21   */
22  public class Harjoitukset {
23  
24      private int harjoitusId;
25      private static int nextHarjoitusId;
26      // private String tiedostonNimi;
27  
28      private Collection<Harjoitus> alkiot = new ArrayList<Harjoitus>();
29      /**
30       * 
31       */
32      public Harjoitukset() {
33          rekisteroi();
34      }
35  
36      /**
37       * @return harjoitusId
38       */
39      public int getHarjoitusId() {
40          return harjoitusId;
41      }
42  
43      /**
44       * @return harjoitusId
45       */
46      public int rekisteroi() {
47          harjoitusId = nextHarjoitusId;
48          nextHarjoitusId++;
49          return harjoitusId;
50      }
51  
52      /**
53       * Lisätään parametrina tuleva harjoitus alkiot-taulukkoon.
54       * @param h 
55       */
56      public void lisaa(Harjoitus h) {
57          alkiot.add(h);
58      }
59  
60      /**
61       * Luetaan tiedostosta tallennetut harjoitukset.
62       * @param tiedostonNimi
63       */
64      public void lueTiedostosta (String tiedostonNimi) {
65          // this.tiedostonNimi = tiedostonNimi;
66          BufferedReader fi;
67          String tempRivi;
68          try {
69              fi = new BufferedReader(new FileReader(tiedostonNimi));
70              while ((tempRivi = fi.readLine()) != null) {
71                  tulkitse(tempRivi);
72              }   
73          } catch (FileNotFoundException ex) {
74              // System.err.println("Harj: Tiedostoa ei löydy, luodaan tallennettaessa.");
75          } catch (IOException ex) {
76              // TODO exception handling
77              ex.printStackTrace();
78          }
79      }
80  
81      /**
82       * Metodi tulkitsee tiedostosta luetun tietovirran. Ja kopioi aiemmin tallennetut oliot tietorakenteeseen.
83       * @param jono
84       */
85      public void tulkitse(String jono) {
86          Harjoitus uusi = new Harjoitus();
87          String temp;
88          StringBuffer sb = new StringBuffer(jono);
89  
90          temp = Mjonot.erota(sb,'|');
91          uusi.setId(Integer.parseInt(temp));
92          temp = Mjonot.erota(sb,'|');
93          uusi.setPvm(temp);
94          temp = Mjonot.erota(sb,'|');
95          uusi.setAloitusaika(temp);
96          temp = Mjonot.erota(sb,'|');
97          uusi.setLopetusaika(temp);
98          temp = Mjonot.erota(sb,'|');
99          uusi.setPaikka(temp);
100         temp = Mjonot.erota(sb,'|');
101         uusi.setSeura(temp);
102         temp = Mjonot.erota(sb,'|');
103         uusi.setOpettaja(temp);
104         temp = Mjonot.erota(sb,'|');
105         uusi.setRoda(Boolean.parseBoolean(temp));
106         temp = Mjonot.erota(sb,'|');
107         uusi.setLisatieto(temp);
108         lisaa(uusi);
109     }
110 
111     /**
112      * @param fileName 
113      * 
114      */
115     @SuppressWarnings("null")
116     public void kirjoitaTiedostoon(String fileName) {
117         PrintStream fo = null;
118         String harjoitusString;
119         try {
120             fo = new PrintStream(new FileOutputStream(fileName));
121         } catch (FileNotFoundException ex) {
122             // TODO Auto-generated catch block
123             ex.printStackTrace();
124         }
125         for (Harjoitus h: alkiot) {
126             harjoitusString = harjoitusStringiksi(h);
127             fo.println(harjoitusString);
128         }
129     }
130 
131     /**
132      * Metodi muuttaa parametrina tuodun olion tiedostoon kirjoitettavaan muotoon.
133      * @param h
134      * @return tempRivi
135      */
136     public String harjoitusStringiksi(Harjoitus h) {
137         String tempRivi = h.getId()+"|"+h.getHarjPvm()+"|"+h.getAloitusAika()
138                 +"|"+h.getLopetusAika()+"|"+h.getPaikka()+"|"
139                 +h.getSeura()+"|"+h.getOpettaja()+"|"
140                 +h.getRoda()+"|"+h.getLisatieto()+"|";  
141         return tempRivi;
142     }
143 
144     /**
145      * Tulostetaan kaikkien alkiot-taulukon alkioiden sisältö.
146      * Eli jokaisen Harjoitus-olion tiedot, olion omaa tulostamista käyttäen.
147      * @param selectedIndex 
148      * @return temp
149      */
150     public String tulosta(int selectedIndex) {
151         String temp = "";
152         if (selectedIndex == -1) {
153             for (Harjoitus h : alkiot) {
154                 temp += h.toString();
155             }
156         } else {
157             Harjoitus h = (Harjoitus) alkiot.toArray()[selectedIndex];
158             temp += h.toString();
159         }
160         return temp;
161         /*
162         // @version ht5 String-palautus
163         String temp = "";
164         for(Harjoitus a: alkiot) {
165             temp += a.tulosta(System.out);
166         }
167         return temp;
168          */
169     }
170 
171     /**
172      * toString-metodi, joka kutsuu suoraan tulosta-metodia.
173      */
174     @Override
175     public String toString() {
176         return tulosta(-1);
177     }
178 
179     /**
180      * Luetaan alkiot taulukon Harjoitus-alkioiden päivämäärät
181      * String-taulukkoon, joka palautetaan. Käytetään 
182      * päivämäärälistaan mainwindowissa. 
183      * @return Stringtaulu päivämääristä
184      */
185     public String[] toPvmStringTaulu() {
186         String[] taulu = new String[alkiot.size()];
187         Iterator<Harjoitus> e = alkiot.iterator();
188         for(int i=0; i<alkiot.size();i++) {
189             taulu[i] = e.next().getHarjPvm();
190         }       
191         return taulu;
192     }
193 
194     /**
195      * Palauttaa harjoituksen, jonka paikka collectionissa täsmää 
196      * parametrina tuotuun indeksiin.
197      * @param ind
198      * @return harjoitus
199      */
200     public Harjoitus naytaHarjoitus(int ind) {
201         int a = 0;
202         for(Harjoitus h : alkiot) {
203             if(a == ind) 
204                 return h;
205             a++;
206         }
207         return null;
208     }
209 
210     /**
211      * @return alkiot
212      */
213     public Collection<Harjoitus> getAlkiot() {
214         return alkiot;
215     }
216 
217     /**
218      * Pääohjelma luokan testaamiseksi.
219      * @param args ei käytössä
220      * @example
221      * <pre name="test">
222      * Harjoitukset h = new Harjoitukset();
223      * Harjoitus a = new Harjoitus();
224      * Harjoitus b = new Harjoitus();
225      * h.lisaa(a);
226      * h.lisaa(b);
227      * String tulos =   "000 "+a.getHarjPv()+"."+a.getHarjKk()+"."+a.getHarjVv()+"\n"+
228      *                  "20:00 22:00 \n"+
229      *                  "Forca Natural\n"+
230      *                  "===\n"+
231      *                  "001 "+b.getHarjPv()+"."+b.getHarjKk()+"."+b.getHarjVv()+"\n"+
232      *                  "20:00 22:00 \n"+
233      *                  "Forca Natural\n"+
234      *                  "===\n";
235      * h.tulosta(-1) === tulos; 
236      * </pre>
237      */
238     public static void main(String[] args) {
239         Harjoitukset har = new Harjoitukset();
240 
241         Harjoitus h1 = new Harjoitus();
242         Harjoitus h2 = new Harjoitus();
243         har.lisaa(h1);
244         har.lisaa(h2);
245 
246         har.tulosta(-1);
247     }
248 
249     // Apumetodit, joilla tulostetaan perustiedot editpaneeleihin.
250 
251     /**
252      * @param id 
253      * @return h.getAloitusAika()
254      */
255     public String getHarjAloitusaika(int id) {
256         for (Harjoitus h: alkiot) {
257             if (h.getId() == id)
258                 return h.getAloitusAika();
259         }
260         return null;
261     }
262 
263     /**
264      * @param id 
265      * @return h.getLopetusAika()
266      */
267     public String getHarjLopetusaika(int id) {
268         for (Harjoitus h: alkiot) {
269             if (h.getId() == id)
270                 return h.getLopetusAika();
271         }
272         return null;
273     }
274 
275     /**
276      * @param id 
277      * @return h.getPaikka()
278      */
279     public String getPaikkakunta(int id) {
280         for (Harjoitus h: alkiot) {
281             if (h.getId() == id)
282                 return h.getPaikka();
283         }
284         return null;
285     }
286 
287     /**
288      * @param id 
289      * @return h.getSeura()
290      */
291     public String getSeura(int id) {
292         for (Harjoitus h: alkiot) {
293             if (h.getId() == id)
294                 return h.getSeura();
295         }
296         return null;
297     }
298 
299     /**
300      * @param id 
301      * @return h.getOpettaja()
302      */
303     public String getOpettaja(int id) {
304         for (Harjoitus h: alkiot) {
305             if (h.getId() == id)
306                 return h.getOpettaja();
307         }
308         return null;
309     }
310 
311     /**
312      * @param ind
313      * @return harjoituksen id
314      */
315     public int getHarjId(int ind) {
316         Harjoitus h = naytaHarjoitus(ind);
317         return h.getId();
318     }
319 
320     /**
321      * @param id
322      */
323     public void poistaHarjoitus(int id) {
324         Harjoitus h = naytaHarjoitus(id);
325         alkiot.remove(h);
326     }
327 
328     /**
329      * @param id
330      * @return h.getLisatieto()
331      */
332     public String getLisatieto(int id) {
333         for (Harjoitus h: alkiot) {
334             if (h.getId() == id)
335                 return h.getLisatieto();
336         }
337         return null;
338     }
339 }
340