diff --git a/README.md b/README.md
index fc165a2..b16466c 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,12 @@
# Mappevurdering-IDATT2003
+
The exam of IDATT2003, "Programming 2" subject.
+
+# Guide to tests
+
+- The test generally start with a @BeforeEach method that sets up everything necessary for positive testing.
+- Then there comes a positive test section and at last the negative tests.
+- Complicated test methods have necessary JavaDocs and often have references pointing to other classes that share content or hold relevant information.
+- Dictionary
+ - **PT** - Positive tests
+ - **NT** - Negative tests
diff --git a/pom.xml b/pom.xml
index 1cf199e..467f022 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,13 +20,7 @@
+ * Class that keeps the 'stock game' gameloop. + * Contains methods for managing game states aswell as performing + * all functionality. + *
+ * + */ public class Exchange { - //TODO: JavaDocs, Write over functions mby - - private final String name; - private int week; - private HashMap+ * Executes a purchase for a player which executes all logic + * and management of money, portfolio and archive. + *
+ * + * @see Purchase + * + * @param symbol - The symbol of the bought stock. + * @param quantity - The amount of a bought stock. + * @param player - which player did this event. + * @return The given transaction details. (Transaction). + * @see Transaction + */ + public Transaction buy(String symbol, BigDecimal quantity, Player player) { + Share share = new Share(getStock(symbol), quantity, BigDecimal.valueOf(random.nextDouble())); + Purchase purchase = new Purchase(share, this.week); + purchase.commit(player); + return player.getTransactionArchive().getPurchases(this.week).getLast(); + } + + /** + * Method to allow a player to sell a stock. + * + *+ * Executes a sale for a player which executes all logic + * and management of money, portfolio and archive. + *
+ * + * @see Sale + * + * @param Share - The instance of the sold share. + * @param player - which player did this event. + * @return The given transaction details. (Transaction). + * @see Transaction + */ + public Transaction sell(Share share, Player player) { + Sale sale = new Sale(share, this.week); + sale.commit(player); + return player.getTransactionArchive().getSales(this.week).getLast(); + } + + /** + * Method to advance the gameloop. + * + *+ * Adds a new price to each of the stock array. + *
+ * + * @see Stock + */ + public void advance() { + for (Stock stocks : stockMap.values()) { + stocks.addNewSalesPrice(BigDecimal.valueOf(random.nextDouble())); } - - public Transaction sell(Share share, Player player) { - Sale sale = new Sale(share, this.week); - sale.commit(player); - return player.getTransactionArchive().getSales(this.week).getLast(); - } - - public void advance() { - for(Stock stocks : stockMap.values()) { - stocks.addNewSalesPrice(BigDecimal.valueOf(random.nextDouble())); - } - } - + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/Main.java b/src/main/java/edu/ntnu/idi/idatt/Main.java index 57408d3..a7ee785 100644 --- a/src/main/java/edu/ntnu/idi/idatt/Main.java +++ b/src/main/java/edu/ntnu/idi/idatt/Main.java @@ -8,4 +8,4 @@ static void main() { System.out.println("Hello world!"); } -} \ No newline at end of file +} diff --git a/src/main/java/edu/ntnu/idi/idatt/Player.java b/src/main/java/edu/ntnu/idi/idatt/Player.java index cb2ae90..3a5e61d 100644 --- a/src/main/java/edu/ntnu/idi/idatt/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt/Player.java @@ -7,51 +7,51 @@ public class Player { - private final String name; - private final BigDecimal startingMoney; - private BigDecimal money; - private Portfolio portfolio = new Portfolio(); - private TransactionArchive transactionArchive = new TransactionArchive(); - - public Player(String name, BigDecimal startingMoney) { - this.name = name; - this.startingMoney = startingMoney; - this.money = this.startingMoney; - } - - /** - * Getters - * - * @return - Their corresponding variables. - */ - - public String getName() { - return name; - } - - public BigDecimal getMoney() { - return money; - } - - public Portfolio getPortfolio() { - return portfolio; - } - - public TransactionArchive getTransactionArchive() { - return transactionArchive; - } - - /** - * Setters for money - * - * @param amount - Amount to be changed correspondingly. - */ - - public void addMoney(BigDecimal amount){ - this.money = this.money.add(amount); - } - - public void withdrawMoney(BigDecimal amount){ - this.money = this.money.subtract(amount); - } + private final String name; + private final BigDecimal startingMoney; + private BigDecimal money; + private Portfolio portfolio = new Portfolio(); + private TransactionArchive transactionArchive = new TransactionArchive(); + + public Player(String name, BigDecimal startingMoney) { + this.name = name; + this.startingMoney = startingMoney; + this.money = this.startingMoney; + } + + /** + * Getters + * + * @return - Their corresponding variables. + */ + + public String getName() { + return name; + } + + public BigDecimal getMoney() { + return money; + } + + public Portfolio getPortfolio() { + return portfolio; + } + + public TransactionArchive getTransactionArchive() { + return transactionArchive; + } + + /** + * Setters for money + * + * @param amount - Amount to be changed correspondingly. + */ + + public void addMoney(BigDecimal amount) { + this.money = this.money.add(amount); + } + + public void withdrawMoney(BigDecimal amount) { + this.money = this.money.subtract(amount); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java b/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java index 1bd3ae6..fd7847f 100644 --- a/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java @@ -7,64 +7,66 @@ /** * PurchaseCalculator class * - *Calculates transaction price based on - * share bought.
+ *+ * Calculates transaction price based on + * share bought. + *
* */ -public class PurchaseCalculator implements TransactionCalculator{ +public class PurchaseCalculator implements TransactionCalculator { - private final BigDecimal purchasePrice; - private final BigDecimal quantity; + private final BigDecimal purchasePrice; + private final BigDecimal quantity; - /** - * Constructor for PurchaseCalculator - * - * @param share - The bought share - */ - public PurchaseCalculator(Share share){ - this.purchasePrice = share.getPurchasePrice(); - this.quantity = share.getQuantity(); - } + /** + * Constructor for PurchaseCalculator + * + * @param share - The bought share + */ + public PurchaseCalculator(Share share) { + this.purchasePrice = share.getPurchasePrice(); + this.quantity = share.getQuantity(); + } - /** - * Method that calculates gross value - * - * @return - BigDecimal of purchase price x share quantity - */ - @Override - public BigDecimal calculateGross() { - return purchasePrice.multiply(quantity); - } + /** + * Method that calculates gross value + * + * @return - BigDecimal of purchase price x share quantity + */ + @Override + public BigDecimal calculateGross() { + return purchasePrice.multiply(quantity); + } - /** - * Method that calculates the commission fee - * - * @return - BigDecimal of gross value x 0.5%. - */ - @Override - public BigDecimal calculateCommision() { - BigDecimal fee = BigDecimal.valueOf(0.005); // Corresponding to 0.5% - return calculateGross().multiply(fee); - } + /** + * Method that calculates the commission fee + * + * @return - BigDecimal of gross value x 0.5%. + */ + @Override + public BigDecimal calculateCommision() { + BigDecimal fee = new BigDecimal("0.005"); // Corresponding to 0.5% + return calculateGross().multiply(fee); + } - /** - * Method that calculates the tax - * - * @return - BigDecimal of 0 based on project requirements. - */ - @Override - public BigDecimal calculateTax() { - return BigDecimal.ZERO; - } + /** + * Method that calculates the tax + * + * @return - BigDecimal of 0 based on project requirements. + */ + @Override + public BigDecimal calculateTax() { + return BigDecimal.ZERO; + } - /** - * Method that calculates the total price of a bought share - * - * @return - BigDecimal of gross value + commision fee + tax - */ - @Override - public BigDecimal calculateTotal() { - return calculateGross().add(calculateCommision()).add(calculateTax()); - } + /** + * Method that calculates the total price of a bought share + * + * @return - BigDecimal of gross value + commision fee + tax + */ + @Override + public BigDecimal calculateTotal() { + return calculateGross().add(calculateCommision()).add(calculateTax()); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/calculator/SaleCalculator.java b/src/main/java/edu/ntnu/idi/idatt/calculator/SaleCalculator.java index 3f59521..6c65302 100644 --- a/src/main/java/edu/ntnu/idi/idatt/calculator/SaleCalculator.java +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/SaleCalculator.java @@ -4,73 +4,76 @@ import java.math.BigDecimal; - /** * SaleCalculator class * - *Calculates transaction profit based on - * share sold.
+ *+ * Calculates transaction profit based on + * share sold. + *
* */ -public class SaleCalculator implements TransactionCalculator{ +public class SaleCalculator implements TransactionCalculator { + + private final BigDecimal purchasePrice; + private final BigDecimal salesPrice; + private final BigDecimal quantity; - private final BigDecimal purchasePrice; - private final BigDecimal salesPrice; - private final BigDecimal quantity; + /** + * Constructor for SaleCaculator + * + * @param share - The sold share + */ + public SaleCalculator(Share share) { + this.purchasePrice = share.getPurchasePrice(); + this.salesPrice = share.getStock().getSalesPrice(); + this.quantity = share.getQuantity(); + } - /** - * Constructor for SaleCaculator - * - * @param share - The sold share - */ - public SaleCalculator(Share share){ - this.purchasePrice = share.getPurchasePrice(); - this.salesPrice = BigDecimal.ZERO; //TODO: Implement here - this.quantity = share.getQuantity(); - } + /** + * Method that calculates gross value + * + * @return - BigDecimal of sale price x share quantity + */ + @Override + public BigDecimal calculateGross() { + return salesPrice.multiply(quantity); + } - /** - * Method that calculates gross value - * - * @return - BigDecimal of sale price x share quantity - */ - @Override - public BigDecimal calculateGross() { - return salesPrice.multiply(quantity); - } + /** + * Method that calculates the commission fee + * + * @return - BigDecimal of gross value x 1%. + */ + @Override + public BigDecimal calculateCommision() { + BigDecimal fee = new BigDecimal("0.01"); // Corresponding to 1% + return calculateGross().multiply(fee); + } - /** - * Method that calculates the commission fee - * - * @return - BigDecimal of gross value x 1%. - */ - @Override - public BigDecimal calculateCommision() { - BigDecimal fee = BigDecimal.valueOf(0.01); // Corresponding to 1% - return calculateGross().multiply(fee); - } + /** + * Method that calculates the tax + * + * @return - BigDecimal of 30% of profit (gross - purchase price x quantity) + */ + @Override + public BigDecimal calculateTax() { + BigDecimal taxPercentage = new BigDecimal("0.3"); // Corresponding to 30% + BigDecimal profit = calculateGross().subtract(purchasePrice.multiply(quantity)) + .subtract(calculateCommision()); // (gross - tax - buy costs) - /** - * Method that calculates the tax - * - * @return - BigDecimal of 30% of profit (gross - purchase price x quantity) - */ - @Override - public BigDecimal calculateTax() { - BigDecimal taxPercentage = BigDecimal.valueOf(0.3); // Corresponding to 30% - BigDecimal profit = calculateGross().subtract(purchasePrice.multiply(quantity)); // gross - purchase price x quanitity - return profit.multiply(taxPercentage); + return profit.multiply(taxPercentage); - } + } - /** - * Method that calculates the total profit of a sold share - * - * @return - BigDecimal of gross value - commision fee - tax - */ - @Override - public BigDecimal calculateTotal() { - return calculateGross().subtract(calculateCommision()).subtract(calculateTax()); - } + /** + * Method that calculates the total profit of a sold share + * + * @return - BigDecimal of gross value - commision fee - tax + */ + @Override + public BigDecimal calculateTotal() { + return calculateGross().subtract(calculateCommision()).subtract(calculateTax()); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java b/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java index a1f9265..fdfa9b1 100644 --- a/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java @@ -5,17 +5,23 @@ /** * TransactionCalculator interface * - *Contains methods that are used upon doing a transaction - * corresponding to a share and a week.
+ *+ * Contains methods that are used upon doing a transaction + * corresponding to a share and a week. + *
+ * * @see PurchaseCalculator * @see SaleCalculator * */ public interface TransactionCalculator { - BigDecimal calculateGross(); // Gross - Norsk: verdi før avgifter - BigDecimal calculateCommision(); // Commision - Norsk: (Kurtasje) En avgift som betales til megleren. - BigDecimal calculateTax(); // Tax - Norsk: Skatt, en avgift som betales til staten. - BigDecimal calculateTotal(); // Totalverdi etter avgifter + BigDecimal calculateGross(); // Gross - Norsk: verdi før avgifter + + BigDecimal calculateCommision(); // Commision - Norsk: (Kurtasje) En avgift som betales til megleren. + + BigDecimal calculateTax(); // Tax - Norsk: Skatt, en avgift som betales til staten. + + BigDecimal calculateTotal(); // Totalverdi etter avgifter } diff --git a/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java index 535259b..5b4a147 100644 --- a/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java @@ -6,60 +6,65 @@ /** * Portfolio class * - *Class that functions as a wallet for a player - * storing all results of transactions made.
+ *+ * Class that functions as a wallet for a player + * storing all results of transactions made. + *
* */ public class Portfolio { - private ArrayListClass that describes the ownership of a specific stock.
+ *+ * Class that describes the ownership of a specific stock. + *
* */ public class Share { - private final Stock stock; - private BigDecimal quantity; - private BigDecimal purchasePrice; - - /** - * Constructor for a Share. - * - * @param stock - The stock this share corresponds to. - * @see Stock - * @param quantity - The total of bought shares of the stock. - * @param purchasePrice - The current price when the stock was bought. - */ - public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) { - this.stock = stock; - this.quantity = quantity; - this.purchasePrice = purchasePrice; - } - - /** - * Getters - * - * @return - Their corresponding variables. - */ - - public Stock getStock() { - return stock; - } - - public BigDecimal getQuantity() { - return quantity; - } - - public BigDecimal getPurchasePrice() { - return purchasePrice; - } + private final Stock stock; + private BigDecimal quantity; + private BigDecimal purchasePrice; + + /** + * Constructor for a Share. + * + * @param stock - The stock this share corresponds to. + * @see Stock + * @param quantity - The total of bought shares of the stock. + * @param purchasePrice - The current price when the stock was bought. + */ + public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) { + this.stock = stock; + this.quantity = quantity; + this.purchasePrice = purchasePrice; + } + + /** + * Getters + * + * @return - Their corresponding variables. + */ + + public Stock getStock() { + return stock; + } + + public BigDecimal getQuantity() { + return quantity; + } + + public BigDecimal getPurchasePrice() { + return purchasePrice; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java b/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java index 11ca9f8..40f071e 100644 --- a/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java @@ -1,68 +1,72 @@ package edu.ntnu.idi.idatt.marked; - import java.math.BigDecimal; +import java.util.ArrayList; import java.util.List; /** * Stock class * - *Class that describes an object of a unique stock.
+ *+ * Class that describes an object of a unique stock. + *
* */ public class Stock { - private final String symbol; - private final String company; - private final List+ * Manages player state at purhcasing stocks. + * Collected as detail in TransactionArchive. + *
+ * + * @see Transaction + * @see TransactionArchive + */ +public class Purchase extends Transaction { + /** + * Constructor for a Purchase + * + * @param share - The purchased share. + * @param week - The current week + */ + public Purchase(Share share, int week) { + super(share, week, new PurchaseCalculator(share)); + } - /** - * @see Transaction - * @param player - The player that does the transaction - */ - @Override - public void commit(Player player) { - player.withdrawMoney(this.getCalculator().calculateTotal()); - player.getPortfolio().addShare(this.getShare()); - player.getTransactionArchive().add(this); + /** + * @see Transaction + * @param player - The player that does the transaction + */ + @Override + public void commit(Player player) { + player.withdrawMoney(this.getCalculator().calculateTotal()); + player.getPortfolio().addShare(this.getShare()); + player.getTransactionArchive().add(this); - this.commited = true; - } + this.commited = true; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/transaction/Sale.java b/src/main/java/edu/ntnu/idi/idatt/transaction/Sale.java index d3dfee2..e0b6292 100644 --- a/src/main/java/edu/ntnu/idi/idatt/transaction/Sale.java +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Sale.java @@ -2,30 +2,40 @@ import edu.ntnu.idi.idatt.Player; import edu.ntnu.idi.idatt.calculator.SaleCalculator; -import edu.ntnu.idi.idatt.calculator.TransactionCalculator; import edu.ntnu.idi.idatt.marked.Share; -public class Sale extends Transaction{ - /** - * Constructor for a Sale - * - * @param share - The sold share - * @param week - The current week - */ - public Sale(Share share, int week) { - super(share, week, new SaleCalculator(share)); - } +/** + * Sale class + * + *+ * Manages player state at selling stocks. + * Collected as detail in TransactionArchive. + *
+ * + * @see Transaction + * @see TransactionArchive + */ +public class Sale extends Transaction { + /** + * Constructor for a Sale + * + * @param share - The sold share + * @param week - The current week + */ + public Sale(Share share, int week) { + super(share, week, new SaleCalculator(share)); + } - /** - * @see Transaction - * @param player - The player that does the transaction - */ - @Override - public void commit(Player player) { - player.addMoney(this.getCalculator().calculateTotal()); - player.getPortfolio().removeShare(this.getShare()); - player.getTransactionArchive().add(this); + /** + * @see Transaction + * @param player - The player that does the transaction + */ + @Override + public void commit(Player player) { + player.addMoney(this.getCalculator().calculateTotal()); + player.getPortfolio().removeShare(this.getShare()); + player.getTransactionArchive().add(this); - this.commited = true; - } + this.commited = true; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/transaction/Transaction.java b/src/main/java/edu/ntnu/idi/idatt/transaction/Transaction.java index 1e955fe..f28629e 100644 --- a/src/main/java/edu/ntnu/idi/idatt/transaction/Transaction.java +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Transaction.java @@ -7,60 +7,65 @@ /** * Transaction class * - *An abstract class that handles a transaction + *
+ * An abstract class that handles a transaction * based on week and share. Utilizes the calculators - * to perform calculations.
+ * to perform calculations. + * + * * @see TransactionCalculator */ public abstract class Transaction { - private final Share share; - private int week; - private TransactionCalculator calculator; - protected boolean commited; + private final Share share; + private int week; + private TransactionCalculator calculator; + protected boolean commited; - /** - * Constructor for a Transaction - * - * @param share - The share the transaction is about. - * @param week - The current week - * @param calculator - Transaction (Purchasae/Sale) - */ - Transaction(Share share, int week, TransactionCalculator calculator) { - this.share = share; - this.week = week; - this.calculator = calculator; - } + /** + * Constructor for a Transaction + * + * @param share - The share the transaction is about. + * @param week - The current week + * @param calculator - Transaction (Purchasae/Sale) + */ + Transaction(Share share, int week, TransactionCalculator calculator) { + this.share = share; + this.week = week; + this.calculator = calculator; + } - /** - * - * Getters - * @return - their corresponding variable - * - */ + /** + * + * Getters + * + * @return - their corresponding variable + * + */ - public Share getShare() { - return share; - } + public Share getShare() { + return share; + } - public int getWeek() { - return week; - } + public int getWeek() { + return week; + } - public TransactionCalculator getCalculator() { - return calculator; - } + public TransactionCalculator getCalculator() { + return calculator; + } - public boolean isCommited() { - return commited; - } + public boolean isCommited() { + return commited; + } - /** - * Abstract commit method to set the isCommited flag that symbolizes a unique transaction. - * - * @param player - The player that does the transaction - */ - abstract public void commit(Player player); + /** + * Abstract commit method to set the isCommited flag that symbolizes a unique + * transaction. + * + * @param player - The player that does the transaction + */ + abstract public void commit(Player player); } diff --git a/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java b/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java index 3fbeaa4..0567591 100644 --- a/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java @@ -6,73 +6,75 @@ /** * TransactionArchive class * - *Manages and handles transaction logic
+ *+ * Manages and handles transaction logic + *
* */ public class TransactionArchive { - private final ArrayList+ * Includes hasStock, getStock and findStocks. + *
+ * + */ + + @Test + void PTFindStock() { + + assertTrue(exchange.hasStock("AAPL")); + assertEquals(stocks.get(0) /* AAPL Stock */, exchange.getStock("AAPL")); + + // FindStocks for letter "n" should be - AAPL, AMD (both symbols and names!) + List+ * Since this method includes basically everything this project + * contains we will just check essential things that everything + * worked as it was supposted. + *
+ */ + @Test + void PTBuy() { + + Transaction transaction = exchange.buy("AAPL", new BigDecimal("1"), player); + assertEquals(transaction, player.getTransactionArchive().getTransactions(1).getLast()); + assertEquals(-1, player.getMoney().compareTo(new BigDecimal("500"))); // Less than 500 + assertEquals(1, player.getPortfolio().getShares().size()); + assertEquals("AAPL", player.getPortfolio().getShares().get(0).getStock().getSymbol()); + + } + + /** + * + *+ * Since this method includes basically everything this project + * contains we will just check essential things that everything + * worked as it was supposted. + *
+ */ + @Test + void PTSell() { + // Player has to have a share to sell it. + exchange.buy("AAPL", new BigDecimal("1"), player); + stocks.get(0).addNewSalesPrice(new BigDecimal("40")); // Simulate increase of AAPL stock price + + Transaction transaction = exchange.sell(player.getPortfolio().getShares().getLast(), player); + assertEquals(transaction, player.getTransactionArchive().getTransactions(1).getLast()); + assertEquals(1, player.getMoney().compareTo(new BigDecimal("500"))); // Less than 500 + assertEquals(0, player.getPortfolio().getShares().size()); + + } + + @Test + void PTAdvance() { + List+ * Includes hasStock, getStock and findStocks. + *
+ * + */ + @Test + void NTFindStock() { + + assertFalse(exchange.hasStock("Test")); + assertThrows(IllegalArgumentException.class, () -> exchange.getStock("thiswillnotwork")); + assertEquals(List.of() /* Empty list */, exchange.findStocks("X")); + + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt/PlayerTest.java new file mode 100644 index 0000000..cd6df5d --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/PlayerTest.java @@ -0,0 +1,44 @@ +package edu.ntnu.idi.idatt; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.math.BigDecimal; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PlayerTest { + + private Player player; + + @BeforeEach + public void PT_setup() { + + player = new Player("TestPlayer", new BigDecimal("500")); + + } + + @Test + void PTConstructor() { + assertEquals("TestPlayer", player.getName()); + assertEquals(new BigDecimal("500"), player.getMoney()); + assertNotNull(player.getPortfolio()); + assertNotNull(player.getTransactionArchive()); + } + + @Test + void PTaddMoney() { + + player.addMoney(new BigDecimal("200")); + assertEquals(new BigDecimal("700"), player.getMoney()); + + } + + @Test + void PTwithdrawMoney() { + player.withdrawMoney(new BigDecimal("200")); + assertEquals(new BigDecimal("300"), player.getMoney()); + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculatorTest.java b/src/test/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculatorTest.java new file mode 100644 index 0000000..5d306c0 --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculatorTest.java @@ -0,0 +1,51 @@ +package edu.ntnu.idi.idatt.calculator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +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; + +class PurchaseCalculatorTest { + + private Stock stock; + private Share share; + private PurchaseCalculator purchaseCalculator; + + @BeforeEach + public void PT_setup() { + // stock instance and parameters + List+ * Since this class is meant as an object + * that contains transaction details, we will look away from + * testing commit() method. + * + * commit() contains only method calls upon player, except changing the commited + * boolean field. + * We will only be vaguely testing it for this field. + * + * @see Exchange + * @see Purchase + *
+ */ +class PurchaseTest { + + private Stock stock; + private Share share; + private Purchase purchase; + + @BeforeEach + public void PT_setup() { + // stock instance and parameters + List+ * Since this class is meant as an object + * that contains transaction details, we will look away from + * testing commit() method. + * + * commit() contains only method calls upon player, except changing the commited + * boolean field. + * We will only be vaguely testing it for this field. + * + * @see Exchange + * @see Sale + * + *
+ */ +class SaleTest { + + private Stock stock; + private Share share; + private Player player; + private Sale sale; + + @BeforeEach + public void PT_setup() { + // stock instance and parameters + List+ * Since we know that the basics of the TransactionArchive works with the + * tests over, we will now move to using the TransactionArchive field made + * from @BeforeEach. + *
+ * + */ + + @Test + void PTgetTransactions() { + + assertEquals(2, transactionArchive.getTransactions(1).size()); // First week all transactions + assertEquals(2, transactionArchive.getTransactions(2).size()); + assertEquals(1, transactionArchive.getPurchases(1).size()); + assertEquals(1, transactionArchive.getSales(1).size()); + assertEquals(1, transactionArchive.getPurchases(2).size()); + assertEquals(1, transactionArchive.getSales(2).size()); + + } + +}