Skip to content

Commit

Permalink
Feat: Updated player class
Browse files Browse the repository at this point in the history
Added method for getting networth
  • Loading branch information
tommyah committed Mar 18, 2026
1 parent 64b386e commit 74494b3
Showing 1 changed file with 111 additions and 68 deletions.
179 changes: 111 additions & 68 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,122 @@

import java.math.BigDecimal;

public class Player {
/**
* Represents a player in the system.
*
* <p>Each player:</p>
* <ul>
* <li>Has a portfolio</li>
* <li>Can buy and sell shares on an {@link Exchange}</li>
* <li>Has a set amount of money to use on said exchange.</li>
* <li>Has a {@link TransactionArchive}</li>
* </ul>
* */
public final class Player {

private final String name;
private final BigDecimal startingMoney;
private BigDecimal money;
private final Portfolio portfolio;
private final TransactionArchive transactionArchive;
/**
* Name of player.
* */
private final String name;

/**
* Creates a new player with a given name and starting capital.
*
* @param name the name of the player
* @param startingMoney the starting amount of money
*/
public Player(String name, BigDecimal startingMoney) {
this.name = name;
this.startingMoney = startingMoney;
this.money = this.startingMoney;
this.portfolio = new Portfolio();
this.transactionArchive = new TransactionArchive();
}
/**
* Starting money of player.
* */
private final BigDecimal startingMoney;

/**
* Returns the name of the player.
*
* @return the player's name
*/
public String getName() {
return name;
}
/**
* Current money of player.
* */
private BigDecimal money;

/**
* Returns the players current balance.
*
* @return the current amount of money
*/
public BigDecimal getMoney() {
return money;
}
/**
* The players' portfolio, holding their shares.
* */
private final Portfolio portfolio;

/**
* Adds money to the players balance.
*
* @param amount the amount to add
*/
public void addMoney(BigDecimal amount) {
money = money.add(amount);
}
/**
* The players' transaction archive,
* holding a history of transactions on the exchange.
* */
private final TransactionArchive transactionArchive;

/**
* Withdraws money from the players balance.
*
* @param amount the amount to withdraw
*/
public void withdrawMoney(BigDecimal amount) {
money = money.subtract(amount);
}
/**
* Creates a new player with a given name and starting capital.
*
* @param name the name of the player
* @param startingMoney the starting amount of money
*/
public Player(final String name, final BigDecimal startingMoney) {
this.name = name;
this.startingMoney = startingMoney;
this.money = this.startingMoney;
this.portfolio = new Portfolio();
this.transactionArchive = new TransactionArchive();
}

/**
* Returns the players portfolio.
*
* @return the portfolio
*/
public Portfolio getPortfolio() {
return portfolio;
}
/**
* Returns the name of the player.
*
* @return the player's name
*/
public String getName() {
return name;
}

/**
* Returns the players transaction archive.
*
* @return the transaction archive
*/
public TransactionArchive getTransactionArchive() {
return transactionArchive;
}
}
/**
* Returns the players current balance.
*
* @return the current amount of money
*/
public BigDecimal getMoney() {
return money;
}

/**
* Adds money to the players balance.
*
* @param amount the amount to add
*/
public void addMoney(final BigDecimal amount) {
money = money.add(amount);
}

/**
* Withdraws money from the players balance.
*
* @param amount the amount to withdraw
*/
public void withdrawMoney(final BigDecimal amount) {
money = money.subtract(amount);
}

/**
* Returns the players portfolio.
*
* @return the portfolio
*/
public Portfolio getPortfolio() {
return portfolio;
}

/**
* Returns the players transaction archive.
*
* @return the transaction archive
*/
public TransactionArchive getTransactionArchive() {
return transactionArchive;
}

/**
* Returns the total net worth of the player,
* including the shares in the players' portfolio.
*
* @return the net worth of the player.
* */
public BigDecimal getNetWorth() {
BigDecimal netWorth = new BigDecimal("0");
netWorth = netWorth.add(portfolio.getNetWorth()).add(money);
return netWorth;
}
}

0 comments on commit 74494b3

Please sign in to comment.