Skip to content

Commit

Permalink
refactor(Exchange): Implement stock loading from file. Test class ref…
Browse files Browse the repository at this point in the history
…actor.
  • Loading branch information
pawelsa committed Mar 25, 2026
1 parent f8e88a5 commit 2c6a8c8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
24 changes: 20 additions & 4 deletions src/main/java/edu/ntnu/idi/idatt/Exchange.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package edu.ntnu.idi.idatt;

import edu.ntnu.idi.idatt.file.ExchangeLoader;
import edu.ntnu.idi.idatt.marked.Share;
import edu.ntnu.idi.idatt.marked.Stock;
import edu.ntnu.idi.idatt.transaction.Purchase;
import edu.ntnu.idi.idatt.transaction.Sale;
import edu.ntnu.idi.idatt.transaction.Transaction;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -20,7 +22,7 @@
* </p>
*
*/
public class Exchange {
public class Exchange extends ExchangeLoader {

private final String name;
private int week;
Expand All @@ -33,12 +35,15 @@ public class Exchange {
* @param name - Name of the current stock Exchange
* @param stocks - List of aviable stocks for this exchange.
*/
public Exchange(String name, List<Stock> stocks) {
public Exchange(String name, String path) {
super(path);
this.name = name;
this.week = 1;

for (Stock stock : stocks) {
stockMap.put(stock.getSymbol(), stock);
try {
this.load().forEach(stock -> stockMap.put(stock.getSymbol(), stock));
} catch (IOException e) {
throw new IllegalArgumentException("Problem loading [" + name + "] exchange : " + e);
}

}
Expand All @@ -58,6 +63,10 @@ public int getWeek() {
return week;
}

public List<Stock> getStocks() {
return stockMap.values().stream().toList();
}

/**
* Method for checking if a specific stock exists in the exchange.
*
Expand Down Expand Up @@ -194,6 +203,13 @@ public Transaction sell(Share share, Player player) {
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);
}
}
}

Expand Down
44 changes: 31 additions & 13 deletions src/test/java/edu/ntnu/idi/idatt/ExchangeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

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.ArrayList;
import java.util.List;

Expand All @@ -17,22 +22,37 @@

class ExchangeTest {

/**
* <p>
* Caution!
* stocks List is not sorted correctly.
* We parse it with hashMap.values().stream().toList
* which dont properly arrange it in memory.
* Take caution when programming or reading here.
*
* @see PTgetGainers_Losers don't take the get(index) literally
* but focus on the differences of last sale value made.
*
* This is a work around for implementing the ExchangeLoader to Exchange
* class.
* </p>
*/
private Exchange exchange;
private List<Stock> stocks;
private Player player;

@BeforeEach
public void PT_setup() {
public void PT_setup() throws IOException {

// Initialize exchange with proper objects
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")));
InputStream is = getClass()
.getClassLoader()
.getResourceAsStream("stocks.csv");

stocks = List.of(AAPL, NVDA, TSLA, AMD);
Path tempFile = Files.createTempFile("stocks", ".csv");
Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING);

exchange = new Exchange("TestExchange", stocks);
exchange = new Exchange("TestExchange", tempFile.toFile().toPath().toString());
stocks = exchange.getStocks();
player = new Player("TestPlayer", new BigDecimal("500"));
}

Expand All @@ -55,12 +75,10 @@ void PTConstructor() {
void PTFindStock() {

assertTrue(exchange.hasStock("AAPL"));
assertEquals(stocks.get(0) /* AAPL Stock */, exchange.getStock("AAPL"));
assertTrue(stocks.contains(exchange.getStock("AAPL")));

// FindStocks for letter "n" should be - AAPL, AMD (both symbols and names!)
List<Stock> expected = List.of(stocks.get(0), stocks.get(3));
//
assertEquals(expected, exchange.findStocks("n"));
assertEquals(2, exchange.findStocks("n").size());

}

Expand Down Expand Up @@ -127,7 +145,7 @@ void PTAdvance() {
void PTgetGainers_Losers() {
// Simulate price change
stocks.get(0).addNewSalesPrice(new BigDecimal("4333"));
stocks.get(1).addNewSalesPrice(new BigDecimal("50"));
stocks.get(1).addNewSalesPrice(new BigDecimal("10"));
stocks.get(2).addNewSalesPrice(new BigDecimal("3"));
stocks.get(3).addNewSalesPrice(new BigDecimal("800"));

Expand Down

0 comments on commit 2c6a8c8

Please sign in to comment.