- Method toString done

This commit is contained in:
Andrin Fassbind
2022-03-06 16:10:18 +01:00
parent 8ff70ab3ac
commit 571728b6fd
2 changed files with 47 additions and 19 deletions
+38 -6
View File
@@ -6,6 +6,7 @@ import ch.zhaw.pm2.racetrack.given.TrackSpecification;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
@@ -61,6 +62,8 @@ public class Track implements TrackSpecification {
// TODO: Add necessary variables
private List<String> track;
private List<Car> cars;
/**
* Initialize a Track from the given track file.
*
@@ -69,7 +72,9 @@ public class Track implements TrackSpecification {
* @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...)
*/
public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException {
// TODO: implementation
// TODO:
track = new ArrayList<>();
cars = new ArrayList<>();
readFile(trackFile);
//TODO: throw error again...
@@ -78,20 +83,43 @@ public class Track implements TrackSpecification {
/**
* 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());
}
}
public void findCar() {
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
ArrayList<Character> allSpaceTypesAsChar = new ArrayList<>();
for (ConfigSpecification.SpaceType spaceType : spaceTypes) {
allSpaceTypesAsChar.add(spaceType.getValue());
}
for (String line : track) {
for (int i = 0; i < line.length(); i++) {
if (!allSpaceTypesAsChar.contains(line.charAt(i))) {
//TODO: CREATE CAR - ADD IT TO CARS - BUT CHECK THAT Symbol is not allready used for another car?
}
}
}
}
private void findFinish() {
//TODO:
}
/**
*
* @return the track
*/
public List<String> getTrack() {
@@ -111,9 +139,8 @@ public class Track implements TrackSpecification {
char charAtPosition = track.get(position.getY()).charAt(position.getX());
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
for (ConfigSpecification.SpaceType spaceType : spaceTypes) {
if(spaceType.getValue() == charAtPosition) {
if (spaceType.getValue() == charAtPosition) {
return spaceType;
}
}
@@ -203,6 +230,11 @@ public class Track implements TrackSpecification {
@Override
public String toString() {
// TODO: implementation
throw new UnsupportedOperationException();
String str = "";
for (String line : track) {
str += line+"\n";
}
return str;
//throw new UnsupportedOperationException();
}
}