From 7fcc385129d84fdfb53932f37edeed3c142f3e5c Mon Sep 17 00:00:00 2001 From: = Date: Wed, 18 Mar 2026 18:49:42 +0100 Subject: [PATCH] Feat: Updated stock class Added methods for getting historical prices, highest recorded price, lowest recorded price, and latest price change. --- .../ntnu/idi/idatt2003/g40/mappe/Stock.java | 165 +++++++++++++----- 1 file changed, 117 insertions(+), 48 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Stock.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Stock.java index c870265..6f433a1 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Stock.java @@ -2,63 +2,132 @@ import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; /** * Represents a stock listed on an exchange. */ -public class Stock { - - private final String symbol; - private final String company; - private final List prices = new ArrayList<>(); - - /** - * Creates a new {@code Stock} with an initial sales price. - * - * @param symbol the unique stock symbol - * @param company the name of the company - * @param salesPrice the initial sales price of the stock - */ - public Stock(String symbol, String company, BigDecimal salesPrice){ - this.symbol = symbol; - this.company = company; - prices.add(salesPrice); - } +public final class Stock { - /** - * Returns the stock symbol. - * - * @return the stock symbol - */ - public String getSymbol(){ - return symbol; - } + /** + * Symbol of this stock. Needs to be 4 characters. + * */ + private final String symbol; - /** - * Returns the stock company. - * - * @return the stocks company - */ - public String getCompany(){ - return company; - } + /** + * Name of the company. + * */ + private final String company; + + /** + * List of prices this stock has had. + * */ + private final List prices = new ArrayList<>(); - /** - * Returns the current sales price of the stock. - * - * @return the curret sales price - */ - public BigDecimal getSalesPrice() { - return prices.getLast(); + /** + * Creates a new {@code Stock} with an initial sales price. + * + * @param symbol the unique stock symbol + * @param company the name of the company + * @param salesPrice the initial sales price of the stock + */ + public Stock(final String symbol, + final String company, + final BigDecimal salesPrice) { + + if (symbol.length() != 4) { + throw new IllegalArgumentException( + "Stock's symbol must be 4 characters!"); } + this.symbol = symbol; + this.company = company; + prices.add(salesPrice); + } + + /** + * Returns the stock symbol. + * + * @return the stock symbol + */ + public String getSymbol() { + return symbol; + } + + /** + * Returns the stock company. + * + * @return the stocks company + */ + public String getCompany() { + return company; + } + + /** + * Returns the current sales price of the stock. + * + * @return the current sales price + */ + public BigDecimal getSalesPrice() { + return prices.getLast(); + } - /** - * Adds a new sales price to the price history. - * - * @param price the new sales price - */ - public void addNewSalesPrice(BigDecimal price) { - prices.add(price); + /** + * Adds a new sales price to the price history. + * + * @param price the new sales price + */ + public void addNewSalesPrice(final BigDecimal price) { + prices.add(price); + } + + /** + * Returns list of all prices for this stock. + * + * @return {@link List} object containing all prices. + * */ + public List getHistoricalPrices() { + return prices; + } + + /** + * Returns highest price this stock has been, or zero is list is empty. + * + * @return {@link BigDecimal} highest price. + * */ + public BigDecimal getHighestPrice() { + return prices.stream() + .max(Comparator.naturalOrder()) + .orElse(BigDecimal.ZERO); + } + + /** + * Returns lowest price this stock has been, or zero if list is empty. + * + * @return {@link BigDecimal} lowest price. + * */ + public BigDecimal getLowestPrice() { + return prices.stream() + .min(Comparator.naturalOrder()) + .orElse(BigDecimal.ZERO); + } + + /** + * Returns the latest price change of this stock. + * + *

Returns 0 if latest price is only price.

+ * + * @return {@link BigDecimal} price change price. + * */ + public BigDecimal getLatestPriceChange() { + if (prices.size() < 2) { + return BigDecimal.ZERO; } + + BigDecimal currentPrice = prices.getLast(); + BigDecimal previousPrice = prices.get(prices.size() - 2); + + return currentPrice.subtract(previousPrice); + } + }