Skip to content

Commit

Permalink
add historical prices and getters for highest and lowest price with p…
Browse files Browse the repository at this point in the history
…ropper null handling
  • Loading branch information
peretr committed Mar 23, 2026
1 parent 2474791 commit c9a69e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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

0 comments on commit c9a69e0

Please sign in to comment.