diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/BankAppController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/BankAppController.java new file mode 100644 index 0000000..dc37c8d --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/BankAppController.java @@ -0,0 +1,4 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers; + +public class BankAppController { +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java index 6ea848e..917b575 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java @@ -1,6 +1,7 @@ package edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers; import edu.ntnu.idi.idatt2003.gruppe42.Millions; +import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty; import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; import edu.ntnu.idi.idatt2003.gruppe42.View.Views.StartView; import java.math.BigDecimal; @@ -40,38 +41,33 @@ public StartViewController(Millions application, StartView startView) { private void handleStart() { if (startView.getSelectedMode() == null) { - Alert alert = new Alert(Alert.AlertType.WARNING); - alert.setTitle("Difficulty missing"); - alert.setHeaderText(null); - alert.setContentText("how much money you got?"); - alert.showAndWait(); + showAlert("How much money you got?"); return; } - BigDecimal money = getStartingMoney(); + String resolvedName = resolveUsername(username); + Difficulty difficulty = Difficulty.fromString(startView.getSelectedMode()); + application.initGame(resolvedName, difficulty); + application.switchToDesktopView(); + } + + private String resolveUsername(String input) { if (username == null || username.trim().isEmpty() || !isValidName(username)) { username = TATE_NAMES.get(new Random().nextInt(TATE_NAMES.size())); } - - Player player = new Player(username, money); - System.out.println(player.getMoney()); - application.switchToDesktopView(); + return input; } private boolean isValidName(String value) { - return value != null && value.matches("[a-zA-Z0-9]+"); + return value.matches("[a-zA-Z0-9]+"); } - private BigDecimal getStartingMoney() { - String selected = startView.getSelectedMode(); - - return switch (selected.charAt(0)) { - case 'E' -> new BigDecimal("1000"); - case 'M' -> new BigDecimal("100"); - case 'H' -> new BigDecimal("0"); - default -> new BigDecimal("1000"); - }; + private void showAlert(String message) { + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setTitle("Difficulty missing"); + alert.setHeaderText(null); + alert.setContentText(message); + alert.showAndWait(); } - } \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java index 0c8c244..226704e 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java @@ -1,6 +1,9 @@ package edu.ntnu.idi.idatt2003.gruppe42; import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.StartViewController; +import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty; +import edu.ntnu.idi.idatt2003.gruppe42.Model.GameFactory; +import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; import edu.ntnu.idi.idatt2003.gruppe42.View.Views.DesktopView; import edu.ntnu.idi.idatt2003.gruppe42.View.Views.StartView; import javafx.application.Application; @@ -12,6 +15,7 @@ public class Millions extends Application { private Scene scene; private Stage stage; private DesktopView desktopView; + private Player player; @Override public void start(Stage stage) throws Exception { @@ -29,6 +33,10 @@ public void start(Stage stage) throws Exception { stage.show(); } + public void initGame(String username, Difficulty difficulty) { + player = GameFactory.createPlayer(username, difficulty); + } + public void switchToStartView() { StartView startView = new StartView(); scene.setRoot(startView.getRoot()); diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Difficulty.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Difficulty.java index 9b9eba9..f4a5b5f 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Difficulty.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Difficulty.java @@ -3,5 +3,15 @@ public enum Difficulty { EASY, MEDIUM, - HARD + HARD; + + public static Difficulty fromString(String value) { + if (value == null || value.isEmpty()) return EASY; + return switch (value.charAt(0)) { + case 'E' -> EASY; + case 'M' -> MEDIUM; + case 'H' -> HARD; + default -> EASY; + }; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/GameFactory.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/GameFactory.java new file mode 100644 index 0000000..7978f42 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/GameFactory.java @@ -0,0 +1,15 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Model; + +import java.math.BigDecimal; + +public class GameFactory { + + public static Player createPlayer(String username, Difficulty difficulty) { + BigDecimal money = switch (difficulty) { + case EASY -> new BigDecimal("1000"); + case MEDIUM -> new BigDecimal("100"); + case HARD -> new BigDecimal("0"); + }; + return new Player(username, money); + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/StartView.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/StartView.java index 38cd521..94924e6 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/StartView.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/StartView.java @@ -64,6 +64,7 @@ public StackPane getRoot() { public TextField getUsernameField() { return usernameField; } + public String getSelectedMode() { RadioButton selectedMode = (RadioButton) startingMoneyGroup.getSelectedToggle(); if (selectedMode == null) {