Parcourir la source

Got login to work

Trinh Tong il y a 6 ans
Parent
révision
e918b28756

+ 18
- 0
src/main/java/rocks/zipcode/atm/CashMachine.java Voir le fichier

@@ -1,9 +1,12 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3
+import javafx.event.ActionEvent;
4
+import javafx.event.EventHandler;
3 5
 import javafx.scene.control.Alert;
4 6
 import rocks.zipcode.atm.bank.AccountData;
5 7
 import rocks.zipcode.atm.bank.Bank;
6 8
 
9
+import java.util.Set;
7 10
 import java.util.function.Consumer;
8 11
 import java.util.function.Supplier;
9 12
 
@@ -24,6 +27,14 @@ public class CashMachine {
24 27
         accountData = data;
25 28
     };
26 29
 
30
+    public boolean checkUserId (int id) {
31
+       return bank.accountExists(id);
32
+    }
33
+
34
+    public boolean comparePassword (int id, String pw) {
35
+        return bank.checkPassword(id, pw);
36
+    }
37
+
27 38
     public void login(int id) {
28 39
         tryCall(
29 40
                 () -> bank.getAccountById(id),
@@ -60,11 +71,17 @@ public class CashMachine {
60 71
         }
61 72
     }
62 73
 
74
+    public Set<Integer> accountSet() {
75
+        return bank.allAccounts();
76
+    }
77
+
63 78
     @Override
64 79
     public String toString() {
65 80
         return accountData != null ? accountData.toString() : "Try account 1000 or 2000 and click submit.";
66 81
     }
67 82
 
83
+
84
+
68 85
     private <T> void tryCall(Supplier<ActionResult<T> > action, Consumer<T> postAction) {
69 86
         try {
70 87
             ActionResult<T> result = action.get();
@@ -81,4 +98,5 @@ public class CashMachine {
81 98
             System.out.println("Error: " + e.getMessage());
82 99
         }
83 100
     }
101
+
84 102
 }

+ 130
- 80
src/main/java/rocks/zipcode/atm/CashMachineApp.java Voir le fichier

@@ -1,22 +1,22 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3 3
 import javafx.event.ActionEvent;
4
-import javafx.event.Event;
5 4
 import javafx.event.EventHandler;
6 5
 import javafx.geometry.Insets;
7 6
 import javafx.geometry.Pos;
8 7
 import javafx.scene.control.*;
9
-import javafx.scene.layout.GridPane;
10
-import javafx.scene.layout.HBox;
8
+import javafx.scene.effect.DropShadow;
9
+import javafx.scene.layout.*;
10
+
11
+import javafx.scene.paint.Color;
12
+import javafx.scene.text.Font;
13
+import javafx.scene.text.Text;
14
+import javafx.scene.text.TextAlignment;
11 15
 import rocks.zipcode.atm.bank.Bank;
12 16
 import javafx.application.Application;
13
-import javafx.scene.Parent;
14 17
 import javafx.scene.Scene;
15
-import javafx.scene.layout.VBox;
16 18
 import javafx.stage.Stage;
17
-import javafx.scene.layout.FlowPane;
18 19
 
19
-import javax.swing.*;
20 20
 
21 21
 /**
22 22
  * @author ZipCodeWilmington
@@ -25,108 +25,115 @@ import javax.swing.*;
25 25
 // Eventually 2 display fields : 1 for account info, 2: account transactions/alerts/status
26 26
 public class CashMachineApp extends Application {
27 27
 
28
+    String checkId;
29
+
30
+
28 31
     private TextField field = new TextField();
29
-    final PasswordField passwordField = new PasswordField();
30 32
     private CashMachine cashMachine = new CashMachine(new Bank());
31 33
 
32
-    Label passwordLabel = new Label ("Password: ");
34
+    private final PasswordField passwordField = new PasswordField();
35
+
36
+
37
+    //Login -> profile, new stages for each, change stages.
38
+    //
39
+
40
+    private Stage mainAccount(int id) {
41
+
42
+        Stage account = new Stage();
43
+
44
+
45
+        account.setTitle("Account Details");
33 46
 
34 47
 
35
-    private Parent createContent() {
36 48
         VBox vbox = new VBox(10);
37
-        vbox.setPrefSize(600, 600);
38 49
 
39
-        TextArea areaInfo = new TextArea();
50
+        Scene accountScene = new Scene(vbox, 500, 600);
40 51
 
41
-        Button btnSubmit = new Button("Set Account ID");
42
-        btnSubmit.setOnAction(e -> {
43
-            int id = Integer.parseInt(field.getText());
44
-            cashMachine.login(id);
52
+        TextArea areaInfo = new TextArea();
53
+        cashMachine.login(id);
45 54
 
46
-            areaInfo.setText(cashMachine.toString());
47
-        });
55
+        areaInfo.setText(cashMachine.toString());
48 56
 
49
-        passwordField.setPromptText("Enter your password");
57
+        vbox.getChildren().addAll(areaInfo);
50 58
 
51
-        Button btnDeposit = new Button("Deposit");
52
-        btnDeposit.setOnAction(e -> {
53
-            float amount = Float.parseFloat(field.getText());
54
-            cashMachine.deposit(amount);
59
+        account.setScene(accountScene);
55 60
 
56
-            areaInfo.setText(cashMachine.toString());
57
-        });
61
+        return account;
62
+    }
58 63
 
59
-        passwordField.setOnAction(new EventHandler<ActionEvent>() {
60
-            @Override
61
-            public void handle(ActionEvent event) {
62 64
 
63
-            }
64
-        });
65
+    private Stage loginStage() {
65 66
 
66
-        Button btnWithdraw = new Button("Withdraw");
67
-        btnWithdraw.setOnAction(e -> {
68
-            float amount = Float.parseFloat(field.getText());
69
-            cashMachine.withdraw(amount);
67
+        Stage login = new Stage();
68
+        login.setTitle("Account Login");
70 69
 
71
-            areaInfo.setText(cashMachine.toString());
72
-        });
73 70
 
74
-        Button btnExit = new Button("Exit");
75
-        btnExit.setOnAction(e -> {
76
-            cashMachine.exit();
71
+//        GridPane grid = new GridPane();
72
+//        grid.setAlignment(Pos.CENTER);
73
+//        grid.setHgap(10);
74
+//        grid.setVgap(10);
77 75
 
78
-            areaInfo.setText(cashMachine.toString());
79
-        });
76
+        VBox vbox = new VBox(10);
77
+        vbox.setSpacing(10);
80 78
 
81
-        MenuItem menuItem1 = new MenuItem("Option 1 ");
82
-        menuItem1.setOnAction(event -> {
79
+        vbox.setPadding(new Insets(25, 25, 25, 25));
83 80
 
84
-        });
81
+        Scene scene = new Scene(vbox, 375, 275);
85 82
 
86
-        MenuButton menuButton = new MenuButton("Options", null, menuItem1);
87
-        // Adding menu options
88
-        //HBox hBox = new HBox(menuButton);
83
+        vbox.setBackground(new Background(new BackgroundFill(Color.color(0.3, 0.6, 0.6), null, null)));
89 84
 
90
-        FlowPane flowpane = new FlowPane();
91 85
 
92
-        flowpane.getChildren().add(btnSubmit);
93
-        flowpane.getChildren().add(btnDeposit);
94
-        flowpane.getChildren().add(btnWithdraw);
95
-        flowpane.getChildren().add(btnExit);
86
+        Text title = new Text("ZipCloudBank");
87
+        title.setTextAlignment(TextAlignment.CENTER);
88
+        title.setFont(Font.font("Verdana", 25));
89
+        title.setFill(Color.WHITE);
96 90
 
97
-        // testing password field
98
-        PasswordField passwordField = new PasswordField();
91
+        Text desc = new Text(" ");
92
+        desc.setFont(Font.font("Verdana", 10));
99 93
 
100
-        // HBox hbox = new HBox(passwordField);
101 94
 
102
-        vbox.getChildren().addAll(field, passwordLabel, passwordField, flowpane, areaInfo);
103
-        return vbox;
104
-    }
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);
105 100
 
101
+//        grid.add(title, 0,0);
106 102
 
107
-    private Stage popUpStage() {
108
-        Stage login = new Stage();
109
-        login.setTitle("Account Login");
103
+        Label userName = new Label ("Enter Account Number");
104
+        userName.setFont(Font.font("Verdana", 10));
105
+        userName.setLabelFor(field);
106
+        userName.setTextFill(Color.WHITESMOKE);
107
+//        grid.add(userName, 0, 2);
108
+//        grid.add(field, 0, 3);
110 109
 
111
-        GridPane grid = new GridPane();
112
-        grid.setAlignment(Pos.CENTER);
113
-        grid.setHgap(10);
114
-        grid.setVgap(10);
115 110
 
116
-        grid.setPadding(new Insets(25, 25, 25, 25));
117 111
 
118
-        Scene scene = new Scene(grid, 300, 275);
112
+        Button btn = new Button("Log in");
113
+        HBox hbBtn = new HBox(10);
114
+        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
115
+        hbBtn.getChildren().add(btn);
116
+//        grid.add(hbBtn, 0, 6);
119 117
 
120
-        Label userName = new Label ("Select Account");
121
-        grid.add(userName, 0, 1);
122
-        MenuItem menuItem1 = new MenuItem("Option 1 ");
123
-        menuItem1.setOnAction(event -> {
118
+        vbox.getChildren().addAll(title, desc, userName, field, btn, hbBtn);
124 119
 
120
+        btn.setOnAction(new EventHandler<ActionEvent>() {
121
+            @Override
122
+            public void handle(ActionEvent event) {
123
+                Integer id = Integer.parseInt(field.getText());
124
+
125
+                if (cashMachine.checkUserId(id)) {
126
+                    login.hide();
127
+                    mainAccount(id).show();
128
+                } else {
129
+                    Alert invalidID = new Alert(Alert.AlertType.INFORMATION);
130
+                    invalidID.setContentText("Invalid ID number.");
131
+                    invalidID.showAndWait();
132
+                    field.clear();
133
+                }
134
+            }
125 135
         });
126 136
 
127
-        MenuButton menuButton = new MenuButton("Options", null, menuItem1);
128
-        grid.add(menuButton, 2, 1);
129
-
130 137
 
131 138
         login.setScene(scene);
132 139
 
@@ -134,23 +141,66 @@ public class CashMachineApp extends Application {
134 141
 
135 142
     }
136 143
 
144
+    public String getPassword() {
145
+        return passwordField.getText();
146
+    }
147
+
148
+    public int getId(String id) {
149
+        return Integer.parseInt(id);
150
+    }
151
+
137 152
     @Override
138 153
     public void start(Stage stage) throws Exception {
139 154
 
140
-        stage.setScene(new Scene(createContent()));
141
-        Stage login = popUpStage();
142
-
143
-        stage.show();
155
+        Stage login = loginStage();
144 156
 
145 157
         login.alwaysOnTopProperty();
146 158
 
147 159
         login.show();
148 160
 
149
-
150
-
151 161
     }
152 162
 
153 163
     public static void main(String[] args) {
154 164
         launch(args);
155 165
     }
166
+
167
+
156 168
 }
169
+
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
+

+ 18
- 0
src/main/java/rocks/zipcode/atm/bank/Bank.java Voir le fichier

@@ -7,6 +7,7 @@ import rocks.zipcode.atm.ActionResult;
7 7
 import java.awt.event.ActionEvent;
8 8
 import java.util.HashMap;
9 9
 import java.util.Map;
10
+import java.util.Set;
10 11
 
11 12
 /**
12 13
  * @author ZipCodeWilmington
@@ -34,6 +35,23 @@ public class Bank {
34 35
         }
35 36
     }
36 37
 
38
+    public Set<Integer> allAccounts() {
39
+        return passwords.keySet();
40
+    }
41
+
42
+    public boolean accountExists(Integer id) {
43
+        boolean hasAccount = false;
44
+
45
+        for (Integer key: allAccounts()) {
46
+            if (key.equals(id)) {
47
+                hasAccount = true;
48
+                break;
49
+            }
50
+        }
51
+
52
+        return hasAccount;
53
+    }
54
+
37 55
     public boolean checkPassword(int id, String password) {
38 56
         boolean correctPassword =  true;
39 57