diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..71d6ce6 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,27 @@ +name: Java CI with Maven + +on: + push: + branches: [ "dev" ] + pull_request: + branches: [ "dev" ] + +jobs: + build: + runs-on: self-hosted + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 25 + uses: actions/setup-java@v4 + with: + java-version: '25' + distribution: 'temurin' + cache: 'maven' + + - name: Build with Maven + run: mvn -B compile --file pom.xml + + - name: Test with Maven + run: mvn -B test --file pom.xml + diff --git a/.idea/misc.xml b/.idea/misc.xml index cdf8ef1..4878ef2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file 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..bff6a52 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,19 +33,56 @@ 25.0.1 + + org.openjfx + javafx-fxml + 25.0.1 + + + + org.openjfx + javafx-web + 25.0.1 + + + + org.openjfx + javafx-swing + 25.0.1 + + + + org.openjfx + javafx-media + 25.0.1 + + + + org.junit.jupiter + junit-jupiter + 6.0.1 + test + - + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.4 + + org.openjfx javafx-maven-plugin 0.0.8 - edu.ntnu.idi.idatt.Main + edu.ntnu.idi.idatt.gui.javafx @@ -78,13 +109,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 new file mode 100644 index 0000000..2153bd9 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/Exchange.java @@ -0,0 +1,161 @@ +package edu.ntnu.idi.idatt; + +import edu.ntnu.idi.idatt.marked.Share; +import edu.ntnu.idi.idatt.marked.Stock; +import edu.ntnu.idi.idatt.transaction.Purchase; +import edu.ntnu.idi.idatt.transaction.Sale; +import edu.ntnu.idi.idatt.transaction.Transaction; + +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 { + + 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); + } + + } + + /** + * + * 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); + } + 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); + } + } + 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())); + } + } + +} 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 new file mode 100644 index 0000000..3a5e61d --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/Player.java @@ -0,0 +1,57 @@ +package edu.ntnu.idi.idatt; + +import edu.ntnu.idi.idatt.marked.Portfolio; +import edu.ntnu.idi.idatt.transaction.TransactionArchive; + +import java.math.BigDecimal; + +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); + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java b/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java new file mode 100644 index 0000000..fd7847f --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java @@ -0,0 +1,72 @@ +package edu.ntnu.idi.idatt.calculator; + +import edu.ntnu.idi.idatt.marked.Share; + +import java.math.BigDecimal; + +/** + * PurchaseCalculator class + * + *

+ * Calculates transaction price based on + * share bought. + *

+ * + */ +public class PurchaseCalculator implements TransactionCalculator { + + 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(); + } + + /** + * 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 = 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 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 new file mode 100644 index 0000000..6c65302 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/SaleCalculator.java @@ -0,0 +1,79 @@ +package edu.ntnu.idi.idatt.calculator; + +import edu.ntnu.idi.idatt.marked.Share; + +import java.math.BigDecimal; + +/** + * SaleCalculator class + * + *

+ * Calculates transaction profit based on + * share sold. + *

+ * + */ +public class SaleCalculator implements TransactionCalculator { + + 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(); + } + + /** + * 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 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) + + 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()); + } + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java b/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java new file mode 100644 index 0000000..fdfa9b1 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java @@ -0,0 +1,27 @@ +package edu.ntnu.idi.idatt.calculator; + +import java.math.BigDecimal; + +/** + * TransactionCalculator interface + * + *

+ * 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 + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/gui/javafx.java b/src/main/java/edu/ntnu/idi/idatt/gui/javafx.java new file mode 100644 index 0000000..2e29e2f --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/gui/javafx.java @@ -0,0 +1,44 @@ +package edu.ntnu.idi.idatt.gui; + +import javafx.application.Application; +import javafx.scene.Group; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.paint.Color; +import javafx.stage.Stage; + +import java.awt.*; +import java.io.IOException; + +public class javafx extends Application { + @Override + public void start (Stage stage) throws IOException { + Image stonk = new Image(getClass().getResource("/edu.ntnu.idi.idatt/stonks.png").toExternalForm()); + Button week = new Button("Week 5: Play"); + week.setPrefSize(150, 50); + week.setTranslateX(565); + Button logo = new Button(); + Button user = new Button(); + ImageView user_image = new ImageView(new Image(getClass().getResource("/edu.ntnu.idi.idatt/user.png").toExternalForm())); + ImageView logo_image = new ImageView(new Image(getClass().getResource("/edu.ntnu.idi.idatt/logo.png").toExternalForm())); + logo_image.setFitHeight(100); + logo_image.setFitWidth(100); + user_image.setFitHeight(100); + user_image.setFitWidth(100); + logo.setGraphic(logo_image); + user.setGraphic(user_image); + user.setTranslateX(1170); + ImageView stonks = new ImageView(stonk); + stonks.setX(440); + stonks.setY(220); + Group noe = new Group(stonks, week, logo, user); + Scene scene1 = new Scene(noe, 1280, 720, Color.DEEPSKYBLUE); + logo.setOnAction(e -> stage.setScene(new Scene(new Group(stonks,week,logo,user),1280, 720, Color.DEEPSKYBLUE))); + user.setOnAction(e -> stage.setScene(new Scene(new Group(logo,user),1280, 720, Color.BLACK))); + week.setOnAction(e -> stage.close()); + stage.setScene(scene1); + stage.show(); + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java new file mode 100644 index 0000000..5b4a147 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java @@ -0,0 +1,70 @@ +package edu.ntnu.idi.idatt.marked; + +import java.util.ArrayList; +import java.util.List; + +/** + * Portfolio class + * + *

+ * Class that functions as a wallet for a player + * storing all results of transactions made. + *

+ * + */ +public class Portfolio { + + 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 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. + * + * @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); + } + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/marked/Share.java b/src/main/java/edu/ntnu/idi/idatt/marked/Share.java new file mode 100644 index 0000000..0cc7432 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Share.java @@ -0,0 +1,51 @@ +package edu.ntnu.idi.idatt.marked; + +import java.math.BigDecimal; + +/** + * Share class + * + *

+ * 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; + } + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java b/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java new file mode 100644 index 0000000..40f071e --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java @@ -0,0 +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. + *

+ * + */ +public class Stock { + + 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.addAll(prices); + } + + /** + * Getters + * + * @return - Their corresponding variables. + */ + + public String getSymbol() { + return symbol; + } + + public String getCompany() { + return company; + } + + public List getPrices() { + return prices; + } + + /** + * 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); + } + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java b/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java new file mode 100644 index 0000000..ac9152f --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java @@ -0,0 +1,42 @@ +package edu.ntnu.idi.idatt.transaction; + +import edu.ntnu.idi.idatt.Player; +import edu.ntnu.idi.idatt.calculator.PurchaseCalculator; +import edu.ntnu.idi.idatt.marked.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); + + 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 new file mode 100644 index 0000000..e0b6292 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Sale.java @@ -0,0 +1,41 @@ +package edu.ntnu.idi.idatt.transaction; + +import edu.ntnu.idi.idatt.Player; +import edu.ntnu.idi.idatt.calculator.SaleCalculator; +import edu.ntnu.idi.idatt.marked.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); + + 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 new file mode 100644 index 0000000..f28629e --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Transaction.java @@ -0,0 +1,71 @@ +package edu.ntnu.idi.idatt.transaction; + +import edu.ntnu.idi.idatt.Player; +import edu.ntnu.idi.idatt.calculator.TransactionCalculator; +import edu.ntnu.idi.idatt.marked.Share; + +/** + * Transaction class + * + *

+ * An abstract class that handles a transaction + * based on week and share. Utilizes the calculators + * to perform calculations. + *

+ * + * @see TransactionCalculator + */ + +public abstract class Transaction { + + 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; + } + + /** + * + * Getters + * + * @return - their corresponding variable + * + */ + + public Share getShare() { + return share; + } + + public int getWeek() { + return week; + } + + public TransactionCalculator getCalculator() { + return calculator; + } + + 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); + +} diff --git a/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java b/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java new file mode 100644 index 0000000..0567591 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java @@ -0,0 +1,80 @@ +package edu.ntnu.idi.idatt.transaction; + +import java.util.ArrayList; +import java.util.List; + +/** + * TransactionArchive class + * + *

+ * Manages and handles transaction logic + *

+ * + */ +public class TransactionArchive { + + 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 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 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 getTransactions(week).stream().filter(t -> t instanceof Sale) + .map(t -> (Sale) t) + .toList(); + } + + /** + * Part 2 + * + * @return + */ + public int countDistinctWeeks() { // TODO: HERE + return -1; + } + +} diff --git a/src/main/resources/edu.ntnu.idi.idatt/logo.png b/src/main/resources/edu.ntnu.idi.idatt/logo.png new file mode 100644 index 0000000..a8980fe Binary files /dev/null and b/src/main/resources/edu.ntnu.idi.idatt/logo.png differ diff --git a/src/main/resources/edu.ntnu.idi.idatt/stonks.png b/src/main/resources/edu.ntnu.idi.idatt/stonks.png new file mode 100644 index 0000000..db95b50 Binary files /dev/null and b/src/main/resources/edu.ntnu.idi.idatt/stonks.png differ diff --git a/src/main/resources/edu.ntnu.idi.idatt/user.png b/src/main/resources/edu.ntnu.idi.idatt/user.png new file mode 100644 index 0000000..4f64ab8 Binary files /dev/null and b/src/main/resources/edu.ntnu.idi.idatt/user.png differ 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()); + + } + +}