diff --git a/src/main/java/Model/Stock.java b/src/main/java/Model/Stock.java index 52c1fd2..ca117b5 100644 --- a/src/main/java/Model/Stock.java +++ b/src/main/java/Model/Stock.java @@ -1,77 +1,92 @@ package Model; + import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; +/** + * STock class. + */ public class Stock { - private final String symbol; - private final String company; - private final List prices; - - public Stock(String symbol, String company, BigDecimal salesPrice) { - if (symbol == null || symbol.isBlank()) { - throw new IllegalArgumentException("Symbol cannot be null or blank"); - } - if (company == null || company.isBlank()) { - throw new IllegalArgumentException("Company name cannot be null or blank"); - } - if (salesPrice == null) { - throw new IllegalArgumentException("Initial sales price cannot be null"); - } - if (salesPrice.compareTo(BigDecimal.ZERO) < 0) { - throw new IllegalArgumentException("Initial sales price cannot be negative"); - } + private final String symbol; + private final String company; + private final List prices; - this.symbol = symbol; - this.company = company; - this.prices = new ArrayList<>(); - this.prices.add(salesPrice); + /** + * Stock method that includes symbol of the stock, company and the salesprice. + */ + public Stock(String symbol, String company, BigDecimal salesPrice) { + if (symbol == null || symbol.isBlank()) { + throw new IllegalArgumentException("Symbol cannot be null or blank"); } - - public String getSymbol() { - return symbol; + if (company == null || company.isBlank()) { + throw new IllegalArgumentException("Company name cannot be null or blank"); } - - public String getCompany() { - return company; + if (salesPrice == null) { + throw new IllegalArgumentException("Initial sales price cannot be null"); } - - public BigDecimal getSalesPrice() { - return prices.get(prices.size() - 1); + if (salesPrice.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Initial sales price cannot be negative"); } + + this.symbol = symbol; + this.company = company; + this.prices = new ArrayList<>(); + this.prices.add(salesPrice); + } + + public String getSymbol() { + return symbol; + } + + public String getCompany() { + return company; + } + + public BigDecimal getSalesPrice() { + return prices.get(prices.size() - 1); + } - public void addNewSalesPrice(BigDecimal price) { - if (price == null) { - throw new IllegalArgumentException("Price cannot be null"); - } - if (price.compareTo(BigDecimal.ZERO) < 0) { - throw new IllegalArgumentException("Price cannot be negative"); - } - prices.add(price); + /** + * Method that makes new salesprice. + */ + public void addNewSalesPrice(BigDecimal price) { + if (price == null) { + throw new IllegalArgumentException("Price cannot be null"); } - - public List getHistoricalPrices() { - return new ArrayList<>(prices); // returnerer en kopi for å beskytte selve listen + if (price.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Price cannot be negative"); } + prices.add(price); + } - public BigDecimal getHighestPrice() { - return prices.stream() - .reduce(prices.get(0), BigDecimal::max); - } + public List getHistoricalPrices() { + return new ArrayList<>(prices); // returnerer en kopi for å beskytte selve listen + } - public BigDecimal getLowestPrice() { - return prices.stream() - .reduce(prices.get(0), BigDecimal::min); - } + public BigDecimal getHighestPrice() { + return prices.stream() + .reduce(prices.get(0), BigDecimal::max); + } - public BigDecimal getLatestPriceChange() { - if (prices.size() < 2) { - return BigDecimal.ZERO; - } + public BigDecimal getLowestPrice() { + return prices.stream() + .reduce(prices.get(0), BigDecimal::min); + } - BigDecimal latest = prices.get(prices.size() - 1); - BigDecimal previous = prices.get(prices.size() - 2); - return latest.subtract(previous); + /** + * Method that gets the latest price change. + * + * @return returns the price change + */ + public BigDecimal getLatestPriceChange() { + if (prices.size() < 2) { + return BigDecimal.ZERO; } + + BigDecimal latest = prices.get(prices.size() - 1); + BigDecimal previous = prices.get(prices.size() - 2); + return latest.subtract(previous); + } }