From 62f07d2d6f4d9126e2c32dc707b2963ca0a1fb02 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 24 May 2026 14:26:34 +0200 Subject: [PATCH] Feat: Added fortune to stock 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. --- .../idatt2003/g40/mappe/engine/Exchange.java | 7 +++-- .../idi/idatt2003/g40/mappe/model/Stock.java | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java index 9c5796a..b73f634 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java @@ -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 sell(BigDecimal amount, final String stockSymbol, final Player player) + public List 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!"); @@ -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); diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java index 11a444e..1ca9664 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java @@ -26,6 +26,14 @@ public final class Stock { * */ private final List prices = new ArrayList<>(); + /** + * Value indicating this stocks fortune. + * + *

Higher fortune yields a higher + * increase when stock market changes price.

+ * */ + private double fortune; + /** * Creates a new {@code Stock} with an initial sales price. * @@ -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. *