Skip to content

add historical prices and getters for highest and lowest price with p… #60

Merged
merged 1 commit into from
Mar 23, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Represents a stock of a company in the {@link Exchange}.
* <p>
* Each {@code Stock} has a unique symbol, a company name, and a history of sales prices.
* Each stock has a unique symbol, a company name, and a history of sales prices.
* You can retrieve the current price or add new prices as the market changes.
* </p>
*/
Expand All @@ -17,7 +17,7 @@ public class Stock {
private List<BigDecimal> prices = new ArrayList<>();

/**
* Constructs a new {@code Stock} with the given symbol, company name, and initial sales price.
* Constructs a new stock with the given symbol, company name, and initial sales price.
* @param symbol symbol the unique stock symbol (e.g., "AAPL")
* @param company company the company name (e.g., "Apple Inc.")
* @param salesPrice salesPrice the initial sales price of the stock
Expand Down Expand Up @@ -45,7 +45,22 @@ public BigDecimal getSalesPrice() {
*
* @param salesPrice the new sales price to add
*/
public void addNewSalesPrices(BigDecimal salesPrice) {
public void addNewSalesPrice(BigDecimal salesPrice) {
prices.add(salesPrice);
}

public List<BigDecimal> getHistoricalPrices() {
return List.copyOf(prices);
}

public BigDecimal getHighestPrice() {
return prices.stream().max(BigDecimal::compareTo).orElse(BigDecimal.ZERO);
}
public BigDecimal getLowestPrice() {
return prices.stream().min(BigDecimal::compareTo).orElse(BigDecimal.ZERO);
}

public BigDecimal getLatestPrice() {
return prices.isEmpty() ? BigDecimal.ZERO : prices.get(prices.size() - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void stockPropertiesTest() {
void addNewPriceTest() {
Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100"));
BigDecimal newPrice = new BigDecimal("110");
stock.addNewSalesPrices(newPrice);
stock.addNewSalesPrice(newPrice);

assertEquals(0, newPrice.compareTo(stock.getSalesPrice()));
}
Expand Down