Skip to content

Commit

Permalink
Feat: Quantity Button and fixes
Browse files Browse the repository at this point in the history
Added functionality for the quantity buttons, and fixed some logic concerning selling/purchasing stocks and synchronizing the view.
  • Loading branch information
tommyah committed May 15, 2026
1 parent b705e59 commit cf09d0f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ public List<Transaction> sell(final BigDecimal amount, final String stockSymbol,
BigDecimal remainingToSell = amount;

for (Share share : sharesOfStock) {
if (remainingToSell.compareTo(BigDecimal.ZERO) <= 0) break;
if (remainingToSell.compareTo(BigDecimal.ZERO) <= 0) {
break;
}

BigDecimal shareQty = share.getQuantity();

Expand Down
18 changes: 10 additions & 8 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,20 @@ public PlayerStatus getStatus() {
* @param transaction the transaction to handle.
* */
public void handleTransaction(final Transaction transaction) {
if (money.floatValue() > transaction.getCalculator().calculateTotal().floatValue()) {
transactionArchive.add(transaction);
if (transaction instanceof Purchase purchase) {
if (transaction instanceof Purchase purchase) {
if (money.floatValue() > transaction.getCalculator().calculateTotal().floatValue()) {
withdrawMoney(purchase.getCalculator().calculateTotal());
portfolio.addShare(purchase.getShare());
} else if (transaction instanceof Sale sale) {
addMoney(sale.getCalculator().calculateTotal());
portfolio.removeShare(sale.getShare());
transactionArchive.add(transaction);
transaction.commit(this);
}
networthAsFloatProp.setValue(getNetWorth().floatValue());
moneyAsFloatProp.setValue(money);
} else if (transaction instanceof Sale sale) {
addMoney(sale.getCalculator().calculateTotal());
portfolio.removeShare(sale.getShare());
transactionArchive.add(transaction);
transaction.commit(this);
}
networthAsFloatProp.setValue(getNetWorth().floatValue());
moneyAsFloatProp.setValue(money);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
public enum DashBoardActions {
BUY_SHARES,
SELL_SHARES,
SELECT_STOCK;
DECREASE_5,
DECREASE_1,
INCREASE_1,
INCREASE_5;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ protected void initInteractions() {

getViewElement().setOnAction(DashBoardActions.SELL_SHARES, () -> {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())) {
List<Transaction> transactions = exchange.sell(new BigDecimal(getViewElement().getQuantityInputField().getText()), getViewElement().getCurrentStock().getSymbol(), player);
List<Transaction> transactions = exchange.sell(
new BigDecimal(getViewElement().getQuantityInputField().getText()),
getViewElement().getCurrentStock().getSymbol(),
player);

for (Transaction t : transactions) {
if(t.isCommited()) {
Expand All @@ -82,6 +85,27 @@ protected void initInteractions() {
}
});

getViewElement().setOnAction(DashBoardActions.DECREASE_5, () -> {
getViewElement().getQuantityInputField().setText(
new BigDecimal(getViewElement().getQuantityInputField().getText())
.subtract(new BigDecimal("5")).toString());
});
getViewElement().setOnAction(DashBoardActions.DECREASE_1, () -> {
getViewElement().getQuantityInputField().setText(
new BigDecimal(getViewElement().getQuantityInputField().getText())
.subtract(new BigDecimal("1")).toString());
});
getViewElement().setOnAction(DashBoardActions.INCREASE_1, () -> {
getViewElement().getQuantityInputField().setText(
new BigDecimal(getViewElement().getQuantityInputField().getText())
.add(new BigDecimal("1")).toString());
});
getViewElement().setOnAction(DashBoardActions.INCREASE_5, () -> {
getViewElement().getQuantityInputField().setText(
new BigDecimal(getViewElement().getQuantityInputField().getText())
.add(new BigDecimal("5")).toString());
});

exchange.weekProperty().addListener((observable,o,n) -> {
getViewElement().updateGraph();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ protected void initLayout() {

registerButton(DashBoardActions.BUY_SHARES, buyBtn);
registerButton(DashBoardActions.SELL_SHARES, sellBtn);
registerButton(DashBoardActions.DECREASE_5, m5QtyBtn);
registerButton(DashBoardActions.DECREASE_1, m1QtyBtn);
registerButton(DashBoardActions.INCREASE_1, p1QtyBtn);
registerButton(DashBoardActions.INCREASE_5, p5QtyBtn);
}

private ColumnConstraints makeCol(final float w) {
Expand Down

0 comments on commit cf09d0f

Please sign in to comment.