From 5ed7b2b7100d65c569b2f04144397533e87e90a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Sk=C3=B8ien?= Date: Mon, 23 Mar 2026 14:58:13 +0100 Subject: [PATCH] implement player status and methods for calculating networth --- .../idi/idatt2003/gruppe42/Model/Player.java | 35 ++++++++++++++++++- .../idatt2003/gruppe42/Model/Portfolio.java | 16 +++++++++ .../idi/idatt2003/gruppe42/Model/Status.java | 19 ++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Status.java diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java index 14beaf7..ba5d1cc 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java @@ -31,7 +31,6 @@ public Player(String name, BigDecimal startingMoney) { this.money = startingMoney; this.portfolio = new Portfolio(); this.transactionArchive = new TransactionArchive(); - } public String getName() { @@ -70,4 +69,38 @@ public void withdrawMoney(BigDecimal amount) { } money = money.subtract(amount); } + + /** + * Calculates the total net worth of a player (portfolio + money). + * @return the player's total net worth + */ + public BigDecimal getNetWorth() { + return money.add(portfolio.getNetWorth()); + } + + /** + * Returns the current status of the player depending on qualifications + * including distinct active weeks and networth (NOVICE / INVESTOR / SPECULATOR). + * @return the current status of the player + */ + public Status getStatus() { + int weeksActive = transactionArchive.countDistinctWeeks(); + BigDecimal netWorth = getNetWorth(); + + BigDecimal investorTarget = startingMoney.multiply(BigDecimal.valueOf(1.2)); + BigDecimal speculatorTarget = startingMoney.multiply(BigDecimal.valueOf(2)); + + boolean isInvestor = weeksActive >= 10 && netWorth.compareTo(investorTarget) >= 0; + boolean isSpeculator = weeksActive >= 20 && netWorth.compareTo(speculatorTarget) >= 0; + + if (isSpeculator) { + return Status.SPECULATOR; + } + else if (isInvestor) { + return Status.INVESTOR; + } + else { + return Status.NOVICE; + } + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Portfolio.java index e1ef3ec..1353056 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Portfolio.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Portfolio.java @@ -1,5 +1,8 @@ package edu.ntnu.idi.idatt2003.gruppe42.Model; +import edu.ntnu.idi.idatt2003.gruppe42.Model.Calculator.SaleCalculator; + +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -61,4 +64,17 @@ public List getShares() { public boolean contains(Share share) { return share != null && shares.contains(share); } + + /** + * Iterates through a player's portfolio and calculates + * the total networth of all shares if sold today. + * @return the total sum of all shares + */ + public BigDecimal getNetWorth() { + BigDecimal totalSum = BigDecimal.ZERO; + for (Share share : shares) { + totalSum = totalSum.add(new SaleCalculator(share).calculateTotal()); + } + return totalSum; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Status.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Status.java new file mode 100644 index 0000000..bf966ea --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Status.java @@ -0,0 +1,19 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Model; + +/** + * Represents the progression level of a player. + *

+ * A player can have one of the following statuses based on their + * trading activity and net worth growth: + *

+ * + */ +public enum Status { + NOVICE, + INVESTOR, + SPECULATOR; +}