Skip to content

Commit

Permalink
Merge pull request #24 from IDATT2003-gruppe06/main
Browse files Browse the repository at this point in the history
Merge Main onto dev to keep dev up to date
  • Loading branch information
nolydvo authored Mar 6, 2026
2 parents a6ae521 + e2450da commit bad7347
Show file tree
Hide file tree
Showing 41 changed files with 363 additions and 274 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ build/
!**/src/test/**/build/

### VS Code ###
.vscode/
#.vscode/
!.vscode/settings.json

### Mac OS ###
.DS_Store
.DS_Store
6 changes: 6 additions & 0 deletions .vscode/settings.json
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",
}
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.12.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<configLocation>google_checks.xml</configLocation>
<failsOnError>true</failsOnError>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
public class Main {
}
public class Main {}
68 changes: 68 additions & 0 deletions src/main/java/millions/Exchange.java
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
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/millions/Player.java
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package temppackage;
package millions;

import java.util.ArrayList;
import java.util.List;

public class Portfolio {
List<Share> shares;

public Portfolio() {
shares = new ArrayList<>();
}

public boolean addShare(Share share) {
return this.shares.add(share);
}

public boolean removeShare(Share share) {
return this.shares.remove(share);
}

public List<Share> getShares() {
return this.shares;
}

public List<Share> getShares(String symbol) {
return this.shares.stream()
.filter(share -> share.getStock().getSymbol().equals(symbol))
.toList();
}

public boolean contains(Share share) {
return this.shares.contains(share);
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/millions/Purchase.java
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);
}
}
25 changes: 25 additions & 0 deletions src/main/java/millions/Sale.java
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package temppackage;
package millions;

import java.math.BigDecimal;

Expand All @@ -12,12 +12,15 @@ public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) {
this.quantity = quantity;
this.purchasePrice = purchasePrice;
}

public Stock getStock() {
return this.stock;
}

public BigDecimal getQuantity() {
return this.quantity;
}

public BigDecimal getPurchasePrice() {
return this.purchasePrice;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package temppackage;
package millions;

import java.math.BigDecimal;
import java.util.List;
Expand All @@ -7,20 +7,25 @@ public class Stock {
String symbol;
String company;
List<BigDecimal> prices;
public Stock(String symbol, String company, List<BigDecimal> prices){

public Stock(String symbol, String company, List<BigDecimal> prices) {
this.symbol = symbol;
this.company = company;
this.prices = prices;
}

public String getSymbol() {
return this.symbol;
}

public String getCompany() {
return this.company;
}
public BigDecimal getPrice() {

public BigDecimal getSalesPrice() {
return this.prices.getLast();
}

public void addNewSalesPrice(BigDecimal price) {
this.prices.add(price);
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/millions/Transaction.java
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);
}
48 changes: 48 additions & 0 deletions src/main/java/millions/TransactionArchive.java
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();
}
}
Loading

0 comments on commit bad7347

Please sign in to comment.