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