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
183
184
188 public static void main(String args[]) {
189 Jasenet jasenet = new Jasenet();
190 try {
191 jasenet.lue_tiedostosta("kelmit");
192 } catch ( SailoException ex ) {
193 System.out.println("Virhe: " + ex.getMessage());
194 }
195
196 Jasen aku = new Jasen(), aku2 = new Jasen();
197 aku.rekisteroi(); aku.vastaa_aku_ankka();
198 aku2.rekisteroi(); aku2.vastaa_aku_ankka();
199
200 try {
201 jasenet.lisaa(aku);
202 jasenet.lisaa(aku2);
203
204 System.out.println("============= Jäsenet testi =================");
205
206 for (Iterator i=jasenet.iterator(); i.hasNext(); ) {
208 Jasen jasen = (Jasen)i.next();
210 System.out.println("Jäsen nro: " + jasen.getTunnus_nro());
211 jasen.tulosta(System.out);
212 }
213
214 } catch ( SailoException ex ) {
215 System.out.println(ex.getMessage());
216 }
217
218 try {
219 jasenet.talleta();
220 } catch ( SailoException e ) {
221 System.out.println("Virhe: " + e.getMessage());
222 }
223
224 }
225
226 }
227
228
229