Skip to content

add new StockController to handle stock movement #120

Merged
merged 1 commit into from
May 18, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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