Skip to content

Commit

Permalink
Feat: Adding buy and sell buttons on stocks tab
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 22, 2026
1 parent 575acb8 commit 9af3c76
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 27 deletions.
64 changes: 60 additions & 4 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package millions.controller;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.math.RoundingMode;
import java.util.stream.Collectors;

import millions.controller.fileIO.CSV.CSVFileHandler;
import millions.controller.fileIO.InvalidFormatException;
import millions.controller.fileIO.UncheckedFileNotFoundException;
import millions.model.Exchange;
import millions.model.Player;
import millions.model.Share;
import millions.model.Stock;
import millions.model.Transaction;
import millions.model.calculators.PurchaseCalculator;

/** Controls game initialization. */
public class GameController {
Expand All @@ -35,9 +39,9 @@ public void startGame(

player = new Player(name, startingMoney);

} catch(UncheckedFileNotFoundException e) {
} catch (UncheckedFileNotFoundException e) {
throw new UncheckedFileNotFoundException(e.getMessage());
} catch(InvalidFormatException e) {
} catch (InvalidFormatException e) {
throw new InvalidFormatException(e.getMessage());
}
}
Expand Down Expand Up @@ -80,4 +84,56 @@ public List<Stock> searchStocks(String searchTerm) {
public Stock getStock(String symbol) {
return exchange.getStock(symbol);
}

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);
}

public Transaction sellShare(Share share) {
if (share == null) {
throw new IllegalArgumentException("Share cannot be null");
}
return exchange.sell(share, player);
}

public List<Share> getOwnedShares(String symbol) {
return new ArrayList<>(player.getPortfolio().getShares(symbol));
}

public void advanceWeek() {
exchange.advance();
}

public BigDecimal getOwnedQuantity(String symbol) {
return player.getPortfolio().getShares(symbol).stream()
.map(Share::getQuantity)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

public int getMaxBuyableQuantity(String symbol) {
Stock stock = getStock(symbol);
if (stock == null || player == null) {
return 0;
}

BigDecimal money = player.getMoney();
BigDecimal price = stock.getSalesPrice();
if (price.compareTo(BigDecimal.ZERO) <= 0) {
return 0;
}

int upperBound = money.divide(price, 0, RoundingMode.FLOOR).intValue();
// Loop so we take care of comission/tax stuff
for (int quantity = upperBound; quantity >= 1; quantity--) {
Share share = new Share(stock, quantity, price);
BigDecimal total = new PurchaseCalculator(share).calculateTotal();
if (total.compareTo(money) <= 0) {
return quantity;
}
}
return 0;
}
}
Loading

0 comments on commit 9af3c76

Please sign in to comment.