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..a159c4a --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/Exchange.java @@ -0,0 +1,81 @@ +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.*; + +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); + } + + public Stock getStock(String symbol) { + if(this.hasStock(symbol)){ + stockMap.get(symbol); + } + return null; //TODO: Exception + } + + 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; + } + + 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(); + } + + 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/Player.java b/src/main/java/edu/ntnu/idi/idatt/Player.java new file mode 100644 index 0000000..cb2ae90 --- /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..1bd3ae6 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java @@ -0,0 +1,70 @@ +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 = BigDecimal.valueOf(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..3f59521 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/SaleCalculator.java @@ -0,0 +1,76 @@ +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 = 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 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 = BigDecimal.valueOf(0.3); // Corresponding to 30% + BigDecimal profit = calculateGross().subtract(purchasePrice.multiply(quantity)); // gross - purchase price x quanitity + 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..a1f9265 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java @@ -0,0 +1,21 @@ +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/marked/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java new file mode 100644 index 0000000..535259b --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java @@ -0,0 +1,65 @@ +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; + + /** + * 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); + } + + /** + * 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..1f9c14b --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Share.java @@ -0,0 +1,49 @@ +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..11ca9f8 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/marked/Stock.java @@ -0,0 +1,68 @@ +package edu.ntnu.idi.idatt.marked; + + +import java.math.BigDecimal; +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 List 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 = 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..157c193 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Purchase.java @@ -0,0 +1,32 @@ +package edu.ntnu.idi.idatt.transaction; + +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)); + } + + /** + * @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..d3dfee2 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Sale.java @@ -0,0 +1,31 @@ +package edu.ntnu.idi.idatt.transaction; + +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)); + } + + /** + * @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..1e955fe --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/Transaction.java @@ -0,0 +1,66 @@ +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..3fbeaa4 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/transaction/TransactionArchive.java @@ -0,0 +1,78 @@ +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 transactions.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(); + } + + /** + * Part 2 + * + * @return + */ + public int countDistinctWeeks(){ //TODO: HERE + return -1; + } + +}