diff --git a/src/main/java/edu/ntnu/idi/idatt/Launcher.java b/src/main/java/edu/ntnu/idi/idatt/Launcher.java index f13f7fd..9fc9145 100644 --- a/src/main/java/edu/ntnu/idi/idatt/Launcher.java +++ b/src/main/java/edu/ntnu/idi/idatt/Launcher.java @@ -1,6 +1,5 @@ package edu.ntnu.idi.idatt; -import edu.ntnu.idi.idatt.storage.CSVtoJSON; import edu.ntnu.idi.idatt.view.SceneManager; import edu.ntnu.idi.idatt.view.entry.StartController; import edu.ntnu.idi.idatt.view.entry.StartModel; @@ -34,8 +33,6 @@ public void start(Stage stage) { SceneManager.init(stage, view.getInstance()); stage.show(); - CSVtoJSON ja = new CSVtoJSON(); - ja.csvFile(); } } diff --git a/src/main/java/edu/ntnu/idi/idatt/model/Exchange.java b/src/main/java/edu/ntnu/idi/idatt/model/Exchange.java index 84d2d9d..d009ed1 100644 --- a/src/main/java/edu/ntnu/idi/idatt/model/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt/model/Exchange.java @@ -1,6 +1,5 @@ package edu.ntnu.idi.idatt.model; -import java.io.IOException; import java.math.BigDecimal; import java.util.*; @@ -10,7 +9,6 @@ import edu.ntnu.idi.idatt.model.transaction.Purchase; import edu.ntnu.idi.idatt.model.transaction.Sale; import edu.ntnu.idi.idatt.model.transaction.Transaction; -import edu.ntnu.idi.idatt.storage.ExchangeLoader; /** * Exchange class @@ -22,29 +20,23 @@ *

* */ -public class Exchange extends ExchangeLoader { +public class Exchange { private final String name; private int week; private HashMap stockMap = new HashMap<>(); - private Random random = new Random(); /** * Constructor for Exchange class * - * @param name - Name of the current stock Exchange - * @param path - Path to .csv file. + * @param name - Name of the current stock Exchange + * @param stocks - List of stocks for this exchange */ - public Exchange(String name, String path) { - super(path); + public Exchange(String name, List stocks) { this.name = name; this.week = 1; - try { - this.load().forEach(stock -> stockMap.put(stock.getSymbol(), stock)); - } catch (IOException e) { - throw new IllegalArgumentException("Problem loading [" + name + "] exchange : " + e); - } + stocks.forEach(stock -> stockMap.put(stock.getSymbol(), stock)); } @@ -164,7 +156,8 @@ public List getLosers(int limit) { * @see Transaction */ public Transaction buy(String symbol, BigDecimal quantity, Player player) { - Share share = new Share(getStock(symbol), quantity, BigDecimal.valueOf(random.nextDouble())); + Stock stock = getStock(symbol); + Share share = new Share(stock, quantity, stock.getSalesPrice()); Purchase purchase = new Purchase(share, this.week); purchase.commit(player); return player.getTransactionArchive().getPurchases(this.week).getLast(); @@ -201,16 +194,6 @@ public Transaction sell(Share share, Player player) { * @see Stock */ public void advance() { - for (Stock stocks : stockMap.values()) { - stocks.addNewSalesPrice(BigDecimal.valueOf(random.nextDouble())); - - // TODO: Move this to JavaFx on Window close? - try { - this.save(stockMap.values().stream().toList()); - } catch (IOException e) { - throw new IllegalArgumentException("Problem loading [" + name + "] exchange : " + e); - } - } } } diff --git a/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java b/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java index 88bdfeb..923df9f 100644 --- a/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java @@ -91,7 +91,7 @@ public BigDecimal getLatestPriceChange() { } /** - * Getter for sale price + * Getter for current sale price * * @return - BigDecimal with current (newest in array) stock price. */ diff --git a/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java b/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java index c8a2863..841a22a 100644 --- a/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java +++ b/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java @@ -1,5 +1,6 @@ package edu.ntnu.idi.idatt.session; +import edu.ntnu.idi.idatt.model.Exchange; import edu.ntnu.idi.idatt.model.player.Player; public class UserSession { @@ -19,6 +20,7 @@ public static UserSession getInstance() { } private Player player; + private Exchange exchange; public Player getPlayer() { return player; @@ -28,4 +30,36 @@ public void setPlayer(Player player) { this.player = player; } + public Exchange getExchange() { + return exchange; + } + + public void setExchange(Exchange exchange) { + this.exchange = exchange; + } + + public SessionBundle getSession() { + return new SessionBundle(player, exchange); + } + + public class SessionBundle { + + private Player player; + private Exchange exchange; + + public SessionBundle(Player player, Exchange exchange) { + this.player = player; + this.exchange = exchange; + } + + public Player getPlayer() { + return player; + } + + public Exchange getExchange() { + return exchange; + } + + } + } diff --git a/src/main/java/edu/ntnu/idi/idatt/storage/CSVtoJSON.java b/src/main/java/edu/ntnu/idi/idatt/storage/CSVtoJSON.java deleted file mode 100644 index cf91bec..0000000 --- a/src/main/java/edu/ntnu/idi/idatt/storage/CSVtoJSON.java +++ /dev/null @@ -1,33 +0,0 @@ -package edu.ntnu.idi.idatt.storage; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; - -import java.io.*; -import java.nio.charset.StandardCharsets; -import java.util.stream.Collectors; - - -public class CSVtoJSON { - - public void csvFile(){ - String csvData; - try (InputStream rawData = CSVtoJSON.class.getResourceAsStream("/stocks.csv")){ - csvData = new BufferedReader(new InputStreamReader(rawData, StandardCharsets.UTF_8)) - .lines() - .collect(Collectors.joining(" ")); - } catch (IOException e) { - throw new RuntimeException(e); - } - - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - try (Writer writer = new FileWriter("src/main/resources/save.json")) { - gson.toJson(csvData, writer); - } catch (IOException e) { - e.printStackTrace(); - } - } -} - diff --git a/src/main/java/edu/ntnu/idi/idatt/storage/ExchangeLoader.java b/src/main/java/edu/ntnu/idi/idatt/storage/ExchangeLoader.java deleted file mode 100644 index 71b4e18..0000000 --- a/src/main/java/edu/ntnu/idi/idatt/storage/ExchangeLoader.java +++ /dev/null @@ -1,100 +0,0 @@ -package edu.ntnu.idi.idatt.storage; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.math.BigDecimal; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -import edu.ntnu.idi.idatt.model.market.Stock; - -public class ExchangeLoader { - - private final File file; - - /** - * Constructors for ExchangeLoader - * - *

- * Utilizes method overloading for different types - * of path formatting. - *

- * - * @throws IllegalArgumentException if specified path doesn't exist. - */ - protected ExchangeLoader(String path) { - file = new File(path); - if (!file.exists()) { - throw new IllegalArgumentException("File at this path doesn't exist!"); - } - } - - protected ExchangeLoader(URL path) { - file = new File(path.toString()); - if (!file.exists()) { - throw new IllegalArgumentException("File at this path doesn't exist!"); - } - } - - /** - * Method for loading from stocks from file - * - * @return a list of loaded stocks. - * @throws IOException on BufferedReader error - */ - protected List load() throws IOException { - - ArrayList stocks = new ArrayList<>(); - - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - List stockStringList = new ArrayList<>(reader.readAllLines()); - - // Remove comments - stockStringList.removeIf(s -> s.isBlank()); - stockStringList.removeIf(s -> s.startsWith("#")); - - for (String stockString : stockStringList) { - String[] stockValues = stockString.split(","); - - // TODO: Loading all historical prices not the recent, saved one. - Stock stock = new Stock(stockValues[0], stockValues[1], List.of(new BigDecimal(stockValues[2]))); - stocks.add(stock); - } - - } catch (IOException e) { - throw new IOException("File loading failed!"); - } - - return stocks; - - } - - /** - * Method for saving stocks to file. - * - * @param stocks The destined list to be saved. - * @throws IOException on BufferedWriter error. - */ - protected void save(List stocks) throws IOException { - - try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { - - for (Stock stock : stocks) { - String data = stock.getSymbol() + "," + stock.getCompany() + "," - + stock.getHistoricalPrices().getLast().toString(); - writer.write(data); - writer.newLine(); - } - - } catch (IOException e) { - throw new IOException("File saving failed!"); - } - - } - -} diff --git a/src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java b/src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java new file mode 100644 index 0000000..d2f1e55 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java @@ -0,0 +1,129 @@ +package edu.ntnu.idi.idatt.storage; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonIOException; +import com.google.gson.JsonSyntaxException; +import com.google.gson.reflect.TypeToken; + +import edu.ntnu.idi.idatt.model.Exchange; +import edu.ntnu.idi.idatt.model.player.Player; +import edu.ntnu.idi.idatt.session.UserSession; +import edu.ntnu.idi.idatt.session.UserSession.SessionBundle; + +/** + * Class for managing user sessions. + * + *

+ * Utilizes gson serialization and deserialization to manage + * player and game state. + */ +public class SessionManager { + + // Gson type to list format. Instead of using wrapper class. + private static Type SESSION_BUNDLE_TYPE = new TypeToken>() { + }.getType(); + + private static Gson gson = new GsonBuilder() + .setPrettyPrinting() + .create(); + + // Static initiator to ensure persistent storage file. + static { + StorageFile.ensureAppDataDirectoryExists(); + } + + /** + * Method for setting new session. + * + * @see UserSession + */ + public static void newSession(Player player, Exchange exchange) { + UserSession.getInstance().setPlayer(player); + UserSession.getInstance().setExchange(exchange); + } + + /** + * Method for saving current session. + */ + public static void saveSession() { + // don't save if current session is null accidentally + if (UserSession.getInstance().getPlayer() == null || UserSession.getInstance().getExchange() == null) { + return; + } + + // Load all sessions + List bundles = loadAllSessions(); + + try (Writer writer = new FileWriter(StorageFile.getStorageFile().toFile())) { + + // Append current session + SessionBundle existing = bundles.stream() + .filter(s -> s.getPlayer().getName().equals(UserSession.getInstance().getPlayer().getName())) + .findFirst().orElse(null); + + if (existing != null) { + bundles.set(bundles.indexOf(existing), UserSession.getInstance().getSession()); + } else { + bundles.add(UserSession.getInstance().getSession()); + } + + gson.toJson(bundles, writer); + + } catch (IOException e) { + throw new RuntimeException("Failed to save current session!", e); + } + } + + /** + * Method for loading a user session by player name. + * + * @return if session was found or not. + */ + public static boolean loadSession(String playerName) { + + List bundles = loadAllSessions(); + + for (SessionBundle session : bundles) { + if (session.getPlayer().getName().equals(playerName)) { + UserSession.getInstance().setPlayer(session.getPlayer()); + UserSession.getInstance().setExchange(session.getExchange()); + return true; + } + } + return false; + } + + /** + * Method for serialization of all sessions. + * + * @return List or empty list if none entires were made. + */ + private static List loadAllSessions() { + try { + if (!Files.exists(StorageFile.getStorageFile()) || Files.size(StorageFile.getStorageFile()) == 0) { + return new ArrayList<>(); + } + } catch (IOException e) { + return new ArrayList<>(); + } + + try { + return gson.fromJson(new FileReader(StorageFile.getStorageFile().toString()), SESSION_BUNDLE_TYPE); + } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) { + return new ArrayList<>(); + } + + } + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/storage/StockParser.java b/src/main/java/edu/ntnu/idi/idatt/storage/StockParser.java new file mode 100644 index 0000000..a4936dc --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/storage/StockParser.java @@ -0,0 +1,76 @@ +package edu.ntnu.idi.idatt.storage; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.math.BigDecimal; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +import edu.ntnu.idi.idatt.model.market.Stock; + +/** + * Utility class for parsing stocks. + * + *

+ * Decomponents files into the .csv (comma separated valuus) + * format, and creats stocks out of them. + *

+ */ +public class StockParser { + + // Disable initialization. + private StockParser() { + + } + + /** + * Method for loading from stocks from file + * + * @param path - The path to the .csv file. + * + * @return a list of loaded stocks. + * @throws IOException on BufferedReader error + */ + public static List load(String path) throws IOException { + File file = new File(path.toString()); + if (!file.exists()) { + throw new IOException("File at this path doesn't exist!"); + } + + if (!Files.probeContentType(Paths.get(file.getPath())).equals("text/csv")) { + throw new IOException("Please choose a .csv file!"); + } + + ArrayList stocks = new ArrayList<>(); + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + List stockStringList = new ArrayList<>(reader.readAllLines()); + + // Remove comments and inproper syntax + stockStringList.removeIf(s -> s.isBlank()); + stockStringList.removeIf(s -> s.startsWith("#")); + stockStringList.removeIf(s -> !s.contains(",")); + + for (String stockString : stockStringList) { + String[] stockValues = stockString.split(","); + if (stockValues.length != 3) { + throw new IOException("Invalid CSV format!"); + } + + Stock stock = new Stock(stockValues[0], stockValues[1], List.of(new BigDecimal(stockValues[2]))); + stocks.add(stock); + } + + } catch (IOException e) { + throw new IOException("File loading failed!"); + } + + return stocks; + + } + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/storage/StorageFile.java b/src/main/java/edu/ntnu/idi/idatt/storage/StorageFile.java new file mode 100644 index 0000000..9fa36e7 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/storage/StorageFile.java @@ -0,0 +1,75 @@ +package edu.ntnu.idi.idatt.storage; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Utility class for system-specific path obtaining. + */ +public class StorageFile { + private static final String STORAGE_FOLDER = "Millions"; + + /** + * Method for obtaining storage file path. + * + * @return Path object of storage file. + */ + public static Path getStorageFile() { + return getAppDataDirectory().resolve("storage.json"); + } + + /** + * Method for ensuring that storage directory exists. + * + *

+ * Attempts to create the application data directory if + * one does not exist. + *

+ */ + public static void ensureAppDataDirectoryExists() { + Path dir = getAppDataDirectory(); + try { + Files.createDirectories(dir); + + Path storageFile = dir.resolve("storage.json"); + if (!Files.exists(storageFile)) { + Files.createFile(storageFile); + } + } catch (IOException e) { + throw new RuntimeException("Could not create app data directory: " + dir, e); + } + } + + /** + * Method for obtaining system-specific directory for application data. + * + * @return Path object of the found directory. + */ + private static Path getAppDataDirectory() { + String userHome = System.getProperty("user.home"); + String os = System.getProperty("os.name").toLowerCase(); + + // AppData folder for windows + if (os.contains("win")) { + String appData = System.getenv("APPDATA"); + + if (appData != null) { + return Paths.get(appData, STORAGE_FOLDER); + } + // If AppData not a environmental variable, sets to user directory. + return Paths.get(userHome, STORAGE_FOLDER); + + } + // Library folder in MacOS + if (os.contains("mac")) { + return Paths.get(userHome, "Library", "Application Support", STORAGE_FOLDER); + } + + // All linux and unix-like systems. + return Paths.get(userHome, ".local", "share", STORAGE_FOLDER); + + } + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/view/entry/StartController.java b/src/main/java/edu/ntnu/idi/idatt/view/entry/StartController.java index 52f7eeb..04eea82 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/entry/StartController.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/entry/StartController.java @@ -1,10 +1,15 @@ package edu.ntnu.idi.idatt.view.entry; import java.io.File; +import java.io.IOException; import java.math.BigDecimal; +import java.util.List; +import edu.ntnu.idi.idatt.model.Exchange; +import edu.ntnu.idi.idatt.model.market.Stock; import edu.ntnu.idi.idatt.model.player.Player; -import edu.ntnu.idi.idatt.session.UserSession; +import edu.ntnu.idi.idatt.storage.SessionManager; +import edu.ntnu.idi.idatt.storage.StockParser; import edu.ntnu.idi.idatt.view.SceneManager; import edu.ntnu.idi.idatt.view.components.AbstractController; import edu.ntnu.idi.idatt.view.primary.MainView; @@ -28,14 +33,31 @@ public void obtainCSVFile() { public void initializeGame() { model.getError().set(" "); // Empty buffers - if (model.getName().get() == null || model.getBalance().get() == null) { - model.getError().set("Name and/or balance fields can't be empty"); + if (model.getName().get() == null) { + model.getError().set("Name field can't be empty"); return; } - if (model.getName().get().isBlank() || - model.getBalance().get().isBlank()) { - model.getError().set("Name and/or balance fields can't be empty"); + if (model.getName().get().isBlank()) { + model.getError().set("Name field can't be empty!"); + return; + } + + boolean loadResult = SessionManager.loadSession(model.getName().get()); + if (loadResult) { + SceneManager.switchTo(new MainView().getInstance()); + return; + } else { + model.isNewGame().set(true); + } + + if (model.getBalance().get() == null) { + model.getError().set("Balance field can't be empty"); + return; + } + + if (model.getBalance().get().isBlank()) { + model.getError().set("Balance field can't be empty!"); return; } @@ -52,9 +74,19 @@ public void initializeGame() { return; } - UserSession.getInstance().setPlayer(new Player(model.getName().get(), balance)); - SceneManager.switchTo(new MainView().getInstance()); + List stocks; + try { + stocks = StockParser.load(csv.getPath()); + } catch (IOException e) { + model.getError().set(e.getMessage()); + return; + } + Player player = new Player(model.getName().get(), balance); + Exchange exchange = new Exchange(player.getName(), stocks); + SessionManager.newSession(player, exchange); + SessionManager.saveSession(); + SceneManager.switchTo(new MainView().getInstance()); } } diff --git a/src/main/java/edu/ntnu/idi/idatt/view/entry/StartModel.java b/src/main/java/edu/ntnu/idi/idatt/view/entry/StartModel.java index e1b0f92..3e4c4ed 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/entry/StartModel.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/entry/StartModel.java @@ -1,6 +1,8 @@ package edu.ntnu.idi.idatt.view.entry; import edu.ntnu.idi.idatt.view.components.Model; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; @@ -11,6 +13,8 @@ public class StartModel implements Model { private final StringProperty error = new SimpleStringProperty(); private final StringProperty fileName = new SimpleStringProperty(); + private final BooleanProperty newGame = new SimpleBooleanProperty(); + public StringProperty getName() { return name; } @@ -27,4 +31,8 @@ public StringProperty getFileName() { return fileName; } + public BooleanProperty isNewGame() { + return newGame; + } + } diff --git a/src/main/java/edu/ntnu/idi/idatt/view/entry/StartView.java b/src/main/java/edu/ntnu/idi/idatt/view/entry/StartView.java index 60180b1..f32cc87 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/entry/StartView.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/entry/StartView.java @@ -24,6 +24,7 @@ public class StartView extends AbstractView { private Label errorLabel; // Global buttons for controller implementation + private VBox newGameWrapper; private Button csvButton; private Button startButton; @@ -44,6 +45,7 @@ public Parent createContent() { // Create and style wrappers VBox wrapper = new VBox(); + newGameWrapper = new VBox(); HBox playerWrapper = new HBox(); HBox balanceWrapper = new HBox(); playerWrapper.setAlignment(Pos.CENTER_LEFT); @@ -98,7 +100,8 @@ public Parent createContent() { VBox.setVgrow(filler, Priority.ALWAYS); // Wrap components together and adjust positioning - wrapper.getChildren().addAll(List.of(playerWrapper, balanceWrapper, csvWrapper)); + newGameWrapper.getChildren().addAll(balanceWrapper, csvWrapper); + wrapper.getChildren().addAll(playerWrapper, newGameWrapper); wrapper.setAlignment(Pos.BASELINE_LEFT); root.getChildren().addAll(List.of(title, wrapper, filler, errorLabel, startButton)); @@ -111,6 +114,7 @@ public void setModel(StartModel model) { this.balanceField.textProperty().bindBidirectional(model.getBalance()); this.errorLabel.textProperty().bind(model.getError()); this.fileLabel.textProperty().bind(model.getFileName()); + this.newGameWrapper.visibleProperty().bind(model.isNewGame()); } public void setController(StartController controller) { diff --git a/src/test/java/edu/ntnu/idi/idatt/model/ExchangeTest.java b/src/test/java/edu/ntnu/idi/idatt/model/ExchangeTest.java index 3c9f878..af14b62 100644 --- a/src/test/java/edu/ntnu/idi/idatt/model/ExchangeTest.java +++ b/src/test/java/edu/ntnu/idi/idatt/model/ExchangeTest.java @@ -45,14 +45,13 @@ class ExchangeTest { @BeforeEach public void PT_setup() throws IOException { - InputStream is = getClass() - .getClassLoader() - .getResourceAsStream("stocks.csv"); + Stock AAPL = new Stock("AAPL", "Apple Inc", List.of(new BigDecimal("30"))); + Stock NVDA = new Stock("NVDA", "NVIDIA", List.of(new BigDecimal("182.81"))); + Stock TSLA = new Stock("TSLA", "Tesla", List.of(new BigDecimal("417.44"))); + Stock AMD = new Stock("AMD", "Advanced Micro Devices", List.of(new BigDecimal("207.32"))); - Path tempFile = Files.createTempFile("stocks", ".csv"); - Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING); - - exchange = new Exchange("TestExchange", tempFile.toFile().toPath().toString()); + List stockss = List.of(AAPL, NVDA, TSLA, AMD); + exchange = new Exchange("TestExchange", stockss); stocks = exchange.getStocks(); player = new Player("TestPlayer", new BigDecimal("500")); } @@ -114,7 +113,7 @@ void PTBuy() { void PTSell() { // Player has to have a share to sell it. exchange.buy("AAPL", new BigDecimal("1"), player); - stocks.get(0).addNewSalesPrice(new BigDecimal("40")); // Simulate increase of AAPL stock price + exchange.getStock("AAPL").addNewSalesPrice(new BigDecimal("40")); // Simulate increase of AAPL stock price Transaction transaction = exchange.sell(player.getPortfolio().getShares().getLast(), player); assertEquals(transaction, player.getTransactionArchive().getTransactions(1).getLast()); @@ -125,18 +124,7 @@ void PTSell() { @Test void PTAdvance() { - List stockPricesBefore = new ArrayList<>(); - for (Stock stock : stocks) { - stockPricesBefore.add(stocks.indexOf(stock), stock.getSalesPrice()); - } - - exchange.advance(); - - for (Stock stock : stocks) { - assertTrue(stockPricesBefore.get(stocks.indexOf(stock)).compareTo(stock.getSalesPrice()) != 0); - // If compareTo returns 0 then its equal. - } - + // TODO: do } /** diff --git a/src/test/java/edu/ntnu/idi/idatt/storage/ExchangeLoaderTest.java b/src/test/java/edu/ntnu/idi/idatt/storage/ExchangeLoaderTest.java deleted file mode 100644 index d3d82d7..0000000 --- a/src/test/java/edu/ntnu/idi/idatt/storage/ExchangeLoaderTest.java +++ /dev/null @@ -1,103 +0,0 @@ -package edu.ntnu.idi.idatt.storage; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.io.IOException; -import java.io.InputStream; -import java.math.BigDecimal; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.List; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import edu.ntnu.idi.idatt.model.market.Stock; - -/** - * Test class for ExchangeLoader - * - *

- * Tests the loading and saving of stock data. - *

- */ -class ExchangeLoaderTest { - - private ExchangeLoader loader; - private List exampleStocks; - - @BeforeEach - public void PT_setup() throws IOException { - - InputStream is = getClass() - .getClassLoader() - .getResourceAsStream("stocks.csv"); - - Path tempFile = Files.createTempFile("stocks", ".csv"); - Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING); - - loader = new ExchangeLoader(tempFile.toFile().toPath().toString()); - - Stock AAPL = new Stock("AAPL", "Apple Inc.", List.of(new BigDecimal("32"))); - Stock NVDA = new Stock("NVDA", "NVIDIA", List.of(new BigDecimal("182.81"))); - Stock TSLA = new Stock("TSLA", "Tesla", List.of(new BigDecimal("417.44"))); - Stock AMD = new Stock("AMD", "Advanced Micro Devices", List.of(new BigDecimal("207.32"))); - - exampleStocks = List.of(AAPL, NVDA, TSLA, AMD); - } - - /** - * Positive test for loading/reading stocks - */ - @Test - void PT_load() { - List stocks = null; - try { - stocks = loader.load(); - } catch (IOException e) { - e.printStackTrace(); - } - - assertEquals(4, stocks.size()); - - } - - /** - * Positive test for saving stocks. - */ - @Test - void PT_save() { - exampleStocks.get(3).addNewSalesPrice(new BigDecimal("99999")); - - // Save - - try { - loader.save(exampleStocks); - } catch (IOException e) { - e.printStackTrace(); - } - - // Try to read again - List stocks = null; - try { - stocks = loader.load(); - } catch (IOException e) { - e.printStackTrace(); - } - - assertEquals(new BigDecimal("99999"), stocks.get(3).getSalesPrice()); - - } - - /** - * Negative tests for constructor - */ - @Test - void NT_IllegalArgumentException_Constructor() { - assertThrows(IllegalArgumentException.class, - () -> new ExchangeLoader("resources/notexistantfile.csv")); - } - -} diff --git a/src/test/java/edu/ntnu/idi/idatt/storage/StockParserTest.java b/src/test/java/edu/ntnu/idi/idatt/storage/StockParserTest.java new file mode 100644 index 0000000..102cf7b --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/storage/StockParserTest.java @@ -0,0 +1,68 @@ +package edu.ntnu.idi.idatt.storage; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import edu.ntnu.idi.idatt.model.market.Stock; + +/** + * Test class for ExchangeLoader + * + *

+ * Tests the loading and saving of stock data. + *

+ */ +class StockParserTest { + + String file; + + @BeforeEach + public void PT_setup() throws IOException { + + InputStream is = getClass() + .getClassLoader() + .getResourceAsStream("stocks.csv"); + + Path tempFile = Files.createTempFile("stocks", ".csv"); + Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING); + + file = tempFile.toFile().toPath().toString(); + + } + + /** + * Positive test for loading/reading stocks + */ + @Test + void PT_load() { + List stocks = null; + try { + stocks = StockParser.load(file); + } catch (IOException e) { + e.printStackTrace(); + } + + assertEquals(4, stocks.size()); + + } + + /** + * Negative tests reading stocks. + */ + @Test + void NT_IllegalArgumentException_Constructor() { + assertThrows(IOException.class, + () -> StockParser.load("resources/notexistantfile.csv")); + } + +}