Skip to content
Snippets Groups Projects
Commit 7f35340b authored by Vesa Lappalainen's avatar Vesa Lappalainen :bicyclist:
Browse files

tallentaa Jasenet

parent 9a28c603
No related branches found
No related tags found
No related merge requests found
1|Ankka Aku 7413|190169-8454|Paratiisitie 13|12345|ANKKALINNA|12-1234|||1996|50.0|30.0|Velkaa Roopelle
2|Ankka Aku 9370|181008-646K|Paratiisitie 13|12345|ANKKALINNA|12-1234|||1996|50.0|30.0|Velkaa Roopelle
......@@ -131,6 +131,36 @@ public class Jasen {
}
/**
* Palauttaa jäsenen tiedot merkkijonona jonka voi tallentaa tiedostoon.
* @return jäsen tolppaeroteltuna merkkijonona
* @example
* <pre name="test">
* Jasen jasen = new Jasen();
* jasen.parse(" 3 | Ankka Aku | 030201-111C");
* jasen.toString().startsWith("3|Ankka Aku|030201-111C|") === true; // on enemmäkin kuin 3 kenttää, siksi loppu |
* </pre>
*/
@Override
public String toString() {
return "" +
getTunnusNro() + "|" +
nimi + "|" +
hetu + "|" +
katuosoite + "|" +
postinumero + "|" +
postiosoite + "|" +
kotipuhelin + "|" +
tyopuhelin + "|" +
autopuhelin + "|" +
liittymisvuosi + "|" +
jmaksu + "|" +
maksu + "|" +
lisatietoja;
}
/**
* Testiohjelma jäsenelle.
* @param args ei käytössä
......
package kerho;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
/**
* Kerhon jäsenistö joka osaa mm. lisätä uuden jäsenen
*
......@@ -79,10 +84,22 @@ public class Jasenet {
/**
* Tallentaa jäsenistön tiedostoon. Kesken.
* @throws SailoException jos talletus epäonnistuu
* @param hakemisto mihin hakemistoon
* @throws SailoException jos tallennus epäonnistuu
*/
public void talleta() throws SailoException {
throw new SailoException("Ei osata vielä tallettaa tiedostoa " + tiedostonNimi);
public void tallenna(String hakemisto) throws SailoException {
File ftied = new File(hakemisto + "/nimet.dat");
try (PrintStream fo = new PrintStream(new FileOutputStream(ftied, false))) {
for (int i=0; i<this.getLkm(); i++) {
Jasen jasen = this.anna(i);
fo.println(jasen.toString());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
throw new SailoException("Tiedosto " + ftied.getAbsolutePath() + " ei aukea");
}
}
......@@ -123,6 +140,16 @@ public class Jasenet {
} catch (SailoException ex) {
System.out.println(ex.getMessage());
}
try {
jasenet.tallenna("kelmit");
} catch (SailoException e) {
// e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
......@@ -132,10 +132,11 @@ public class Kerho {
/**
* Tallettaa kerhon tiedot tiedostoon
* @param nimi kerho hakemiston nimi
* @throws SailoException jos tallettamisessa ongelmia
*/
public void talleta() throws SailoException {
jasenet.talleta();
public void tallenna(String nimi) throws SailoException {
jasenet.tallenna(nimi);
// TODO: yritä tallettaa toinen vaikka toinen epäonnistuisi
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment