1   package kerho;
2   
3   import java.util.Iterator;
4   import java.util.NoSuchElementException;
5   
6   
7   /**
8    * Kerhon jäsenistö joka osaa mm. lisätä uuden jäsenen
9    *
10   * @author Vesa Lappalainen
11   * @version 1.0, 22.02.2003
12   */
13  public class Jasenet implements Iterable<Jasen> {
14    //private boolean muutettu = false;
15    private String tiedostonNimi = "";
16    private String bakNimi = "";
17    private String kokoNimi = "";
18   
19    private static final int MAX_JASENIA = 5;
20    private int lkm = 0;
21  
22    /**
23     * Taulukko jäsenistä 
24     */
25    private Jasen alkiot[] = new Jasen[MAX_JASENIA];
26  
27  
28    /**
29     * Oletusmuodostaja
30     */
31    public Jasenet() {
32        // Attribuuttien oma alustus riittää
33    }
34    
35  
36    /**
37     * Lisää uuden jäsenen tietorakenteeseen.  Ottaa jäsenen omistukseensa.
38     * @param jasen lisätäävän jäsenen viite.  Huom tietorakenne muuttuu omistajaksi
39     * @throws SailoException jos tietorakenne on jo täynnä
40     * @example
41     * <pre name="test">
42     * #THROWS SailoException 
43     * #PACKAGEIMPORT
44     * Jasenet jasenet = new Jasenet();
45     * Jasen aku1 = new Jasen(), aku2 = new Jasen();
46     * jasenet.getLkm() === 0;
47     * jasenet.lisaa(aku1); jasenet.getLkm() === 1;
48     * jasenet.lisaa(aku2); jasenet.getLkm() === 2;
49     * jasenet.lisaa(aku1); jasenet.getLkm() === 3;
50     * jasenet.anna(0) === aku1;
51     * jasenet.anna(1) === aku2;
52     * jasenet.anna(2) === aku1;
53     * jasenet.anna(1) == aku1 === false;
54     * jasenet.anna(1) == aku2 === true;
55     * jasenet.anna(3) === aku1; #THROWS IndexOutOfBoundsException 
56     * jasenet.lisaa(aku1); jasenet.getLkm() === 4;
57     * jasenet.lisaa(aku1); jasenet.getLkm() === 5;
58     * jasenet.lisaa(aku1);  #THROWS SailoException
59     * </pre>
60     */
61    public void lisaa(Jasen jasen) throws SailoException {
62      if ( lkm >= alkiot.length ) throw new SailoException("Liikaa alkioita");
63      alkiot[lkm] = jasen;
64      lkm++;
65    }
66  
67  
68    /**
69     * Palauttaa viitteen i:teen jäseneen.
70     * @param i monennenko jäsenen viite halutaan
71     * @return viite jäseneen, jonka indeksi on i
72     * @throws IndexOutOfBoundsException jos i ei ole sallitulla alueella  
73     */
74    public Jasen anna(int i) throws IndexOutOfBoundsException  {
75      if ( i < 0 || lkm <= i ) throw new IndexOutOfBoundsException("Laiton indeksi: " + i);
76      return alkiot[i];
77    }
78  
79  
80    /**
81     * Lukee jäsenistön tiedostosta.  Kesken.
82     * @param tied tiedoston nimen alkuosa
83     * @throws SailoException jos lukeminen epäonnistuu
84     */
85    public void lueTiedostosta(String tied) throws SailoException {
86      tiedostonNimi = tied + ".dat";
87      kokoNimi = "Kelmien kerho";
88    }
89  
90    
91    /**
92     * Tallentaa jäsenistön tiedostoon.  Kesken.
93     * @throws SailoException jos talletus epäonnistuu
94     */
95    public void talleta() throws SailoException { 
96        // Ei vielä tee mitään
97    }
98  
99    
100   /**
101    * Palauttaa Kerhon koko nimen
102    * @return Kerhon koko nimi merkkijononna
103    */
104   public String getKokoNimi()           { return kokoNimi;                   }
105 
106   
107   /**
108    * Palauttaa kerhon jäsenten lukumäärän
109    * @return jäsenten lukumäärä
110    */
111   public int getLkm()                    { return lkm;                         }
112 
113   
114   /**
115    * Palauttaa tiedoston nimen, jota käytetään tallennukseen
116    * @return tallennustiedoston nimi
117    */
118   public String getTiedostonNimi()      { return tiedostonNimi;              }
119 
120   
121   /**
122    * Palauttaa varakopiotiedoston nimen
123    * @return varakopiotiedoston nimi
124    */
125   public String getBakNimi()            { return bakNimi;                    }
126 
127   
128   /**
129    * Tekee nykyisestä tiedostosta varakopiotiedoston. Kesken.
130    * @param bak_tark tarkennin varakopioiedostoille
131    * @return onnistuiko (true) vai ei (false)
132    */
133   public boolean teeBak(String bak_tark) { bakNimi = bak_tark; return true;   }
134 
135   /**
136    * Luokka jäsenten iteroimiseksi.
137    * @example
138    * <pre name="test">
139    * #THROWS SailoException 
140    * #PACKAGEIMPORT
141    * #import java.util.*;
142    * 
143    * Jasenet jasenet = new Jasenet();
144    * Jasen aku1 = new Jasen(), aku2 = new Jasen();
145    * aku1.rekisteroi(); aku2.rekisteroi();
146    *
147    * jasenet.lisaa(aku1); 
148    * jasenet.lisaa(aku2); 
149    * jasenet.lisaa(aku1); 
150    * 
151    * StringBuffer ids = new StringBuffer(30);
152    * for (Jasen jasen:jasenet) 
153    *   ids.append(" "+jasen.getTunnusNro());           // NOPMD
154    * 
155    * String tulos = " " + aku1.getTunnusNro() + " " + aku2.getTunnusNro() + " " + aku1.getTunnusNro();
156    * 
157    * ids.toString() === tulos; 
158    * 
159    * ids = new StringBuffer(30);
160    * for (Iterator<Jasen>  i=jasenet.iterator(); i.hasNext(); ) {
161    *   Jasen jasen = i.next();
162    *   ids.append(" "+jasen.getTunnusNro());           // NOPMD
163    * }
164    * 
165    * ids.toString() === tulos;
166    * 
167    * Iterator<Jasen>  i=jasenet.iterator();
168    * i.next() == aku1  === true;
169    * i.next() == aku2  === true;
170    * i.next() == aku1  === true;
171    * 
172    * i.next();  #THROWS NoSuchElementException
173    *  
174    * </pre>
175    */
176   public class JasenetIterator implements Iterator<Jasen> {
177       private int kohdalla = -1;
178 
179       /**
180        * Onko olemassa vielä seuraavaa jäsentä
181        * @see java.util.Iterator#hasNext()
182        * @return true jos on vielä jäseniä
183        */
184       public boolean hasNext() {
185 //       if ( kohdalla + 1 >= lkm )  return false;
186 //       return true;
187         return kohdalla + 1 < lkm;
188       }
189 
190       
191     /**
192      * Annetaan seuraava jäsen
193      * @return seuraava jäsen
194      * @throws NoSuchElementException jos seuraava alkiota ei enää ole
195      * @see java.util.Iterator#next()
196      */
197     public Jasen next() throws NoSuchElementException {
198         if ( !hasNext() ) throw new NoSuchElementException("Ei oo");
199         kohdalla++;
200         return alkiot[kohdalla];
201       }
202 
203     
204       
205     /**
206      * Tuhoamista ei ole toteutettu
207      * @throws UnsupportedOperationException aina
208      * @see java.util.Iterator#remove()
209      */
210     public void remove() throws UnsupportedOperationException {
211         throw new UnsupportedOperationException("Me ei poisteta");
212       }
213     }
214 
215     /**
216      * Palautetaan iteraattori jäsenistöön.
217      * @return jäsen iteraattori
218      */
219     public Iterator<Jasen> iterator() {
220 //      return alkiot.iterator();
221       return new JasenetIterator();
222     }
223   
224   
225   /**
226    * Testiohjelma jäsenistölle
227    * @param args ei käytössä
228    */
229   public static void main(String args[]) {
230     Jasenet jasenet = new Jasenet();
231 
232     Jasen aku = new Jasen(), aku2 = new Jasen();
233     aku.rekisteroi();    aku.vastaaAkuAnkka();
234     aku2.rekisteroi();   aku2.vastaaAkuAnkka();
235 
236     try {
237       jasenet.lisaa(aku);
238       jasenet.lisaa(aku2);
239   
240       System.out.println("============= Jäsenet testi =================");
241 
242       for (Iterator<Jasen>  i=jasenet.iterator(); i.hasNext(); ) {
243 //        Jasen jasen = jasenet.anna(i);
244 //        System.out.println("Jäsen nro: " + i);
245         Jasen jasen = i.next();
246         jasen.tulosta(System.out);
247       }
248       jasenet.anna(100);
249       
250     } catch ( IndexOutOfBoundsException ex ) {
251         System.out.println(ex.getMessage());
252     } catch ( SailoException ex ) {
253       System.out.println(ex.getMessage());
254     }
255   }
256 
257 }
258 
259 
260