-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
+42 -3
View File
@@ -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<String> 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<String> 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();
}