Skip to content

Commit

Permalink
Feat: Added fortune to stock
Browse files Browse the repository at this point in the history
Fortune double value represents how "lucky" that stock will be in the next week. Setter and getter method for value. Also added compatibilty with exchange advance week method.
  • Loading branch information
tommyah committed May 24, 2026
1 parent bfb3228 commit 62f07d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ public Transaction sell(final Share share, final Player player)
*
* @throws IllegalArgumentException if any parameter is null, or if player does not have enough shares.
* */
public List<Transaction> sell(BigDecimal amount, final String stockSymbol, final Player player)
public List<Transaction> sell(BigDecimal amount,
final String stockSymbol,
final Player player)
throws IllegalArgumentException {
if (amount == null || player == null || !Validator.NOT_EMPTY.isValid(stockSymbol)) {
throw new IllegalArgumentException("Invalid sell!");
Expand Down Expand Up @@ -253,7 +255,8 @@ public void advance() {
for (Stock stock : stockMap.values()) {
BigDecimal currentPrice = stock.getSalesPrice();

double change = (random.nextDouble() * 0.10) - 0.05;
double change = ((random.nextDouble() * 0.10) - 0.05) + stock.getFortune();
stock.setFortune(0);
BigDecimal factor = BigDecimal.valueOf(1 + change);

BigDecimal newPrice = currentPrice.multiply(factor);
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public final class Stock {
* */
private final List<BigDecimal> prices = new ArrayList<>();

/**
* Value indicating this stocks fortune.
*
* <p>Higher fortune yields a higher
* increase when stock market changes price.</p>
* */
private double fortune;

/**
* Creates a new {@code Stock} with an initial sales price.
*
Expand All @@ -42,10 +50,29 @@ public Stock(final String symbol,
} else {
this.symbol = symbol;
this.company = company;
this.fortune = 0;
prices.add(salesPrice);
}
}

/**
* Setter method for fortune.
*
* @param newFortune the new value to set this stocks' fortune.
* */
public void setFortune(final double newFortune) {
fortune = newFortune;
}

/**
* Getter method for fortune.
*
* @return fortune.
* */
public double getFortune() {
return fortune;
}

/**
* Returns the stock symbol.
*
Expand Down

0 comments on commit 62f07d2

Please sign in to comment.