Bladeren bron

money amounts converted to float.

Lauren Green 6 jaren geleden
bovenliggende
commit
9bbd942d72

+ 2
- 2
src/main/java/rocks/zipcode/atm/CashMachine.java Bestand weergeven

@@ -29,7 +29,7 @@ public class CashMachine {
29 29
         );
30 30
     }
31 31
 
32
-    public void deposit(int amount) {
32
+    public void deposit(float amount) {
33 33
         if (accountData != null) {
34 34
             tryCall(
35 35
                     () -> bank.deposit(accountData, amount),
@@ -38,7 +38,7 @@ public class CashMachine {
38 38
         }
39 39
     }
40 40
 
41
-    public void withdraw(int amount) {
41
+    public void withdraw(float amount) {
42 42
         if (accountData != null) {
43 43
             tryCall(
44 44
                     () -> bank.withdraw(accountData, amount),

+ 2
- 9
src/main/java/rocks/zipcode/atm/CashMachineApp.java Bestand weergeven

@@ -45,16 +45,10 @@ public class CashMachineApp extends Application {
45 45
 
46 46
         });
47 47
 */
48
-        //Make Button Do something.
49
-        Button btnForgotAccountNumber = new Button("Forgot Account Number");
50
-       /*
51
-        btnSubmit.setOnAction(e -> {
52 48
 
53
-        });
54
-*/
55 49
         Button btnDeposit = new Button("Deposit");
56 50
         btnDeposit.setOnAction(e -> {
57
-            int amount = Integer.parseInt(field2.getText());
51
+            float amount = Float.parseFloat(field2.getText());
58 52
             cashMachine.deposit(amount);
59 53
 
60 54
             areaInfo.setText(cashMachine.toString());
@@ -62,7 +56,7 @@ public class CashMachineApp extends Application {
62 56
 
63 57
         Button btnWithdraw = new Button("Withdraw");
64 58
         btnWithdraw.setOnAction(e -> {
65
-            int amount = Integer.parseInt(field2.getText());
59
+            float amount = Float.parseFloat(field2.getText());
66 60
             cashMachine.withdraw(amount);
67 61
 
68 62
             areaInfo.setText(cashMachine.toString());
@@ -80,7 +74,6 @@ public class CashMachineApp extends Application {
80 74
 
81 75
         flowpane.getChildren().add(btnSubmit);
82 76
         flowpane.getChildren().add(btnNewAccount);
83
-        flowpane.getChildren().add(btnForgotAccountNumber);
84 77
 
85 78
         FlowPane flowpane2 = new FlowPane();
86 79
 

+ 5
- 5
src/main/java/rocks/zipcode/atm/bank/Account.java Bestand weergeven

@@ -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
     }

+ 4
- 4
src/main/java/rocks/zipcode/atm/bank/AccountData.java Bestand weergeven

@@ -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
 
@@ -39,6 +39,6 @@ public final class AccountData {
39 39
         return "Account id: " + id + '\n' +
40 40
                 "Name: " + name + '\n' +
41 41
                 "Email: " + email + '\n' +
42
-                "Balance: " + balance;
42
+                "Balance: " + String.format("%.2f", balance);
43 43
     }
44 44
 }

+ 4
- 14
src/main/java/rocks/zipcode/atm/bank/Bank.java Bestand weergeven

@@ -17,11 +17,11 @@ public class Bank {
17 17
 
18 18
     public Bank() {
19 19
         accounts.put(1000, new BasicAccount(new AccountData(
20
-                1000, "Example 1", "example1@gmail.com", 500
20
+                1000, "Nick Foles", "liimvp@gmail.com", 500
21 21
         )));
22 22
 
23 23
         accounts.put(2000, new PremiumAccount(new AccountData(
24
-                2000, "Example 2", "example2@gmail.com", 200
24
+                2000, "Harry Potter", "seeker4life@wizmail.com", 200
25 25
         )));
26 26
     }
27 27
 
@@ -50,24 +50,14 @@ public class Bank {
50 50
         }
51 51
     }
52 52
 
53
-    public ActionResult<AccountData> getByName(int id) {
54
-        Account account = accounts.get(id);
55
-
56
-        if (account != null) {
57
-            return ActionResult.success(account.getAccountData());
58
-        } else {
59
-            return ActionResult.fail("No account with id: " + id);
60
-        }
61
-    }
62
-
63
-    public ActionResult<AccountData> deposit(AccountData accountData, int amount) {
53
+    public ActionResult<AccountData> deposit(AccountData accountData, float amount) {
64 54
         Account account = accounts.get(accountData.getId());
65 55
         account.deposit(amount);
66 56
 
67 57
         return ActionResult.success(account.getAccountData());
68 58
     }
69 59
 
70
-    public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {
60
+    public ActionResult<AccountData> withdraw(AccountData accountData, float amount) {
71 61
         Account account = accounts.get(accountData.getId());
72 62
         boolean ok = account.withdraw(amount);
73 63
 

+ 1
- 1
src/main/java/rocks/zipcode/atm/bank/PremiumAccount.java Bestand weergeven

@@ -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(float amount) {
16 16
         return getBalance() + OVERDRAFT_LIMIT >= amount;
17 17
     }
18 18
 }