Skip to content

Commit

Permalink
feat(Player): Implement getNetWorth and getStatus. Add corresponding …
Browse files Browse the repository at this point in the history
…tests.
  • Loading branch information
pawelsa committed Mar 25, 2026
1 parent d1ae6ae commit fe55a8e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import edu.ntnu.idi.idatt.transaction.TransactionArchive;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Map;

public class Player {

Expand Down Expand Up @@ -54,4 +56,39 @@ public void addMoney(BigDecimal amount) {
public void withdrawMoney(BigDecimal amount) {
this.money = this.money.subtract(amount);
}

/**
* Method for obtaining players net worth.
*
* @return players net worth.
*/
public BigDecimal getNetWorth() {
return money.add(portfolio.getNetWorth());
}

/**
* Method for obtaining players trading status.
*
* <p>
* A symbolic title/rank that describes the way the
* players trading career has been going.
* </p>
*
* @return String of corresponding title. TODO: Change to ENUM!!!
*/
public String getStatus() {
int tradingWeeks = transactionArchive.countDistinctWeeks();
BigDecimal netWorth = this.getNetWorth().divide(this.startingMoney);

if (tradingWeeks >= 20 && netWorth.compareTo(new BigDecimal("2")) >= 0) {
return "Speculator";
}

if (tradingWeeks >= 10 && netWorth.compareTo(new BigDecimal("1.2")) >= 0) {
return "Investor";
}

return "Novice";
}

}
50 changes: 50 additions & 0 deletions src/test/java/edu/ntnu/idi/idatt/PlayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.math.BigDecimal;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import edu.ntnu.idi.idatt.marked.Share;
import edu.ntnu.idi.idatt.marked.Stock;
import edu.ntnu.idi.idatt.transaction.Purchase;

class PlayerTest {

private Player player;
Expand Down Expand Up @@ -41,4 +46,49 @@ void PTwithdrawMoney() {
assertEquals(new BigDecimal("300"), player.getMoney());
}

@Test
void PTgetNetWorth() {
assertEquals(player.getNetWorth(), new BigDecimal("500"));

// Add to player portfolio
Stock stock = new Stock("A", "A", List.of(new BigDecimal("20")));
Share share = new Share(stock, new BigDecimal("3.5"), new BigDecimal("30"));
player.getPortfolio().addShare(share);

BigDecimal sum = player.getMoney().add(player.getPortfolio().getNetWorth());
assertEquals(sum, player.getNetWorth());

}

@Test
void PTgetStatus() {
assertEquals("Novice", player.getStatus());

// Simulate progress
Stock stock = new Stock("A", "A", List.of(new BigDecimal("20")));
Share share = new Share(stock, new BigDecimal("3.5"), new BigDecimal("30"));

for (int i = 0; i <= 20; i++) {
player.getTransactionArchive().add(new Purchase(share, i));
}

// Check different ifs
player.addMoney(new BigDecimal("90")); // 18%
assertEquals("Novice", player.getStatus());
player.addMoney(new BigDecimal("10")); // 20%
assertEquals("Investor", player.getStatus());
player.addMoney(new BigDecimal("400")); // 100%
assertEquals("Speculator", player.getStatus());

// new Player to check if Speculator dont happen week < 20
Player plr = new Player("plr", new BigDecimal("500"));

for (int i = 0; i <= 15; i++) {
plr.getTransactionArchive().add(new Purchase(share, i));
}
plr.addMoney(new BigDecimal("600")); // 120%
assertEquals("Investor", plr.getStatus());

}

}

0 comments on commit fe55a8e

Please sign in to comment.