package kerho;
import java.io.*;
import java.util.*;
import fi.jyu.mit.ohj2.*;
public class Jasenet {
private static final int MAX_JASENIA = 25;
private int lkm = 0;
private boolean muutettu = false;
private String nimi = "";
private String koko_nimi = "";
private Jasen alkiot[] = new Jasen[MAX_JASENIA];
public Jasenet() { }
public void lisaa(Jasen jasen) throws SailoException {
muutettu = true;
if ( lkm >= alkiot.length ) throw new SailoException("Liikaa alkioita");
alkiot[lkm] = jasen;
lkm++;
}
public void korvaa(Jasen jasen) throws SailoException {
int id = jasen.getTunnus_nro();
for (int i = 0; i < lkm; i++ ) {
if ( alkiot[i].getTunnus_nro() == id ) {
alkiot[i] = jasen;
muutettu = true;
return;
}
}
lisaa(jasen);
}
public Jasen anna(int i) throws SailoException {
if ( i < 0 || lkm <= i ) throw new SailoException("Laiton indeksi " + i);
return alkiot[i];
}
public void lue_tiedostosta(String tied) throws SailoException {
nimi = tied;
BufferedReader fi = Tiedosto.avaa_lukemista_varten(getTiedoston_nimi());
if ( fi == null ) throw new SailoException("Tiedosto " + getTiedoston_nimi() + " ei aukea");
try {
koko_nimi = fi.readLine();
if ( koko_nimi == null ) throw new SailoException("xxxxx");
String rivi = fi.readLine();
if ( rivi == null ) throw new SailoException("xxxxx");
int max_koko = Mjonot.erotaInt(rivi,10);
while ( ( rivi = fi.readLine() ) != null ) {
rivi = rivi.trim();
if ( rivi.equals("") || rivi.startsWith(";") ) continue;
Jasen jasen = new Jasen();
jasen.parse(rivi); lisaa(jasen);
}
muutettu = false;
} catch ( IOException e ) {
} finally {
try {
fi.close();
} catch (IOException ex) {
}
}
}
public void talleta() throws SailoException {
if ( !muutettu ) return;
File fbak = new File(getBak_nimi());
File ftied = new File(getTiedoston_nimi());
if ( !fbak.delete() ) ; if ( !ftied.renameTo(fbak) ) ;
PrintWriter fo = Tiedosto.avaa_kirjoittamista_varten(ftied.getPath());
if ( fo == null )
throw new SailoException("Tiedosto " + ftied.getName() + "ei aukea");
try {
fo.println(getKoko_nimi());
fo.println(alkiot.length);
for (Iterator i=iterator(); i.hasNext(); ) {
Jasen jasen = (Jasen)i.next();
fo.println(jasen.toString());
}
} finally {
fo.close();
}
muutettu = false;
}
public String getKoko_nimi() { return koko_nimi; }
public int getLkm() { return lkm; }
public String getTiedoston_nimi() { return nimi + ".dat"; }
public String getBak_nimi() { return nimi + ".bak"; }
public class JasenetIterator implements Iterator {
private int kohdalla = -1;
public boolean hasNext() {
return kohdalla + 1 < lkm;
}
public Object next() throws NoSuchElementException {
if ( !hasNext() ) throw new NoSuchElementException("Ei oo");
kohdalla++;
return alkiot[kohdalla];
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Me ei poisteta");
}
}
public Iterator iterator() {
return new JasenetIterator();
}
public class Vertailija implements Comparator {
int k;
public Vertailija(int k) { this.k = k; }
public int compare(Object o1, Object o2) {
Jasen j1 = (Jasen)o1;
Jasen j2 = (Jasen)o2;
String s1 = j1.getAvain(k);
String s2 = j2.getAvain(k);
return s1.compareTo(s2);
}
}
public List etsi(int k, String hakuehto) {
List loytyneet = new ArrayList();
for (Iterator i = iterator(); i.hasNext(); ) {
Jasen jasen = (Jasen)i.next();
String kentta = jasen.anna(k);
if ( WildChars.onkoSamat(kentta,hakuehto) )
loytyneet.add(jasen);
}
Collections.sort(loytyneet,new Vertailija(k));
return loytyneet;
}
public Jasen annaId(int id) {
for (Iterator i = iterator(); i.hasNext(); ) {
Jasen jasen = (Jasen)i.next();
if ( id == jasen.getTunnus_nro() ) return jasen;
}
return null;
}
public static void main(String args[]) {
Jasenet jasenet = new Jasenet();
try {
jasenet.lue_tiedostosta("kelmit");
} catch ( SailoException ex ) {
System.out.println("Virhe: " + ex.getMessage());
}
Jasen aku = new Jasen(), aku2 = new Jasen();
aku.rekisteroi(); aku.vastaa_aku_ankka();
aku2.rekisteroi(); aku2.vastaa_aku_ankka();
try {
jasenet.lisaa(aku);
jasenet.lisaa(aku2);
System.out.println("============= Jäsenet testi =================");
for (Iterator i=jasenet.iterator(); i.hasNext(); ) {
Jasen jasen = (Jasen)i.next();
System.out.println("Jäsen nro: " + jasen.getTunnus_nro());
jasen.tulosta(System.out);
}
} catch ( SailoException ex ) {
System.out.println(ex.getMessage());
}
try {
jasenet.talleta();
} catch ( SailoException e ) {
System.out.println("Virhe: " + e.getMessage());
}
}
}