Skip to content

Commit

Permalink
Added logging for exceptions in startview. Continued JavaDoc document…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Nikollai committed May 24, 2026
1 parent 0459af1 commit ff73c1b
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 23 deletions.
35 changes: 33 additions & 2 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List<Stock> getStocks() {
* Gives alphabetic sort of findStocks
*
* @param searchTerm
* @return
* @return Alphabetically sorted list of stocks
*/
public List<Stock> searchStocks(String searchTerm) {
if (searchTerm == null || searchTerm.isBlank()) {
Expand All @@ -78,26 +78,45 @@ public List<Stock> searchStocks(String searchTerm) {
* Get stocks with symbol
*
* @param symbol
* @return
* @return stocks with matching symbol
*/
public Stock getStock(String symbol) {
return exchange.getStock(symbol);
}

/**
* Purchases a stock
*
* @param symbol Symbol of desired stock
* @param quantity How much of a stock to purchase
* @return Transaction object for purchase
*/
public Transaction buyStock(String symbol, BigDecimal quantity) {
if (quantity == null || quantity.compareTo(BigDecimal.ZERO) <= 0) {
throw new IllegalArgumentException("Quantity must be positive");
}
return exchange.buy(symbol, player, quantity);
}

/**
* Sells a share
*
* @param share Share to sell
* @return Transaction object for sale
*/
public Transaction sellShare(Share share) {
if (share == null) {
throw new IllegalArgumentException("Share cannot be null");
}
return exchange.sell(share, player);
}

/**
* Returns a players owned shares
*
* @param symbol Symbol for stock
* @return List of shares
*/
public List<Share> getOwnedShares(String symbol) {
return new ArrayList<>(player.getPortfolio().getShares(symbol));
}
Expand All @@ -106,12 +125,24 @@ public void advanceWeek() {
exchange.advance();
}

/**
* Returns the players owned quantity of a given share
*
* @param symbol Symbol for shares
* @return BigDecimal Quantity
*/
public BigDecimal getOwnedQuantity(String symbol) {
return player.getPortfolio().getShares(symbol).stream()
.map(Share::getQuantity)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

/**
* Returns the maximum quantity of a stock the player can purchase
*
* @param symbol Symbol for stock
* @return int Quantity
*/
public int getMaxBuyableQuantity(String symbol) {
Stock stock = getStock(symbol);
if (stock == null || player == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

/**
* <p>Bundles StockFileReader and CSVStockFileParser together to reduce boilerplate code when reading stocks from a csv file</p>
* Bundles StockFileReader and CSVStockFileParser together to reduce boilerplate code when reading stocks from a csv file
*/
public class CSVFileHandler {
StockFileReader reader;
Expand All @@ -24,7 +24,7 @@ public CSVFileHandler() {
/**
* Reads and parses stocks from a csv file
* @param filePath Path to stock file
* @return list of stock object created from parsed file
* @return list of stock objects created from parsed file
* @throws InvalidFormatException Throws an InvalidFormatException received from parser
* @throws UncheckedFileNotFoundException Upon Receiving a FilenotFoundException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public CSVStockFileParser() {}
* Verifies the amount of data fields present in supplied CSV file.
*
* @param lines Lines from csv file
* @return Boolean: True if file satisfies expected format (3 fields)
* @return Boolean: True if file satisfies expected format
*/
public boolean verifyCSV(List<String> lines) {
return lines.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package millions.controller.fileIO;

/**
* RuntimeException wrapper for FileNotFoundException.
*/
public class UncheckedFileNotFoundException extends RuntimeException {
public UncheckedFileNotFoundException(String message) {
super(message);
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/millions/model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public Exchange(String name, List<Stock> stockList) {
}
}

/**
* Purchases a quantity of a given stock.
*
* @param symbol Symbol identifying the stock.
* @param player The player doing the purchase.
* @param quantity Quantity to purchase.
* @throws IllegalArgumentException If a stock isn't found
* @return Transaction object for purchase.
*/
public Transaction buy(String symbol, Player player, BigDecimal quantity) {

Stock stock = this.stocks.get(symbol);
Expand All @@ -61,6 +70,13 @@ public Transaction buy(String symbol, Player player, int quantity) {
return this.buy(symbol, player, BigDecimal.valueOf(quantity));
}

/**
* Creates a transaction for a sale.
*
* @param share share to sell.
* @param player player performing the sale.
* @return Transaction object for sale.
*/
public Transaction sell(Share share, Player player) {
Transaction sale = saleFactory.createTransaction(share, weekNumber);

Expand Down Expand Up @@ -95,6 +111,12 @@ public List<Stock> findStocks(String searchTerm) {
.toList();
}

/**
* Returns the best performing stocks.
*
* @param limit Mmount of stocks to collect.
* @return Sorted list of stock objects sorted
*/
public List<Stock> getGainers(int limit) {
Collection<Stock> stocksCollection = stocks.values();
return stocksCollection.stream()
Expand All @@ -103,6 +125,12 @@ public List<Stock> getGainers(int limit) {
.collect(Collectors.toList());
}

/**
* Returns the worst performing stocks.
*
* @param limit Amount of stocks to collect
* @return Sorted list of stock objects
*/
public List<Stock> getLosers(int limit) {
Collection<Stock> stocksCollection = stocks.values();
return stocksCollection.stream()
Expand All @@ -111,6 +139,9 @@ public List<Stock> getLosers(int limit) {
.collect(Collectors.toList());
}

/**
* Advances the current game week by performing new price calculations for all stocks.
*/
public void advance() {
this.weekNumber++;
for (Stock stock : this.stocks.values()) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/millions/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public void withdrawMoney(BigDecimal amount) {
}

/**
* @return
* Calculates the skill level of the player
* @return String player status level
*/
public String getStatus() {
// TODO dobbel sjekk logikken
int weeksTraded = transactionArchive.countDistinctWeeks();

String status = "Novice";
Expand All @@ -81,21 +81,21 @@ public String getStatus() {
}

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

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

/**
* @return
* @return player portfolio
*/
public Portfolio getPortfolio() {
return this.portfolio;
Expand All @@ -120,28 +120,28 @@ public void removeShareFromPortfolio(Share share) {
}

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

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

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

/**
* @param listener
* @param listener PlayerListener
*/
public void removeListener(PlayerListener listener) {
listeners.remove(listener);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/millions/model/Portfolio.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ public Portfolio() {

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

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

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

/**
* @param symbol
* @return
* @param symbol Symbol for share
* @return List of shares
*/
public List<Share> getShares(String symbol) {
return this.shares.stream()
Expand All @@ -47,7 +47,7 @@ public List<Share> getShares(String symbol) {
}

/**
* @return
* @return BigDecimal net worth
*/
public BigDecimal getNetWorth() {
BigDecimal total = BigDecimal.ZERO;
Expand All @@ -59,8 +59,8 @@ public BigDecimal getNetWorth() {
}

/**
* @param share
* @return
* @param share Share
* @return Boolean
*/
public boolean contains(Share share) {
return this.shares.contains(share);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/millions/view/StartView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.net.URL;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
Expand All @@ -15,10 +18,11 @@
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import millions.App;

/** The initial game setup screen where the player enters their info. */
public class StartView extends VBox {

private static final Logger logger = Logger.getLogger(StartView.class.getName());
private TextField nameField;
private TextField startingAmountField;
private TextField preRunWeeksField;
Expand Down Expand Up @@ -110,6 +114,7 @@ private File loadDefaultStocksFile() {
URL resource = Objects.requireNonNull(getClass().getResource("/data/default-stocks.csv"));
return Paths.get(resource.toURI()).toFile();
} catch (URISyntaxException e) {
logger.log(Level.SEVERE, "Error accessing default stocks file", e);
throw new IllegalStateException("Could not load default stocks file", e);
}
}
Expand Down

0 comments on commit ff73c1b

Please sign in to comment.