Skip to content

Commit

Permalink
Feat: Updated stock class
Browse files Browse the repository at this point in the history
Added methods for getting historical prices, highest recorded price, lowest recorded price, and latest price change.
  • Loading branch information
tommyah committed Mar 18, 2026
1 parent 74494b3 commit 7fcc385
Showing 1 changed file with 117 additions and 48 deletions.
165 changes: 117 additions & 48 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigDecimal> 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<BigDecimal> 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<BigDecimal> 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.
*
* <p>Returns 0 if latest price is only price.</p>
*
* @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);
}

}

0 comments on commit 7fcc385

Please sign in to comment.