Skip to content
Snippets Groups Projects
Commit 9ad7dd30 authored by Nicolai Hollup Brand's avatar Nicolai Hollup Brand :penguin:
Browse files

Merge branch 'feat/create-story-gui' into 'feat/part-three'

Feat/create story gui

See merge request !34
parents fc5c8eba 152f9cc9
No related branches found
No related tags found
2 merge requests!34Feat/create story gui,!7Feat/part three
Pipeline #230893 passed
Showing
with 292 additions and 32 deletions
...@@ -3,6 +3,7 @@ package edu.ntnu.idatt2001.group_30.paths; ...@@ -3,6 +3,7 @@ package edu.ntnu.idatt2001.group_30.paths;
import edu.ntnu.idatt2001.group_30.paths.model.Player; import edu.ntnu.idatt2001.group_30.paths.model.Player;
import edu.ntnu.idatt2001.group_30.paths.model.Story; import edu.ntnu.idatt2001.group_30.paths.model.Story;
import edu.ntnu.idatt2001.group_30.paths.model.goals.*; import edu.ntnu.idatt2001.group_30.paths.model.goals.*;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -20,6 +21,7 @@ public enum PathsSingleton { ...@@ -20,6 +21,7 @@ public enum PathsSingleton {
INSTANCE; INSTANCE;
private Story story; private Story story;
private File storyFile;
private Player player = new Player("Default", 100, 100, 100); private Player player = new Player("Default", 100, 100, 100);
private boolean passageMoving = false; private boolean passageMoving = false;
private HealthGoal healthGoal; private HealthGoal healthGoal;
...@@ -44,18 +46,52 @@ public enum PathsSingleton { ...@@ -44,18 +46,52 @@ public enum PathsSingleton {
this.story = story; this.story = story;
} }
/**
* This method retrieves the file to the story.
* @return The file of the story, given as a File object.
*/
public File getStoryFile() {
return storyFile;
}
/**
* This method changes the file of the story.
* @param storyFile New file, given as a File object.
*/
public void setStoryFile(File storyFile) {
this.storyFile = storyFile;
}
/**
* This method retrieves the player.
* @return Current player of game, given as a Player object.
*/
public Player getPlayer() { public Player getPlayer() {
return player; return player;
} }
/**
* This method sets the player to a new player.
* @param player New player, given as a Player object.
*/
public void setPlayer(Player player) { public void setPlayer(Player player) {
this.player = player; this.player = player;
} }
/**
* This method allows a goal to be changed, given the goal type.
* @param newGoal New goal, given as a Goal implemented Object
*/
public void changeGoal(Goal<?> newGoal) { public void changeGoal(Goal<?> newGoal) {
setGoal(GoalType.getGoalType(newGoal.getClass().getSimpleName()), newGoal); setGoal(GoalType.getGoalType(newGoal.getClass().getSimpleName()), newGoal);
} }
/**
* This method sets a given goal to a new goal by the goal type.
* @param goalType Type of goal, given as a GoalType enum.
* @param goal New goal, given as a Goal object.
* @param <T> The type of Goal.
*/
public <T> void setGoal(GoalType goalType, Goal<?> goal) { public <T> void setGoal(GoalType goalType, Goal<?> goal) {
switch (goalType) { switch (goalType) {
case HEALTH_GOAL -> healthGoal = (HealthGoal) goal; case HEALTH_GOAL -> healthGoal = (HealthGoal) goal;
...@@ -66,22 +102,44 @@ public enum PathsSingleton { ...@@ -66,22 +102,44 @@ public enum PathsSingleton {
} }
} }
/**
* This method retrieves the health goal.
* @return Health goal, given as a HealthGoal object.
*/
public HealthGoal getHealthGoal() { public HealthGoal getHealthGoal() {
return healthGoal; return healthGoal;
} }
/**
* This method retrieves the score goal.
* @return Score goal, given as a ScoreGoal object.
*/
public ScoreGoal getScoreGoal() { public ScoreGoal getScoreGoal() {
return scoreGoal; return scoreGoal;
} }
/**
* This method retrieves the inventory goal.
* @return Inventory goal, given as a InventoryGoal object.
*/
public InventoryGoal getInventoryGoal() { public InventoryGoal getInventoryGoal() {
return inventoryGoal; return inventoryGoal;
} }
/**
* This method retrieves the gold goal.
* @return Gold goal, given as a GoldGoal object.
*/
public GoldGoal getGoldGoal() { public GoldGoal getGoldGoal() {
return goldGoal; return goldGoal;
} }
/**
* This method gets a goal variable given the GoalType.
* @param goalType Type of goal, given as a GoalType enum.
* @return The goal variable.
* @param <T> Type of goal.
*/
public <T> Goal<?> getGoal(GoalType goalType) { public <T> Goal<?> getGoal(GoalType goalType) {
return switch (goalType) { return switch (goalType) {
case HEALTH_GOAL -> healthGoal; case HEALTH_GOAL -> healthGoal;
...@@ -91,10 +149,18 @@ public enum PathsSingleton { ...@@ -91,10 +149,18 @@ public enum PathsSingleton {
}; };
} }
/**
* This method retrieves the character load out image.
* @return Image of the character created, given as an ImageView object.
*/
public ImageView getCharacterImageView() { public ImageView getCharacterImageView() {
return characterImageView; return characterImageView;
} }
/**
* This method sets the character load out.
* @param characterImageView New character image, given as an ImageView object.
*/
public void setCharacterImageView(ImageView characterImageView) { public void setCharacterImageView(ImageView characterImageView) {
this.characterImageView = characterImageView; this.characterImageView = characterImageView;
} }
...@@ -103,8 +169,8 @@ public enum PathsSingleton { ...@@ -103,8 +169,8 @@ public enum PathsSingleton {
* Returns a list of all the non-null goals. * Returns a list of all the non-null goals.
* @return A list of all the non-null goals, given as a List of Goal objects. * @return A list of all the non-null goals, given as a List of Goal objects.
*/ */
public List<Goal> getGoals() { public List<Goal<?>> getGoals() {
List<Goal> goals = Stream List<Goal<?>> goals = Stream
.of(healthGoal, scoreGoal, goldGoal) .of(healthGoal, scoreGoal, goldGoal)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
......
...@@ -21,6 +21,10 @@ public class Controller { ...@@ -21,6 +21,10 @@ public class Controller {
protected final StageManager STAGE_MANAGER = StageManager.getInstance(); protected final StageManager STAGE_MANAGER = StageManager.getInstance();
protected final Map<Class<? extends View<?>>, Runnable> availableViews = new HashMap<>(); protected final Map<Class<? extends View<?>>, Runnable> availableViews = new HashMap<>();
/**
* Creates a new Controller with the given view classes.
* @param viewClasses The view classes that this controller is responsible for.
*/
@SafeVarargs @SafeVarargs
public Controller(Class<? extends View<?>>... viewClasses) { public Controller(Class<? extends View<?>>... viewClasses) {
for (Class<? extends View<?>> viewClass : viewClasses) { for (Class<? extends View<?>> viewClass : viewClasses) {
...@@ -53,10 +57,17 @@ public class Controller { ...@@ -53,10 +57,17 @@ public class Controller {
return actionEvent -> STAGE_MANAGER.goBackTo(viewClass); return actionEvent -> STAGE_MANAGER.goBackTo(viewClass);
} }
/**
* This method is used to get the root stage of the application.
* @return The root stage of the application, which is the stage that is used to display the views.
*/
public Stage getRootStage() { public Stage getRootStage() {
return STAGE_MANAGER.getStage(); return STAGE_MANAGER.getStage();
} }
/**
* This method is used to go to the home view.
*/
public void goToHome() { public void goToHome() {
STAGE_MANAGER.setCurrentView(ViewFactory.createView(HomeView.class)); STAGE_MANAGER.setCurrentView(ViewFactory.createView(HomeView.class));
} }
......
package edu.ntnu.idatt2001.group_30.paths.controller; package edu.ntnu.idatt2001.group_30.paths.controller;
import edu.ntnu.idatt2001.group_30.paths.view.views.NewGameView; import edu.ntnu.idatt2001.group_30.paths.view.views.LoadGameView;
/**
* The class CreatePlayerController is responsible for managing the CreatePlayerView.
*
* @author Trym Hamer Gudvangen
*/
public class CreatePlayerController extends Controller { public class CreatePlayerController extends Controller {
/**
* Creates a new CreatePlayerController.
*/
public CreatePlayerController() { public CreatePlayerController() {
super(NewGameView.class); super(LoadGameView.class);
} }
} }
...@@ -9,6 +9,9 @@ import edu.ntnu.idatt2001.group_30.paths.view.views.HomeView; ...@@ -9,6 +9,9 @@ import edu.ntnu.idatt2001.group_30.paths.view.views.HomeView;
*/ */
public class HelpController extends Controller { public class HelpController extends Controller {
/**
* Creates a new HelpController.
*/
public HelpController() { public HelpController() {
super(HomeView.class); super(HomeView.class);
} }
......
...@@ -2,7 +2,7 @@ package edu.ntnu.idatt2001.group_30.paths.controller; ...@@ -2,7 +2,7 @@ package edu.ntnu.idatt2001.group_30.paths.controller;
import edu.ntnu.idatt2001.group_30.paths.view.views.CreatePlayerView; import edu.ntnu.idatt2001.group_30.paths.view.views.CreatePlayerView;
import edu.ntnu.idatt2001.group_30.paths.view.views.HelpView; import edu.ntnu.idatt2001.group_30.paths.view.views.HelpView;
import edu.ntnu.idatt2001.group_30.paths.view.views.NewGameView; import edu.ntnu.idatt2001.group_30.paths.view.views.LoadGameView;
import edu.ntnu.idatt2001.group_30.paths.view.views.PlaythroughView; import edu.ntnu.idatt2001.group_30.paths.view.views.PlaythroughView;
/** /**
...@@ -16,7 +16,7 @@ public class HomeController extends Controller { ...@@ -16,7 +16,7 @@ public class HomeController extends Controller {
* Creates a new HomeController. * Creates a new HomeController.
*/ */
public HomeController() { public HomeController() {
super(HelpView.class, NewGameView.class, CreatePlayerView.class, PlaythroughView.class); super(HelpView.class, LoadGameView.class, CreatePlayerView.class, PlaythroughView.class);
} }
/** /**
......
...@@ -3,20 +3,34 @@ package edu.ntnu.idatt2001.group_30.paths.controller; ...@@ -3,20 +3,34 @@ package edu.ntnu.idatt2001.group_30.paths.controller;
import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE; import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE;
import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileReader; import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileReader;
import edu.ntnu.idatt2001.group_30.paths.view.views.NewStoryView;
import edu.ntnu.idatt2001.group_30.paths.view.views.PlaythroughView; import edu.ntnu.idatt2001.group_30.paths.view.views.PlaythroughView;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
/**
* This class is used to control the NewGameView.
*
* @author Trym Hamer Gudvangen
*/
public class NewGameController extends Controller { public class NewGameController extends Controller {
/**
* Creates a new NewGameController.
*/
public NewGameController() { public NewGameController() {
super(PlaythroughView.class); super(PlaythroughView.class, NewStoryView.class);
} }
/**
* Sets the story to the storyFile.
* @param storyFile The story to set, as a File.
*/
public void setStory(File storyFile) { public void setStory(File storyFile) {
StoryFileReader storyFileReader = new StoryFileReader(); StoryFileReader storyFileReader = new StoryFileReader();
try { try {
INSTANCE.setStory(storyFileReader.parse(storyFile)); INSTANCE.setStory(storyFileReader.parse(storyFile));
INSTANCE.setStoryFile(storyFile);
} catch (IOException | InstantiationException ex) { } catch (IOException | InstantiationException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
......
package edu.ntnu.idatt2001.group_30.paths.controller;
import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE;
import edu.ntnu.idatt2001.group_30.paths.model.Passage;
import edu.ntnu.idatt2001.group_30.paths.model.Story;
import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileWriter;
import edu.ntnu.idatt2001.group_30.paths.view.views.NewStoryView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javafx.stage.FileChooser;
/**
* This class is used to control the NewStoryView.
*
* @author Trym Hamer Gudvangen
*/
public class NewStoryController extends Controller {
/**
* Creates a new NewStoryController.
*/
public NewStoryController() {
super(NewStoryView.class);
}
/**
* Adds a story to the PathsSingleton.
* @param title The title of the story, as a String.
* @param passages The passages of the story, as a List of Passages.
* @throws IOException If the story could not be saved to file system.
*/
public void addStory(String title, List<Passage> passages) throws IOException {
Story story = new Story(title, passages.isEmpty() ? null : passages.get(0));
passages.forEach(story::addPassage);
INSTANCE.setStory(story);
saveStory(story);
}
/**
* This method saves the story to the file system.
* @param story The story to save, as a Story.
* @throws IOException If the story could not be saved to file system.
*/
public void saveStory(Story story) throws IOException {
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(new File("./src/main/resources/story-files"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Paths files", "*.paths"));
File selectedFile = fileChooser.showSaveDialog(null);
if (selectedFile != null) {
StoryFileWriter storyFileWriter = new StoryFileWriter();
storyFileWriter.create(story, selectedFile);
} else {
throw new FileNotFoundException("File was not saved to file system");
}
INSTANCE.setStoryFile(selectedFile);
}
}
...@@ -20,7 +20,7 @@ import javafx.scene.image.ImageView; ...@@ -20,7 +20,7 @@ import javafx.scene.image.ImageView;
* *
* @author Nicolai H. Brand. * @author Nicolai H. Brand.
*/ */
public class PlaytroughController extends Controller { public class PlaythroughController extends Controller {
private Playthrough playthrough; private Playthrough playthrough;
...@@ -30,7 +30,7 @@ public class PlaytroughController extends Controller { ...@@ -30,7 +30,7 @@ public class PlaytroughController extends Controller {
private final StringProperty passageContent = new SimpleStringProperty(); private final StringProperty passageContent = new SimpleStringProperty();
private final ObservableList<Link> links = FXCollections.observableList(new ArrayList<>()); private final ObservableList<Link> links = FXCollections.observableList(new ArrayList<>());
private final ObservableList<String> inventory = FXCollections.observableList(new ArrayList<>()); private final ObservableList<String> inventory = FXCollections.observableList(new ArrayList<>());
private final ObservableMap<Goal, Boolean> goals = FXCollections.observableMap(new HashMap<>()); private final ObservableMap<Goal<?>, Boolean> goals = FXCollections.observableMap(new HashMap<>());
private final StringProperty health = new SimpleStringProperty(); private final StringProperty health = new SimpleStringProperty();
private final StringProperty score = new SimpleStringProperty(); private final StringProperty score = new SimpleStringProperty();
private final StringProperty gold = new SimpleStringProperty(); private final StringProperty gold = new SimpleStringProperty();
...@@ -40,7 +40,7 @@ public class PlaytroughController extends Controller { ...@@ -40,7 +40,7 @@ public class PlaytroughController extends Controller {
* Creates a new instance of the controller. * Creates a new instance of the controller.
* It initializes the controller and starts a new game. * It initializes the controller and starts a new game.
*/ */
public PlaytroughController() { public PlaythroughController() {
super(HomeView.class, HelpView.class); super(HomeView.class, HelpView.class);
startNewPlaythrough(); startNewPlaythrough();
} }
...@@ -178,7 +178,7 @@ public class PlaytroughController extends Controller { ...@@ -178,7 +178,7 @@ public class PlaytroughController extends Controller {
* Returns the goals of the game as an observable map. * Returns the goals of the game as an observable map.
* @return the goals of the game. * @return the goals of the game.
*/ */
public ObservableMap<Goal, Boolean> getGoals() { public ObservableMap<Goal<?>, Boolean> getGoals() {
return goals; return goals;
} }
......
...@@ -9,7 +9,7 @@ import java.util.List; ...@@ -9,7 +9,7 @@ import java.util.List;
* *
* @author Nicolai H. Brand, Trym Hamer Gudvangen * @author Nicolai H. Brand, Trym Hamer Gudvangen
*/ */
public record Game(Player player, Story story, List<Goal> goals) { public record Game(Player player, Story story, List<Goal<?>> goals) {
/** /**
* This method constructs a Game object with the given parameters. * This method constructs a Game object with the given parameters.
* *
......
...@@ -13,7 +13,7 @@ import java.util.Objects; ...@@ -13,7 +13,7 @@ import java.util.Objects;
*/ */
public class Story { public class Story {
private final String title; private String title;
private final Map<Link, Passage> passages; private final Map<Link, Passage> passages;
private final Passage openingPassage; private final Passage openingPassage;
...@@ -25,13 +25,7 @@ public class Story { ...@@ -25,13 +25,7 @@ public class Story {
* @throws IllegalArgumentException This exception is thrown if title or openingPassage is invalid * @throws IllegalArgumentException This exception is thrown if title or openingPassage is invalid
*/ */
public Story(String title, Passage openingPassage) throws IllegalArgumentException { public Story(String title, Passage openingPassage) throws IllegalArgumentException {
//if (title.isBlank() || !title.matches("[a-zA-Z]")) { setTitle(title);
// throw new IllegalArgumentException("Title cannot be blank, empty, or contain special characters.");
//}
if (title.isBlank()) throw new IllegalArgumentException(
"Title cannot be blank, empty, or contain special characters."
);
this.title = title;
if (openingPassage == null) throw new IllegalArgumentException("Opening passage cannot be null"); if (openingPassage == null) throw new IllegalArgumentException("Opening passage cannot be null");
this.openingPassage = openingPassage; this.openingPassage = openingPassage;
this.passages = new HashMap<>(); this.passages = new HashMap<>();
...@@ -107,6 +101,17 @@ public class Story { ...@@ -107,6 +101,17 @@ public class Story {
return title; return title;
} }
/**
* This method sets the title of the story.
* @param title The new title of the story, given as a {@code String}.
*/
public void setTitle(String title) {
if (title == null || title.isBlank()) throw new IllegalArgumentException(
"Title cannot be blank, empty, or contain special characters."
);
this.title = title;
}
/** /**
* This method retrieves all the passages of a story. * This method retrieves all the passages of a story.
* @return All the passages of the Story as a {@code Collection<Passages>}. * @return All the passages of the Story as a {@code Collection<Passages>}.
......
...@@ -6,8 +6,18 @@ package edu.ntnu.idatt2001.group_30.paths.model.actions; ...@@ -6,8 +6,18 @@ package edu.ntnu.idatt2001.group_30.paths.model.actions;
* @author Trym Hamer Gudvangen * @author Trym Hamer Gudvangen
*/ */
public enum ActionType { public enum ActionType {
GOLD_ACTION, GOLD_ACTION("Gold Action"),
HEALTH_ACTION, HEALTH_ACTION("Health Action"),
INVENTORY_ACTION, INVENTORY_ACTION("Inventory Action"),
SCORE_ACTION, SCORE_ACTION("Score Action");
private final String displayName;
ActionType(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
} }
...@@ -18,7 +18,6 @@ public class GoldAction implements Action<Integer> { ...@@ -18,7 +18,6 @@ public class GoldAction implements Action<Integer> {
*/ */
public GoldAction(int gold) { public GoldAction(int gold) {
this.gold = gold; this.gold = gold;
//TODO: Add exception?
} }
/** /**
......
...@@ -18,7 +18,6 @@ public class HealthAction implements Action<Integer> { ...@@ -18,7 +18,6 @@ public class HealthAction implements Action<Integer> {
*/ */
public HealthAction(int health) { public HealthAction(int health) {
this.health = health; this.health = health;
//TODO: Add exception?
} }
/** /**
......
...@@ -18,7 +18,6 @@ public class InventoryAction implements Action<String> { ...@@ -18,7 +18,6 @@ public class InventoryAction implements Action<String> {
*/ */
public InventoryAction(String item) { public InventoryAction(String item) {
this.item = item; this.item = item;
//TODO: Add exception?
} }
/** /**
......
...@@ -18,7 +18,6 @@ public class ScoreAction implements Action<Integer> { ...@@ -18,7 +18,6 @@ public class ScoreAction implements Action<Integer> {
*/ */
public ScoreAction(int points) { public ScoreAction(int points) {
this.points = points; this.points = points;
//TODO: Add exception?
} }
/** /**
......
...@@ -20,7 +20,24 @@ import java.util.*; ...@@ -20,7 +20,24 @@ import java.util.*;
public class StoryFileWriter { public class StoryFileWriter {
/** /**
* Creates a new story file with the given name and writes the story to it. * 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 written to the file. * @param story The story to be written to the file.
* @param fileName The name of the file to be created. * @param fileName The name of the file to be created.
* @throws IOException if an I/O error occurs with the writer, or if the file already exists. * @throws IOException if an I/O error occurs with the writer, or if the file already exists.
...@@ -30,6 +47,37 @@ public class StoryFileWriter { ...@@ -30,6 +47,37 @@ public class StoryFileWriter {
Objects.requireNonNull(fileName, "File name cannot be null"); Objects.requireNonNull(fileName, "File name cannot be null");
File file = FileHandler.createFile(fileName); File file = FileHandler.createFile(fileName);
create(story, file);
}
/**
* 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 written to the file.
* @param file The file the story is going to be written to.
* @throws IOException if an I/O error occurs with the writer, or if the file already exists.
*/
public void create(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 FileAlreadyExistsException( if (FileHandler.fileExists(file)) throw new FileAlreadyExistsException(
"You cannot overwrite a pre-existing story file" "You cannot overwrite a pre-existing story file"
); );
...@@ -38,6 +86,8 @@ public class StoryFileWriter { ...@@ -38,6 +86,8 @@ public class StoryFileWriter {
writeStory(story, file); writeStory(story, file);
} }
//TODO: add test for story files...
/** /**
* Writes the story to the given file. * Writes the story to the given file.
* @param story The story to be written to the file. * @param story The story to be written to the file.
......
package edu.ntnu.idatt2001.group_30.paths.model.goals; package edu.ntnu.idatt2001.group_30.paths.model.goals;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/**
* This class is a factory for creating Goal objects.
*
* @author Trym Hamer Gudvangen
*/
public class GoalFactory { public class GoalFactory {
/**
* This method creates a Goal object based on the given goal type and goal value.
* @param goalType The type of goal, given as a GoalType object.
* @param goalValue The value of the goal, given as an Object.
* @return A Goal object.
* @throws IllegalArgumentException If the goal type or value is invalid.
*/
public static Goal<?> getGoal(GoalType goalType, Object goalValue) throws IllegalArgumentException { public static Goal<?> getGoal(GoalType goalType, Object goalValue) throws IllegalArgumentException {
switch (goalType) { switch (goalType) {
case GOLD_GOAL, HEALTH_GOAL, SCORE_GOAL -> { case GOLD_GOAL, HEALTH_GOAL, SCORE_GOAL -> {
...@@ -37,6 +48,13 @@ public class GoalFactory { ...@@ -37,6 +48,13 @@ public class GoalFactory {
throw new IllegalArgumentException("Invalid goal type or value"); throw new IllegalArgumentException("Invalid goal type or value");
} }
/**
* This method creates a Goal object based on the given goal type and goal value.
* @param goalType The type of goal, given as a String.
* @param goalValue The value of the goal, given as an Object.
* @return A Goal object.
* @throws IllegalArgumentException If the goal type or value is invalid.
*/
public static Goal<?> getGoal(String goalType, Object goalValue) throws IllegalArgumentException { public static Goal<?> getGoal(String goalType, Object goalValue) throws IllegalArgumentException {
return getGoal(GoalType.getGoalType(goalType), goalValue); return getGoal(GoalType.getGoalType(goalType), goalValue);
} }
......
...@@ -3,6 +3,11 @@ package edu.ntnu.idatt2001.group_30.paths.model.goals; ...@@ -3,6 +3,11 @@ package edu.ntnu.idatt2001.group_30.paths.model.goals;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* This enum represents the different types of goals.
*
* @author Trym Hamer Gudvangen
*/
public enum GoalType { public enum GoalType {
GOLD_GOAL("GoldGoal"), GOLD_GOAL("GoldGoal"),
HEALTH_GOAL("HealthGoal"), HEALTH_GOAL("HealthGoal"),
...@@ -18,14 +23,27 @@ public enum GoalType { ...@@ -18,14 +23,27 @@ public enum GoalType {
} }
} }
/**
* This constructor creates a GoalType object based on the given string value.
* @param stringVal The string value of the GoalType object.
*/
GoalType(String stringVal) { GoalType(String stringVal) {
this.stringVal = stringVal; this.stringVal = stringVal;
} }
/**
* This method retrieves the GoalType object based on the given string value.
* @param goalType The string value of the GoalType object.
* @return The GoalType object.
*/
public static GoalType getGoalType(String goalType) { public static GoalType getGoalType(String goalType) {
return stringToEnum.get(goalType); return stringToEnum.get(goalType);
} }
/**
* This method retrieves the string value of the GoalType object.
* @return The string value of the GoalType object.
*/
public String getStringVal() { public String getStringVal() {
return stringVal; return stringVal;
} }
......
...@@ -18,7 +18,6 @@ public class GoldGoal implements Goal<Integer> { ...@@ -18,7 +18,6 @@ public class GoldGoal implements Goal<Integer> {
*/ */
public GoldGoal(int minimumGold) { public GoldGoal(int minimumGold) {
this.minimumGold = minimumGold; this.minimumGold = minimumGold;
//TODO: Add exception?
} }
/** /**
......
...@@ -18,7 +18,6 @@ public class ScoreGoal implements Goal<Integer> { ...@@ -18,7 +18,6 @@ public class ScoreGoal implements Goal<Integer> {
*/ */
public ScoreGoal(int minimumPoints) { public ScoreGoal(int minimumPoints) {
this.minimumPoints = minimumPoints; this.minimumPoints = minimumPoints;
//TODO: Add exception?
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment