Skip to content

Commit

Permalink
Added Checkstyle to Stock
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent 098388e commit dd22359
Showing 1 changed file with 71 additions and 56 deletions.
127 changes: 71 additions & 56 deletions src/main/java/Model/Stock.java
Original file line number Diff line number Diff line change
@@ -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<BigDecimal> 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<BigDecimal> 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<BigDecimal> 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<BigDecimal> 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);
}
}

0 comments on commit dd22359

Please sign in to comment.