#3 JenniferChao4 - In Progress...

Open
JenniferChao4 wants to merge 1 commits from JenniferChao4/CashMachine:master into master

BIN
src/main/java/rocks/zipcode/.DS_Store View File


+ 34
- 2
src/main/java/rocks/zipcode/atm/ActionResult.java View File

@@ -1,5 +1,11 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3
+import javafx.scene.control.Alert;
4
+import javafx.scene.control.PasswordField;
5
+import javafx.scene.control.TextInputDialog;
6
+
7
+import java.util.Optional;
8
+
3 9
 /**
4 10
  * @author ZipCodeWilmington
5 11
  */
@@ -32,7 +38,33 @@ public class ActionResult<T> {
32 38
         return new ActionResult<E>(data);
33 39
     }
34 40
 
35
-    public static <E> ActionResult<E> fail(String errorMessage) {
36
-        return new ActionResult<E>(errorMessage);
41
+//    public static <E> ActionResult<E> fail(String errorMessage) {
42
+//        return new ActionResult<E>(errorMessage);
43
+//    }
44
+
45
+    public static void fail(String errorMessage) {
46
+        Alert alert = new Alert(Alert.AlertType.ERROR);
47
+        alert.setTitle("Error Dialog");
48
+        alert.setHeaderText("An error has occurred!");
49
+        alert.setContentText(errorMessage);
50
+
51
+        alert.showAndWait();
37 52
     }
53
+
54
+    public static Optional<String> getInfo(String title, String text) {
55
+        TextInputDialog textDialog = new TextInputDialog();
56
+        textDialog.setTitle(title);
57
+        textDialog.setHeaderText(null);
58
+
59
+        textDialog.setContentText(text);
60
+        return textDialog.showAndWait();
61
+    }
62
+
63
+    public static String getPassword(String prompt) {
64
+        PasswordField passwordField = new PasswordField();
65
+        passwordField.setPromptText(prompt);
66
+
67
+        return passwordField.getText();
68
+    }
69
+
38 70
 }

+ 26
- 4
src/main/java/rocks/zipcode/atm/CashMachine.java View File

@@ -29,19 +29,40 @@ public class CashMachine {
29 29
         );
30 30
     }
31 31
 
32
-    public void deposit(int amount) {
32
+    public void newID(int id) {
33
+        tryCall(
34
+                () -> bank.createAccountById(id),
35
+                update
36
+        );
37
+    }
38
+
39
+    public void newName(int id) {
40
+        tryCall(
41
+                () -> bank.editAccountName1(id),
42
+                update
43
+        );
44
+    }
45
+
46
+    public void newEmail(int id) {
47
+        tryCall(
48
+                () -> bank.editAccountEmail(id),
49
+                update
50
+        );
51
+    }
52
+
53
+    public void deposit(int id) {
33 54
         if (accountData != null) {
34 55
             tryCall(
35
-                    () -> bank.deposit(accountData, amount),
56
+                    () -> bank.deposit(id),
36 57
                     update
37 58
             );
38 59
         }
39 60
     }
40 61
 
41
-    public void withdraw(int amount) {
62
+    public void withdraw(int id) {
42 63
         if (accountData != null) {
43 64
             tryCall(
44
-                    () -> bank.withdraw(accountData, amount),
65
+                    () -> bank.withdraw(id),
45 66
                     update
46 67
             );
47 68
         }
@@ -72,4 +93,5 @@ public class CashMachine {
72 93
             System.out.println("Error: " + e.getMessage());
73 94
         }
74 95
     }
96
+
75 97
 }

+ 49
- 2
src/main/java/rocks/zipcode/atm/CashMachineApp.java View File

@@ -1,5 +1,6 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3
+import javafx.scene.control.Label;
3 4
 import rocks.zipcode.atm.bank.Bank;
4 5
 import javafx.application.Application;
5 6
 import javafx.scene.Parent;
@@ -19,13 +20,15 @@ public class CashMachineApp extends Application {
19 20
     private TextField field = new TextField();
20 21
     private CashMachine cashMachine = new CashMachine(new Bank());
21 22
 
23
+    Stage mainStage;
24
+
22 25
     private Parent createContent() {
23 26
         VBox vbox = new VBox(10);
24
-        vbox.setPrefSize(600, 600);
27
+        vbox.setPrefSize(700, 500);
25 28
 
26 29
         TextArea areaInfo = new TextArea();
27 30
 
28
-        Button btnSubmit = new Button("Set Account ID");
31
+        Button btnSubmit = new Button("Get ID Info");
29 32
         btnSubmit.setOnAction(e -> {
30 33
             int id = Integer.parseInt(field.getText());
31 34
             cashMachine.login(id);
@@ -33,6 +36,31 @@ public class CashMachineApp extends Application {
33 36
             areaInfo.setText(cashMachine.toString());
34 37
         });
35 38
 
39
+        Button btnNewID = new Button("Create New ID");
40
+        btnNewID.setOnAction(e -> {
41
+            int id = Integer.parseInt(field.getText());
42
+            cashMachine.newID(id);
43
+
44
+            areaInfo.setText(cashMachine.toString());
45
+        });
46
+
47
+        Button btnNewName = new Button("Edit name");
48
+        btnNewName.setOnAction(e -> {
49
+            int id = Integer.parseInt(field.getText());
50
+            cashMachine.newName(id);
51
+
52
+            areaInfo.setText(cashMachine.toString());
53
+        });
54
+
55
+        Button btnNewEmail = new Button("Edit email");
56
+        btnNewEmail.setOnAction(e -> {
57
+            int id = Integer.parseInt(field.getText());
58
+            cashMachine.newEmail(id);
59
+
60
+            areaInfo.setText(cashMachine.toString());
61
+        });
62
+
63
+
36 64
         Button btnDeposit = new Button("Deposit");
37 65
         btnDeposit.setOnAction(e -> {
38 66
             int amount = Integer.parseInt(field.getText());
@@ -59,6 +87,9 @@ public class CashMachineApp extends Application {
59 87
         FlowPane flowpane = new FlowPane();
60 88
 
61 89
         flowpane.getChildren().add(btnSubmit);
90
+        flowpane.getChildren().add(btnNewID);
91
+        flowpane.getChildren().add(btnNewName);
92
+        flowpane.getChildren().add(btnNewEmail);
62 93
         flowpane.getChildren().add(btnDeposit);
63 94
         flowpane.getChildren().add(btnWithdraw);
64 95
         flowpane.getChildren().add(btnExit);
@@ -66,9 +97,25 @@ public class CashMachineApp extends Application {
66 97
         return vbox;
67 98
     }
68 99
 
100
+
101
+//    private Parent mainScene() {
102
+//        VBox vbox = new VBox(10);
103
+//        vbox.setPrefSize(700, 500);
104
+//
105
+//        Scene scene1 = createContent();
106
+//
107
+//        Label label1 = new Label("Welcome to the first scene!");
108
+//        Button button1 = new Button("Go to scene 2");
109
+//        button1.setOnAction(e -> mainStage.setScene(createContent()));
110
+//
111
+//        return vbox;
112
+//    }
113
+
69 114
     @Override
70 115
     public void start(Stage stage) throws Exception {
116
+        // mainStage = stage;
71 117
         stage.setScene(new Scene(createContent()));
118
+        // Scene loggedIn = new Scene(mainScene());
72 119
         stage.show();
73 120
     }
74 121
 

+ 1
- 1
src/main/java/rocks/zipcode/atm/bank/Account.java View File

@@ -3,7 +3,7 @@ package rocks.zipcode.atm.bank;
3 3
 /**
4 4
  * @author ZipCodeWilmington
5 5
  */
6
-public abstract class Account {
6
+public class Account {
7 7
 
8 8
     private AccountData accountData;
9 9
 

+ 8
- 4
src/main/java/rocks/zipcode/atm/bank/AccountData.java View File

@@ -10,6 +10,7 @@ public final class AccountData {
10 10
     private final String email;
11 11
 
12 12
     private final int balance;
13
+    // private final String password;
13 14
 
14 15
     AccountData(int id, String name, String email, int balance) {
15 16
         this.id = id;
@@ -34,11 +35,14 @@ public final class AccountData {
34 35
         return balance;
35 36
     }
36 37
 
38
+    // public String getPassword() {return password;}
39
+
37 40
     @Override
38 41
     public String toString() {
39
-        return "Account id: " + id + '\n' +
40
-                "Name: " + name + '\n' +
41
-                "Email: " + email + '\n' +
42
-                "Balance: " + balance;
42
+        return "Hello " + name + "!" + '\n' + '\n' +
43
+                "Account ID: " + id + '\n' +
44
+                "E-mail: " + email + '\n' +
45
+                "Current Balance: " + balance;
43 46
     }
47
+
44 48
 }

+ 130
- 15
src/main/java/rocks/zipcode/atm/bank/Bank.java View File

@@ -1,9 +1,12 @@
1 1
 package rocks.zipcode.atm.bank;
2 2
 
3
+import javafx.scene.control.TextInputDialog;
3 4
 import rocks.zipcode.atm.ActionResult;
4 5
 
5 6
 import java.util.HashMap;
6 7
 import java.util.Map;
8
+import java.util.Optional;
9
+import java.util.Scanner;
7 10
 
8 11
 /**
9 12
  * @author ZipCodeWilmington
@@ -11,42 +14,154 @@ import java.util.Map;
11 14
 public class Bank {
12 15
 
13 16
     private Map<Integer, Account> accounts = new HashMap<>();
17
+    private String name;
18
+    private String email;
19
+
20
+    public String getName() {
21
+        return name;
22
+    }
23
+
24
+    public void setName(String name) {
25
+        this.name = name;
26
+    }
27
+
28
+    public String getEmail() {
29
+        return email;
30
+    }
31
+
32
+    public void setEmail(String email) {
33
+        this.email = email;
34
+    }
14 35
 
15 36
     public Bank() {
16
-        accounts.put(1000, new BasicAccount(new AccountData(
17
-                1000, "Example 1", "example1@gmail.com", 500
18
-        )));
37
+//
38
+//        this.name = "No name available";
39
+//        this.email = "No e-mail available";
40
+
41
+        accounts.put(1000, new BasicAccount(new AccountData(1000, "Example 1", "example1@gmail.com", 500)));
19 42
 
20 43
         accounts.put(2000, new PremiumAccount(new AccountData(
21
-                2000, "Example 2", "example2@gmail.com", 200
22
-        )));
44
+                2000, "Example 2", "example2@gmail.com", 200)));
23 45
     }
24 46
 
25 47
     public ActionResult<AccountData> getAccountById(int id) {
26 48
         Account account = accounts.get(id);
27 49
 
50
+        ActionResult result;
51
+
28 52
         if (account != null) {
29
-            return ActionResult.success(account.getAccountData());
53
+            result = ActionResult.success(account.getAccountData());
30 54
         } else {
31
-            return ActionResult.fail("No account with id: " + id + "\nTry account 1000 or 2000");
55
+            result = null;
56
+            ActionResult.fail("Account ID " + id + " does not exist. Please create new account.");
32 57
         }
58
+
59
+        return result;
60
+    }
61
+
62
+    public ActionResult<AccountData> createAccountById(int id) {
63
+        Account account = accounts.get(id);
64
+
65
+        ActionResult result;
66
+
67
+        if (account == null) {
68
+
69
+            Optional<String> newName = ActionResult.getInfo("Set Name", "Enter name: ");
70
+            Optional<String> newEmail = ActionResult.getInfo("Set E-mail", "Enter e-mail: ");
71
+            Optional<String> initialDeposit = ActionResult.getInfo("Initial Deposit", "Enter initial deposit: ");
72
+
73
+            // Fetch user input
74
+            AccountData accountData = new AccountData(id, newName.get(), newEmail.get(), Integer.parseInt(initialDeposit.get()));
75
+            Account newAccount = new Account(accountData);
76
+
77
+            accounts.put(id, newAccount);
78
+
79
+            result = ActionResult.success(newAccount.getAccountData());
80
+        } else {
81
+            result = null;
82
+            ActionResult.fail("Account ID " + id + " already exists. Please try again.");
83
+        }
84
+
85
+        return result;
86
+    }
87
+
88
+    public ActionResult<AccountData> editAccountName1(int id) {
89
+        Account account = accounts.get(id);
90
+        AccountData accountInfo = account.getAccountData();
91
+
92
+        Optional<String> newName = ActionResult.getInfo("Edit name", "Enter new name: ");
93
+
94
+        AccountData accountData = new AccountData(id, newName.get(), accountInfo.getEmail(), accountInfo.getBalance());
95
+        Account newAccount = new Account(accountData);
96
+
97
+        accounts.put(id, newAccount);
98
+
99
+        return ActionResult.success(newAccount.getAccountData());
33 100
     }
34 101
 
35
-    public ActionResult<AccountData> deposit(AccountData accountData, int amount) {
36
-        Account account = accounts.get(accountData.getId());
37
-        account.deposit(amount);
102
+//    public ActionResult<AccountData> editAccountName(String name) {
103
+//
104
+//        AccountData accountData = new AccountData(55, name, "akjsdkas@kasd.com", 500);
105
+//
106
+//        Account account = new Account(accountData);
107
+//
108
+//        accounts.put(55, account);
109
+//
110
+//        return ActionResult.success(account.getAccountData());
111
+//    }
112
+
113
+    public ActionResult<AccountData> editAccountEmail(int id) {
114
+        Account account = accounts.get(id);
115
+        AccountData accountInfo = account.getAccountData();
38 116
 
39
-        return ActionResult.success(account.getAccountData());
117
+        Optional<String> newEmail = ActionResult.getInfo("Edit E-mail", "Enter new e-mail: ");
118
+
119
+        AccountData accountData = new AccountData(id, accountInfo.getName(), newEmail.get(), accountInfo.getBalance());
120
+        Account newAccount = new Account(accountData);
121
+
122
+        accounts.put(id, newAccount);
123
+
124
+        return ActionResult.success(newAccount.getAccountData());
40 125
     }
41 126
 
42
-    public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {
43
-        Account account = accounts.get(accountData.getId());
127
+    public ActionResult<AccountData> deposit(int id) {
128
+        Account account = accounts.get(id);
129
+        AccountData accountInfo = account.getAccountData();
130
+
131
+        Optional<String> depositAmount = ActionResult.getInfo("Deposit into Account", "Enter amount to deposit: ");
132
+
133
+        AccountData accountData = new AccountData(id, accountInfo.getName(), accountInfo.getEmail(), accountInfo.getBalance() + Integer.parseInt(depositAmount.get()));
134
+        Account newAccount = new Account(accountData);
135
+
136
+        accounts.put(id, newAccount);
137
+
138
+        return ActionResult.success(newAccount.getAccountData());
139
+    }
140
+
141
+    public ActionResult<AccountData> withdraw(int id) {
142
+        Account account = accounts.get(id);
143
+        AccountData accountInfo = account.getAccountData();
144
+
145
+        Optional<String> withdrawAmount = ActionResult.getInfo("Withdraw from Account", "Enter amount to withdraw: ");
146
+
147
+        int amount = Integer.parseInt(withdrawAmount.get());
148
+
44 149
         boolean ok = account.withdraw(amount);
45 150
 
151
+        ActionResult result;
152
+
46 153
         if (ok) {
47
-            return ActionResult.success(account.getAccountData());
154
+            AccountData accountData = new AccountData(id, accountInfo.getName(), accountInfo.getEmail(), accountInfo.getBalance() - Integer.parseInt(withdrawAmount.get()));
155
+            Account newAccount = new Account(accountData);
156
+
157
+            accounts.put(id, newAccount);
158
+
159
+            result = ActionResult.success(newAccount.getAccountData());
48 160
         } else {
49
-            return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
161
+            result = null;
162
+            ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
50 163
         }
164
+
165
+        return result;
51 166
     }
52 167
 }