Skip to content

Finished issue player-status-method #19

Merged
merged 3 commits into from
Mar 25, 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
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";
}

}
15 changes: 8 additions & 7 deletions src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.stream.Collectors;

import edu.ntnu.idi.idatt.calculator.SaleCalculator;

/**
* Portfolio class
*
Expand Down Expand Up @@ -70,15 +72,14 @@ public boolean contains(Share share) {
}

/**
* Method for getting the net value of the player.
* Method for getting the net value of the portfolio..
*
* @return - Net value of the players shares.
* @return - Net value of the portfolio.
*/
public BigDecimal playerNetValue(){
BigDecimal netValue = shares.stream()
.map(Share-> Share.getPurchasePrice().multiply(Share.getQuantity()))
.reduce(BigDecimal.ZERO, BigDecimal::add);
return netValue;
public BigDecimal getNetWorth() {
return shares.stream()
.map(s -> new SaleCalculator(s).calculateTotal())
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ public List<Sale> getSales(int week) {
*
* @return
*/
public int countDistinctWeeks() { // TODO: HERE
return -1;
public int countDistinctWeeks() {
return (int) transactions.stream()
.map(Transaction::getWeek)
.distinct()
.count();
}

}
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());

}

}
7 changes: 5 additions & 2 deletions src/test/java/edu/ntnu/idi/idatt/marked/PortfolioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import edu.ntnu.idi.idatt.calculator.SaleCalculator;

public class PortfolioTest {

private Stock stock;
Expand Down Expand Up @@ -82,12 +84,13 @@ void NTremoveShare() {
* Positive test for finding net value of the players shares.
*/
@Test
void PTplayerNetValue(){
void PTgetNetWorth() {
Share share1 = new Share(stock, new BigDecimal("1"), new BigDecimal("135.8"));
Share share2 = new Share(stock, new BigDecimal("2"), new BigDecimal("254"));
portfolio.addShare(share1);
portfolio.addShare(share2);
assertEquals(new BigDecimal("643.8"), portfolio.playerNetValue());
assertEquals(new SaleCalculator(share1).calculateTotal().add(new SaleCalculator(share2).calculateTotal()),
portfolio.getNetWorth());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,18 @@ void PTgetTransactions() {

}

/**
* Tests for countDistinctWeeks().
*/
@Test
void PTcountDistinctWeeks() {
assertEquals(2, transactionArchive.countDistinctWeeks());
}

@Test
void NTcountDistinctWeeks() {
TransactionArchive tArchive = new TransactionArchive();
assertEquals(0, tArchive.countDistinctWeeks());
}

}