-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
EspenTinius
committed
May 12, 2026
1 parent
2832c97
commit 7c4d022
Showing
10 changed files
with
567 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/SaveGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.model; | ||
|
|
||
| /** | ||
| * Represents one save game entry. | ||
| * | ||
| * <p> | ||
| * Holds the display name and the current balance for a single | ||
| * saved game. | ||
| * </p> | ||
| */ | ||
| 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; | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/SaveGameService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| * | ||
| * <p> | ||
| * Save file format (one entry per line): | ||
| * </p> | ||
| * | ||
| * <pre> | ||
| * # Comment lines start with hash | ||
| * SaveName, 1234567.89 | ||
| * </pre> | ||
| * | ||
| * <p> | ||
| * Lines that don't match the expected format are skipped. | ||
| * </p> | ||
| */ | ||
| 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. | ||
| * | ||
| * <p> | ||
| * Returns an empty list if the file cannot be read or is empty. | ||
| * </p> | ||
| * | ||
| * @return the loaded {@link SaveGame} entries. | ||
| */ | ||
| public List<SaveGame> loadSaves() { | ||
| List<SaveGame> saves = new ArrayList<>(); | ||
| Path path = Paths.get(filePath); | ||
| try { | ||
| List<String> 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}. | ||
| * | ||
| * <p> | ||
| * Returns null for invalid lines, comment lines, and blank lines. | ||
| * </p> | ||
| * | ||
| * @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; | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.