Pārlūkot izejas kodu

Before I go too far trying Scene Builder

NedRedmond 6 gadus atpakaļ
vecāks
revīzija
dbf2c5f522

+ 10
- 0
pom.xml Parādīt failu

@@ -18,6 +18,16 @@
18 18
         <!-- dependencies -->
19 19
     </properties>
20 20
 
21
+    <dependencies>
22
+        <!-- https://mvnrepository.com/artifact/com.jfoenix/jfoenix -->
23
+        <dependency>
24
+            <groupId>com.jfoenix</groupId>
25
+            <artifactId>jfoenix</artifactId>
26
+            <version>8.0.7</version>
27
+        </dependency>
28
+
29
+    </dependencies>
30
+
21 31
     <build>
22 32
         <plugins>
23 33
             <plugin>

+ 14
- 3
src/main/java/rocks/zipcode/atm/CashMachine.java Parādīt failu

@@ -22,6 +22,9 @@ public class CashMachine {
22 22
         accountData = data;
23 23
     };
24 24
 
25
+    private boolean errorExists = false;
26
+    private String errorMessage = null;
27
+
25 28
     public void login(int id) {
26 29
         tryCall(
27 30
                 () -> bank.getAccountById(id),
@@ -55,21 +58,29 @@ public class CashMachine {
55 58
 
56 59
     @Override
57 60
     public String toString() {
58
-        return accountData != null ? accountData.toString() : "Try account 1000 or 2000 and click submit.";
61
+        String displayString;
62
+        if (errorExists) {
63
+            displayString = errorMessage;
64
+        } else {
65
+            displayString = accountData != null ? accountData.toString() : "Try account 1000 or 2000 and click submit.";
66
+        }
67
+        return displayString;
59 68
     }
60 69
 
61 70
     public <T> void tryCall(Supplier<ActionResult<T> > action, Consumer<T> postAction) {
62 71
         try {
63 72
             ActionResult<T> result = action.get();
64 73
             if (result.isSuccess()) {
74
+                errorExists = false;
65 75
                 T data = result.getData();
66 76
                 postAction.accept(data);
67 77
             } else {
68
-                String errorMessage = result.getErrorMessage();
78
+                errorExists = true;
79
+                errorMessage = result.getErrorMessage();
69 80
                 throw new RuntimeException(errorMessage);
70 81
             }
71 82
         } catch (Exception e) {
72 83
             System.out.println("Error: " + e.getMessage());
73 84
         }
74 85
     }
75
-}
86
+}

+ 38
- 8
src/main/java/rocks/zipcode/atm/CashMachineApp.java Parādīt failu

@@ -1,5 +1,12 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3
+import javafx.event.ActionEvent;
4
+import javafx.event.EventHandler;
5
+import javafx.geometry.Insets;
6
+import javafx.geometry.Pos;
7
+import javafx.scene.control.Label;
8
+import javafx.scene.layout.GridPane;
9
+import javafx.scene.layout.StackPane;
3 10
 import rocks.zipcode.atm.bank.Bank;
4 11
 import javafx.application.Application;
5 12
 import javafx.scene.Parent;
@@ -15,11 +22,40 @@ import javafx.scene.layout.FlowPane;
15 22
  * @author ZipCodeWilmington
16 23
  */
17 24
 public class CashMachineApp extends Application {
18
-
25
+    private Stage window;
26
+    Scene mainContent, loginScene;
19 27
     private TextField field = new TextField();
20 28
     private CashMachine cashMachine = new CashMachine(new Bank());
21 29
 
22
-    private Parent createContent() {
30
+    @Override
31
+    public void start(Stage stage) throws Exception {
32
+        window = stage;
33
+        window.setTitle("Zip Cloud Bank");
34
+
35
+
36
+        //Log In Layout
37
+        Label loginLabel = new Label("Welcome to Zip Cloud Bank!\nPlease log in.");
38
+        Button btnLogIn = new Button("Log In");
39
+        btnLogIn.setStyle("-fx-font-size: 15pt");
40
+        btnLogIn.setOnAction(e -> {
41
+            mainContent = new Scene(mainContent());
42
+            window.setScene(mainContent);
43
+        });
44
+
45
+        GridPane loginLayout = new GridPane();
46
+        loginLayout.setHgap(12);
47
+        loginLayout.setVgap(12);
48
+        loginLayout.setAlignment(Pos.CENTER);
49
+        loginLayout.getChildren().addAll(loginLabel, field, btnLogIn);
50
+
51
+        loginScene = new Scene(loginLayout,600, 600);
52
+        window.setScene(loginScene);
53
+
54
+
55
+        window.show();
56
+    }
57
+
58
+    private Parent mainContent() {
23 59
         VBox vbox = new VBox(10);
24 60
         vbox.setPrefSize(600, 600);
25 61
 
@@ -66,12 +102,6 @@ public class CashMachineApp extends Application {
66 102
         return vbox;
67 103
     }
68 104
 
69
-    @Override
70
-    public void start(Stage stage) throws Exception {
71
-        stage.setScene(new Scene(createContent()));
72
-        stage.show();
73
-    }
74
-
75 105
     public static void main(String[] args) {
76 106
         launch(args);
77 107
     }

+ 1
- 1
src/main/java/rocks/zipcode/atm/CashMachineSkin.fxml Parādīt failu

@@ -8,7 +8,7 @@
8 8
 
9 9
 <AnchorPane xmlns="http://javafx.com/javafx"
10 10
             xmlns:fx="http://javafx.com/fxml"
11
-            fx:controller="$CONTROLLER_NAME$"
11
+            fx:controller="rocks.zipcode.atm.CashMachineApp"
12 12
             prefHeight="400.0" prefWidth="600.0">
13 13
 
14 14
 </AnchorPane>

+ 20
- 4
src/main/java/rocks/zipcode/atm/bank/Bank.java Parādīt failu

@@ -14,11 +14,27 @@ public class Bank {
14 14
 
15 15
     public Bank() {
16 16
         accounts.put(1000, new BasicAccount(new AccountData(
17
-                1000, "Example 1", "example1@gmail.com", 500
17
+                1000, "Gulliver Grimwald", "ggrim@gmail.com", 500
18
+        )));
19
+
20
+        accounts.put(6666, new PremiumAccount(new AccountData(
21
+                6666, "B. L. Zebub", "mephis@hotmail.com", 666
18 22
         )));
19 23
 
20 24
         accounts.put(2000, new PremiumAccount(new AccountData(
21
-                2000, "Example 2", "example2@gmail.com", 200
25
+                2000, "Richard Ricardo", "rricardo@business.com", 20000
26
+        )));
27
+
28
+        accounts.put(0420, new BasicAccount(new AccountData(
29
+                0420, "Guy Dudero", "lmfao4eva@ymail.com", 69
30
+        )));
31
+
32
+        accounts.put(9999, new PremiumAccount(new AccountData(
33
+                9999, "Final Boss", "bosskey@gmail.com", 9999
34
+        )));
35
+
36
+        accounts.put(0001, new BasicAccount(new AccountData(
37
+                0001, "Bill Busker", "bboy@yandex.com", 0
22 38
         )));
23 39
     }
24 40
 
@@ -41,9 +57,9 @@ public class Bank {
41 57
 
42 58
     public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {
43 59
         Account account = accounts.get(accountData.getId());
44
-        boolean ok = account.withdraw(amount);
60
+        boolean canWithdraw = account.withdraw(amount);
45 61
 
46
-        if (ok) {
62
+        if (canWithdraw) {
47 63
             return ActionResult.success(account.getAccountData());
48 64
         } else {
49 65
             return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());