1 package kerho;
2 import java.io.*;
3 import java.util.*;
4 import fi.jyu.mit.ohj2.*;
5
12 public class Jasenet {
13 private static final int MAX_JASENIA = 25;
14 private int lkm = 0;
15 private boolean muutettu = false;
16 private String nimi = "";
17 private String koko_nimi = "";
18 private Jasen alkiot[] = new Jasen[MAX_JASENIA];
19
20 public Jasenet() { }
21
22
27 public void lisaa(Jasen jasen) throws SailoException {
28 muutettu = true;
29 if ( lkm >= alkiot.length ) throw new SailoException("Liikaa alkioita");
30 alkiot[lkm] = jasen;
31 lkm++;
32 }
33
34
35
41 public Jasen anna(int i) throws SailoException {
42 if ( i < 0 || lkm <= i ) throw new SailoException("Laiton indeksi " + i);
43 return alkiot[i];
44 }
45
46
47
48
49
50
55 public void lue_tiedostosta(String tied) throws SailoException {
56 nimi = tied;
57 BufferedReader fi = Tiedosto.avaa_lukemista_varten(getTiedoston_nimi());
58 if ( fi == null ) throw new SailoException("Tiedosto " + getTiedoston_nimi() + " ei aukea");
59
60 try {
61 koko_nimi = fi.readLine();
62 if ( koko_nimi == null ) throw new SailoException("xxxxx");
63 String rivi = fi.readLine();
64 if ( rivi == null ) throw new SailoException("xxxxx");
65 int max_koko = Mjonot.erotaInt(rivi,10);
67 while ( ( rivi = fi.readLine() ) != null ) {
68 rivi = rivi.trim();
69 if ( rivi.equals("") || rivi.startsWith(";") ) continue;
70 Jasen jasen = new Jasen();
71 jasen.parse(rivi); lisaa(jasen);
73 }
74 muutettu = false;
75
76
77 } catch ( IOException e ) {
78
79
80 } finally {
81 try {
82 fi.close();
83 } catch (IOException ex) {
84 }
85 }
86 }
87
88
100 public void talleta() throws SailoException {
101 if ( !muutettu ) return;
102
103 File fbak = new File(getBak_nimi());
104 File ftied = new File(getTiedoston_nimi());
105 if ( !fbak.delete() ) ; if ( !ftied.renameTo(fbak) ) ;
108 PrintWriter fo = Tiedosto.avaa_kirjoittamista_varten(ftied.getName());
109 if ( fo == null )
110 throw new SailoException("Tiedosto " + ftied.getName() + "ei aukea");
111 try {
112 fo.println(getKoko_nimi());
113 fo.println(alkiot.length);
114 for (Iterator i=iterator(); i.hasNext(); ) {
115 Jasen jasen = (Jasen)i.next();
116 fo.println(jasen.toString());
117 }
118 } finally {
120 fo.close();
121 }
122
123 muutettu = false;
124 }
125
126
130 public String getKoko_nimi() { return koko_nimi; }
131
132
136 public int getLkm() { return lkm; }
137
138
142 public String getTiedoston_nimi() { return nimi + ".dat"; }
143
144
148 public String getBak_nimi() { return nimi + ".bak"; }
149
150
155 public class JasenetIterator implements Iterator {
156 private int kohdalla = -1;
157
158 public boolean hasNext() {
159 return kohdalla + 1 < lkm;
162 }
163
164 public Object next() throws NoSuchElementException {
165 if ( !hasNext() ) throw new NoSuchElementException("Ei oo");
166 kohdalla++;
167 return alkiot[kohdalla];
168 }
169
170 public void remove() throws UnsupportedOperationException {
171 throw new UnsupportedOperationException("Me ei poisteta");
172 }
173 }
174
175
179 public Iterator iterator() {
180 return new JasenetIterator();
181 }
182
183 public class Vertailija implements Comparator {
184 int k;
185 public Vertailija(int k) { this.k = k; }
186
187 public int compare(Object o1, Object o2) {
188 Jasen j1 = (Jasen)o1;
189 Jasen j2 = (Jasen)o2;
190
191 String s1 = j1.getAvain(k);
192 String s2 = j2.getAvain(k);
193
194 return s1.compareTo(s2);
195
196 }
197
198 }
199
200 public List etsi(int k, String hakuehto) {
201 List loytyneet = new ArrayList();
202
203 for (Iterator i = iterator(); i.hasNext(); ) {
204 Jasen jasen = (Jasen)i.next();
205 String kentta = jasen.anna(k);
206 if ( WildChars.onkoSamat(kentta,hakuehto) )
207 loytyneet.add(jasen);
208 }
209
210 Collections.sort(loytyneet,new Vertailija(k));
211 return loytyneet;
212 }
213
214
219 public Jasen annaId(int id) {
220 for (Iterator i = iterator(); i.hasNext(); ) {
221 Jasen jasen = (Jasen)i.next();
222 if ( id == jasen.getTunnus_nro() ) return jasen;
223 }
224 return null;
225
226 }
227
228
229
233 public static void main(String args[]) {
234 Jasenet jasenet = new Jasenet();
235 try {
236 jasenet.lue_tiedostosta("kelmit");
237 } catch ( SailoException ex ) {
238 System.out.println("Virhe: " + ex.getMessage());
239 }
240
241 Jasen aku = new Jasen(), aku2 = new Jasen();
242 aku.rekisteroi(); aku.vastaa_aku_ankka();
243 aku2.rekisteroi(); aku2.vastaa_aku_ankka();
244
245 try {
246 jasenet.lisaa(aku);
247 jasenet.lisaa(aku2);
248
249 System.out.println("============= Jäsenet testi =================");
250
251 for (Iterator i=jasenet.iterator(); i.hasNext(); ) {
253 Jasen jasen = (Jasen)i.next();
255 System.out.println("Jäsen nro: " + jasen.getTunnus_nro());
256 jasen.tulosta(System.out);
257 }
258
259 } catch ( SailoException ex ) {
260 System.out.println(ex.getMessage());
261 }
262
263 try {
264 jasenet.talleta();
265 } catch ( SailoException e ) {
266 System.out.println("Virhe: " + e.getMessage());
267 }
268
269 }
270
271 }
272
273
274