#7 Nick Satinover

Offen
nsatinover möchte 5 Commits von nsatinover/CashMachine:master nach master zusammenführen

+ 4
- 0
src/main/java/rocks/zipcode/atm/ActionResult.java Datei anzeigen

@@ -32,6 +32,10 @@ public class ActionResult<T> {
32 32
         return new ActionResult<E>(data);
33 33
     }
34 34
 
35
+    public static <E> ActionResult<E> overdrawn(String overdrawnMessage) {
36
+        return new ActionResult<E>(overdrawnMessage);
37
+    }
38
+
35 39
     public static <E> ActionResult<E> fail(String errorMessage) {
36 40
         return new ActionResult<E>(errorMessage);
37 41
     }

+ 15
- 3
src/main/java/rocks/zipcode/atm/CashMachine.java Datei anzeigen

@@ -23,13 +23,14 @@ public class CashMachine {
23 23
     };
24 24
 
25 25
     public void login(int id) {
26
+
26 27
         tryCall(
27 28
                 () -> bank.getAccountById(id),
28 29
                 update
29 30
         );
30 31
     }
31 32
 
32
-    public void deposit(int amount) {
33
+    public void deposit(float amount) {
33 34
         if (accountData != null) {
34 35
             tryCall(
35 36
                     () -> bank.deposit(accountData, amount),
@@ -38,7 +39,7 @@ public class CashMachine {
38 39
         }
39 40
     }
40 41
 
41
-    public void withdraw(int amount) {
42
+    public void withdraw(float amount) {
42 43
         if (accountData != null) {
43 44
             tryCall(
44 45
                     () -> bank.withdraw(accountData, amount),
@@ -51,11 +52,22 @@ public class CashMachine {
51 52
         if (accountData != null) {
52 53
             accountData = null;
53 54
         }
55
+
56
+    }
57
+
58
+    public boolean isValidId(int id){
59
+        if (bank.isValidId(id)) {
60
+            return true;
61
+        } else {
62
+            return false;
63
+        }
54 64
     }
55 65
 
56 66
     @Override
57 67
     public String toString() {
58
-        return accountData != null ? accountData.toString() : "Try account 1000 or 2000 and click submit.";
68
+//
69
+        // if (accountData != null){isValidId();}
70
+        return accountData != null ? accountData.toString() : "Set Account ID:\n1000\n2000\n6666\n7337";
59 71
     }
60 72
 
61 73
     private <T> void tryCall(Supplier<ActionResult<T> > action, Consumer<T> postAction) {

+ 78
- 23
src/main/java/rocks/zipcode/atm/CashMachineApp.java Datei anzeigen

@@ -1,5 +1,7 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3
+import javafx.geometry.Pos;
4
+import javafx.scene.layout.GridPane;
3 5
 import rocks.zipcode.atm.bank.Bank;
4 6
 import javafx.application.Application;
5 7
 import javafx.scene.Parent;
@@ -7,71 +9,124 @@ import javafx.scene.Scene;
7 9
 import javafx.scene.control.Button;
8 10
 import javafx.scene.control.TextArea;
9 11
 import javafx.scene.control.TextField;
10
-import javafx.scene.layout.VBox;
11 12
 import javafx.stage.Stage;
12
-import javafx.scene.layout.FlowPane;
13
+import javafx.scene.paint.Color;
13 14
 
14 15
 /**
15 16
  * @author ZipCodeWilmington
16 17
  */
17 18
 public class CashMachineApp extends Application {
19
+    Color blue = Color.rgb(0,0,255);
18 20
 
19
-    private TextField field = new TextField();
20 21
     private CashMachine cashMachine = new CashMachine(new Bank());
21 22
 
23
+    private TextField setAccountIdField = new TextField();
24
+    private TextField setDepositField = new TextField();
25
+    private TextField setWithdrawField = new TextField();
26
+
22 27
     private Parent createContent() {
23
-        VBox vbox = new VBox(10);
24
-        vbox.setPrefSize(600, 600);
28
+        setAccountIdField.setMinSize(250, 40);
29
+        setDepositField.setMinSize(250, 40);
30
+        setWithdrawField.setMinSize(250, 40);
25 31
 
26
-        TextArea areaInfo = new TextArea();
32
+        GridPane gridPane = new GridPane();
33
+        gridPane.setPrefSize(500, 400);
34
+        gridPane.setAlignment(Pos.CENTER);
35
+        gridPane.setStyle("-fx-background-color: Green;");
27 36
 
28
-        Button btnSubmit = new Button("Set Account ID");
29
-        btnSubmit.setOnAction(e -> {
30
-            int id = Integer.parseInt(field.getText());
31
-            cashMachine.login(id);
37
+        TextArea areaInfo = new TextArea();
38
+        areaInfo.setMaxSize(250, 160);
32 39
 
33
-            areaInfo.setText(cashMachine.toString());
34
-        });
35 40
 
36 41
         Button btnDeposit = new Button("Deposit");
42
+        btnDeposit.setMinSize(250, 40);
43
+        btnDeposit.setTextFill(blue);
44
+        btnDeposit.setDisable(true);
37 45
         btnDeposit.setOnAction(e -> {
38
-            int amount = Integer.parseInt(field.getText());
46
+            float amount = Float.parseFloat(setWithdrawField.getText());
39 47
             cashMachine.deposit(amount);
40
-
41 48
             areaInfo.setText(cashMachine.toString());
42 49
         });
43 50
 
44 51
         Button btnWithdraw = new Button("Withdraw");
52
+        btnWithdraw.setMinSize(250, 40);
53
+        btnWithdraw.setTextFill(blue);
54
+        btnWithdraw.setDisable(true);
45 55
         btnWithdraw.setOnAction(e -> {
46
-            int amount = Integer.parseInt(field.getText());
56
+            float amount = Float.parseFloat(setWithdrawField.getText());
47 57
             cashMachine.withdraw(amount);
48
-
49 58
             areaInfo.setText(cashMachine.toString());
50 59
         });
51 60
 
52 61
         Button btnExit = new Button("Exit");
62
+        btnExit.setMinSize(250, 160);
63
+        btnExit.setTextFill(blue);
64
+        btnExit.setDisable(true);
53 65
         btnExit.setOnAction(e -> {
66
+            btnDeposit.setDisable(true);
67
+            btnWithdraw.setDisable(true);
68
+            btnExit.setDisable(true);
69
+            resetOnExit();
54 70
             cashMachine.exit();
71
+            areaInfo.setText(cashMachine.toString());
72
+        });
73
+
74
+        Button btnSubmit = new Button("Set Account ID");
75
+        btnSubmit.setMinSize(250, 40);
76
+        btnSubmit.setTextFill(blue);
77
+        btnSubmit.setOnAction(e -> {
78
+            int id = Integer.parseInt(setAccountIdField.getText());
79
+            if (isValidId(id)){
80
+                btnDeposit.setDisable(false);
81
+                btnWithdraw.setDisable(false);
82
+                btnExit.setDisable(false);
83
+            }
84
+            else {
85
+                btnDeposit.setDisable(true);
86
+                btnWithdraw.setDisable(true);
87
+                btnExit.setDisable(true);
88
+            }
89
+            cashMachine.login(id);
55 90
 
56 91
             areaInfo.setText(cashMachine.toString());
57 92
         });
58 93
 
59
-        FlowPane flowpane = new FlowPane();
60 94
 
61
-        flowpane.getChildren().add(btnSubmit);
62
-        flowpane.getChildren().add(btnDeposit);
63
-        flowpane.getChildren().add(btnWithdraw);
64
-        flowpane.getChildren().add(btnExit);
65
-        vbox.getChildren().addAll(field, flowpane, areaInfo);
66
-        return vbox;
95
+        GridPane.setConstraints(btnSubmit, 0, 0);
96
+        GridPane.setConstraints(btnDeposit, 0, 1);
97
+        GridPane.setConstraints(btnWithdraw, 0, 2);
98
+        GridPane.setConstraints(btnExit, 0, 3);
99
+        GridPane.setConstraints(setAccountIdField, 1, 0);
100
+        setWithdrawField.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
101
+        GridPane.setConstraints(setWithdrawField, 1, 1, 1, 2);
102
+        GridPane.setConstraints(areaInfo, 1, 3);
103
+        gridPane.getChildren().addAll(btnSubmit, btnDeposit, btnWithdraw, btnExit,
104
+                setAccountIdField, setWithdrawField, areaInfo);
105
+        return gridPane;
67 106
     }
68 107
 
69 108
     @Override
70 109
     public void start(Stage stage) throws Exception {
71 110
         stage.setScene(new Scene(createContent()));
111
+        stage.setTitle("Zip Code ATM");
72 112
         stage.show();
73 113
     }
74 114
 
115
+    public boolean isValidId(int id){
116
+        if (cashMachine.isValidId(id)) {
117
+            return true;
118
+        }
119
+        else {
120
+            return false;
121
+        }
122
+    }
123
+
124
+    public void resetOnExit(){
125
+        setAccountIdField.clear();
126
+        setDepositField.clear();
127
+        setWithdrawField.clear();
128
+    }
129
+
75 130
     public static void main(String[] args) {
76 131
         launch(args);
77 132
     }

+ 5
- 5
src/main/java/rocks/zipcode/atm/bank/Account.java Datei anzeigen

@@ -15,11 +15,11 @@ public abstract class Account {
15 15
         return accountData;
16 16
     }
17 17
 
18
-    public void deposit(int amount) {
18
+    public void deposit(float amount) {
19 19
         updateBalance(getBalance() + amount);
20 20
     }
21 21
 
22
-    public boolean withdraw(int amount) {
22
+    public boolean withdraw(float amount) {
23 23
         if (canWithdraw(amount)) {
24 24
             updateBalance(getBalance() - amount);
25 25
             return true;
@@ -28,15 +28,15 @@ public abstract class Account {
28 28
         }
29 29
     }
30 30
 
31
-    protected boolean canWithdraw(int amount) {
31
+    protected boolean canWithdraw(float amount) {
32 32
         return getBalance() >= amount;
33 33
     }
34 34
 
35
-    public int getBalance() {
35
+    public float getBalance() {
36 36
         return accountData.getBalance();
37 37
     }
38 38
 
39
-    private void updateBalance(int newBalance) {
39
+    private void updateBalance(float newBalance) {
40 40
         accountData = new AccountData(accountData.getId(), accountData.getName(), accountData.getEmail(),
41 41
                 newBalance);
42 42
     }

+ 3
- 3
src/main/java/rocks/zipcode/atm/bank/AccountData.java Datei anzeigen

@@ -9,9 +9,9 @@ public final class AccountData {
9 9
     private final String name;
10 10
     private final String email;
11 11
 
12
-    private final int balance;
12
+    private final float balance;
13 13
 
14
-    AccountData(int id, String name, String email, int balance) {
14
+    AccountData(int id, String name, String email, float balance) {
15 15
         this.id = id;
16 16
         this.name = name;
17 17
         this.email = email;
@@ -30,7 +30,7 @@ public final class AccountData {
30 30
         return email;
31 31
     }
32 32
 
33
-    public int getBalance() {
33
+    public float getBalance() {
34 34
         return balance;
35 35
     }
36 36
 

+ 27
- 4
src/main/java/rocks/zipcode/atm/bank/Bank.java Datei anzeigen

@@ -1,6 +1,8 @@
1 1
 package rocks.zipcode.atm.bank;
2 2
 
3 3
 import rocks.zipcode.atm.ActionResult;
4
+import rocks.zipcode.atm.CashMachine;
5
+import rocks.zipcode.atm.CashMachineApp;
4 6
 
5 7
 import java.util.HashMap;
6 8
 import java.util.Map;
@@ -20,33 +22,54 @@ public class Bank {
20 22
         accounts.put(2000, new PremiumAccount(new AccountData(
21 23
                 2000, "Example 2", "example2@gmail.com", 200
22 24
         )));
25
+
26
+        accounts.put(6666, new PremiumAccount(new AccountData(
27
+                6666, "Nick Satinover", "nsatinover@gmail.com", 20000
28
+        )));
29
+
30
+        accounts.put(7337, new BasicAccount(new AccountData(
31
+                7337, "Kris HaHaYourBroke", "kris@onedollar.com", 1
32
+        )));
23 33
     }
24 34
 
25 35
     public ActionResult<AccountData> getAccountById(int id) {
26 36
         Account account = accounts.get(id);
27
-
28 37
         if (account != null) {
29 38
             return ActionResult.success(account.getAccountData());
30 39
         } else {
31
-            return ActionResult.fail("No account with id: " + id + "\nTry account 1000 or 2000");
40
+            return ActionResult.fail("No account with id: " + id + "\nTry account 1000, 2000, 6666, 7337");
32 41
         }
33 42
     }
34 43
 
35
-    public ActionResult<AccountData> deposit(AccountData accountData, int amount) {
44
+    public ActionResult<AccountData> deposit(AccountData accountData, float amount) {
36 45
         Account account = accounts.get(accountData.getId());
37 46
         account.deposit(amount);
38 47
 
39 48
         return ActionResult.success(account.getAccountData());
40 49
     }
41 50
 
42
-    public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {
51
+    public ActionResult<AccountData> withdraw(AccountData accountData, float amount) {
43 52
         Account account = accounts.get(accountData.getId());
44 53
         boolean ok = account.withdraw(amount);
45 54
 
46 55
         if (ok) {
56
+            if (account.getBalance() < 0) {
57
+                Overdraft.popupMessage("Overdraft", "Warning, your account is now in overdraft");
58
+            }
47 59
             return ActionResult.success(account.getAccountData());
48 60
         } else {
61
+            Overdraft.popupMessage("Declined", "Declined, account contains insufficient funds");
49 62
             return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
50 63
         }
51 64
     }
65
+
66
+    public boolean isValidId(int id){
67
+        Account account = accounts.get(id);
68
+        if (account != null) {
69
+            return true;
70
+        } else {
71
+            return false;
72
+        }
73
+    }
74
+
52 75
 }

+ 54
- 0
src/main/java/rocks/zipcode/atm/bank/Overdraft.java Datei anzeigen

@@ -0,0 +1,54 @@
1
+package rocks.zipcode.atm.bank;
2
+
3
+import javafx.scene.control.Button;
4
+import javafx.scene.control.Label;
5
+import javafx.stage.Stage;
6
+import javafx.scene.*;
7
+import javafx.scene.layout.*;
8
+import javafx.scene.control.*;
9
+import javafx.geometry.*;
10
+import javafx.stage.Modality;
11
+
12
+import java.awt.*;
13
+
14
+public class Overdraft {
15
+
16
+    public static void popupMessage(String title, String message){
17
+        // "Warning, your account is now in popupMessage";
18
+        Stage window = new Stage();
19
+        window.initModality(Modality.APPLICATION_MODAL); //Block Input until window closed/ resolved
20
+        window.setTitle(title);
21
+        window.setMinWidth(250);
22
+
23
+        Label label = new Label();
24
+        label.setText(message);
25
+
26
+        Button closeButton = new Button("Close");
27
+        closeButton.setOnAction(e -> window.close());
28
+
29
+        VBox layout = new VBox(10);
30
+        layout.getChildren().addAll(label, closeButton);
31
+        layout.setAlignment(Pos.CENTER);
32
+
33
+        Scene scene = new Scene(layout);
34
+        window.setScene(scene);
35
+        window.showAndWait();
36
+
37
+    }
38
+}
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+

+ 2
- 2
src/main/java/rocks/zipcode/atm/bank/PremiumAccount.java Datei anzeigen

@@ -5,14 +5,14 @@ package rocks.zipcode.atm.bank;
5 5
  */
6 6
 public class PremiumAccount extends Account {
7 7
 
8
-    private static final int OVERDRAFT_LIMIT = 100;
8
+    private static final int OVERDRAFT_LIMIT = 1000;
9 9
 
10 10
     public PremiumAccount(AccountData accountData) {
11 11
         super(accountData);
12 12
     }
13 13
 
14 14
     @Override
15
-    protected boolean canWithdraw(int amount) {
15
+    protected boolean canWithdraw(float amount) {
16 16
         return getBalance() + OVERDRAFT_LIMIT >= amount;
17 17
     }
18 18
 }