diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java index 90050c1..3053f88 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java @@ -3,12 +3,17 @@ import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock; import edu.ntnu.idi.idatt2003.g40.mappe.service.FileConverter; import edu.ntnu.idi.idatt2003.g40.mappe.service.FileParser; +import edu.ntnu.idi.idatt2003.g40.mappe.service.SaveGameService; import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; import edu.ntnu.idi.idatt2003.g40.mappe.utils.ConfigValues; import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager; import edu.ntnu.idi.idatt2003.g40.mappe.view.ingame.InGameView; import edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu.MainMenuController; import edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu.MainMenuView; +import edu.ntnu.idi.idatt2003.g40.mappe.view.playgame.PlayGameController; +import edu.ntnu.idi.idatt2003.g40.mappe.view.playgame.PlayGameView; +import edu.ntnu.idi.idatt2003.g40.mappe.view.settings.SettingsController; +import edu.ntnu.idi.idatt2003.g40.mappe.view.settings.SettingsView; import java.io.IOException; import java.util.List; import java.util.Objects; @@ -26,6 +31,7 @@ *

Initializes the application through the javafx framework.

* */ public class Main extends Application { + static void main() { FileParser parser1 = new FileParser("src/main/resources/dummydata.txt"); FileConverter converter1 = new FileConverter(); @@ -50,17 +56,37 @@ public void start(final Stage stage) throws Exception { stage.setScene(scene); stage.setWidth(ConfigValues.VIEWPORT_WIDTH.getValue()); stage.setHeight(ConfigValues.VIEWPORT_HEIGHT.getValue()); + EventManager eventManager = new EventManager(); ViewManager viewManager = new ViewManager(stage, eventManager); + // Main menu MainMenuView mainMenuView = new MainMenuView(); new MainMenuController(mainMenuView, eventManager); + // Play game (mellom hovedmeny og spillet) + PlayGameView playGameView = new PlayGameView(); + new PlayGameController(playGameView, eventManager); + + // Last lagrede spill fra disk. + SaveGameService saveGameService = new SaveGameService(); + playGameView.setSaves(saveGameService.loadSaves()); + + + // Settings + SettingsView settingsView = new SettingsView(); + new SettingsController(settingsView, eventManager); + + // In-game InGameView inGameView = new InGameView(); + // Registrer alle views og start på hovedmenyen viewManager.addView(mainMenuView); + viewManager.addView(playGameView); + viewManager.addView(settingsView); viewManager.addView(inGameView); viewManager.setScene(mainMenuView); + stage.show(); } -} +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/SaveGame.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/SaveGame.java new file mode 100644 index 0000000..e22273f --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/SaveGame.java @@ -0,0 +1,47 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.model; + +/** + * Represents one save game entry. + * + *

+ * Holds the display name and the current balance for a single + * saved game. + *

+ */ +public class SaveGame { + + /** Display name of the save. */ + private final String name; + + /** Current balance in the save. */ + private final double balance; + + /** + * Constructor. + * + * @param name the display name of the save. + * @param balance the current balance value. + */ + public SaveGame(final String name, final double balance) { + this.name = name; + this.balance = balance; + } + + /** + * Getter method for the name. + * + * @return the save name. + */ + public String getName() { + return name; + } + + /** + * Getter method for the balance. + * + * @return the balance value. + */ + public double getBalance() { + return balance; + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/SaveGameService.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/SaveGameService.java new file mode 100644 index 0000000..82b310d --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/SaveGameService.java @@ -0,0 +1,107 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.service; + +import edu.ntnu.idi.idatt2003.g40.mappe.model.SaveGame; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +/** + * Service for loading and saving {@link SaveGame} entries from disk. + * + *

+ * Save file format (one entry per line): + *

+ * + *
+ * # Comment lines start with hash
+ * SaveName, 1234567.89
+ * 
+ * + *

+ * Lines that don't match the expected format are skipped. + *

+ */ +public class SaveGameService { + + /** Default location of the save file. */ + private static final String DEFAULT_PATH = "src/main/resources/saves.txt"; + + /** Path to the save file. */ + private final String filePath; + + /** + * Constructor with default path. + */ + public SaveGameService() { + this(DEFAULT_PATH); + } + + /** + * Constructor with custom path. + * + * @param filePath the path to the save file. + */ + public SaveGameService(final String filePath) { + this.filePath = filePath; + } + + /** + * Loads all save games from the file. + * + *

+ * Returns an empty list if the file cannot be read or is empty. + *

+ * + * @return the loaded {@link SaveGame} entries. + */ + public List loadSaves() { + List saves = new ArrayList<>(); + Path path = Paths.get(filePath); + try { + List lines = Files.readAllLines(path); + for (String line : lines) { + SaveGame save = parseLine(line); + if (save != null) { + saves.add(save); + } + } + } catch (IOException e) { + System.err.println("Could not read save file: " + e.getMessage()); + } + return saves; + } + + /** + * Parses a single line into a {@link SaveGame}. + * + *

+ * Returns null for invalid lines, comment lines, and blank lines. + *

+ * + * @param line the raw line from the file. + * @return the parsed {@link SaveGame}, or null if the line is invalid. + */ + private SaveGame parseLine(final String line) { + if (line == null || line.isBlank() || line.trim().startsWith("#")) { + return null; + } + String[] parts = line.split(","); + if (parts.length != 2) { + return null; + } + try { + String name = parts[0].trim(); + double balance = Double.parseDouble(parts[1].trim()); + if (name.isEmpty()) { + return null; + } + return new SaveGame(name, balance); + } catch (NumberFormatException e) { + return null; + } + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java index 554ccfc..aa0f941 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java @@ -146,4 +146,11 @@ public ArrayList