Skip to content

Commit

Permalink
feat: Added Exchange class as backbone of the project.
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsa committed Feb 12, 2026
1 parent 326ed30 commit cf8ebfe
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/Exchange.java
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()));
}
}


}

0 comments on commit cf8ebfe

Please sign in to comment.