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 @@ maven-compiler-plugin 3.14.1 - - - org.apache.maven.plugins - maven-surefire-plugin - 3.5.4 - - + org.apache.maven.plugins maven-javadoc-plugin @@ -39,12 +33,27 @@ 25.0.1 + + org.junit.jupiter + junit-jupiter + 6.0.1 + test + + + - + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.4 + + org.openjfx @@ -78,13 +87,7 @@ - - org.junit.jupiter - junit-jupiter - 6.0.1 - - - + - \ No newline at end of file + diff --git a/src/main/java/edu/ntnu/idi/idatt/Exchange.java b/src/main/java/edu/ntnu/idi/idatt/Exchange.java index a159c4a..2153bd9 100644 --- a/src/main/java/edu/ntnu/idi/idatt/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt/Exchange.java @@ -9,73 +9,153 @@ import java.math.BigDecimal; import java.util.*; +/** + * Exchange class + * + *

+ * 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 stockMap = new HashMap<>(); - private Random random = new Random(); - - public Exchange(String name, List stocks) { - this.name = name; - this.week = 1; - - for(Stock stock : stocks){ - stockMap.put(stock.getSymbol(), stock); - } - - } - - public String getName() { - return name; - } - - public int getWeek() { - return week; - } - - public boolean hasStock(String symbol) { - return stockMap.containsKey(symbol); + private final String name; + private int week; + private HashMap stockMap = new HashMap<>(); + private Random random = new Random(); + + /** + * Constructor for Exchange class + * + * @param name - Name of the current stock Exchange + * @param stocks - List of aviable stocks for this exchange. + */ + public Exchange(String name, List stocks) { + this.name = name; + this.week = 1; + + for (Stock stock : stocks) { + stockMap.put(stock.getSymbol(), stock); } - public Stock getStock(String symbol) { - if(this.hasStock(symbol)){ - stockMap.get(symbol); - } - return null; //TODO: Exception + } + + /** + * + * Getters + * + * @return - their corresponding variables. + */ + + public String getName() { + return name; + } + + public int getWeek() { + return week; + } + + /** + * Method for checking if a specific stock exists in the exchange. + * + * @param symbol - String symbol of a specific stock. + * @return - true/false if the exchange has the specific stock. + */ + public boolean hasStock(String symbol) { + return stockMap.containsKey(symbol); + } + + /** + * Getter for a specific stock. + * + * @param symbol - String symbol of a specific stock. + * @return - The found stock if existant. + * @throws IllegalArgumentException if invalid symbol given. + */ + public Stock getStock(String symbol) { + if (this.hasStock(symbol)) { + return stockMap.get(symbol); } - - public List findStocks(String searchTerm) { - ArrayList stocksFound = new ArrayList<>(); - - for(Stock stock : stockMap.values()) { - if(stock.getCompany().contains(searchTerm) || stock.getSymbol().contains(searchTerm)){ - stocksFound.add(stock); - } - } - return stocksFound; + throw new IllegalArgumentException("This stock doesn't exist in [" + name + "] exchange."); + } + + /** + * Method for searching after stocks. + * + * @param searchTerm - String or character sequence of corporation name / + * corresponding symbol. + * @return - List of found stocks. + */ + public List findStocks(String searchTerm) { + ArrayList stocksFound = new ArrayList<>(); + + for (Stock stock : stockMap.values()) { + if (stock.getCompany().contains(searchTerm) || stock.getSymbol().contains(searchTerm)) { + stocksFound.add(stock); + } } - - 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(); + return stocksFound; + } + + /** + * Method to allow a player to buy a stock. + * + *

+ * 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 ArrayList shares; + private ArrayList shares = new ArrayList<>(); - /** - * Setter for ArrayList shares. - * - * @param share - The bought share - * @return - was the list modified? - */ - public boolean addShare(Share share){ - return shares.add(share); - } + /** + * Setter for ArrayList shares. + * + * @param share - The bought share + * @return - was the list modified? + */ + public boolean addShare(Share share) { + return shares.add(share); + } - /** - * Setter for ArrayList shares. - * - * @param share - The sold share - * @return - was the list modified? - */ - public boolean removeShare(Share share){ - return shares.add(share); + /** + * Setter for ArrayList shares. + * + * @param share - The sold share + * @return - was the list modified? + */ + public boolean removeShare(Share share) { + if (!contains(share)) { + throw new IllegalArgumentException("Portfolio doesn't contain this share."); } + return shares.remove(share); + } - /** - * Getter for ArrayList shares. - * - * @return - List of all shares owned. - */ - public List getShares() { - return shares; - } + /** + * Getter for ArrayList shares. + * + * @return - List of all shares owned. + */ + public List getShares() { + return shares; + } - /** - * Getter for ArrayList shares. - * - * @param symbol - The symbol of the stock corresponding to the share. - * @return - List of shares owned that corresponds with a company symbol. - */ - public List getShares(String symbol) { - return shares.stream().filter(s -> s.getStock().getSymbol().equals(symbol)).toList(); - } + /** + * Getter for ArrayList shares. + * + * @param symbol - The symbol of the stock corresponding to the share. + * @return - List of shares owned that corresponds with a company symbol. + */ + public List getShares(String symbol) { + return shares.stream().filter(s -> s.getStock().getSymbol().equals(symbol)).toList(); + } - /** - * Method for checking if the portfolio contains a specific share - * - * @return - If the share was found. - */ - public boolean contains(Share share) { - return shares.contains(share); - } + /** + * Method for checking if the portfolio contains a specific share + * + * @return - If the share was found. + */ + public boolean contains(Share share) { + return shares.contains(share); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/marked/Share.java b/src/main/java/edu/ntnu/idi/idatt/marked/Share.java index 1f9c14b..0cc7432 100644 --- a/src/main/java/edu/ntnu/idi/idatt/marked/Share.java +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Share.java @@ -5,45 +5,47 @@ /** * Share class * - *

Class 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 prices; + private final String symbol; + private final String company; + private final ArrayList prices = new ArrayList<>(); - /** - * Constructor for a Stock. - * - * @param symbol - String that indicates the symbol of a stock, ex. "APPL" as a short form of company. - * @param company - String, company name, ex. "Apple Inc." - * @param prices - An array of BigInteger that indicates the price corresponding with time. - */ - public Stock(String symbol, String company, List prices) { - this.symbol = symbol; - this.company = company; - this.prices = prices; - } + /** + * Constructor for a Stock. + * + * @param symbol - String that indicates the symbol of a stock, ex. "APPL" as a + * short form of company. + * @param company - String, company name, ex. "Apple Inc." + * @param prices - An array of BigInteger that indicates the price + * corresponding with time. + */ + public Stock(String symbol, String company, List prices) { + this.symbol = symbol; + this.company = company; + this.prices.addAll(prices); + } - /** - * Getters - * - * @return - Their corresponding variables. - */ + /** + * Getters + * + * @return - Their corresponding variables. + */ - public String getSymbol() { - return symbol; - } + public String getSymbol() { + return symbol; + } - public String getCompany() { - return company; - } + public String getCompany() { + return company; + } - public List getPrices() { - return prices; - } + public List getPrices() { + return prices; + } - /** - * Getter for sale price - * - * @return - BigDecimal with current (newest in array) stock price. - */ - public BigDecimal getSalesPrice() { - return prices.getLast(); - } + /** + * Getter for sale price + * + * @return - BigDecimal with current (newest in array) stock price. + */ + public BigDecimal getSalesPrice() { + return prices.getLast(); + } - /** - * Method that adds new price to the price array. - * - * @param price - BigDecimal, new price. - */ - public void addNewSalesPrice(BigDecimal price) { - prices.add(price); - } + /** + * Method that adds new price to the price array. + * + * @param price - BigDecimal, new price. + */ + public void addNewSalesPrice(BigDecimal price) { + prices.add(price); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java b/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java index 157c193..ac9152f 100644 --- a/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java @@ -2,31 +2,41 @@ import edu.ntnu.idi.idatt.Player; import edu.ntnu.idi.idatt.calculator.PurchaseCalculator; -import edu.ntnu.idi.idatt.calculator.TransactionCalculator; import edu.ntnu.idi.idatt.marked.Share; -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)); - } +/** + * Purchase class + * + *

+ * 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 transactions = new ArrayList<>(); + private final ArrayList transactions = new ArrayList<>(); - /** - * Method for adding a new transaction to ArrayList transactions. - * - * @param transaction - The transaction instance - * @return - was the list modified? - */ - public boolean add(Transaction transaction){ - return transactions.add(transaction); - } + /** + * Method for adding a new transaction to ArrayList transactions. + * + * @param transaction - The transaction instance + * @return - was the list modified? + */ + public boolean add(Transaction transaction) { + return transactions.add(transaction); + } - /** - * Method for checking if there has been any transactions previously. - * - * @return - was the transactions ArrayList empty? - */ - public boolean isEmpty(){ - return transactions.isEmpty(); - } + /** + * Method for checking if there has been any transactions previously. + * + * @return - was the transactions ArrayList empty? + */ + public boolean isEmpty() { + return transactions.isEmpty(); + } - /** - * Getter for transactions done - * - * @param week - Transaction interval - * @return - List of Transaction done in a specified week. - */ - public List getTransactions(int week) { - return transactions.stream().filter(transaction -> transaction.getWeek() == week).toList(); - } + /** + * Getter for transactions done + * + * @param week - Transaction interval + * @return - List of Transaction done in a specified week. + */ + public List getTransactions(int week) { + return transactions.stream().filter(transaction -> transaction.getWeek() == week).toList(); + } - /** - * Getter for purchases done - * - * @param week - Purchase interval - * @return - List of Purchase done in a specified week. - */ - public List getPurchases(int week) { - return transactions.stream().filter(t -> t instanceof Purchase) - .map(t -> (Purchase) t) - .toList(); - } + /** + * Getter for purchases done + * + * @param week - Purchase interval + * @return - List of Purchase done in a specified week. + */ + public List getPurchases(int week) { + return getTransactions(week).stream().filter(t -> t instanceof Purchase) + .map(t -> (Purchase) t) + .toList(); + } - /** - * Getter for sales done - * - * @param week - Sale interval - * @return - List of Sale done in a specified week. - */ - public List getSales(int week) { - return transactions.stream().filter(t -> t instanceof Sale) - .map(t -> (Sale) t) - .toList(); - } + /** + * Getter for sales done + * + * @param week - Sale interval + * @return - List of Sale done in a specified week. + */ + public List getSales(int week) { + return getTransactions(week).stream().filter(t -> t instanceof Sale) + .map(t -> (Sale) t) + .toList(); + } - /** - * Part 2 - * - * @return - */ - public int countDistinctWeeks(){ //TODO: HERE - return -1; - } + /** + * Part 2 + * + * @return + */ + public int countDistinctWeeks() { // TODO: HERE + return -1; + } } diff --git a/src/test/java/edu/ntnu/idi/idatt/ExchangeTest.java b/src/test/java/edu/ntnu/idi/idatt/ExchangeTest.java new file mode 100644 index 0000000..743f3be --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/ExchangeTest.java @@ -0,0 +1,140 @@ +package edu.ntnu.idi.idatt; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import edu.ntnu.idi.idatt.marked.Stock; +import edu.ntnu.idi.idatt.transaction.Transaction; + +class ExchangeTest { + + private Exchange exchange; + private List stocks; + private Player player; + + @BeforeEach + public void PT_setup() { + + // Initialize exchange with proper objects + Stock AAPL = new Stock("AAPL", "Apple Inc.", List.of(new BigDecimal("32"))); + Stock NVDA = new Stock("NVDA", "NVIDIA", List.of(new BigDecimal("182.81"))); + Stock TSLA = new Stock("TSLA", "Tesla", List.of(new BigDecimal("417.44"))); + Stock AMD = new Stock("AMD", "Advanced Micro Devices", List.of(new BigDecimal("207.32"))); + + stocks = List.of(AAPL, NVDA, TSLA, AMD); + + exchange = new Exchange("TestExchange", stocks); + player = new Player("TestPlayer", new BigDecimal("500")); + } + + @Test + void PTConstructor() { + assertEquals("TestExchange", exchange.getName()); + assertEquals(1, exchange.getWeek()); + } + + /** + * Positive tests for stock-related methods. + * + *

+ * 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 expected = List.of(stocks.get(0), stocks.get(3)); + // + assertEquals(expected, exchange.findStocks("n")); + + } + + /** + * + *

+ * 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 stockPricesBefore = new ArrayList<>(); + for (Stock stock : stocks) { + stockPricesBefore.add(stocks.indexOf(stock), stock.getSalesPrice()); + } + + exchange.advance(); + + for (Stock stock : stocks) { + assertTrue(stockPricesBefore.get(stocks.indexOf(stock)).compareTo(stock.getSalesPrice()) != 0); + // If compareTo returns 0 then its equal. + } + + } + + /** + * Negative tests for stock-related methods. + * + *

+ * 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 prices = List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")); + stock = new Stock("AAPL", "Apple Inc.", prices); + + // Share instance parameters + BigDecimal quantity = new BigDecimal("3.3"); + share = new Share(stock, quantity, stock.getSalesPrice()); + + // PurchaseCalculator instance + purchaseCalculator = new PurchaseCalculator(share); + } + + /** + * + * Since calculateTotal() method is composed of all the other methods in this + * class, + * it will singlehandedly validate the methods of this class. + */ + @Test + void PT_calculationTotal() { + + BigDecimal expected = new BigDecimal("40.0") + .multiply(new BigDecimal("3.3")) + .multiply(new BigDecimal("1.005")); + assertEquals(expected, purchaseCalculator.calculateTotal()); + + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/calculator/SaleCalculatorTest.java b/src/test/java/edu/ntnu/idi/idatt/calculator/SaleCalculatorTest.java new file mode 100644 index 0000000..3672172 --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/calculator/SaleCalculatorTest.java @@ -0,0 +1,77 @@ +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 SaleCalculatorTest { + + private Stock stock; + private Share share; + private SaleCalculator saleCalculator; + + @BeforeEach + public void PT_setup() { + // stock instance and parameters + List prices = List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")); + stock = new Stock("AAPL", "Apple Inc.", prices); + + // Share instance parameters + BigDecimal quantity = new BigDecimal("3.3"); + share = new Share(stock, quantity, stock.getSalesPrice()); + + // PurchaseCalculator instance + saleCalculator = new SaleCalculator(share); + } + + /** + * + * Since calculateTotal() method is composed of all the other methods in this + * class, + * it will singlehandedly validate the methods of this class. + * + * @see SaleCalculator + */ + @Test + void PT_calculationTotal() { + + // For imitation, let's add a new weeks stock price. + stock.addNewSalesPrice(new BigDecimal("47.8")); + saleCalculator = new SaleCalculator(share); + /** + * Update the SaleCalculator sale price variable. Due to the formulation of the + * task, + * we have to contain the purchasePrice, salesPrice and quantity as fields + * instead of + * accessing them through the share itself (Share holds a final reference to a + * Stock) + * which suggests contineous update of the field. + * + * Since this class is instanced in the constructor of Sale which is again + * itself instanced for each player event, this method is contineously updated + * in a way compared the line over. + * + * @see Exchange + */ + + BigDecimal gross = share.getStock().getSalesPrice().multiply(share.getQuantity()); + BigDecimal commision = gross.multiply(new BigDecimal("0.01")); + BigDecimal tax = gross.subtract(commision).subtract( + share.getPurchasePrice() + .multiply(share.getQuantity())) + .multiply(new BigDecimal("0.3")); + + BigDecimal expected = gross.subtract(commision).subtract(tax); + assertEquals(expected, saleCalculator.calculateTotal()); + + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/marked/PortfolioTest.java b/src/test/java/edu/ntnu/idi/idatt/marked/PortfolioTest.java new file mode 100644 index 0000000..f0d0913 --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/marked/PortfolioTest.java @@ -0,0 +1,81 @@ +package edu.ntnu.idi.idatt.marked; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class PortfolioTest { + + private Stock stock; + private Share share; + private Portfolio portfolio; + + @BeforeEach + public void PT_setup() { + // stock instance and parameters + List prices = List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")); + stock = new Stock("AAPL", "Apple Inc.", prices); + + // Share instance parameters + BigDecimal quantity = new BigDecimal("3.3"); + share = new Share(stock, quantity, stock.getSalesPrice()); + + // Portfolio instance + portfolio = new Portfolio(); + } + + @Test + void PTaddShare() { + + assertTrue(portfolio.addShare(share)); + assertEquals(List.of(share), portfolio.getShares()); + + } + + void addDefaultShare() { // Since PTaddShare test works, we will use this to initialize + // the rest of the tests under. + portfolio.addShare(share); + } + + @Test + void PTgetSharesBySymbol() { + addDefaultShare(); + + assertEquals(List.of(), portfolio.getShares("SYMBL")); + assertEquals(List.of(share), portfolio.getShares("AAPL")); + + } + + @Test + void PTremoveShare() { + addDefaultShare(); + + assertTrue(portfolio.removeShare(share)); + assertEquals(List.of(), portfolio.getShares()); + + } + + @Test + void PTContains() { + assertFalse(portfolio.contains(share)); + + portfolio.addShare(share); + + assertTrue(portfolio.contains(share)); + } + + @Test + void NTremoveShare() { + Share exception = new Share(stock, new BigDecimal("2.3"), stock.getSalesPrice()); + assertThrows(IllegalArgumentException.class, () -> portfolio.removeShare(exception)); + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/marked/ShareTest.java b/src/test/java/edu/ntnu/idi/idatt/marked/ShareTest.java new file mode 100644 index 0000000..ac69822 --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/marked/ShareTest.java @@ -0,0 +1,37 @@ +package edu.ntnu.idi.idatt.marked; + +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; + +public class ShareTest { + + private Share share; + private Stock stock; + + @BeforeEach + public void PT_setup() { + List prices = List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")); + stock = new Stock("AAPL", "Apple Inc.", prices); + + // Share parameters + BigDecimal quantity = new BigDecimal("3.3"); + + share = new Share(stock, quantity, stock.getSalesPrice()); + } + + @Test + void constructorTest() { + + assertEquals(stock, share.getStock()); + assertEquals(new BigDecimal("3.3"), share.getQuantity()); + assertEquals(stock.getSalesPrice(), share.getPurchasePrice()); + + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/marked/StockTest.java b/src/test/java/edu/ntnu/idi/idatt/marked/StockTest.java new file mode 100644 index 0000000..7d81482 --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/marked/StockTest.java @@ -0,0 +1,42 @@ +package edu.ntnu.idi.idatt.marked; + +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; + +public class StockTest { + + private Stock stock; + + @BeforeEach + public void PT_setup() { + List prices = List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")); + stock = new Stock("AAPL", "Apple Inc.", prices); + + } + + @Test + void constructorTest() { + assertEquals("AAPL", stock.getSymbol()); + assertEquals("Apple Inc.", stock.getCompany()); + + assertEquals(List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")), stock.getPrices()); + + assertEquals(new BigDecimal("40.0"), stock.getSalesPrice()); + } + + @Test + void addNewSalesPriceTest() { + BigDecimal value = new BigDecimal("15.6"); + stock.addNewSalesPrice(value); + + assertEquals(value, stock.getSalesPrice()); + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/transaction/PurchaseTest.java b/src/test/java/edu/ntnu/idi/idatt/transaction/PurchaseTest.java new file mode 100644 index 0000000..25f9c9e --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/transaction/PurchaseTest.java @@ -0,0 +1,73 @@ +package edu.ntnu.idi.idatt.transaction; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +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.Player; +import edu.ntnu.idi.idatt.marked.Share; +import edu.ntnu.idi.idatt.marked.Stock; + +/** + * Testing for Purchase class + * + *

+ * 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 prices = List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")); + stock = new Stock("AAPL", "Apple Inc.", prices); + + // Share instance and parameters + BigDecimal quantity = new BigDecimal("3.3"); + share = new Share(stock, quantity, stock.getSalesPrice()); + + // Transaction instance + purchase = new Purchase(share, 1); + } + + /** + * + * Since calculateTotal() method is composed of all the other methods in this + * class, + * it will singlehandedly validate the methods of this class. + */ + @Test + void PTconstructorTest() { + + assertEquals(share, purchase.getShare()); + assertEquals(1, purchase.getWeek()); + assertEquals(new BigDecimal("132.00"), purchase.getCalculator().calculateGross()); // Because of none stored + // *Calculator, we test with a existing method we can measure the values of. + assertFalse(purchase.isCommited()); + purchase.commit(new Player("ExamplePlayer", new BigDecimal("2500"))); + assertTrue(purchase.isCommited()); + + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/transaction/SaleTest.java b/src/test/java/edu/ntnu/idi/idatt/transaction/SaleTest.java new file mode 100644 index 0000000..2f5523b --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/transaction/SaleTest.java @@ -0,0 +1,81 @@ +package edu.ntnu.idi.idatt.transaction; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +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.Exchange; +import edu.ntnu.idi.idatt.Player; +import edu.ntnu.idi.idatt.marked.Share; +import edu.ntnu.idi.idatt.marked.Stock; + +/** + * Testing for Sale class + * + *

+ * 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 prices = List.of(new BigDecimal("46.2"), + new BigDecimal("40.0")); + stock = new Stock("AAPL", "Apple Inc.", prices); + + // Share instance and parameters + BigDecimal quantity = new BigDecimal("3.3"); + share = new Share(stock, quantity, stock.getSalesPrice()); + + // Player instance and parameters. + // We need a portfolio that has the share already to not throw an exception. + player = new Player("ExamplePlayer", new BigDecimal("500")); + player.getPortfolio().addShare(share); + + // Transaction instance + sale = new Sale(share, 1); + } + + /** + * + * Since calculateTotal() method is composed of all the other methods in this + * class, + * it will singlehandedly validate the methods of this class. + */ + @Test + void PTconstructorTest() { + + assertEquals(share, sale.getShare()); + assertEquals(1, sale.getWeek()); + assertEquals(new BigDecimal("132.00"), sale.getCalculator().calculateGross()); // Because of none stored + // *Calculator, we test with a existing method we can measure the values of. + assertFalse(sale.isCommited()); + sale.commit(player); + assertTrue(sale.isCommited()); + + } + +} diff --git a/src/test/java/edu/ntnu/idi/idatt/transaction/TransactionArchiveTest.java b/src/test/java/edu/ntnu/idi/idatt/transaction/TransactionArchiveTest.java new file mode 100644 index 0000000..482a45c --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt/transaction/TransactionArchiveTest.java @@ -0,0 +1,90 @@ +package edu.ntnu.idi.idatt.transaction; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +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 TransactionArchiveTest { + + List transactions; + TransactionArchive transactionArchive; + + @BeforeEach + public void getDefaultValues() { + + Stock AAPL = new Stock("AAPL", "Apple Inc.", List.of(new BigDecimal("32"))); + Stock NVDA = new Stock("NVDA", "NVIDIA", List.of(new BigDecimal("182.81"))); + Stock TSLA = new Stock("TSLA", "Tesla", List.of(new BigDecimal("417.44"))); + Stock AMD = new Stock("AMD", "Advanced Micro Devices", List.of(new BigDecimal("207.32"))); + + Share AAPLShare = new Share(AAPL, new BigDecimal("1.0"), AAPL.getSalesPrice()); + Share NVDAShare = new Share(NVDA, new BigDecimal("1.0"), NVDA.getSalesPrice()); + Share TSLAShare = new Share(TSLA, new BigDecimal("1.0"), TSLA.getSalesPrice()); + Share AMDShare = new Share(AMD, new BigDecimal("1.0"), AMD.getSalesPrice()); + + Purchase purchase1 = new Purchase(AAPLShare, 1); + Purchase purchase2 = new Purchase(AMDShare, 2); + Sale sale1 = new Sale(TSLAShare, 1); + Sale sale2 = new Sale(NVDAShare, 2); + + transactions = List.of(purchase1, purchase2, sale1, sale2); + + transactionArchive = new TransactionArchive(); + transactions.forEach(transaction -> transactionArchive.add(transaction)); + + } + + @Test + void PTaddTransactions() { + TransactionArchive archive = new TransactionArchive(); + + assertTrue(archive.add(transactions.get(0))); + assertEquals(1, archive.getTransactions(1).size()); + assertTrue(archive.add(transactions.get(2))); // Add one more + assertEquals(2, archive.getTransactions(1).size()); + + } + + @Test + void PTisEmpty() { + + TransactionArchive archive = new TransactionArchive(); + + assertTrue(archive.isEmpty()); + archive.add(transactions.get(0)); + assertFalse(archive.isEmpty()); + + } + + /** + * + *

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