-
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 #120 from einaskoi/einar/marketMovement
add new StockController to handle stock movement
- Loading branch information
Showing
2 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/StockController.java
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,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; | ||
| } | ||
| } |
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