From 8ff70ab3ac552f6a5494e6b2959a33c94fafa182 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sun, 6 Mar 2022 15:15:39 +0100 Subject: [PATCH] -Method readFile done and tested -Method getSpaceType done and once tested --- .../java/ch/zhaw/pm2/racetrack/Track.java | 45 ++++++++++++++- .../java/ch/zhaw/pm2/racetrack/TrackTest.java | 55 +++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 61deadf..68e5625 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -1,9 +1,13 @@ package ch.zhaw.pm2.racetrack; +import ch.zhaw.pm2.racetrack.given.ConfigSpecification; import ch.zhaw.pm2.racetrack.given.TrackSpecification; import java.io.File; import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; /** * This class represents the racetrack board. @@ -56,7 +60,7 @@ public class Track implements TrackSpecification { public static final char CRASH_INDICATOR = 'X'; // TODO: Add necessary variables - + private List track; /** * Initialize a Track from the given track file. * @@ -66,7 +70,32 @@ public class Track implements TrackSpecification { */ public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException { // TODO: implementation - throw new UnsupportedOperationException(); + readFile(trackFile); + + //TODO: throw error again... + //throw new UnsupportedOperationException(); // was allready in !! + } + + /** + * This method reads the File and saves it to the track ArrayList Line by Line + * @param trackFile the File where the track has been documented + * @throws FileNotFoundException if the FilePath is invalid. + */ + private void readFile(File trackFile) throws FileNotFoundException { + track = new ArrayList<>(); + Scanner scanner = new Scanner(trackFile); + while (scanner.hasNextLine()) { + track.add(scanner.nextLine()); + } + } + + + /** + * + * @return the track + */ + public List getTrack() { + return track; } /** @@ -78,7 +107,17 @@ public class Track implements TrackSpecification { */ @Override public Config.SpaceType getSpaceType(PositionVector position) { - // TODO: implementation + // TODO: implementation // Done first tests passed but what to do with error + char charAtPosition = track.get(position.getY()).charAt(position.getX()); + + ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values(); + + for (ConfigSpecification.SpaceType spaceType : spaceTypes) { + if(spaceType.getValue() == charAtPosition) { + return spaceType; + } + } + throw new UnsupportedOperationException(); } diff --git a/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java b/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java new file mode 100644 index 0000000..7034a97 --- /dev/null +++ b/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java @@ -0,0 +1,55 @@ +package ch.zhaw.pm2.racetrack; + +import ch.zhaw.pm2.racetrack.given.ConfigSpecification; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.FileNotFoundException; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class TrackTest { + Track trackObj; + + @BeforeEach + void setup() { + File file = new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt"); + try { + trackObj = new Track(file); + + } catch (Exception e) { + System.err.println("Error in Test compareTrack" + e.getMessage()); + } + } + + @Test + void canReadFile() { + File file = new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt"); + Assertions.assertThrows(FileNotFoundException.class,() -> new Track(file)); + } + + /** + * Dirty test... + */ + @Test + void compareTrack() { + try { + List track = trackObj.getTrack(); + for (String s: track + ) { + System.out.println(s); + } + } catch (Exception e) { + System.err.println("Error in Test compareTrack" + e.getMessage()); + } + } + + @Test + void getSpaceTyp() { + Assertions.assertEquals(ConfigSpecification.SpaceType.FINISH_RIGHT,trackObj.getSpaceType(new PositionVector(22,24))); + } +}