diff --git a/puntti/batman/liikkeet.dat b/puntti/batman/liikkeet.dat index 6a69d3d435f01320acec0f49dee895e78e682997..667ba835d9c9fb967b29f19a0cf36c0b04c1910f 100644 --- a/puntti/batman/liikkeet.dat +++ b/puntti/batman/liikkeet.dat @@ -1,2 +1,4 @@ -1|Kyykky3|leveä|2|Syvä|Tanko -2|Kyykky12|neutraali|9|Syvä|Tanko +1|Kyykky6|neutraali|2|Syvä|Tanko +2|Kyykky2|leveä|8|Syvä|Tanko +3|Kyykky16|neutraali|9|Syvä|Tanko +4|Kyykky5|neutraali|8|Syvä|Tanko diff --git a/puntti/src/puntti/Liike.java b/puntti/src/puntti/Liike.java index db16024ffce621f78992a3aea17193d56962f714..c4547602e71bf9e302ecc5f2275deda7ec668ed6 100644 --- a/puntti/src/puntti/Liike.java +++ b/puntti/src/puntti/Liike.java @@ -5,6 +5,7 @@ package puntti; import java.io.PrintStream; +import fi.jyu.mit.ohj2.Mjonot; import testaus.ErilaisetArvonnat; @@ -114,6 +115,48 @@ public class Liike { + /** + * Asettaa lid ja varmistaa että + * seuraava numero on suurempi kuin tähän mennessä suurin. + * @param numero asetettava lid + */ + private void setLid(int numero) { + lid = numero; + if (lid >= seuraavaLid) seuraavaLid = lid + 1; + } + + + /** + * erottelee tiedostosta luetulta rivitä tiedot + * @param rivi josta erotellaan liikkeen tiedot + * @example + * <pre name="test"> + * Liike liike = new Liike(); + * liike.parse("7 | kyykky | leveä"); + * liike.getLid() === 7; + * liike.toString().startsWith("7|kyykky|leveä") === true; + * + * liike.lisaaLiike(); + * int n = liike.getLid(); + * liike.parse(""+(n+20)); + * liike.lisaaLiike(); + * liike.getLid() === n+20+1; + * + * </pre> + + */ + public void parse(String rivi) { + var sb = new StringBuilder(rivi); + setLid(Mjonot.erota(sb, '|', getLid())); + liike = Mjonot.erota(sb, '|', liike); + oteLeveys = Mjonot.erota(sb, '|', oteLeveys); + stoppi = Mjonot.erota(sb, '|', stoppi); + syvyys = Mjonot.erota(sb, '|', syvyys); + suoritusValine = Mjonot.erota(sb, '|', suoritusValine); + } + + + /** * palauttaa liikkeen tiedot merkkijonona jonka voi tallentaa tiedostoon. * @return liike tolppaeroteltuna merkkijonona @@ -121,7 +164,8 @@ public class Liike { * <pre name="test"> * Liike liike = new Liike(); * liike.parse("7 | kyykky | leveä"); - * liike.toString.startsWith("7|kyykky|leveä") === true; + * liike.toString().startsWith("7|kyykky|leveä") === true; + * </pre> */ @Override public String toString() { diff --git a/puntti/src/puntti/Liikkeet.java b/puntti/src/puntti/Liikkeet.java index d33da0ae5fcc30212ecc6b6de2834dbf558a8998..6e5e68376ea394e4acb17c054891fd10b852438a 100644 --- a/puntti/src/puntti/Liikkeet.java +++ b/puntti/src/puntti/Liikkeet.java @@ -3,7 +3,9 @@ package puntti; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileInputStream; import java.io.PrintStream; +import java.util.Scanner; /** * @@ -91,6 +93,31 @@ public class Liikkeet { return lkm; } + + /** + * Lukee käyttäjän tiedostosta + * @param hakemisto tiedoston hakemisto + * @throws SailoException jos lukeminen ei onnistu + */ + public void lueTiedostosta(String hakemisto) throws SailoException{ + String tiedNimi = hakemisto +"/liikkeet.dat"; + File tiedosto = new File(tiedNimi); + + try (Scanner fi = new Scanner(new FileInputStream(tiedosto))){ + while(fi.hasNext()) { + String s = fi.nextLine(); + if (s == null || "".equals(s) || s.charAt(0) == ';') continue; + Liike liike = new Liike(); + liike.parse(s); + lisaa(liike); + } + }catch (FileNotFoundException e ) { + throw new SailoException("Ei saa luettua tiedostoa " + tiedNimi); + } + + } + + /** * @param hakemisto mihin hakemistoon tehdään uusi tiedosto * @throws SailoException jos tallentaminen ei onnistu @@ -115,6 +142,16 @@ public class Liikkeet { */ public static void main(String[] args) { Liikkeet liikkeet = new Liikkeet(); + + + try { + liikkeet.lueTiedostosta("batman"); + } catch (SailoException ex) { + + System.out.println(ex.getMessage()); + } + + Liike kyykky = new Liike(); Liike kyykky2 = new Liike(); diff --git a/puntti/src/puntti/test/LiikeTest.java b/puntti/src/puntti/test/LiikeTest.java index 088ef9f96cd1b83aaf576814342f217e8cd007ed..b4551db585fbbf0ef0793c981cd253f1e481891d 100644 --- a/puntti/src/puntti/test/LiikeTest.java +++ b/puntti/src/puntti/test/LiikeTest.java @@ -7,7 +7,7 @@ import puntti.*; /** * Test class made by ComTest - * @version 2025.03.04 23:56:18 // Generated by ComTest + * @version 2025.03.29 21:32:12 // Generated by ComTest * */ @SuppressWarnings("all") @@ -15,16 +15,42 @@ public class LiikeTest { // Generated by ComTest BEGIN - /** testLisaaLiike67 */ + /** testLisaaLiike71 */ @Test - public void testLisaaLiike67() { // Liike: 67 + public void testLisaaLiike71() { // Liike: 71 Liike kyykky1 = new Liike(); - assertEquals("From: Liike line: 69", 0, kyykky1.getLid()); + assertEquals("From: Liike line: 73", 0, kyykky1.getLid()); kyykky1.lisaaLiike(); Liike kyykky2 = new Liike(); kyykky2.lisaaLiike(); int n1 = kyykky1.getLid(); int n2 = kyykky2.getLid(); - assertEquals("From: Liike line: 75", n2-1, n1); + assertEquals("From: Liike line: 79", n2-1, n1); + } // Generated by ComTest END + + + // Generated by ComTest BEGIN + /** testParse133 */ + @Test + public void testParse133() { // Liike: 133 + Liike liike = new Liike(); + liike.parse("7 | kyykky | leveä"); + assertEquals("From: Liike line: 136", 7, liike.getLid()); + assertEquals("From: Liike line: 137", true, liike.toString().startsWith("7|kyykky|leveä")); + liike.lisaaLiike(); + int n = liike.getLid(); + liike.parse(""+(n+20)); + liike.lisaaLiike(); + assertEquals("From: Liike line: 143", n+20+1, liike.getLid()); + } // Generated by ComTest END + + + // Generated by ComTest BEGIN + /** testToString164 */ + @Test + public void testToString164() { // Liike: 164 + Liike liike = new Liike(); + liike.parse("7 | kyykky | leveä"); + assertEquals("From: Liike line: 167", true, liike.toString().startsWith("7|kyykky|leveä")); } // Generated by ComTest END } \ No newline at end of file