-
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.
feat: Added Exchange class as backbone of the project.
- Loading branch information
Showing
1 changed file
with
81 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())); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |