Skip to content

Commit

Permalink
Docs: Adding javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 11, 2026
1 parent 2c0a768 commit e72b5a4
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/main/java/millions/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import millions.controller.GameController;
import millions.view.StartView;

/**
* Main JavaFX application entry point for the Millions stock trading game.
*/
public class App extends Application {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import millions.model.calculators.SaleCalculator;
import millions.model.calculators.TransactionCalculator;

/**
* Factory for creating transaction calculators.
*/
public class TransactionCalculatorFactory {

private TransactionCalculatorFactory() {}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import millions.model.Player;
import millions.model.Stock;

/** Controls game initialization. */
public class GameController {
private Player player;
private Exchange exchange;
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/millions/controller/fileIO/CSVStockFileParser.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
package millions.controller.fileIO;

import millions.model.Stock;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import millions.model.Stock;

/** Parses CSV lines into Stock objects. */
public class CSVStockFileParser {
private List<String> lines;

public CSVStockFileParser(List<String> lines) {
if (verifyCSV(lines)) {
this.lines = lines;
}
else {
} else {
// throw file format error
}
}

// returns true if all entries have exactly 3 data points
public boolean verifyCSV(List<String> lines) {
return lines.stream()
.filter(l -> !(l.startsWith("#") || l.isBlank()))
.noneMatch(l -> l.split(",").length != 3);

.filter(l -> !(l.startsWith("#") || l.isBlank()))
.noneMatch(l -> l.split(",").length != 3);
}

public List<Stock> parse() {
List<Stock> stocks = new ArrayList<>();
lines.stream()
.filter(l -> !((l.startsWith("#") || l.isBlank())))
.forEach(l -> {
String[] split = l.split(",");
String symbol = split[0];
String company = split[1];
BigDecimal price = new BigDecimal(split[2]);
stocks.add(new Stock(symbol, company, price));
});
.forEach(
l -> {
String[] split = l.split(",");
String symbol = split[0];
String company = split[1];
BigDecimal price = new BigDecimal(split[2]);
stocks.add(new Stock(symbol, company, price));
});
return stocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.util.List;

//TODO: Validation of data before writing
/**
* Writes stock data to a CSV file.
*/
public class CSVStockFileWriter implements StockFileWriter {
private final List<Stock> stocks;
private String finalString;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/millions/controller/fileIO/StockFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.List;


/**
* Reads a file and returns its lines as a list of strings.
*/
public class StockFileReader {
private final Path filePath;

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/millions/controller/fileIO/StockFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.nio.file.Path;

/**
* Interface for writing stock data to a file.
*/
public interface StockFileWriter {
public void formatString();
public boolean write(Path path);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/millions/model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import millions.model.factories.SaleFactory;
import millions.model.factories.TransactionFactory;

/**
* The stock exchange where players buy and sell shares. Manages stocks and simulates weekly price changes.
*/
public class Exchange {
private String name;
private Map<String, Stock> stocks;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/millions/model/ExchangeListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package millions.model;

/**
* Listener for exchange events such as week advances and completed transactions.
*/
public interface ExchangeListener {

void onWeekAdvanced(int newWeek);
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/millions/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.List;

/** Player class. */
public class Player {
private String name;
private BigDecimal startingMoney;
Expand All @@ -15,6 +16,11 @@ public class Player {
public int weeksTraded;
private final List<PlayerListener> listeners = new ArrayList<>();

/**
* @param name Name of player
* @param startingMoney Amount of money the player starts with
* @throws IllegalArgumentException
*/
public Player(String name, BigDecimal startingMoney) {
this.name = name;
this.startingMoney = startingMoney;
Expand All @@ -31,6 +37,10 @@ public Player(String name, BigDecimal startingMoney) {
}
}

/**
* @param amount How much money to add
* @throws IllegalArgumentException
*/
public void addMoney(BigDecimal amount) {
if (amount == null || amount.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Amount cannot be null or negative");
Expand All @@ -39,6 +49,10 @@ public void addMoney(BigDecimal amount) {
notifyMoneyChanged();
}

/**
* @param amount How much money to withdeaw
* @throws IllegalArgumentException
*/
public void withdrawMoney(BigDecimal amount) {
if (amount == null || amount.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Amount cannot be null or negative");
Expand All @@ -47,6 +61,9 @@ public void withdrawMoney(BigDecimal amount) {
notifyMoneyChanged();
}

/**
* @return
*/
public String getStatus() {
// TODO dobbel sjekk logikken
int weeksTraded = transactionArchive.countDistinctWeeks();
Expand All @@ -63,42 +80,69 @@ public String getStatus() {
return status;
}

/**
* @return
*/
public String getName() {
return this.name;
}

/**
* @return
*/
public BigDecimal getMoney() {
return this.money;
}

/**
* @return
*/
public Portfolio getPortfolio() {
return this.portfolio;
}

/**
* @param share Share to be added
*/
public void addShareToPortfolio(Share share) {
this.portfolio.addShare(share);
notifyPortfolioChanged();
notifyStatusChanged();
}

/**
* @param share Share to be removed
*/
public void removeShareFromPortfolio(Share share) {
this.portfolio.removeShare(share);
notifyPortfolioChanged();
notifyStatusChanged();
}

/**
* @return
*/
public BigDecimal getNetWorth() {
return this.money.add(this.portfolio.getNetWorth());
}

/**
* @return
*/
public TransactionArchive getTransactionArchive() {
return this.transactionArchive;
}

/**
* @param listener
*/
public void addListener(PlayerListener listener) {
listeners.add(listener);
}

/**
* @param listener
*/
public void removeListener(PlayerListener listener) {
listeners.remove(listener);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/millions/model/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;

/** Listener for player state changes. */
public interface PlayerListener {

void onMoneyChanged(BigDecimal newBalance);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/millions/model/Portfolio.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,50 @@
import java.util.List;
import millions.model.calculators.SaleCalculator;

/** A collection of shares owned by a player. */
public class Portfolio {
List<Share> shares;

public Portfolio() {
shares = new ArrayList<>();
}

/**
* @param share Share to be added
* @return
*/
public boolean addShare(Share share) {
return this.shares.add(share);
}

/**
* @param share Share to be removed
* @return
*/
public boolean removeShare(Share share) {
return this.shares.remove(share);
}

/**
* @return
*/
public List<Share> getShares() {
return this.shares;
}

/**
* @param symbol
* @return
*/
public List<Share> getShares(String symbol) {
return this.shares.stream()
.filter(share -> share.getStock().getSymbol().equals(symbol))
.toList();
}

/**
* @return
*/
public BigDecimal getNetWorth() {
BigDecimal total = BigDecimal.ZERO;
for (Share share : shares) {
Expand All @@ -39,6 +58,10 @@ public BigDecimal getNetWorth() {
return total;
}

/**
* @param share
* @return
*/
public boolean contains(Share share) {
return this.shares.contains(share);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/millions/model/Purchase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import millions.model.calculators.PurchaseCalculator;

/**
* A transaction representing the purchase of shares.
*/
public class Purchase extends Transaction {

public Purchase(Share share, int week) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/millions/model/Sale.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import millions.model.calculators.SaleCalculator;

/**
* A transaction representing the sale of shares.
*/
public class Sale extends Transaction {

public Sale(Share share, int week) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/millions/model/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import java.math.BigDecimal;

/** Represents a holding of a specific stock with a quantity and purchase price. */
public class Share {
Stock stock;
BigDecimal quantity;
BigDecimal purchasePrice;

/**
* @param stock Which stock the share is for.
* @param quantity How many stocks
* @param purchasePrice Purchase price of the share
* @throws IllegalArgumentException
*/
public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) {
this.stock = stock;
this.quantity = quantity;
Expand All @@ -23,18 +30,28 @@ public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) {
}
}

/** Share() with int quantity */
public Share(Stock stock, int quantity, BigDecimal purchasePrice) {
this(stock, BigDecimal.valueOf(quantity), purchasePrice);
}

/**
* @return
*/
public Stock getStock() {
return this.stock;
}

/**
* @return
*/
public BigDecimal getQuantity() {
return this.quantity;
}

/**
* @return
*/
public BigDecimal getPurchasePrice() {
return this.purchasePrice;
}
Expand Down
Loading

0 comments on commit e72b5a4

Please sign in to comment.