Skip to content

Commit

Permalink
Merge pull request #120 from einaskoi/einar/marketMovement
Browse files Browse the repository at this point in the history
add new StockController to handle stock movement
  • Loading branch information
peretr authored May 18, 2026
2 parents 29a0aac + 57e2099 commit 8453e91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;

import java.math.BigDecimal;
import java.util.Random;

public class StockController {
private Stock stock;
private int timeframe;
private int trend;
private int delay;
private int time;
private Random random;
private double timeMultiplier;
private BigDecimal price;
private BigDecimal startPrice;
private BigDecimal offset;
private BigDecimal priceChange;


public StockController(Stock stock) {
this.stock = stock;
time = 0;
random = new Random();
timeMultiplier = (random.nextDouble() + 0.1) / 20;
startPrice = stock.getSalesPrice();

price = startPrice;
priceChange = new BigDecimal(random.nextInt(startPrice.intValue()/2));
}

public BigDecimal updatePrice() {
BigDecimal marketFluctuation = BigDecimal.valueOf(Math.sin(timeMultiplier * time))
.multiply(priceChange)
.add(startPrice);

double speculatorFluctuation = random.nextDouble(2) - 1;
offset = BigDecimal.valueOf(speculatorFluctuation*30);

time += 1;
price = marketFluctuation;

return price.add(offset);
}

public void handleEvent() {

}

public boolean isTrajectorySteep() {
return true;
}

public boolean isDelay() {
return delay > 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.StockController;
import edu.ntnu.idi.idatt2003.gruppe42.View.Views.Components.StockGraph;
import java.math.BigDecimal;
import java.util.ArrayList;
Expand All @@ -11,7 +12,7 @@ public class Stock {
private final String company;
private StockGraph stockGraph;
private List<BigDecimal> prices = new ArrayList<>();

private StockController stockController;
/** Constructs a new stock. */
public Stock(
final String symbol,
Expand All @@ -21,6 +22,7 @@ public Stock(
this.symbol = symbol;
this.company = company;
prices.add(salesPrice);
stockController = new StockController(this);
}

/** @return the symbol. */
Expand Down Expand Up @@ -70,9 +72,7 @@ public BigDecimal getLatestPriceChange() {

/** Updates the price. */
public void updatePrice() {
BigDecimal oldPrice = getSalesPrice();
BigDecimal newPrice = oldPrice.add(new BigDecimal("1"));
newPrice = newPrice.max(BigDecimal.ZERO);
BigDecimal newPrice = stockController.updatePrice();
prices.add(newPrice);
}

Expand Down

0 comments on commit 8453e91

Please sign in to comment.