diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandler.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandler.java index cbcd63ca02489f32015c72e517f13252442a0ea7..358227ecd9c0e93c00a7f19f463a6dedc696d541 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandler.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandler.java @@ -45,11 +45,40 @@ public class StoryFileHandler { * @throws IOException This exception is thrown if an I/O error occurs with the writer. */ public void createStoryFile(Story story, String fileName) throws IOException { - Objects.requireNonNull(story, "Story cannot be null"); Objects.requireNonNull(fileName, "File name cannot be null"); File file = FileHandler.createFile(fileName); + createStoryFile(story, file); + } + + //TODO: add test for story files... + + /** + * This method takes a story and writes its contents to a .paths file. The story information is transcribed + * in the given format: + * <pre> + * Story title + * + * ::Opening Passage Title + * Opening Passage Content + * [Link Text](Link Reference) + * + * ::Another Passage Title + * Passage Content + * [Link Text](Link Reference) + * {@code <Action Type>}\Action Value/ + * [Link Text](Link Reference) + * + * ... + * </pre> + * @param story The story to be saved, given as a Story object. + * @param file The file the story will be saved to, given as a File object. + * @throws IOException This exception is thrown if an I/O error occurs with the writer. + */ + public void createStoryFile(Story story, File file) throws IOException { + Objects.requireNonNull(story, "Story cannot be null"); + Objects.requireNonNull(file, "File cannot be null"); if (FileHandler.fileExists(file)) throw new IllegalArgumentException( - "You cannot overwrite a pre-existing story file" + "You cannot overwrite a pre-existing story file" ); try (BufferedWriter storyBufferedWriter = new BufferedWriter(new FileWriter(file))) { storyBufferedWriter.write(story.toString()); diff --git a/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandlerTest.java b/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandlerTest.java index b7d7a4da7f4b84096514102fb907069a2456fd51..f32e1eee340bcd7d25b1457af0e2a4a5f9d637c1 100644 --- a/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandlerTest.java +++ b/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileHandlerTest.java @@ -12,8 +12,6 @@ import edu.ntnu.idatt2001.group_30.paths.model.actions.GoldAction; import edu.ntnu.idatt2001.group_30.paths.model.actions.HealthAction; import edu.ntnu.idatt2001.group_30.paths.model.actions.InventoryAction; import edu.ntnu.idatt2001.group_30.paths.model.actions.ScoreAction; -import edu.ntnu.idatt2001.group_30.paths.model.filehandling.FileHandler; -import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileHandler; import java.io.*; import java.nio.file.FileSystems; import java.nio.file.Path; @@ -234,7 +232,7 @@ class StoryFileHandlerTest { Assertions.assertThrows( NullPointerException.class, () -> { - storyFileHandler.createStoryFile(story, null); + storyFileHandler.createStoryFile(story, (String) null); } ); }