@@ -34,43 +34,44 @@ public static void main(String[] args) {
34
34
35
35
account1 .deposit (200 );
36
36
withdraw (account1 , 300 );
37
- System .out .println ("Account 1: " + account1 .getBalance ());
37
+ System .out .printf ("Account 1 Balance: $%.2f \n " , account1 .getBalance ());
38
38
39
39
account2 .deposit (200 );
40
40
withdraw (account2 , 1500 );
41
- System .out .println ("Account 2: " + account2 .getBalance ());
41
+ System .out .printf ("Account 2 Balance: $%.2f \n " , account2 .getBalance ());
42
42
}
43
43
44
- public static void withdraw (BankAccount account , int amount ) {
44
+ public static void withdraw (BankAccount account , double amount ) {
45
45
try {
46
46
account .withdraw (amount );
47
47
} catch (NotEnoughMoneyException e ) {
48
- int diff = Math .abs (amount - account .getBalance ());
49
- System .out .println (e .toString ());
48
+ System .out .println (e .getMessage ());
50
49
}
51
50
}
52
51
}
53
52
54
53
class BankAccount {
55
54
56
- private int balance ;
55
+ private double balance ;
57
56
58
- public BankAccount (int balance ) {
57
+ public BankAccount (double balance ) {
59
58
this .balance = balance ;
60
59
}
61
60
62
- public void deposit (int amount ) {
63
- balance += amount ;
61
+ public void deposit (double amount ) {
62
+ if (amount > 0 ) {
63
+ balance += amount ;
64
+ }
64
65
}
65
66
66
- public void withdraw (int amount ) throws NotEnoughMoneyException {
67
- if (amount > balance ) throw new NotEnoughMoneyException (
68
- "Bank balance is short $" + Math .abs (balance - amount )
69
- );
67
+ public void withdraw (double amount ) throws NotEnoughMoneyException {
68
+ if (amount > balance ) {
69
+ throw new NotEnoughMoneyException ( String . format ( "Bank balance is short $%.2f" , Math .abs (balance - amount )));
70
+ }
70
71
balance -= amount ;
71
72
}
72
73
73
- public int getBalance () {
74
+ public double getBalance () {
74
75
return balance ;
75
76
}
76
77
}
0 commit comments