Skip to content

Commit

Permalink
add gamefactory and cleaned up StartView class
Browse files Browse the repository at this point in the history
  • Loading branch information
einaskoi committed May 11, 2026
1 parent 4ae6f81 commit cb95e93
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers;

public class BankAppController {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}

}
8 changes: 8 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public StackPane getRoot() {
public TextField getUsernameField() {
return usernameField;
}

public String getSelectedMode() {
RadioButton selectedMode = (RadioButton) startingMoneyGroup.getSelectedToggle();
if (selectedMode == null) {
Expand Down

0 comments on commit cb95e93

Please sign in to comment.