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