1 package kerho;
2 import java.io.*;
3 import fi.jyu.mit.ohj2.*;
4 import java.util.*;
5
11 public class Harrastukset {
12 private boolean muutettu = false;
13 private String nimi = "";
14 private Collection alkiot = new ArrayList();
15
16 public Harrastukset() { }
17
18
23 public void lisaa(Harrastus har) {
24 alkiot.add(har);
25 muutettu = true;
26 }
27
28
29
34 public void lue_tiedostosta(String tied) throws SailoException {
35 nimi = tied;
36 BufferedReader fi = Tiedosto.avaa_lukemista_varten(getTiedoston_nimi());
37 if ( fi == null ) throw new SailoException("Tiedosto " + getTiedoston_nimi() + " ei aukea");
38
39 String rivi;
40 try {
41 while ( ( rivi = fi.readLine() ) != null ) {
42 rivi = rivi.trim();
43 if ( rivi.equals("") || rivi.startsWith(";") ) continue;
44 Harrastus har = new Harrastus();
45 har.parse(rivi); lisaa(har);
47 }
48 muutettu = false;
49
50 } catch ( IOException e ) {
51
52
53 } finally {
54 try {
55 fi.close();
56 } catch (IOException ex) {
57 }
58 }
59 }
60
61
65 public void talleta() throws SailoException {
66 if ( !muutettu ) return;
67
68 File fbak = new File(getBak_nimi());
69 File ftied = new File(getTiedoston_nimi());
70 if ( !fbak.delete() ) ; if ( !ftied.renameTo(fbak) ) ;
73 PrintWriter fo = Tiedosto.avaa_kirjoittamista_varten(ftied.getName());
74 if ( fo == null )
75 throw new SailoException("Tiedosto " + ftied.getName() + "ei aukea");
76 try {
77 for (Iterator i=iterator(); i.hasNext(); ) {
78 Harrastus har = (Harrastus)i.next();
79 fo.println(har.toString());
80 }
81 } finally {
82 fo.close();
83 }
84
85 muutettu = false;
86 }
87
88
92 public int getLkm() { return alkiot.size(); }
93
94
98 public String getTiedoston_nimi() { return nimi + ".har"; }
99
100
104 public String getBak_nimi() { return nimi + ".hbak"; }
105
106
110 public Iterator iterator() {
111 return alkiot.iterator();
112 }
113
114
119 public class HarrastuksetIterator implements Iterator {
120 private int jasenid;
121 private Iterator iter = alkiot.iterator();
122 private Harrastus seuraava = null;
123
124 public HarrastuksetIterator(int jid) {
125 jasenid = jid;
126 }
127
128 public boolean hasNext() {
129 if ( seuraava != null ) return true;
130 while ( true ) {
131 if ( !iter.hasNext() ) return false;
132 seuraava = (Harrastus)iter.next();
133 if ( seuraava.getJasen_id()== jasenid ) return true;
134 seuraava = null;
135 }
136 }
137
138 public Object next() throws NoSuchElementException {
139 if ( seuraava != null ) {
140 Harrastus pal = seuraava;
141 seuraava = null;
142 return pal;
143 }
144 while ( true ) {
145 Harrastus har = (Harrastus)iter.next();
146 if ( har.getJasen_id()== jasenid ) return har;
147 }
148 }
149
150 public void remove() throws UnsupportedOperationException {
151 throw new UnsupportedOperationException("Me ei poisteta");
152 }
153
154 }
155
156
162 public Iterator iterator(int jasen_id) {
163 return new HarrastuksetIterator(jasen_id);
164 }
165
166 private static void testi(Object o) {
167 Harrastus har = (Harrastus)o;
168 System.out.print(har.getJasen_id() + " ");
169 har.tulosta(System.out);
170 }
171
172
176 public static void main(String args[]) {
177 Harrastukset harrasteet = new Harrastukset();
178 Harrastus pitsi1 = new Harrastus();
179 pitsi1.vastaa_pitsin_nyplays(2);
180 Harrastus pitsi2 = new Harrastus();
181 pitsi2.vastaa_pitsin_nyplays(1);
182 Harrastus pitsi3 = new Harrastus();
183 pitsi3.vastaa_pitsin_nyplays(2);
184 Harrastus pitsi4 = new Harrastus();
185 pitsi4.vastaa_pitsin_nyplays(1);
186
187 harrasteet.lisaa(pitsi1);
188 harrasteet.lisaa(pitsi2);
189 harrasteet.lisaa(pitsi3);
190 harrasteet.lisaa(pitsi2);
191 harrasteet.lisaa(pitsi4);
192
193 System.out.println("============= Harrastukset testi =================");
194
195
196 for (Iterator i=harrasteet.iterator(2); i.hasNext(); ) {
197 testi(i.next());
198 }
199 { System.out.println("============= Iteraattori testi =================");
201 Iterator i=harrasteet.iterator(2);
202 System.out.println(i.hasNext() + " " + i.hasNext());
203 testi(i.next());
204 System.out.println(i.hasNext() + " " + i.hasNext());
205 testi(i.next());
206 System.out.println(i.hasNext() + " " + i.hasNext());
207 testi(i.next());
208 System.out.println(i.hasNext() + " " + i.hasNext());
209 }
210
211 }
212
213 }
214
215
216