#2 demetrm - WIP

Otwarty
demetrm chce scalić 6 commity/ów z demetrm/CashMachine:master do master

BIN
src/main/java/rocks/zipcode/.DS_Store Wyświetl plik


+ 34
- 2
src/main/java/rocks/zipcode/atm/CashMachine.java Wyświetl plik

@@ -1,8 +1,10 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3
+import rocks.zipcode.atm.bank.Account;
3 4
 import rocks.zipcode.atm.bank.AccountData;
4 5
 import rocks.zipcode.atm.bank.Bank;
5 6
 
7
+import java.util.ArrayList;
6 8
 import java.util.function.Consumer;
7 9
 import java.util.function.Supplier;
8 10
 
@@ -29,7 +31,7 @@ public class CashMachine {
29 31
         );
30 32
     }
31 33
 
32
-    public void deposit(int amount) {
34
+    public void deposit(double amount) {
33 35
         if (accountData != null) {
34 36
             tryCall(
35 37
                     () -> bank.deposit(accountData, amount),
@@ -38,7 +40,7 @@ public class CashMachine {
38 40
         }
39 41
     }
40 42
 
41
-    public void withdraw(int amount) {
43
+    public void withdraw(double amount) {
42 44
         if (accountData != null) {
43 45
             tryCall(
44 46
                     () -> bank.withdraw(accountData, amount),
@@ -53,6 +55,36 @@ public class CashMachine {
53 55
         }
54 56
     }
55 57
 
58
+    public String accountIdToString(){
59
+        return "Account id: " + accountData.getId() + "\n";
60
+    }
61
+
62
+    public String nameToString(){
63
+        return "Name : " + accountData.getName() + "\n";
64
+    }
65
+
66
+    public String emailToString(){
67
+        return "Email : " + accountData.getEmail() + "\n";
68
+    }
69
+
70
+    public String balanceToString(){
71
+        return "Balance : " + accountData.getBalance() + "\n";
72
+    }
73
+
74
+    public ArrayList<String> listAccounts(){
75
+        return bank.listAccounts();
76
+    }
77
+
78
+    public int newBasicAccount(String name, String email, double balance) {
79
+        bank.addBasicAccount(name,email,balance);
80
+        return bank.newId();
81
+    }
82
+
83
+    public int newPremiumAccount(String name, String email, double balance) {
84
+        bank.addPremiumAccount(name,email,balance);
85
+        return bank.newId();
86
+    }
87
+
56 88
     @Override
57 89
     public String toString() {
58 90
         return accountData != null ? accountData.toString() : "Try account 1000 or 2000 and click submit.";

+ 163
- 40
src/main/java/rocks/zipcode/atm/CashMachineApp.java Wyświetl plik

@@ -1,78 +1,201 @@
1 1
 package rocks.zipcode.atm;
2 2
 
3
-import rocks.zipcode.atm.bank.Bank;
4 3
 import javafx.application.Application;
4
+import javafx.geometry.Insets;
5
+import javafx.geometry.Pos;
5 6
 import javafx.scene.Parent;
6 7
 import javafx.scene.Scene;
7
-import javafx.scene.control.Button;
8
-import javafx.scene.control.TextArea;
9
-import javafx.scene.control.TextField;
8
+import javafx.scene.control.*;
9
+import javafx.scene.layout.FlowPane;
10
+import javafx.scene.layout.GridPane;
10 11
 import javafx.scene.layout.VBox;
12
+import javafx.scene.paint.Color;
13
+import javafx.scene.text.Font;
14
+import javafx.scene.text.Text;
11 15
 import javafx.stage.Stage;
12
-import javafx.scene.layout.FlowPane;
16
+import rocks.zipcode.atm.bank.Bank;
17
+
18
+import java.util.ArrayList;
13 19
 
14
-/**
15
- * @author ZipCodeWilmington
16
- */
17 20
 public class CashMachineApp extends Application {
21
+    CashMachine cashMachine = new CashMachine(new Bank());
22
+    Text account = new Text();
23
+    Text name = new Text();
24
+    Text email = new Text();
25
+    Text balance = new Text();
26
+    Button btnDeposit = new Button("Deposit");
27
+    Button btnWithdraw = new Button("Withdraw");
28
+    Button btnExit = new Button("Logout");
29
+    Button btnSubmit = new Button("Login");
30
+    Button btnClose = new Button("Exit");
31
+    Button btnNewAccount = new Button("New Account");
32
+    TextField name1 = new TextField("Name");
33
+    TextField email1 = new TextField("Email");
34
+    TextField balance1 = new TextField("Balance");
35
+    RadioButton basicAcct = new RadioButton("Basic Account");
36
+    RadioButton premAcct = new RadioButton("Premium Account");
37
+    Button btnSubmit2 = new Button("Create Account");
38
+
39
+    FlowPane fp = new FlowPane();
40
+    FlowPane fp2 = new FlowPane();
41
+    GridPane gp2 = new GridPane();
42
+    VBox vb3 = new VBox();
43
+    VBox vb = new VBox();
44
+    VBox vb2 = new VBox();
18 45
 
19 46
     private TextField field = new TextField();
20
-    private CashMachine cashMachine = new CashMachine(new Bank());
47
+    private TextField field2 = new TextField();
48
+
49
+    Scene scene1 = new Scene(vb1(),300,250, Color.BURLYWOOD);
50
+    Scene scene2 = new Scene(vb2(),300,250, Color.BURLYWOOD);
51
+    Scene scene3 = new Scene(vb3(),300,250, Color.BURLYWOOD);
52
+
53
+    public static void main(String[] args) {
54
+        launch(args);
55
+    }
56
+    Stage primaryStage = new Stage();
21 57
 
22
-    private Parent createContent() {
23
-        VBox vbox = new VBox(10);
24
-        vbox.setPrefSize(600, 600);
58
+    @Override
59
+    public void start(Stage primaryStage) {
60
+        this.primaryStage = primaryStage;
25 61
 
26
-        TextArea areaInfo = new TextArea();
62
+        primaryStage.setTitle("Zip Cloud Bank");
63
+        primaryStage.setScene(scene1);
64
+        primaryStage.show();
65
+    }
66
+
67
+    public Parent vb1(){
68
+        field.setFocusTraversable(false);
69
+        field.setPromptText("Enter Account ID");
70
+        field.setPadding(new Insets(5,0,5,0));
71
+        field.setMaxWidth(250);
72
+
73
+        Label zipCloud = new Label("ZipCloud Bank");
74
+        Label phrase = new Label("We Make It Rain");
75
+        zipCloud.setPrefSize(180,60);
76
+        zipCloud.setFont(new Font("Gotham",24));
77
+        zipCloud.setAlignment(Pos.TOP_CENTER);
78
+        phrase.setPrefSize(150,20);
79
+        phrase.setPadding(new Insets(0,0,20,0));
80
+        phrase.setAlignment(Pos.TOP_CENTER);
27 81
 
28
-        Button btnSubmit = new Button("Set Account ID");
29 82
         btnSubmit.setOnAction(e -> {
30 83
             int id = Integer.parseInt(field.getText());
31 84
             cashMachine.login(id);
32
-
33
-            areaInfo.setText(cashMachine.toString());
85
+            primaryStage.setScene(scene2);
34 86
         });
35 87
 
36
-        Button btnDeposit = new Button("Deposit");
88
+        fp.getChildren().addAll(btnSubmit,btnClose);
89
+        fp.setPadding(new Insets(0,25,0,25));
90
+        vb.getChildren().addAll(zipCloud, phrase, field,fp);
91
+        vb.setAlignment(Pos.CENTER);
92
+        vb.setStyle("-fx-background-color: burlywood; -fx-padding: 2; -fx-border-color: dimgrey; -fx-border-style: solid; -fx-border-width: 2; -fx-vgap: 5");
93
+
94
+        return vb;
95
+    }
96
+
97
+    public Parent vb2(){
98
+
37 99
         btnDeposit.setOnAction(e -> {
38
-            int amount = Integer.parseInt(field.getText());
100
+            double amount = Integer.parseInt(field2.getText());
39 101
             cashMachine.deposit(amount);
40 102
 
41
-            areaInfo.setText(cashMachine.toString());
103
+            gp2.setVisible(true);
104
+            account.setText(cashMachine.accountIdToString());
105
+            name.setText(cashMachine.nameToString());
106
+            email.setText(cashMachine.emailToString());
107
+            balance.setText(cashMachine.balanceToString());
42 108
         });
43 109
 
44
-        Button btnWithdraw = new Button("Withdraw");
45 110
         btnWithdraw.setOnAction(e -> {
46
-            int amount = Integer.parseInt(field.getText());
111
+            double amount = Integer.parseInt(field2.getText());
47 112
             cashMachine.withdraw(amount);
48 113
 
49
-            areaInfo.setText(cashMachine.toString());
114
+            gp2.setVisible(true);
115
+            account.setText(cashMachine.accountIdToString());
116
+            name.setText(cashMachine.nameToString());
117
+            email.setText(cashMachine.emailToString());
118
+            balance.setText(cashMachine.balanceToString());
119
+        });
120
+
121
+        btnNewAccount.setOnAction(e -> {
122
+            primaryStage.setScene(scene3);
123
+            field2.clear();
124
+            field.clear();
125
+            gp2.setVisible(false);
50 126
         });
51 127
 
52
-        Button btnExit = new Button("Exit");
53 128
         btnExit.setOnAction(e -> {
129
+            gp2.setVisible(false);
54 130
             cashMachine.exit();
55
-
56
-            areaInfo.setText(cashMachine.toString());
131
+            field.clear();
132
+            primaryStage.setScene(scene1);
57 133
         });
58 134
 
59
-        FlowPane flowpane = new FlowPane();
60
-
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;
135
+        btnClose.setOnAction(e -> {
136
+            System.exit(0);
137
+        }); //add alert box to ask Are you sure??
138
+
139
+        Label zipCloud2 = new Label("ZipCloud Bank");
140
+        zipCloud2.setPrefSize(180,80);
141
+        zipCloud2.setFont(new Font("Gotham",24));
142
+        zipCloud2.setAlignment(Pos.TOP_CENTER);
143
+
144
+        Menu accountMenu = new Menu("Switch Accounts");
145
+        MenuBar menuBar = new MenuBar();
146
+        menuBar.getMenus().add(accountMenu);
147
+        menuBar.setStyle("-fx-background-color: burlywood;");
148
+        ArrayList<String> accountList = cashMachine.listAccounts();
149
+        for(String acct : accountList) {
150
+            MenuItem newItem = new MenuItem(acct);
151
+            newItem.setOnAction(e -> btnExit.fire());
152
+            accountMenu.getItems().add(newItem);
153
+        }
154
+        fp2.getChildren().addAll(btnDeposit,btnWithdraw,btnNewAccount,btnExit, btnClose);
155
+        fp2.setAlignment(Pos.CENTER);
156
+
157
+        gp2.setVgap(.1);
158
+        gp2.setHgap(10);
159
+        gp2.setPadding(new Insets(25,25,25,25));
160
+        gp2.setOpacity(20);
161
+        gp2.setAlignment(Pos.BOTTOM_CENTER);
162
+        gp2.setPrefSize(100,50);
163
+        gp2.setStyle("-fx-font-size: 10; -fx-text-wrap: true; -fx-font-smoothing-type: LCD; -fx-font-family: fantasy");
164
+
165
+        gp2.add(account,0,0);
166
+        gp2.add(name,0,1);
167
+        gp2.add(email,1,0);
168
+        gp2.add(balance,1,1);
169
+
170
+        vb2.getChildren().addAll(menuBar, zipCloud2, field2,fp2,gp2);
171
+        vb2.setAlignment(Pos.BOTTOM_CENTER);
172
+        vb2.setStyle("-fx-background-color: burlywood; -fx-padding: 2; -fx-border-color: dimgrey; -fx-border-style: solid; -fx-border-width: 2; -fx-vgap: 5");
173
+
174
+        return vb2;
67 175
     }
68 176
 
69
-    @Override
70
-    public void start(Stage stage) throws Exception {
71
-        stage.setScene(new Scene(createContent()));
72
-        stage.show();
73
-    }
177
+    public Parent vb3(){
178
+        final ToggleGroup group = new ToggleGroup();
179
+        basicAcct.setToggleGroup(group);
180
+        premAcct.setToggleGroup(group);
181
+
182
+        btnSubmit2.setOnAction(e ->{
183
+            if (basicAcct.isSelected()) {
184
+                cashMachine.newBasicAccount(name1.getText(), email1.getText(), Double.parseDouble(balance1.getText()));
185
+                int id = Integer.valueOf(cashMachine.newBasicAccount(name1.getText(), email1.getText(), Double.parseDouble(balance1.getText())));
186
+                cashMachine.login(id);
187
+                primaryStage.setScene(scene2);
188
+            } else {
189
+                int id = Integer.valueOf(cashMachine.newPremiumAccount(name1.getText(), email1.getText(), Double.parseDouble(balance1.getText())));
190
+                cashMachine.login(id);
191
+                primaryStage.setScene(scene2);
192
+            }
193
+            name1.clear(); email1.clear(); balance1.clear();
194
+        });
74 195
 
75
-    public static void main(String[] args) {
76
-        launch(args);
196
+        vb3.getChildren().addAll(name1,email1,balance1,basicAcct,premAcct,btnSubmit2);
197
+        vb3.setStyle("-fx-background-color: burlywood; -fx-padding: 2; -fx-border-color: dimgrey; -fx-border-style: solid; -fx-border-width: 2; -fx-vgap: 5");
198
+
199
+        return vb3;
77 200
     }
78 201
 }

+ 5
- 5
src/main/java/rocks/zipcode/atm/bank/Account.java Wyświetl plik

@@ -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(double amount) {
19 19
         updateBalance(getBalance() + amount);
20 20
     }
21 21
 
22
-    public boolean withdraw(int amount) {
22
+    public boolean withdraw(double 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(double amount) {
32 32
         return getBalance() >= amount;
33 33
     }
34 34
 
35
-    public int getBalance() {
35
+    public double getBalance() {
36 36
         return accountData.getBalance();
37 37
     }
38 38
 
39
-    private void updateBalance(int newBalance) {
39
+    private void updateBalance(double 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 Wyświetl plik

@@ -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 double balance;
13 13
 
14
-    AccountData(int id, String name, String email, int balance) {
14
+    AccountData(int id, String name, String email, double 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 double getBalance() {
34 34
         return balance;
35 35
     }
36 36
 

+ 35
- 2
src/main/java/rocks/zipcode/atm/bank/Bank.java Wyświetl plik

@@ -2,6 +2,8 @@ package rocks.zipcode.atm.bank;
2 2
 
3 3
 import rocks.zipcode.atm.ActionResult;
4 4
 
5
+import java.security.Key;
6
+import java.util.ArrayList;
5 7
 import java.util.HashMap;
6 8
 import java.util.Map;
7 9
 
@@ -20,6 +22,29 @@ 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(3000, new BasicAccount(new AccountData(
27
+                3000, "Chester Marlow", "cmarlow@gmail.com", 1000
28
+        )));
29
+
30
+        accounts.put(4000, new PremiumAccount(new AccountData(
31
+                4000, "Rodney Fillet", "filletallday@gmail.com", 10000
32
+        )));
33
+    }
34
+
35
+    public void addBasicAccount(String name, String email, double balance) {
36
+         accounts.put(newId(), new BasicAccount(new AccountData(newId(), name, email, balance)));
37
+    }
38
+    public void addPremiumAccount(String name, String email, double balance) {
39
+        accounts.put(newId(), new PremiumAccount(new AccountData(newId(), name, email, balance)));
40
+    }
41
+
42
+    public int newId(){
43
+        int highestId = 0;
44
+        for(Integer id : accounts.keySet()){
45
+            if(id>highestId) highestId = id;
46
+        }
47
+        return highestId;
23 48
     }
24 49
 
25 50
     public ActionResult<AccountData> getAccountById(int id) {
@@ -32,14 +57,14 @@ public class Bank {
32 57
         }
33 58
     }
34 59
 
35
-    public ActionResult<AccountData> deposit(AccountData accountData, int amount) {
60
+    public ActionResult<AccountData> deposit(AccountData accountData, double amount) {
36 61
         Account account = accounts.get(accountData.getId());
37 62
         account.deposit(amount);
38 63
 
39 64
         return ActionResult.success(account.getAccountData());
40 65
     }
41 66
 
42
-    public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {
67
+    public ActionResult<AccountData> withdraw(AccountData accountData, double amount) {
43 68
         Account account = accounts.get(accountData.getId());
44 69
         boolean ok = account.withdraw(amount);
45 70
 
@@ -49,4 +74,12 @@ public class Bank {
49 74
             return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
50 75
         }
51 76
     }
77
+
78
+    public ArrayList<String> listAccounts(){
79
+        ArrayList<String> accountList = new ArrayList<>();
80
+        for(Integer key : accounts.keySet()){
81
+            accountList.add(accounts.get(key).getAccountData().getName());
82
+        }
83
+        return accountList;
84
+    }
52 85
 }

+ 1
- 1
src/main/java/rocks/zipcode/atm/bank/PremiumAccount.java Wyświetl plik

@@ -12,7 +12,7 @@ public class PremiumAccount extends Account {
12 12
     }
13 13
 
14 14
     @Override
15
-    protected boolean canWithdraw(int amount) {
15
+    protected boolean canWithdraw(double amount) {
16 16
         return getBalance() + OVERDRAFT_LIMIT >= amount;
17 17
     }
18 18
 }