Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/file-io-development' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 25, 2026
2 parents 9d4abda + 0950372 commit f4b97df
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CSVStockFileParser() {}
public boolean verifyCSV(List<String> lines) {
return lines.stream()
.filter(l -> !(l.startsWith("#") || l.isBlank()))
.noneMatch(l -> l.split(",").length != 3);
.noneMatch(l -> l.split(",").length != 4);
}

/**
Expand Down Expand Up @@ -49,9 +49,14 @@ public List<Stock> parse(List<String> lines) {
String symbol = split[0];
String company = split[1];
BigDecimal price = new BigDecimal(split[2]);
stocks.add(new Stock(symbol, company, price));
String[] functionValues = split[3].split(";");
List<BigDecimal> convertedFunctionValues = new ArrayList<>();
for (String functionValue : functionValues) {
convertedFunctionValues.add(new BigDecimal(functionValue));
}
stocks.add(new Stock(symbol, company, price, convertedFunctionValues));
} catch (NumberFormatException e) {
throw new InvalidFormatException("Error with number conversion on line: " + l + "\n" + "Last field must be a number");
throw new InvalidFormatException("Error with number conversion on line: " + l + "\n" + "ensure all number fields are actually numbers");
} catch (IllegalArgumentException e) {
throw new InvalidFormatException("Illegal argument on line: " + l + "\n" + e.getMessage());
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/millions/model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import millions.model.calculators.PriceChangeCalculator;
import millions.model.factories.PurchaseFactory;
import millions.model.factories.SaleFactory;
import millions.model.factories.TransactionFactory;
Expand Down Expand Up @@ -143,15 +145,19 @@ public List<Stock> getLosers(int limit) {
* Advances the current game week by performing new price calculations for all stocks.
*/
public void advance() {
PriceChangeCalculator priceChangeCalculator = new PriceChangeCalculator();
this.weekNumber++;
for (Stock stock : this.stocks.values()) {
double change = 0.9 + random.nextDouble() * 0.2;
stock.addNewSalesPrice(
stock
.getSalesPrice()
.multiply(BigDecimal.valueOf(change))
.setScale(2, RoundingMode.HALF_UP));
// RoundingMode from AI suggestion
BigDecimal change = priceChangeCalculator.calculateChange(stock);
stock.addNewSalesPrice(stock.getSalesPrice().add(change));

// double change = 0.9 + random.nextDouble() * 0.2;
// stock.addNewSalesPrice(
// stock
// .getSalesPrice()
// .multiply(BigDecimal.valueOf(change))
// .setScale(2, RoundingMode.HALF_UP));
// // RoundingMode from AI suggestion
}
notifyWeekAdvanced();
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/millions/model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@
public class Stock {
String symbol;
String company;
List<BigDecimal> volatility;
List<BigDecimal> prices;

/**
* @param symbol Stock ticker symbol
* @param company company name
* @param prices List of prices
* @param volatilityParameters numbers used for price change calculation functions
* @throws IllegalArgumentException
*/
public Stock(String symbol, String company, List<BigDecimal> prices) {
public Stock(String symbol, String company, List<BigDecimal> prices, List<BigDecimal> volatilityParameters) {
this.symbol = symbol;
this.company = company;
this.prices = new ArrayList<>(prices);

this.volatility = volatilityParameters;

if (volatilityParameters.size() != 5) {
throw new IllegalArgumentException("Invalid volatility function count");
}

if (symbol == null || symbol.isBlank()) {
throw new IllegalArgumentException("Symbol cannot be null or blank");
}
Expand All @@ -31,8 +39,8 @@ public Stock(String symbol, String company, List<BigDecimal> prices) {
}

/** Stock() with single price instead of list */
public Stock(String symbol, String company, BigDecimal initialPrice) {
this(symbol, company, new ArrayList<>(List.of(initialPrice)));
public Stock(String symbol, String company, BigDecimal initialPrice, List<BigDecimal> volatilityFunctions) {
this(symbol, company, new ArrayList<>(List.of(initialPrice)), volatilityFunctions);
}

/**
Expand Down Expand Up @@ -110,6 +118,10 @@ public BigDecimal getLatestPriceChange() {
return currentPrice.subtract(lastPrice);
}

public List<BigDecimal> getVolatilityParameters() {
return this.volatility;
}

@Override
public String toString() {
return "Stock [symbol: " + symbol + ", company: " + company + ", prices: " + prices + "]";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package millions.model.calculators;

import millions.model.Stock;

import java.math.BigDecimal;
import java.util.List;

public class PriceChangeCalculator {

public PriceChangeCalculator() {}

public BigDecimal calculateChange(Stock stock) {
BigDecimal change = BigDecimal.ZERO;
List<BigDecimal> volatilityParameters = stock.getVolatilityParameters();
change = change.add(upChange(volatilityParameters.get(0)));
change = change.add(downChange(volatilityParameters.get(1)));
change = change.add(randomChange(volatilityParameters.get(2)));
change = change.add(sinChange(volatilityParameters.get(3)));
change = change.add(cosChange(volatilityParameters.get(4)));
return change;
}

private BigDecimal upChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return input;

}
private BigDecimal downChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return input.negate();
}

private BigDecimal randomChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return new BigDecimal(Math.random()*10).multiply(input);
}

private BigDecimal sinChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return new BigDecimal(Math.sin(input.doubleValue()));
}
private BigDecimal cosChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return new BigDecimal(Math.cos(input.doubleValue()));
}
}

0 comments on commit f4b97df

Please sign in to comment.