-
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 #3 from solvena/mappe_del1
Mappe del1
- Loading branch information
Showing
26 changed files
with
690 additions
and
1 deletion.
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 @@ | ||
| target/ |
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 +1 @@ | ||
| Branch test | ||
| Branch test 3 |
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,114 @@ | ||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Random; | ||
|
|
||
| public class Exchange { | ||
|
|
||
| private final String name; | ||
| private int week; | ||
| private final Map<String, Stock> stockMap; | ||
| private final Random random; | ||
|
|
||
| public Exchange(String name, List<Stock> stocks) { | ||
| this.name = name; | ||
| this.week = 1; | ||
| this.stockMap = new HashMap<>(); | ||
| this.random = new Random(); | ||
|
|
||
| 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) { | ||
| return stockMap.get(symbol); | ||
| } | ||
|
|
||
| public List<Stock> findStocks(String searchTerm) { | ||
| List<Stock> result = new ArrayList<>(); | ||
| String lowerSearch = searchTerm.toLowerCase(); | ||
|
|
||
| for (Stock stock : stockMap.values()) { | ||
| if (stock.getSymbol().toLowerCase().contains(lowerSearch) | ||
| || stock.getCompany().toLowerCase().contains(lowerSearch)) { | ||
| result.add(stock); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public Transaction buy(String symbol, BigDecimal quantity, Player player) { | ||
| Stock stock = getStock(symbol); | ||
|
|
||
| // unngå nullpointerexception | ||
| if (stock == null) { | ||
| return null; | ||
| } | ||
|
|
||
| // lager en ny "andel" basert på nåværende salgspris | ||
| Share shareToBuy = new Share(stock, quantity, stock.getSalesPrice()); | ||
|
|
||
| // oppretter kjøpstransaksjonen for den uka | ||
| Purchase purchase = new Purchase(shareToBuy, this.week); | ||
|
|
||
| // committer til player | ||
| purchase.commit(player); | ||
|
|
||
| return purchase; | ||
| } | ||
|
|
||
| public Transaction sell(Share share, Player player) { | ||
| // unngå nullpointerexception | ||
| if (share == null) { | ||
| return null; | ||
| } | ||
|
|
||
| // oppretter salgstransaksjonen for den uka | ||
| Sale sale = new Sale(share, this.week); | ||
|
|
||
| // commiter til player | ||
| sale.commit(player); | ||
|
|
||
| return sale; | ||
| } | ||
|
|
||
| public void advance() { | ||
|
|
||
| week++; | ||
|
|
||
| for (Stock stock : stockMap.values()) { // henter stock-objektene | ||
|
|
||
| BigDecimal currentPrice = stock.getSalesPrice(); // henter siste pris fra Stock | ||
|
|
||
| double changePercent = (random.nextDouble() - 0.5) * 0.1; | ||
|
|
||
| BigDecimal change = currentPrice.multiply(BigDecimal.valueOf(changePercent)); | ||
|
|
||
| BigDecimal newPrice = currentPrice.add(change); | ||
|
|
||
| if (newPrice.compareTo(BigDecimal.ZERO) > 0) { // unngå negativ pris | ||
| stock.addNewSalesPrice(newPrice); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
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 @@ | ||
| 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 String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| public BigDecimal getMoney() { | ||
| return this.money; | ||
| } | ||
|
|
||
| public void addMoney(BigDecimal amount) { | ||
| this.money = this.money.add(amount); | ||
| } | ||
|
|
||
| public void withdrawMoney(BigDecimal amount) { | ||
| this.money = this.money.subtract(amount); | ||
| } | ||
|
|
||
| public Portfolio getPortfolio() { | ||
| return this.portfolio; | ||
| } | ||
|
|
||
| public TransactionArchive getTransactionArchive() { | ||
| return this.transactionArchive; | ||
| } | ||
|
|
||
|
|
||
| } |
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,38 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Portfolio { | ||
|
|
||
| private final List<Share> shares; | ||
|
|
||
| public Portfolio() { | ||
| this.shares = new ArrayList<>(); | ||
| } | ||
|
|
||
| public boolean addShare(Share share) { | ||
| return shares.add(share); | ||
| } | ||
|
|
||
| public boolean removeShare(Share share) { | ||
| return shares.remove(share); | ||
| } | ||
|
|
||
| public List<Share> getShares() { | ||
| return new ArrayList<>(shares); | ||
| } | ||
|
|
||
| public List<Share> getShares(String symbol) { | ||
| List<Share> result = new ArrayList<>(); | ||
| for (Share share : shares) { | ||
| if (share.getStock().getSymbol().equals(symbol)) { | ||
| result.add(share); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public boolean contains(Share share) { | ||
| return shares.contains(share); | ||
| } | ||
|
|
||
| } |
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,22 @@ | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class Purchase extends Transaction { | ||
| public Purchase(Share share, int week) { | ||
| super(share, week, new PurchaseCalculator(share)); | ||
| } | ||
|
|
||
| @Override | ||
| public void commit(Player player) { | ||
| BigDecimal price = this.getCalculator().calculateTotal(); | ||
|
|
||
| if (isCommitted() || (player.getMoney().compareTo(price) < 0)) { | ||
| return; | ||
| } | ||
|
|
||
| player.withdrawMoney(price); | ||
| player.getPortfolio().addShare(this.getShare()); | ||
| player.getTransactionArchive().add(this); | ||
|
|
||
| this.committed = 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,29 @@ | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class PurchaseCalculator implements TransactionCalculator { | ||
| private BigDecimal purchasePrice; | ||
| private BigDecimal quantity; | ||
|
|
||
| public PurchaseCalculator(Share share) { | ||
| this.purchasePrice = share.getPurchasePrice(); | ||
| this.quantity = share.getQuantity(); | ||
| } | ||
|
|
||
| public BigDecimal calculateGross() { | ||
| return this.purchasePrice.multiply(this.quantity); | ||
| } | ||
|
|
||
| public BigDecimal calculateCommission() { | ||
| BigDecimal rate = new BigDecimal("0.005"); | ||
| return calculateGross().multiply(rate); | ||
| } | ||
|
|
||
| public BigDecimal calculateTax() { | ||
| BigDecimal tax = new BigDecimal("0"); | ||
| return tax; | ||
| } | ||
|
|
||
| public BigDecimal calculateTotal() { | ||
| return calculateGross().add(calculateCommission()).add(calculateTax()); | ||
| } | ||
| } |
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 @@ | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class Sale extends Transaction { | ||
| public Sale(Share share, int week) { | ||
| super(share, week, new SaleCalculator(share)); | ||
| } | ||
|
|
||
| public void commit(Player player) { | ||
| BigDecimal price = getCalculator().calculateTotal(); | ||
|
|
||
| if (isCommitted() || !player.getPortfolio().contains(this.getShare())) { | ||
| return; | ||
| } | ||
|
|
||
| player.addMoney(price); | ||
| player.getPortfolio().removeShare(this.getShare()); | ||
| player.getTransactionArchive().add(this); | ||
|
|
||
| this.committed = 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,36 @@ | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class SaleCalculator implements TransactionCalculator{ | ||
| private BigDecimal purchasePrice; | ||
| private BigDecimal salesPrice; | ||
| private BigDecimal quantity; | ||
|
|
||
| public SaleCalculator(Share share) { | ||
| this.purchasePrice = share.getPurchasePrice(); | ||
| this.salesPrice = share.getStock().getSalesPrice(); | ||
| this.quantity = share.getQuantity(); | ||
| } | ||
|
|
||
| public BigDecimal calculateGross() { | ||
| return this.salesPrice.multiply(this.quantity); | ||
| } | ||
|
|
||
| public BigDecimal calculateCommission() { | ||
| BigDecimal rate = new BigDecimal("0.01"); | ||
| return calculateGross().multiply(rate); | ||
| } | ||
|
|
||
| public BigDecimal calculateTax() { | ||
| BigDecimal sellingCost = this.purchasePrice.multiply(this.quantity); | ||
| BigDecimal profit = calculateGross().subtract(calculateCommission()).subtract(sellingCost); | ||
| BigDecimal rate = new BigDecimal("0.3"); | ||
| return profit.multiply(rate); | ||
| } | ||
|
|
||
| public BigDecimal calculateTotal() { | ||
| return calculateGross().subtract(calculateCommission()).subtract(calculateTax()); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
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,27 @@ | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class Share { | ||
|
|
||
| private final Stock stock; | ||
| private final BigDecimal quantity; | ||
| private final BigDecimal purchasePrice; | ||
|
|
||
| public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) { | ||
| this.stock = stock; | ||
| this.quantity = quantity; | ||
| this.purchasePrice = purchasePrice; | ||
| } | ||
|
|
||
| public Stock getStock() { | ||
| return stock; | ||
| } | ||
|
|
||
| public BigDecimal getQuantity() { | ||
| return quantity; | ||
| } | ||
|
|
||
| public BigDecimal getPurchasePrice() { | ||
| return purchasePrice; | ||
| } | ||
|
|
||
| } |
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,33 @@ | ||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Stock { | ||
|
|
||
| private final String symbol; | ||
| private final String company; | ||
| private final List<BigDecimal> prices; | ||
|
|
||
| public Stock(String symbol, String company, BigDecimal salesPrice) { | ||
| this.symbol = symbol; | ||
| this.company = company; | ||
| this.prices = new ArrayList<>(); | ||
| this.prices.add(salesPrice); | ||
| } | ||
|
|
||
| public String getSymbol() { | ||
| return symbol; | ||
| } | ||
|
|
||
| public String getCompany() { | ||
| return company; | ||
| } | ||
|
|
||
| public BigDecimal getSalesPrice() { | ||
| return prices.get(prices.size() - 1); | ||
| } | ||
|
|
||
| public void addNewSalesPrice(BigDecimal price) { | ||
| prices.add(price); | ||
| } | ||
| } |
Oops, something went wrong.