-
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 #24 from IDATT2003-gruppe06/main
Merge Main onto dev to keep dev up to date
- Loading branch information
Showing
41 changed files
with
363 additions
and
274 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
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,6 @@ | ||
| { | ||
| "java.configuration.updateBuildConfiguration": "interactive", | ||
| "java.format.settings.profile": "GoogleStyle", | ||
| "editor.formatOnSave": true, | ||
| "editor.defaultFormatter": "redhat.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
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 |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| public class Main { | ||
| } | ||
| public class Main {} |
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,68 @@ | ||
| package millions; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Random; | ||
|
|
||
| public class Exchange { | ||
| private String name; | ||
| private Map<String, Stock> stocks; | ||
| private int weekNumber; | ||
| private Random random = new Random(); | ||
|
|
||
| public Exchange(String name, List<Stock> stockList) { | ||
| this.name = name; | ||
| this.stocks = new HashMap<>(); | ||
| this.weekNumber = 1; | ||
|
|
||
| // Populate the stocks map to get ticker -> stock | ||
| for (Stock stock : stockList) { | ||
| this.stocks.put(stock.getSymbol(), stock); | ||
| } | ||
| } | ||
|
|
||
| public void buy(Player player, Stock stock, BigDecimal quantity) { | ||
| Share shareToBuy = new Share(stock, quantity, stock.getSalesPrice()); | ||
| Purchase purchase = new Purchase(shareToBuy, this.weekNumber); | ||
| purchase.commit(player); | ||
| } | ||
|
|
||
| public void sell(Player player, Share share) { | ||
| Sale sale = new Sale(share, weekNumber); | ||
| sale.commit(player); | ||
| } | ||
|
|
||
| public Map<String, Stock> getStocks() { | ||
| return this.stocks; | ||
| } | ||
|
|
||
| public Stock getStock(String symbol) { | ||
| return this.stocks.get(symbol); | ||
| } | ||
|
|
||
| public boolean hasStock(String symbol) { | ||
| return this.stocks.containsKey(symbol); | ||
| } | ||
|
|
||
| public List<Stock> findStocks(String searchTerm) { | ||
| return this.stocks.values().stream() | ||
| .filter(s -> s.getSymbol().contains(searchTerm) || s.getCompany().contains(searchTerm)) | ||
| .toList(); | ||
| } | ||
|
|
||
| public void advance() { | ||
| this.weekNumber++; | ||
| for (Stock stock : this.stocks.values()) { | ||
| double change = 0.9 + random.nextDouble() * 0.2; | ||
| stock.addNewSalesPrice( | ||
| stock | ||
| .getSalesPrice() | ||
| .multiply(BigDecimal.valueOf(change)) | ||
| .setScale(2, RoundingMode.HALF_UP)); | ||
| // RoundingMode from AI suggestion | ||
| } | ||
| } | ||
| } |
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,43 @@ | ||
| package millions; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public class Player { | ||
| private String name; | ||
| private BigDecimal startingMoney; | ||
| private BigDecimal money; | ||
| private Portfolio portfolio; | ||
| private TransactionArchive transactionArchive; | ||
|
|
||
| public Player(String name, BigDecimal startingMoney) { | ||
| this.name = name; | ||
| this.startingMoney = startingMoney; | ||
| this.money = startingMoney; | ||
| this.portfolio = new Portfolio(); | ||
| this.transactionArchive = new TransactionArchive(); | ||
| } | ||
|
|
||
| public void addMoney(BigDecimal amount) { | ||
| this.money = this.money.add(amount); | ||
| } | ||
|
|
||
| public void withdrawMoney(BigDecimal amount) { | ||
| this.money = this.money.subtract(amount); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| public BigDecimal getMoney() { | ||
| return this.money; | ||
| } | ||
|
|
||
| public Portfolio getPortfolio() { | ||
| return this.portfolio; | ||
| } | ||
|
|
||
| public TransactionArchive getTransactionArchive() { | ||
| return this.transactionArchive; | ||
| } | ||
| } |
13 changes: 12 additions & 1 deletion
13
src/main/java/temppackage/Portfolio.java → src/main/java/millions/Portfolio.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
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,25 @@ | ||
| package millions; | ||
|
|
||
| import millions.calculators.PurchaseCalculator; | ||
|
|
||
| public class Purchase extends Transaction { | ||
|
|
||
| public Purchase(Share share, int week) { | ||
| super(share, week, new PurchaseCalculator(share)); | ||
| } | ||
|
|
||
| @Override | ||
| public void commit(Player player) { | ||
| if (isCommitted()) { | ||
| throw new IllegalStateException("Already committed"); | ||
| } | ||
|
|
||
| if (player.getMoney().compareTo(getCalculator().calculateTotal()) < 0) { | ||
| throw new IllegalStateException("Not enought money"); | ||
| } | ||
| player.withdrawMoney(getCalculator().calculateTotal()); | ||
| player.getPortfolio().addShare(getShare()); | ||
| player.getTransactionArchive().add(this); | ||
| setCommitted(true); | ||
| } | ||
| } |
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,25 @@ | ||
| package millions; | ||
|
|
||
| import millions.calculators.SaleCalculator; | ||
|
|
||
| public class Sale extends Transaction { | ||
|
|
||
| public Sale(Share share, int week) { | ||
| super(share, week, new SaleCalculator(share)); | ||
| } | ||
|
|
||
| @Override | ||
| public void commit(Player player) { | ||
| if (isCommitted()) { | ||
| throw new IllegalStateException("Already committed"); | ||
| } | ||
|
|
||
| if (!player.getPortfolio().contains(getShare())) { | ||
| throw new IllegalStateException("Does not own the share"); | ||
| } | ||
| player.addMoney(getCalculator().calculateTotal()); | ||
| player.getPortfolio().removeShare(getShare()); | ||
| player.getTransactionArchive().add(this); | ||
| setCommitted(true); | ||
| } | ||
| } |
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
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
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,40 @@ | ||
| package millions; | ||
|
|
||
| import millions.calculators.TransactionCalculator; | ||
|
|
||
| public abstract class Transaction { | ||
|
|
||
| private Share share; | ||
| private int week; | ||
| private TransactionCalculator transactionCalculator; | ||
| private boolean committed; | ||
|
|
||
| protected Transaction(Share share, int week, TransactionCalculator transactionCalculator) { | ||
| this.share = share; | ||
| this.week = week; | ||
| this.transactionCalculator = transactionCalculator; | ||
| this.committed = false; | ||
| } | ||
|
|
||
| public Share getShare() { | ||
| return this.share; | ||
| } | ||
|
|
||
| public int getWeek() { | ||
| return this.week; | ||
| } | ||
|
|
||
| public TransactionCalculator getCalculator() { | ||
| return this.transactionCalculator; | ||
| } | ||
|
|
||
| public boolean isCommitted() { | ||
| return this.committed; | ||
| } | ||
|
|
||
| protected void setCommitted(boolean committed) { | ||
| this.committed = committed; | ||
| } | ||
|
|
||
| public abstract void commit(Player player); | ||
| } |
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,48 @@ | ||
| package millions; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class TransactionArchive { | ||
|
|
||
| List<Transaction> transactions; | ||
|
|
||
| public TransactionArchive() { | ||
| this.transactions = new ArrayList<>(); | ||
| } | ||
|
|
||
| public boolean add(Transaction transaction) { | ||
| if (transactions.contains(transaction)) { | ||
| return false; | ||
| } | ||
| this.transactions.add(transaction); | ||
| return true; | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return transactions.isEmpty(); | ||
| } | ||
|
|
||
| public List<Transaction> getTransactions(int week) { | ||
| return transactions.stream().filter(x -> x.getWeek() == week).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<Purchase> getPurchases(int week) { | ||
| return transactions.stream() | ||
| .filter(t -> t.getWeek() == week && t instanceof Purchase) | ||
| .map(t -> (Purchase) t) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<Sale> getSales(int week) { | ||
| return transactions.stream() | ||
| .filter(t -> t.getWeek() == week && t instanceof Sale) | ||
| .map(t -> (Sale) t) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public int countDistinctWeeks() { | ||
| return (int) transactions.stream().map(Transaction::getWeek).distinct().count(); | ||
| } | ||
| } |
Oops, something went wrong.