Skip to content

implement player status and methods for calculating networth #59

Merged
merged 1 commit into from
Mar 23, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public Player(String name, BigDecimal startingMoney) {
this.money = startingMoney;
this.portfolio = new Portfolio();
this.transactionArchive = new TransactionArchive();

}

public String getName() {
Expand Down Expand Up @@ -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;
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Portfolio.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -61,4 +64,17 @@ public List<Share> 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;
}
}
19 changes: 19 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

/**
* Represents the progression level of a player.
* <p>
* A player can have one of the following statuses based on their
* trading activity and net worth growth:
* </p>
* <ul>
* <li>{@link #NOVICE}: Starting level with no requirements.</li>
* <li>{@link #INVESTOR}: Achieved by trading for at least 10 distinct weeks and increasing net worth by at least 20%.</li>
* <li>{@link #SPECULATOR}: Achieved by trading for at least 20 distinct weeks and doubling net worth.</li>
* </ul>
*/
public enum Status {
NOVICE,
INVESTOR,
SPECULATOR;
}