1
00:00:00,450 --> 00:00:03,990
All right, we've set up our classes now we need to inject some logic into them.

2
00:00:05,250 --> 00:00:10,890
Going back to requirements, talked text, and from first glance, it seems that we need to force every

3
00:00:10,890 --> 00:00:13,590
child the class to override two common methods.

4
00:00:13,890 --> 00:00:15,510
Deposit and withdraw.

5
00:00:16,020 --> 00:00:20,670
And to do that, we need to define two abstract methods inside the parent class.

6
00:00:21,090 --> 00:00:22,740
So we'll go to account Java.

7
00:00:23,680 --> 00:00:24,880
And here we'll see.

8
00:00:26,080 --> 00:00:26,620
Public.

9
00:00:27,750 --> 00:00:35,340
Abstract void, and it will deposit some amount of money, double amounts.

10
00:00:36,570 --> 00:00:38,860
Now, a withdrawal can be successful or it can fail.

11
00:00:38,880 --> 00:00:40,050
So here we'll say public.

12
00:00:42,280 --> 00:00:44,410
Abstract bullying.

13
00:00:46,340 --> 00:00:48,440
That can withdraw some amount of money.

14
00:00:55,340 --> 00:01:01,370
All right, and as we override, deposit and withdraw, we need to define this protected method inside

15
00:01:01,370 --> 00:01:02,450
of account Java.

16
00:01:08,700 --> 00:01:12,810
Remember, that protected means this method is only accessible from within its children.

17
00:01:13,200 --> 00:01:16,200
It's not public, which means you can't access it from anywhere.

18
00:01:16,440 --> 00:01:18,650
It's protected only child.

19
00:01:18,650 --> 00:01:23,120
The classes, checking savings and loan can access the ruined method.

20
00:01:23,770 --> 00:01:26,070
Anyways, we seem to be getting an error.

21
00:01:26,550 --> 00:01:30,840
OK, got an important decimal format before we can use it, and we're good.

22
00:01:31,650 --> 00:01:37,350
And now remember that if a parent defines an abstract method, the child class is forced to override

23
00:01:37,350 --> 00:01:37,470
it.

24
00:01:37,950 --> 00:01:39,060
So we'll go to checking.

25
00:01:39,480 --> 00:01:41,100
That's why we're seeing these errors.

26
00:01:41,430 --> 00:01:45,390
Checking needs to provide its own logic for deposit and withdraw.

27
00:01:46,440 --> 00:01:49,130
OK, so I'll just override them for now.

28
00:01:50,550 --> 00:01:52,470
We won't provide any logic just yet.

29
00:01:53,160 --> 00:01:54,930
Not until we write unit tests.

30
00:01:56,450 --> 00:01:59,320
Checking we'll do the same thing for savings.

31
00:02:01,720 --> 00:02:02,470
And.

32
00:02:03,680 --> 00:02:06,140
Merge doing that to get rid of the areas for now.

33
00:02:12,540 --> 00:02:17,910
Now, the reason I'm not writing the logic just yet is because we need to unit test all of the behavior

34
00:02:17,910 --> 00:02:21,510
inside of the requirements that text filed before we write the code for it.

35
00:02:21,870 --> 00:02:26,700
That way, we make sure that any code we write, any logic we provide doesn't have any bugs.

36
00:02:27,030 --> 00:02:29,460
So long as every test passes, we know that we're good.

37
00:02:30,180 --> 00:02:35,730
So I'll go to a count tests that Java and I'll start by on commenting the accounts array.

38
00:02:36,570 --> 00:02:43,680
And that doesn't seem to be like a good amount of indentation, so I'll fix that and import the accounts

39
00:02:43,680 --> 00:02:44,280
class.

40
00:02:46,470 --> 00:02:51,060
And now, before running each test, we got to make sure the array is being populated first, so we'll

41
00:02:51,060 --> 00:02:57,300
start by annotating a method with before this before method is going to run before each test.

42
00:02:57,840 --> 00:02:59,040
Public void setup.

43
00:03:00,330 --> 00:03:05,640
Where we set up our accounts are by going back over to learn the parts and populating it with three

44
00:03:05,640 --> 00:03:06,390
objects.

45
00:03:07,680 --> 00:03:08,340
Checking.

46
00:03:09,960 --> 00:03:10,980
Savings.

47
00:03:13,280 --> 00:03:14,360
That did not work.

48
00:03:19,400 --> 00:03:19,970
And loan.

49
00:03:22,380 --> 00:03:23,160
OK, perfect.

50
00:03:23,190 --> 00:03:26,220
We're going to start by unit testing the behavior for withdrawing.

51
00:03:26,670 --> 00:03:31,860
We want to make sure the correct amount gets withdrawn from the checking account under normal circumstances.

52
00:03:32,520 --> 00:03:35,340
So what we'll do is create a unit test.

53
00:03:36,770 --> 00:03:39,620
Public void would draw.

54
00:03:42,300 --> 00:03:46,410
And what we need to do is withdraw from the checking account that's at index zero.

55
00:03:47,020 --> 00:03:48,390
So accounts.

56
00:03:50,060 --> 00:03:52,640
At Index zero, that would draw.

57
00:03:53,910 --> 00:03:56,610
And we're going to withdraw 14 40.

58
00:03:59,290 --> 00:04:01,780
Then we'll make an assertion assert equals.

59
00:04:03,550 --> 00:04:09,930
The assertion expects eighty four point five one after we get the account balance accounts out of like

60
00:04:09,970 --> 00:04:11,680
zero dark get balance.

61
00:04:17,720 --> 00:04:19,370
So instead of checking, not Java.

62
00:04:20,350 --> 00:04:23,500
We'll update the current balance, SuperDart set balance.

63
00:04:24,740 --> 00:04:28,220
By minus thing, the amount from the current balance.

64
00:04:30,340 --> 00:04:34,210
All right, and before we run our tests, let's just return true by default.

65
00:04:34,630 --> 00:04:36,040
We'll address that later on.

66
00:04:37,410 --> 00:04:39,780
Now we can run our test and see if it works.

67
00:04:43,140 --> 00:04:48,540
And it still fails, and it seems like the value is off by decimal points, because this is money,

68
00:04:48,540 --> 00:04:50,790
we're going to round to two decimal points.

69
00:04:51,180 --> 00:04:54,750
So what we can do is call the protected method super dot around.

70
00:04:57,040 --> 00:04:57,850
Right over here.

71
00:05:06,740 --> 00:05:07,670
Rerun your test.

72
00:05:11,100 --> 00:05:11,790
And perfect.

73
00:05:12,600 --> 00:05:16,860
OK, now looking back at the requirements, now we're going to unit test this behavior.

74
00:05:17,370 --> 00:05:23,610
The checking account charges an overdraft fee of five dollars and fifty cents if the amount being withdrawn

75
00:05:23,610 --> 00:05:25,260
exceeds the account balance.

76
00:05:25,500 --> 00:05:30,210
So we want to make sure this overdraft fee gets deducted if they try to withdraw more than what's in

77
00:05:30,210 --> 00:05:30,930
their balance.

78
00:05:31,260 --> 00:05:32,490
So back here?

79
00:05:33,520 --> 00:05:35,050
We'll create another unit tests.

80
00:05:36,340 --> 00:05:36,760
Test.

81
00:05:38,700 --> 00:05:43,140
And the unit test will be called overdraft public void overdraft.

82
00:05:44,660 --> 00:05:48,440
And we need to withdraw from the checking account and index zero.

83
00:05:48,890 --> 00:05:53,110
So we'll say accounts and index zero, that would draw.

84
00:05:54,250 --> 00:05:58,900
And we'll be withdrawing fifteen thirty four point forty three.

85
00:06:01,680 --> 00:06:05,100
And then here we'll make an assertion will assert equals.

86
00:06:07,110 --> 00:06:15,750
The expected value is negative $15 and 42 cents after we get the account's balance accounts at index

87
00:06:15,750 --> 00:06:18,240
zero, don't get balance.

88
00:06:21,530 --> 00:06:24,380
Run the test, and it obviously should fail.

89
00:06:25,850 --> 00:06:27,950
Now we can write code to make it pass.

90
00:06:28,980 --> 00:06:30,840
So back in checking.

91
00:06:31,880 --> 00:06:32,660
We'll see if.

92
00:06:33,740 --> 00:06:40,100
The current balance to get balance minus the amount being withdrawn.

93
00:06:41,940 --> 00:06:43,010
Smaller than zero.

94
00:06:46,410 --> 00:06:52,020
Then we'll update the current balance, super accept balance, super tight get balance.

95
00:06:54,380 --> 00:06:55,970
Minus amounts.

96
00:06:57,050 --> 00:06:59,630
Minus five dollars and fifty cents.

97
00:07:00,970 --> 00:07:01,870
But hold on a second.

98
00:07:01,900 --> 00:07:03,640
This seems like a pretty important value.

99
00:07:03,670 --> 00:07:06,220
So it'd be safer to use a constant tear instead.

100
00:07:06,670 --> 00:07:10,120
So what I'll do is I'll declare a private.

101
00:07:11,440 --> 00:07:16,390
Static final double overdraft fee.

102
00:07:18,500 --> 00:07:20,480
Is equal to five dollars and fifty cents.

103
00:07:21,630 --> 00:07:24,090
And will use that constant instead.

104
00:07:26,300 --> 00:07:29,330
Because there's no risk of accidentally changing a constant.

105
00:07:30,110 --> 00:07:35,360
All right, and in the event that this if statement gets executed, we want to break the entire function

106
00:07:35,360 --> 00:07:39,710
by returning crew here because obviously you don't want this to run.

107
00:07:40,040 --> 00:07:42,260
And then that's true and that will just mess everything up.

108
00:07:42,830 --> 00:07:46,430
So we'll go back to account tests, run the test.

109
00:07:49,950 --> 00:07:55,620
And once again, with doubles, we got the issue of having way too many decimal points, we're dealing

110
00:07:55,620 --> 00:07:58,440
with money, so we went around to two decimal places.

111
00:07:58,950 --> 00:08:00,690
So over here.

112
00:08:01,670 --> 00:08:04,610
And checking, we'll have to call SuperDart around.

113
00:08:10,580 --> 00:08:11,570
We were on the test.

114
00:08:17,540 --> 00:08:17,870
OK.

115
00:08:18,440 --> 00:08:22,220
Back door requirements, that text will a unit test this behavior?

116
00:08:22,490 --> 00:08:26,960
We want to make sure that withdrawal doesn't happen if the amount surpasses the overdraft limit.

117
00:08:27,500 --> 00:08:29,810
So we'll create a unit test.

118
00:08:33,429 --> 00:08:33,970
Public.

119
00:08:35,710 --> 00:08:36,280
Void.

120
00:08:38,000 --> 00:08:39,409
Overdraft limit.

121
00:08:42,679 --> 00:08:44,810
Inside, I'm going to withdraw.

122
00:08:50,080 --> 00:08:54,640
Seventeen twenty six from the checking accounts.

123
00:08:56,260 --> 00:09:00,730
And then we're going to assert that the resulting balance shouldn't change, it should remain.

124
00:09:01,330 --> 00:09:04,090
Uh, fifteen twenty four point five one.

125
00:09:06,510 --> 00:09:11,130
When we get the account balance accounts, zero, don't get balance.

126
00:09:12,080 --> 00:09:12,470
OK.

127
00:09:12,860 --> 00:09:13,790
Run the test.

128
00:09:16,320 --> 00:09:17,250
And it fails.

129
00:09:17,430 --> 00:09:19,770
Now we just got to write code to make it pass.

130
00:09:20,840 --> 00:09:23,090
Back here, we'll check if the current balance.

131
00:09:25,160 --> 00:09:30,320
If super don't get balance minus amounts.

132
00:09:32,310 --> 00:09:37,590
Is smaller than negative 200, then well, wait a second.

133
00:09:37,680 --> 00:09:42,750
This seems like an important value, and it would be safer once again to use a constant here instead.

134
00:09:43,260 --> 00:09:47,010
So we'll declare a private static.

135
00:09:48,040 --> 00:09:49,930
Final double.

136
00:09:51,350 --> 00:09:55,820
Constant named overdraft limit equal to $200.

137
00:09:59,620 --> 00:10:04,450
And we'll say if superdog, that balance minus amount is smaller than the overdraft limit.

138
00:10:06,990 --> 00:10:08,310
Then we'll just return false.

139
00:10:09,490 --> 00:10:11,260
That will break the entire function.

140
00:10:11,310 --> 00:10:12,720
Let's make this an if.

141
00:10:15,040 --> 00:10:15,670
And you know what?

142
00:10:15,760 --> 00:10:18,550
Let's just create a chain of if else.

143
00:10:20,110 --> 00:10:21,910
That looks a lot better to me.

144
00:10:24,060 --> 00:10:24,870
All right.

145
00:10:25,530 --> 00:10:26,880
We can rerun our test.

146
00:10:29,980 --> 00:10:31,570
And that works perfect.

147
00:10:32,820 --> 00:10:37,230
All right, back here, the next piece of behavior we're going to unit test is the withdrawal fee.

148
00:10:37,590 --> 00:10:42,810
We want to make sure that it's charged a $5 fee if they attempt to withdraw from a savings account.

149
00:10:43,350 --> 00:10:45,930
So we'll create a unit test.

150
00:10:47,670 --> 00:10:48,180
Test.

151
00:10:48,750 --> 00:10:51,990
Public void, withdrawal fee.

152
00:10:57,790 --> 00:11:03,700
And we're going to withdraw $100 from the savings accounts, accounts and index one.

153
00:11:04,300 --> 00:11:06,190
Well, withdraw $100.

154
00:11:07,710 --> 00:11:14,190
And then we're going to make an assertion that the resulting balance is twenty one hundred thirty six

155
00:11:14,190 --> 00:11:19,350
dollars point sixty cents as we get the current balance for the savings account.

156
00:11:20,130 --> 00:11:22,290
Count that index one, don't get balance.

157
00:11:23,580 --> 00:11:24,090
All right.

158
00:11:24,120 --> 00:11:25,050
Run the test.

159
00:11:28,070 --> 00:11:29,510
And it fails.

160
00:11:30,200 --> 00:11:36,380
Now we just got to write code to make it past so inside the redraw method for savings.

161
00:11:37,160 --> 00:11:41,210
We'll update the balance super set balance.

162
00:11:41,870 --> 00:11:45,290
We'll get the current balance, super Typekit balance.

163
00:11:46,420 --> 00:11:47,650
Minus the amount.

164
00:11:48,660 --> 00:11:50,310
Minus $5.

165
00:11:52,080 --> 00:11:57,360
Once again, $5, it seems like an important value, so we're going to declare it as a constant here,

166
00:11:57,360 --> 00:11:59,940
we'll say private static.

167
00:12:01,000 --> 00:12:02,260
Final double.

168
00:12:04,480 --> 00:12:05,350
What drove the.

169
00:12:08,900 --> 00:12:10,370
Is equal to $5.

170
00:12:13,720 --> 00:12:15,490
And here we'll use a constant instead.

171
00:12:18,980 --> 00:12:21,830
And, as always, will around to two decimal places.

172
00:12:26,890 --> 00:12:29,890
And return true to indicate that the withdrawal was successful.

173
00:12:30,550 --> 00:12:35,260
Now, usually in a savings account, you shouldn't be able to make a withdrawal that exceeds your balance.

174
00:12:35,680 --> 00:12:38,300
But it didn't say anything in the requirements tax.

175
00:12:38,320 --> 00:12:40,750
So whatever in the Java world, we're going to permit it.

176
00:12:42,400 --> 00:12:43,270
Run the test.

177
00:12:46,650 --> 00:12:47,520
And it passes.

178
00:12:49,000 --> 00:12:51,820
All right, we're going to continue with the remaining tasks in the next video.

