-
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 #43 from IDATT2003-gruppe06/dev
Keep file-io-development up to date to avoid future merge conflicts
- Loading branch information
Showing
36 changed files
with
1,132 additions
and
75 deletions.
There are no files selected for viewing
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 |
|---|---|---|
|
|
@@ -38,3 +38,5 @@ build/ | |
|
|
||
| ### Mac OS ### | ||
| .DS_Store | ||
|
|
||
| shell.nix | ||
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
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,51 @@ | ||
| package millions; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import javafx.application.Application; | ||
| import javafx.scene.Scene; | ||
| import javafx.stage.Stage; | ||
| import millions.controller.GameController; | ||
| import millions.view.GameView; | ||
| import millions.view.StartView; | ||
|
|
||
| /** Main JavaFX application entry point for the Millions stock trading game. */ | ||
| public class App extends Application { | ||
|
|
||
| @Override | ||
| public void start(Stage stage) { | ||
| GameController controller = new GameController(); | ||
| StartView startView = new StartView(stage); | ||
|
|
||
| startView | ||
| .getStartButton() | ||
| .setOnAction( | ||
| event -> { | ||
| try { | ||
| controller.startGame( | ||
| startView.getName(), | ||
| new BigDecimal(startView.getStartingAmount()), | ||
| startView.getSelectedFile().toPath(), | ||
| startView.getPreRunWeeks()); | ||
|
|
||
| GameView gameView = new GameView(controller); | ||
| controller.getPlayer().addListener(gameView); | ||
| controller.getExchange().addListener(gameView); | ||
|
|
||
| Scene gameScene = new Scene(gameView, 1920, 1080); | ||
| stage.setScene(gameScene); | ||
| } catch (RuntimeException ex) { | ||
| System.err.println(ex); | ||
| System.exit(0); | ||
| } | ||
| }); | ||
|
|
||
| Scene scene = new Scene(startView, 400, 350); | ||
| stage.setTitle("Millions"); | ||
| stage.setScene(scene); | ||
| stage.show(); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| launch(args); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/millions/calculators/TransactionCalculatorFactory.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,22 @@ | ||
| package millions.calculators; | ||
|
|
||
| import millions.model.Share; | ||
| import millions.model.calculators.PurchaseCalculator; | ||
| import millions.model.calculators.SaleCalculator; | ||
| import millions.model.calculators.TransactionCalculator; | ||
|
|
||
| /** | ||
| * Factory for creating transaction calculators. | ||
| */ | ||
| public class TransactionCalculatorFactory { | ||
|
|
||
| private TransactionCalculatorFactory() {} | ||
|
|
||
| public TransactionCalculator createPurchaseCalculator(Share share) { | ||
| return new PurchaseCalculator(share); | ||
| } | ||
|
|
||
| public TransactionCalculator createSaleCalculator(Share share) { | ||
| return new SaleCalculator(share); | ||
| } | ||
| } |
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,76 @@ | ||
| package millions.controller; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.nio.file.Path; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import millions.controller.fileIO.CSVStockFileParser; | ||
| import millions.controller.fileIO.StockFileReader; | ||
| import millions.model.Exchange; | ||
| import millions.model.Player; | ||
| import millions.model.Stock; | ||
|
|
||
| /** Controls game initialization. */ | ||
| public class GameController { | ||
| private Player player; | ||
| private Exchange exchange; | ||
|
|
||
| public void startGame( | ||
| String name, BigDecimal startingMoney, Path stockFilePath, int preRunWeeks) { | ||
| if (preRunWeeks < 0) { | ||
| throw new IllegalArgumentException("Pre run weeks cannot be negative"); | ||
| } | ||
|
|
||
| StockFileReader reader = new StockFileReader(stockFilePath); | ||
| List<String> lines = reader.readFile(); | ||
| CSVStockFileParser parser = new CSVStockFileParser(lines); | ||
| List<Stock> stocks = parser.parse(); | ||
|
|
||
| exchange = new Exchange("Exchange", stocks); | ||
| for (int i = 0; i < preRunWeeks; i++) { | ||
| exchange.advance(); | ||
| } | ||
|
|
||
| player = new Player(name, startingMoney); | ||
| } | ||
|
|
||
| public Player getPlayer() { | ||
| return player; | ||
| } | ||
|
|
||
| public Exchange getExchange() { | ||
| return exchange; | ||
| } | ||
|
|
||
| public List<Stock> getStocks() { | ||
| return exchange.getStocks().values().stream() | ||
| .sorted(Comparator.comparing(Stock::getSymbol)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
| * Gives alphabetic sort of findStocks | ||
| * | ||
| * @param searchTerm | ||
| * @return | ||
| */ | ||
| public List<Stock> searchStocks(String searchTerm) { | ||
| if (searchTerm == null || searchTerm.isBlank()) { | ||
| return getStocks(); | ||
| } | ||
| return exchange.findStocks(searchTerm).stream() | ||
| .sorted(Comparator.comparing(Stock::getSymbol)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
| * Get stocks with symbol | ||
| * | ||
| * @param symbol | ||
| * @return | ||
| */ | ||
| public Stock getStock(String symbol) { | ||
| return exchange.getStock(symbol); | ||
| } | ||
| } |
27 changes: 13 additions & 14 deletions
27
src/main/java/millions/controller/fileIO/CSVStockFileParser.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 |
|---|---|---|
| @@ -1,42 +1,41 @@ | ||
| package millions.controller.fileIO; | ||
|
|
||
| import millions.model.Stock; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import millions.model.Stock; | ||
|
|
||
| /** Parses CSV lines into Stock objects. */ | ||
| public class CSVStockFileParser { | ||
| private List<String> lines; | ||
|
|
||
| public CSVStockFileParser(List<String> lines) { | ||
| if (verifyCSV(lines)) { | ||
| this.lines = lines; | ||
| } | ||
| else { | ||
| } else { | ||
| // throw file format error | ||
| } | ||
| } | ||
|
|
||
| // returns true if all entries have exactly 3 data points | ||
| public boolean verifyCSV(List<String> lines) { | ||
| return lines.stream() | ||
| .filter(l -> !(l.startsWith("#") || l.isBlank())) | ||
| .noneMatch(l -> l.split(",").length != 3); | ||
|
|
||
| .filter(l -> !(l.startsWith("#") || l.isBlank())) | ||
| .noneMatch(l -> l.split(",").length != 3); | ||
| } | ||
|
|
||
| public List<Stock> parse() { | ||
| List<Stock> stocks = new ArrayList<>(); | ||
| lines.stream() | ||
| .filter(l -> !((l.startsWith("#") || l.isBlank()))) | ||
| .forEach(l -> { | ||
| String[] split = l.split(","); | ||
| String symbol = split[0]; | ||
| String company = split[1]; | ||
| BigDecimal price = new BigDecimal(split[2]); | ||
| stocks.add(new Stock(symbol, company, price)); | ||
| }); | ||
| .forEach( | ||
| l -> { | ||
| String[] split = l.split(","); | ||
| String symbol = split[0]; | ||
| String company = split[1]; | ||
| BigDecimal price = new BigDecimal(split[2]); | ||
| stocks.add(new Stock(symbol, company, price)); | ||
| }); | ||
| return stocks; | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.