-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from danieskj/pawel-sapula/basic-structure
Basic project structure finished
- Loading branch information
Showing
12 changed files
with
694 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Stock> stockMap = new HashMap<>(); | ||
| private Random random = new Random(); | ||
|
|
||
| public Exchange(String name, List<Stock> 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<Stock> findStocks(String searchTerm) { | ||
| ArrayList<Stock> 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())); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
src/main/java/edu/ntnu/idi/idatt/calculator/PurchaseCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package edu.ntnu.idi.idatt.calculator; | ||
|
|
||
| import edu.ntnu.idi.idatt.marked.Share; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| /** | ||
| * PurchaseCalculator class | ||
| * | ||
| * <p>Calculates transaction price based on | ||
| * share bought.</p> | ||
| * | ||
| */ | ||
| 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()); | ||
| } | ||
|
|
||
| } |
76 changes: 76 additions & 0 deletions
76
src/main/java/edu/ntnu/idi/idatt/calculator/SaleCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package edu.ntnu.idi.idatt.calculator; | ||
|
|
||
| import edu.ntnu.idi.idatt.marked.Share; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
|
|
||
| /** | ||
| * SaleCalculator class | ||
| * | ||
| * <p>Calculates transaction profit based on | ||
| * share sold.</p> | ||
| * | ||
| */ | ||
| 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()); | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/edu/ntnu/idi/idatt/calculator/TransactionCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package edu.ntnu.idi.idatt.calculator; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| /** | ||
| * TransactionCalculator interface | ||
| * | ||
| * <p>Contains methods that are used upon doing a transaction | ||
| * corresponding to a share and a week.</p> | ||
| * @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 | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package edu.ntnu.idi.idatt.marked; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Portfolio class | ||
| * | ||
| * <p>Class that functions as a wallet for a player | ||
| * storing all results of transactions made.</p> | ||
| * | ||
| */ | ||
| public class Portfolio { | ||
|
|
||
| private ArrayList<Share> 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<Share> 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<Share> 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); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.