-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement Finish barebone MVC to finish the game.
- Loading branch information
PawelSapula
committed
May 24, 2026
1 parent
b4a8c63
commit a4a82c9
Showing
7 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/edu/ntnu/idi/idatt/view/primary/finish/FinishController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package edu.ntnu.idi.idatt.view.primary.finish; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import java.util.ArrayList; | ||
|
|
||
| import edu.ntnu.idi.idatt.model.portfolio.Share; | ||
| import edu.ntnu.idi.idatt.session.UserSession; | ||
| import edu.ntnu.idi.idatt.view.components.AbstractController; | ||
| import edu.ntnu.idi.idatt.view.util.CssUtils; | ||
| import javafx.scene.Parent; | ||
|
|
||
| public class FinishController extends AbstractController<FinishModel> { | ||
|
|
||
| private UserSession session = UserSession.getInstance(); | ||
|
|
||
| public FinishController(FinishModel model) { | ||
| super(model); | ||
| makeFinishedGameState(); | ||
| } | ||
|
|
||
| public void makeFinishedGameState() { | ||
| // Sell all shares | ||
|
|
||
| // Copy shares (Avoid ConcurrentModificationException) | ||
| ArrayList<Share> holdings = new ArrayList<>(session.getPlayer().getPortfolio().getShares()); | ||
|
|
||
| // Sell | ||
| holdings.forEach(share -> session.getExchange().sell(share, session.getPlayer())); | ||
|
|
||
| // Update gamestate | ||
| session.updateGameState(); | ||
|
|
||
| } | ||
|
|
||
| public String getWeek() { | ||
| return String.format("Week: %d", session.getExchange().getWeek()); | ||
| } | ||
|
|
||
| public String getTransactionAmount() { | ||
| return String.format("Transactions made: %d", | ||
| session.getPlayer().getTransactionArchive().getTransactions().size()); | ||
| } | ||
|
|
||
| public String getPlayerStatus() { | ||
| return String.format("Player status: %s", | ||
| session.getPlayer().getStatus()); | ||
| } | ||
|
|
||
| public String getPlayerName() { | ||
| return String.format("Player name: %s", session.getPlayer().getName()); | ||
| } | ||
|
|
||
| public String getTotalNetWorth() { | ||
| return String.format("Total net worth: %.2f $", | ||
| session.getPlayer().getMoney()); | ||
| } | ||
|
|
||
| private BigDecimal calculateProfits() { | ||
| BigDecimal profit = session.getPlayer().getMoney().subtract( | ||
| session.getPlayer().getStartingMoney()); | ||
| return profit; | ||
| } | ||
|
|
||
| private BigDecimal calculateROI() { | ||
| BigDecimal profit = session.getPlayer().getMoney().subtract( | ||
| session.getPlayer().getStartingMoney()); | ||
| BigDecimal profitPercent = profit.divide(session.getPlayer().getStartingMoney()).setScale(2, RoundingMode.HALF_UP); | ||
|
|
||
| BigDecimal returnOfInvestement = profitPercent.multiply(new BigDecimal("100")); | ||
|
|
||
| return returnOfInvestement; | ||
| } | ||
|
|
||
| public String getProfits() { | ||
| return String.format("Total profits: %.2f $", | ||
| calculateProfits()); | ||
| } | ||
|
|
||
| public String getROI() { | ||
| return String.format("Return of investement: %.2f %%", calculateROI()); | ||
| } | ||
|
|
||
| public void setProfitsColor(Parent profitsLabel) { | ||
| CssUtils.apply(profitsLabel, CssUtils.generateValueColors(calculateProfits())); | ||
| } | ||
|
|
||
| public void setRoiColor(Parent roiLabel) { | ||
| CssUtils.apply(roiLabel, CssUtils.generateValueColors(calculateROI())); | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/edu/ntnu/idi/idatt/view/primary/finish/FinishModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package edu.ntnu.idi.idatt.view.primary.finish; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| import edu.ntnu.idi.idatt.view.components.Model; | ||
| import javafx.beans.property.SimpleObjectProperty; | ||
|
|
||
| public class FinishModel implements Model { | ||
|
|
||
| private final SimpleObjectProperty<BigDecimal> profitProperty = new SimpleObjectProperty<>(); | ||
| private final SimpleObjectProperty<BigDecimal> roiProperty = new SimpleObjectProperty<>(); | ||
|
|
||
| public SimpleObjectProperty<BigDecimal> getProfitProperty() { | ||
| return profitProperty; | ||
| } | ||
|
|
||
| public SimpleObjectProperty<BigDecimal> getRoiProperty() { | ||
| return roiProperty; | ||
| } | ||
|
|
||
| } |
98 changes: 98 additions & 0 deletions
98
src/main/java/edu/ntnu/idi/idatt/view/primary/finish/FinishView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package edu.ntnu.idi.idatt.view.primary.finish; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| import edu.ntnu.idi.idatt.view.SceneFactory; | ||
| import edu.ntnu.idi.idatt.view.SceneManager; | ||
| import edu.ntnu.idi.idatt.view.components.AbstractView; | ||
| import edu.ntnu.idi.idatt.view.components.ui.UICompositor; | ||
| import edu.ntnu.idi.idatt.view.util.CssUtils; | ||
| import javafx.application.Platform; | ||
| import javafx.geometry.Pos; | ||
| import javafx.scene.Parent; | ||
| import javafx.scene.control.Button; | ||
| import javafx.scene.control.Label; | ||
| import javafx.scene.layout.HBox; | ||
| import javafx.scene.layout.StackPane; | ||
| import javafx.scene.layout.VBox; | ||
|
|
||
| public class FinishView extends AbstractView<StackPane> { | ||
|
|
||
| public FinishView() { | ||
| StackPane root = new StackPane(); | ||
| super(root); | ||
| root.getStyleClass().add("dark"); | ||
| } | ||
|
|
||
| Label playerName; | ||
| Label playerStatus; | ||
| Label weeksPassed; | ||
| Label transactionsAmount; | ||
| Label totalNetWorth; | ||
| Label totalProfits; | ||
| Label returnOfInvestement; | ||
|
|
||
| Button newGame; | ||
| Button exit; | ||
|
|
||
| @Override | ||
| public Parent createContent() { | ||
|
|
||
| Label title = new Label("Finished game! - Summary"); | ||
| CssUtils.apply(title, CssUtils.BIG_TEXT_32); | ||
|
|
||
| playerName = new Label(); | ||
| playerStatus = new Label(); | ||
| weeksPassed = new Label(); | ||
| transactionsAmount = new Label(); | ||
| totalNetWorth = new Label(); | ||
| totalProfits = new Label(); | ||
| returnOfInvestement = new Label(); | ||
|
|
||
| List<Label> labels = List.of(playerName, playerStatus, new Label(" "), weeksPassed, transactionsAmount, | ||
| totalNetWorth, totalProfits, | ||
| returnOfInvestement); | ||
|
|
||
| labels.forEach(l -> CssUtils.apply(l, CssUtils.MED_TEXT_16)); | ||
|
|
||
| newGame = new Button("New game.."); | ||
| exit = new Button("Exit.."); | ||
|
|
||
| newGame.getStyleClass().add("button"); | ||
| exit.getStyleClass().add("button"); | ||
|
|
||
| UICompositor.Builder finishUIBuilder = new UICompositor.Builder() | ||
| .parent(new VBox()) | ||
| .growWithAlignment(Pos.CENTER) | ||
| .addContent(title); | ||
|
|
||
| labels.forEach(l -> finishUIBuilder.addContent(l)); | ||
|
|
||
| finishUIBuilder | ||
| .wrap(new HBox(10)) | ||
| .growWithAlignment(Pos.CENTER) | ||
| .addAllContent(newGame, exit) | ||
| .unwrap(); | ||
|
|
||
| return finishUIBuilder.build().makeUI(); | ||
|
|
||
| } | ||
|
|
||
| public void setController(FinishController controller) { | ||
| playerName.setText(controller.getPlayerName()); | ||
| playerStatus.setText(controller.getPlayerStatus()); | ||
| weeksPassed.setText(controller.getWeek()); | ||
| transactionsAmount.setText(controller.getTransactionAmount()); | ||
| totalNetWorth.setText(controller.getTotalNetWorth()); | ||
| totalProfits.setText(controller.getProfits()); | ||
| returnOfInvestement.setText(controller.getROI()); | ||
|
|
||
| controller.setProfitsColor(totalProfits); | ||
| controller.setRoiColor(returnOfInvestement); | ||
|
|
||
| newGame.setOnAction((e) -> SceneManager.switchTo(SceneFactory.createStartView())); | ||
| exit.setOnAction((e) -> Platform.exit()); | ||
| } | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters