diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/StockController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/StockController.java new file mode 100644 index 0000000..e1389b9 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/StockController.java @@ -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; + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java index fcc8c7a..4e63ff2 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java @@ -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; @@ -11,7 +12,7 @@ public class Stock { private final String company; private StockGraph stockGraph; private List prices = new ArrayList<>(); - + private StockController stockController; /** Constructs a new stock. */ public Stock( final String symbol, @@ -21,6 +22,7 @@ public Stock( this.symbol = symbol; this.company = company; prices.add(salesPrice); + stockController = new StockController(this); } /** @return the symbol. */ @@ -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); }