-Method readFile done and tested

-Method getSpaceType done and once tested
This commit is contained in:
Andrin Fassbind 2022-03-06 15:15:39 +01:00
parent 16d287e273
commit 8ff70ab3ac
2 changed files with 97 additions and 3 deletions

View File

@ -1,9 +1,13 @@
package ch.zhaw.pm2.racetrack; package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.given.ConfigSpecification;
import ch.zhaw.pm2.racetrack.given.TrackSpecification; import ch.zhaw.pm2.racetrack.given.TrackSpecification;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/** /**
* This class represents the racetrack board. * This class represents the racetrack board.
@ -56,7 +60,7 @@ public class Track implements TrackSpecification {
public static final char CRASH_INDICATOR = 'X'; public static final char CRASH_INDICATOR = 'X';
// TODO: Add necessary variables // TODO: Add necessary variables
private List<String> track;
/** /**
* Initialize a Track from the given track file. * Initialize a Track from the given track file.
* *
@ -66,7 +70,32 @@ public class Track implements TrackSpecification {
*/ */
public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException { public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException {
// TODO: implementation // 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<String> getTrack() {
return track;
} }
/** /**
@ -78,7 +107,17 @@ public class Track implements TrackSpecification {
*/ */
@Override @Override
public Config.SpaceType getSpaceType(PositionVector position) { 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(); throw new UnsupportedOperationException();
} }

View File

@ -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<String> 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)));
}
}