Skip to content

Javadocs #42

Merged
merged 2 commits into from
May 11, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/millions/App.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
package millions;

import java.math.BigDecimal;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import millions.controller.GameController;
import millions.view.GameView;
import millions.view.StartView;

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

@Override
public void start(Stage stage) {
GameController controller = new GameController();
StartView startView = new StartView(stage);

startView
.getStartButton()
.setOnAction(
event -> {
try {
controller.startGame(
startView.getName(),
new BigDecimal(startView.getStartingAmount()),
startView.getSelectedFile().toPath(),
startView.getPreRunWeeks());

GameView gameView = new GameView(controller);
controller.getPlayer().addListener(gameView);
controller.getExchange().addListener(gameView);

Scene gameScene = new Scene(gameView, 1920, 1080);
stage.setScene(gameScene);
} catch (RuntimeException ex) {
System.err.println(ex);
System.exit(0);
}
});

Scene scene = new Scene(startView, 400, 350);
stage.setTitle("Millions");
stage.setScene(scene);
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
47 changes: 45 additions & 2 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@

import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import millions.controller.fileIO.CSVStockFileParser;
import millions.controller.fileIO.StockFileReader;
import millions.model.Exchange;
import millions.model.Player;
import millions.model.Stock;

/** Controls game initialization. */
public class GameController {
private Player player;
private Exchange exchange;

public void startGame(String name, BigDecimal startingMoney, Path stockFilePath) {
public void startGame(
String name, BigDecimal startingMoney, Path stockFilePath, int preRunWeeks) {
if (preRunWeeks < 0) {
throw new IllegalArgumentException("Pre run weeks cannot be negative");
}

StockFileReader reader = new StockFileReader(stockFilePath);
List<String> lines = reader.readFile();
CSVStockFileParser parser = new CSVStockFileParser(lines);
List<Stock> stocks = parser.parse();

player = new Player(name, startingMoney);
exchange = new Exchange("Exchange", stocks);
for (int i = 0; i < preRunWeeks; i++) {
exchange.advance();
}

player = new Player(name, startingMoney);
}

public Player getPlayer() {
Expand All @@ -30,4 +42,35 @@ public Player getPlayer() {
public Exchange getExchange() {
return exchange;
}

public List<Stock> getStocks() {
return exchange.getStocks().values().stream()
.sorted(Comparator.comparing(Stock::getSymbol))
.collect(Collectors.toList());
}

/**
* Gives alphabetic sort of findStocks
*
* @param searchTerm
* @return
*/
public List<Stock> searchStocks(String searchTerm) {
if (searchTerm == null || searchTerm.isBlank()) {
return getStocks();
}
return exchange.findStocks(searchTerm).stream()
.sorted(Comparator.comparing(Stock::getSymbol))
.collect(Collectors.toList());
}

/**
* Get stocks with symbol
*
* @param symbol
* @return
*/
public Stock getStock(String symbol) {
return exchange.getStock(symbol);
}
}
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
11 changes: 11 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 Expand Up @@ -66,6 +69,14 @@ public Transaction sell(Share share, Player player) {
return sale;
}

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

public int getWeekNumber() {
return this.weekNumber;
}

public Map<String, Stock> getStocks() {
return this.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
Loading