Skip to content

Commit

Permalink
Added methods to stock class:
Browse files Browse the repository at this point in the history
- getHistoricalPrices: returns a list of all prices for a stock
- getHighestPrice: returns the highest historical price for a stock
- getLowestPrice: returns the lowest historical price for a stock
- getLatestPriceChange: returns the difference between the current and previous price for a stock
  • Loading branch information
Nikollai committed Mar 10, 2026
1 parent e2450da commit 4efe591
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/millions/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,35 @@ public BigDecimal getSalesPrice() {
public void addNewSalesPrice(BigDecimal price) {
this.prices.add(price);
}

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

public BigDecimal getHighestPrice() {
BigDecimal highestPrice = this.prices.get(0);
for (BigDecimal price : this.prices) {
if (price.compareTo(highestPrice) > 0) {
highestPrice = price;
}
}
return highestPrice;
}

public BigDecimal getLowestPrice() {
BigDecimal lowestPrice = this.prices.get(0);
for (BigDecimal price : this.prices) {
if (price.compareTo(lowestPrice) < 0) {
lowestPrice = price;
}
}
return lowestPrice;
}

public BigDecimal getLatestPriceChange() {
BigDecimal currentPrice = this.prices.getLast();
BigDecimal lastPrice = this.prices.get(this.prices.size() - 2);

return currentPrice.subtract(lastPrice);
}
}

0 comments on commit 4efe591

Please sign in to comment.