Skip to content
Snippets Groups Projects
Commit c4983021 authored by Trym Hamer Gudvangen's avatar Trym Hamer Gudvangen
Browse files

feat: add ability to create a file by a File object

parent 177f548e
No related branches found
No related tags found
2 merge requests!34Feat/create story gui,!7Feat/part three
......@@ -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());
......
......@@ -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);
}
);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment