Selaa lähdekoodia

Alerts on exit

Trinh Tong 6 vuotta sitten
vanhempi
commit
e54aceabdf

+ 1
- 1
src/main/java/rocks/zipcode/atm/CashMachine.java Näytä tiedosto

@@ -77,7 +77,7 @@ public class CashMachine {
77 77
 
78 78
     @Override
79 79
     public String toString() {
80
-        return accountData != null ? accountData.toString() : "Try account 1000 or 2000 and click submit.";
80
+        return accountData != null ? accountData.toString() : "Bye!";
81 81
     }
82 82
 
83 83
 

+ 98
- 72
src/main/java/rocks/zipcode/atm/CashMachineApp.java Näytä tiedosto

@@ -5,9 +5,7 @@ import javafx.event.EventHandler;
5 5
 import javafx.geometry.Insets;
6 6
 import javafx.geometry.Pos;
7 7
 import javafx.scene.control.*;
8
-import javafx.scene.effect.DropShadow;
9 8
 import javafx.scene.layout.*;
10
-
11 9
 import javafx.scene.paint.Color;
12 10
 import javafx.scene.text.Font;
13 11
 import javafx.scene.text.Text;
@@ -25,9 +23,6 @@ import javafx.stage.Stage;
25 23
 // Eventually 2 display fields : 1 for account info, 2: account transactions/alerts/status
26 24
 public class CashMachineApp extends Application {
27 25
 
28
-    String checkId;
29
-
30
-
31 26
     private TextField field = new TextField();
32 27
     private CashMachine cashMachine = new CashMachine(new Bank());
33 28
 
@@ -46,15 +41,55 @@ public class CashMachineApp extends Application {
46 41
 
47 42
 
48 43
         VBox vbox = new VBox(10);
44
+        vbox.setBackground(new Background(
45
+                new BackgroundFill(
46
+                        Color.color(0.4, 0.6, 0.9), null, null)));
49 47
 
50
-        Scene accountScene = new Scene(vbox, 500, 600);
51 48
 
52 49
         TextArea areaInfo = new TextArea();
50
+
51
+        Button btnDeposit = new Button("Deposit");
52
+        btnDeposit.setOnAction(e -> {
53
+            int amount = Integer.parseInt(field.getText());
54
+            cashMachine.deposit(amount);
55
+
56
+            areaInfo.setText(cashMachine.toString());
57
+        });
58
+
59
+        Button btnWithdraw = new Button("Withdraw");
60
+        btnWithdraw.setOnAction(e -> {
61
+            int amount = Integer.parseInt(field.getText());
62
+            cashMachine.withdraw(amount);
63
+
64
+            areaInfo.setText(cashMachine.toString());
65
+        });
66
+
67
+        Button btnExit = new Button("Exit");
68
+        btnExit.setOnAction(new EventHandler<ActionEvent>() {
69
+            @Override
70
+            public void handle(ActionEvent event) {
71
+                cashMachine.exit();
72
+                areaInfo.setText(cashMachine.toString());
73
+
74
+                Alert exitAlert = new Alert(Alert.AlertType.INFORMATION);
75
+                exitAlert.setContentText("Now Exiting Application.");
76
+                exitAlert.showAndWait();
77
+
78
+                Stage stage = (Stage) btnExit.getScene().getWindow();
79
+                stage.close();
80
+            }
81
+        });
82
+
83
+        //
84
+
85
+        Scene accountScene = new Scene(vbox, 400, 500);
86
+
87
+
53 88
         cashMachine.login(id);
54 89
 
55 90
         areaInfo.setText(cashMachine.toString());
56 91
 
57
-        vbox.getChildren().addAll(areaInfo);
92
+        vbox.getChildren().addAll(areaInfo, field, btnDeposit, btnWithdraw, btnExit);
58 93
 
59 94
         account.setScene(accountScene);
60 95
 
@@ -68,68 +103,103 @@ public class CashMachineApp extends Application {
68 103
         login.setTitle("Account Login");
69 104
 
70 105
 
71
-//        GridPane grid = new GridPane();
72
-//        grid.setAlignment(Pos.CENTER);
73
-//        grid.setHgap(10);
74
-//        grid.setVgap(10);
106
+        GridPane grid = new GridPane();
107
+        grid.setAlignment(Pos.CENTER);
108
+        grid.setHgap(10);
109
+        grid.setVgap(10);
75 110
 
76 111
         VBox vbox = new VBox(10);
77 112
         vbox.setSpacing(10);
78 113
 
79 114
         vbox.setPadding(new Insets(25, 25, 25, 25));
80 115
 
81
-        Scene scene = new Scene(vbox, 375, 275);
82
-
83
-        vbox.setBackground(new Background(new BackgroundFill(Color.color(0.3, 0.6, 0.6), null, null)));
116
+        Scene scene = new Scene(vbox, 375, 350);
84 117
 
118
+        vbox.setBackground(new Background(
119
+                new BackgroundFill(
120
+                        Color.color(0.4, 0.6, 0.9), null, null)));
85 121
 
122
+        // Title
86 123
         Text title = new Text("ZipCloudBank");
87 124
         title.setTextAlignment(TextAlignment.CENTER);
88 125
         title.setFont(Font.font("Verdana", 25));
89 126
         title.setFill(Color.WHITE);
90 127
 
128
+        grid.add(title, 0,0);
129
+
130
+        // Desc
91 131
         Text desc = new Text(" ");
92 132
         desc.setFont(Font.font("Verdana", 10));
133
+        grid.add(desc, 0, 1);
93 134
 
94 135
 
95
-        DropShadow ds = new DropShadow();
96
-        ds.setOffsetY(4.0f);
97
-        ds.setOffsetX(4.0f);
98
-        ds.setColor(Color.color(0.4f, 0.4f, 0.4f));
99
-//        title.setEffect(ds);
100
-
101
-//        grid.add(title, 0,0);
102
-
136
+        // Enter username
103 137
         Label userName = new Label ("Enter Account Number");
104 138
         userName.setFont(Font.font("Verdana", 10));
105 139
         userName.setLabelFor(field);
106 140
         userName.setTextFill(Color.WHITESMOKE);
107
-//        grid.add(userName, 0, 2);
108
-//        grid.add(field, 0, 3);
109 141
 
142
+        grid.add(userName, 0, 2);
143
+        grid.add(field, 0, 3);
144
+
145
+        // Enter PW
146
+        TextField pw = new TextField();
147
+        Label pwLabel = new Label("Enter Password");
148
+        pwLabel.setFont(Font.font("Verdana", 10));
149
+        pwLabel.setTextFill(Color.WHITESMOKE);
110 150
 
151
+        grid.add(pwLabel, 0, 4);
152
+        grid.add(pw, 0, 5);
111 153
 
154
+        // Login Button
112 155
         Button btn = new Button("Log in");
113 156
         HBox hbBtn = new HBox(10);
114 157
         hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
115 158
         hbBtn.getChildren().add(btn);
116
-//        grid.add(hbBtn, 0, 6);
117 159
 
118
-        vbox.getChildren().addAll(title, desc, userName, field, btn, hbBtn);
160
+        grid.add(btn, 0, 6);
161
+
162
+//        // new user text
163
+//        Text newUser = new Text("\nNew user?");
164
+//        newUser.setFont(Font.font("Verdana", 10));
165
+//        newUser.setFill(Color.WHITESMOKE);
166
+//
167
+//        grid.add(newUser, 0, 8);
168
+//
169
+//        // New user button
170
+//        Button newbtn = new Button("Create Account");
171
+//        HBox newHBox = new HBox(10);
172
+//        newHBox.setAlignment(Pos.CENTER);
173
+//        newHBox.getChildren().add(newbtn);
174
+//
175
+//        grid.add(newHBox, 0,9);
176
+
119 177
 
178
+        vbox.getChildren().addAll(grid);
179
+
180
+        // -------- Button actions ---------
181
+
182
+        // Login button action
120 183
         btn.setOnAction(new EventHandler<ActionEvent>() {
121 184
             @Override
122 185
             public void handle(ActionEvent event) {
123 186
                 Integer id = Integer.parseInt(field.getText());
124 187
 
125
-                if (cashMachine.checkUserId(id)) {
188
+                if (cashMachine.checkUserId(id) && cashMachine.comparePassword(id, pw.getText())) {
126 189
                     login.hide();
127 190
                     mainAccount(id).show();
128
-                } else {
191
+                } else if (!cashMachine.checkUserId(id)) {
129 192
                     Alert invalidID = new Alert(Alert.AlertType.INFORMATION);
130 193
                     invalidID.setContentText("Invalid ID number.");
131 194
                     invalidID.showAndWait();
132 195
                     field.clear();
196
+                    pw.clear();
197
+                } else if (!cashMachine.comparePassword(id, pw.getText())) {
198
+                    Alert invalidID = new Alert(Alert.AlertType.INFORMATION);
199
+                    invalidID.setContentText("Wrong password.");
200
+                    invalidID.showAndWait();
201
+                    field.clear();
202
+                    pw.clear();
133 203
                 }
134 204
             }
135 205
         });
@@ -141,13 +211,6 @@ public class CashMachineApp extends Application {
141 211
 
142 212
     }
143 213
 
144
-    public String getPassword() {
145
-        return passwordField.getText();
146
-    }
147
-
148
-    public int getId(String id) {
149
-        return Integer.parseInt(id);
150
-    }
151 214
 
152 215
     @Override
153 216
     public void start(Stage stage) throws Exception {
@@ -167,40 +230,3 @@ public class CashMachineApp extends Application {
167 230
 
168 231
 }
169 232
 
170
-
171
-//        login.setOnCloseRequest( event -> {
172
-//            // when button is clicked then open new one
173
-//            stage.show();
174
-//        });
175
-
176
-//         btn.setOnAction(e -> {
177
-//            if (cashMachine.checkUserId(getId())) {
178
-//
179
-//                if (cashMachine.comparePassword(getId(), passwordAccount)) {
180
-//                    login.close();
181
-//
182
-//                } else {
183
-//                    actionText.setText("Password was incorrect.");
184
-//                    grid.add(actionText, 1, 6);
185
-//                }
186
-//            } else {
187
-//                actionText.setText("Invalid Id.");
188
-//                grid.add(actionText, 1, 6);
189
-//            }
190
-//        });
191
-
192
-
193
-
194
-
195
-//int loginId = Integer.parseInt(loginAccount);
196
-
197
-
198
-//        Label passWord = new Label("Password");
199
-//        grid.add(passWord, 0, 3);
200
-//
201
-//
202
-//        PasswordField passwordField = new PasswordField();
203
-//        grid.add(passwordField, 0, 4);
204
-//
205
-//        String passwordAccount = passwordField.getText();
206
-

+ 2
- 0
src/main/java/rocks/zipcode/atm/bank/AccountData.java Näytä tiedosto

@@ -16,6 +16,7 @@ public final class AccountData {
16 16
         this.name = name;
17 17
         this.email = email;
18 18
         this.balance = balance;
19
+
19 20
     }
20 21
 
21 22
     public int getId() {
@@ -38,6 +39,7 @@ public final class AccountData {
38 39
         return balance;
39 40
     }
40 41
 
42
+
41 43
     @Override
42 44
     public String toString() {
43 45
         return "Account id: " + id + '\n' +