Browse Source

CashMachine

Chaitali Patel 6 years ago
parent
commit
c3185f54f5

+ 10
- 0
src/main/java/rocks/zipcode/atm/CashMachine.java View File

@@ -46,6 +46,14 @@ public class CashMachine {
46 46
             );
47 47
         }
48 48
     }
49
+    public void balance(int amount) {
50
+        if (accountData != null) {
51
+            tryCall(
52
+                    () -> bank.balance(accountData, amount),
53
+                    update
54
+            );
55
+        }
56
+    }
49 57
 
50 58
     public void exit() {
51 59
         if (accountData != null) {
@@ -72,4 +80,6 @@ public class CashMachine {
72 80
             System.out.println("Error: " + e.getMessage());
73 81
         }
74 82
     }
83
+
84
+
75 85
 }

+ 20
- 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.layout.HBox;
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
+
22 24
     private Parent createContent() {
23 25
         VBox vbox = new VBox(10);
24
-        vbox.setPrefSize(600, 600);
26
+        vbox.setPrefSize(800, 600);
25 27
 
26 28
         TextArea areaInfo = new TextArea();
27 29
 
28
-        Button btnSubmit = new Button("Set Account ID");
30
+        Button btnSubmit = new Button("Account ID");
31
+        btnSubmit.setStyle("-fx-border-color: #0000ff; -fx-border-width: 5px;");
29 32
         btnSubmit.setOnAction(e -> {
30 33
             int id = Integer.parseInt(field.getText());
31 34
             cashMachine.login(id);
@@ -34,6 +37,7 @@ public class CashMachineApp extends Application {
34 37
         });
35 38
 
36 39
         Button btnDeposit = new Button("Deposit");
40
+        btnDeposit.setStyle("-fx-border-color: #0000ff; -fx-border-width: 5px;");
37 41
         btnDeposit.setOnAction(e -> {
38 42
             int amount = Integer.parseInt(field.getText());
39 43
             cashMachine.deposit(amount);
@@ -42,6 +46,7 @@ public class CashMachineApp extends Application {
42 46
         });
43 47
 
44 48
         Button btnWithdraw = new Button("Withdraw");
49
+        btnWithdraw.setStyle("-fx-border-color: #0000ff; -fx-border-width: 5px;");
45 50
         btnWithdraw.setOnAction(e -> {
46 51
             int amount = Integer.parseInt(field.getText());
47 52
             cashMachine.withdraw(amount);
@@ -49,7 +54,17 @@ public class CashMachineApp extends Application {
49 54
             areaInfo.setText(cashMachine.toString());
50 55
         });
51 56
 
57
+        Button btnBalance = new Button("Balance");
58
+        btnBalance.setStyle("-fx-border-color: #0000ff; -fx-border-width: 5px;");
59
+        btnBalance.setOnAction(e -> {
60
+            int amount = Integer.parseInt(field.getText());
61
+            cashMachine.balance(amount);
62
+
63
+            areaInfo.setText(cashMachine.toString());
64
+        });
65
+
52 66
         Button btnExit = new Button("Exit");
67
+        btnExit.setStyle("-fx-border-color: #0000ff; -fx-border-width: 5px;");
53 68
         btnExit.setOnAction(e -> {
54 69
             cashMachine.exit();
55 70
 
@@ -68,8 +83,11 @@ public class CashMachineApp extends Application {
68 83
 
69 84
     @Override
70 85
     public void start(Stage stage) throws Exception {
86
+        stage.setTitle("Welcome to ZipCloudBank");
87
+
71 88
         stage.setScene(new Scene(createContent()));
72 89
         stage.show();
90
+
73 91
     }
74 92
 
75 93
     public static void main(String[] args) {

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

@@ -40,4 +40,9 @@ public abstract class Account {
40 40
         accountData = new AccountData(accountData.getId(), accountData.getName(), accountData.getEmail(),
41 41
                 newBalance);
42 42
     }
43
+
44
+
45
+    public void balance(int amount) {
46
+
47
+    }
43 48
 }

+ 7
- 0
src/main/java/rocks/zipcode/atm/bank/Bank.java View File

@@ -49,4 +49,11 @@ public class Bank {
49 49
             return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
50 50
         }
51 51
     }
52
+
53
+    public <T> ActionResult<T> balance(AccountData accountData, int amount) {
54
+        Account account = accounts.get(accountData.getId());
55
+        account.balance(amount);
56
+
57
+        return (ActionResult<T>) ActionResult.success(account.getAccountData());
58
+    }
52 59
 }