1 00:00:00,240 --> 00:00:05,010 In this video, we'll do the remaining tasks for the bank class now, task five of us to make the add 2 00:00:05,010 --> 00:00:06,570 transaction method private. 3 00:00:06,600 --> 00:00:12,270 The reason we're doing this is because we don't want to be able to add transactions from outside of 4 00:00:12,270 --> 00:00:12,840 this class. 5 00:00:14,910 --> 00:00:20,430 It's the bank that can add successful transactions or ignore failed transactions, so make sure this 6 00:00:20,430 --> 00:00:23,550 is private because the caller shouldn't have access to this method. 7 00:00:24,360 --> 00:00:24,940 All right. 8 00:00:24,960 --> 00:00:30,030 And now going back to requirements that text, there are still some pieces of behavior that we need 9 00:00:30,030 --> 00:00:31,730 to unit test over here. 10 00:00:31,740 --> 00:00:34,760 Depending on the account, some transactions may be denied. 11 00:00:34,770 --> 00:00:36,600 We're going to write to unit tests for this. 12 00:00:36,870 --> 00:00:42,060 We're going to check and see if the bank keeps a record of successful transactions and if it ignores 13 00:00:42,120 --> 00:00:43,350 failed transactions. 14 00:00:43,740 --> 00:00:49,320 And the last thing we want to unit test is if the bank can effectively deduct taxes from taxable accounts. 15 00:00:49,620 --> 00:00:53,580 In other words, from checking accounts because those are the only ones that we made taxable. 16 00:00:53,880 --> 00:00:54,210 All right. 17 00:00:54,210 --> 00:00:59,820 So we'll start writing some unit tests inside, test, create a new file. 18 00:01:00,030 --> 00:01:04,470 We'll call it bank tests the Java. 19 00:01:06,070 --> 00:01:11,830 And before we do anything, I think I left you the initial set up on learning the part, so I'll go 20 00:01:11,830 --> 00:01:12,580 to that right now. 21 00:01:17,200 --> 00:01:18,250 We'll copy this code. 22 00:01:20,460 --> 00:01:21,180 Over to here. 23 00:01:22,320 --> 00:01:23,610 Part the bank class. 24 00:01:25,180 --> 00:01:26,800 In part, the before annotation. 25 00:01:28,940 --> 00:01:30,650 Import the checking class. 26 00:01:32,930 --> 00:01:37,940 And that's all we've got to imports, all right now, we're going to create a unit test at test. 27 00:01:39,140 --> 00:01:43,640 Named successful transaction, so we'll say public void successful. 28 00:01:44,700 --> 00:01:45,750 Transactions. 29 00:01:52,120 --> 00:01:56,020 And inside of this method, we're going to make two transactions a deposit. 30 00:01:56,200 --> 00:02:00,840 Deposits always work because that's how we designed them, as well as a withdrawal, the withdrawal 31 00:02:00,850 --> 00:02:02,200 we're going to make sure is successful. 32 00:02:02,500 --> 00:02:06,190 So we'll copy the code over from learning the parts. 33 00:02:06,850 --> 00:02:08,320 We don't have to do it from scratch. 34 00:02:10,330 --> 00:02:11,440 Desktop bank, oh, yeah. 35 00:02:11,500 --> 00:02:15,220 These don't exist yet, that's fine, we'll have to import transaction. 36 00:02:18,670 --> 00:02:20,460 Import equals. 37 00:02:24,070 --> 00:02:30,490 So this basically confirms the bank has two transactions after a successful withdrawal and deposit. 38 00:02:30,820 --> 00:02:36,190 Now the bank class doesn't have a withdraw transaction or a deposit transaction, so we'll go back to 39 00:02:36,190 --> 00:02:37,300 Bank Java. 40 00:02:41,210 --> 00:02:45,410 You create a public void method called withdraw. 41 00:02:46,520 --> 00:02:47,390 Transaction. 42 00:02:48,860 --> 00:02:52,010 They received a transaction object transaction. 43 00:02:56,330 --> 00:03:01,700 The idea inside this transaction indicates the account where the transaction is going to take place. 44 00:03:01,940 --> 00:03:08,330 So what we're going to do is get the accounts that matches this Transaction ID transaction get ID. 45 00:03:10,870 --> 00:03:14,110 And based on whatever account that we get, we're going to withdraw. 46 00:03:15,820 --> 00:03:18,610 Whatever amount is listed inside the transaction. 47 00:03:21,140 --> 00:03:23,640 Now, remember, that would draw returns a Boolean. 48 00:03:23,880 --> 00:03:28,950 So we're going to check if the withdraw method returns true if it succeeds. 49 00:03:30,490 --> 00:03:33,490 In this case, we're going to add the transaction to the list. 50 00:03:35,490 --> 00:03:37,470 Opie now understand why this is private. 51 00:03:37,650 --> 00:03:42,510 That's because it's only the bank that should have the capability of adding or ignoring transactions. 52 00:03:42,750 --> 00:03:45,780 This method should not be accessible outside this class. 53 00:03:47,270 --> 00:03:51,770 We should still have an error over here, that's because we need to make a deposit transaction method. 54 00:03:52,100 --> 00:03:58,400 So here I'll make another public void method called deposit transaction. 55 00:03:59,610 --> 00:04:01,740 But expects a transaction object. 56 00:04:08,770 --> 00:04:10,810 Once again, we're going to get the accounts. 57 00:04:11,870 --> 00:04:16,730 Where this transaction needs to take place, so we're going to get the account that matches this transaction 58 00:04:16,730 --> 00:04:16,940 ID. 59 00:04:17,839 --> 00:04:23,420 And then from that account, we'll make the deposit transaction Typekit amount. 60 00:04:25,360 --> 00:04:29,930 And no need for an if statement here, because we designed deposits to always work. 61 00:04:29,950 --> 00:04:31,270 They don't even return a Boolean. 62 00:04:31,690 --> 00:04:35,050 And now we'll just say a transaction transaction. 63 00:04:35,960 --> 00:04:39,650 So going back to our test class, if we rerun the unit test. 64 00:04:41,850 --> 00:04:42,750 We get a failure. 65 00:04:46,580 --> 00:04:50,720 The method transaction from the type bank is not visible. 66 00:04:51,500 --> 00:04:52,730 Oh, and that's fine. 67 00:04:54,110 --> 00:04:55,610 Inside the main class. 68 00:04:56,690 --> 00:05:02,210 We're still trying to call our transaction, but remember that we made everything private so you can 69 00:05:02,210 --> 00:05:03,310 just remove all of this. 70 00:05:03,320 --> 00:05:04,160 We don't need it anymore. 71 00:05:04,220 --> 00:05:06,470 That was just for the last video. 72 00:05:08,340 --> 00:05:09,870 OK, we'll proceed. 73 00:05:12,800 --> 00:05:15,020 And the test should work perfect. 74 00:05:19,860 --> 00:05:22,450 OK, going back to requirements not taxed. 75 00:05:23,230 --> 00:05:26,110 Remember that this requirement applies to unit tests. 76 00:05:26,230 --> 00:05:29,020 Now we want to check if failed transactions are denied. 77 00:05:29,350 --> 00:05:34,090 So instead of bank test that Java, we're going to create another unit test test. 78 00:05:36,490 --> 00:05:37,610 Public void. 79 00:05:37,630 --> 00:05:40,960 I'm going to call the unit test failed transactions. 80 00:05:42,440 --> 00:05:45,500 And failed, you know, what will make this singular? 81 00:05:46,610 --> 00:05:50,990 And inside a failed transaction, we're going to make a withdrawal that is designed not to work, a 82 00:05:50,990 --> 00:05:52,490 withdrawal that returns false. 83 00:05:53,060 --> 00:05:55,640 I should already have something set up for you to learn the parts. 84 00:05:55,730 --> 00:05:57,050 I do not. 85 00:05:57,160 --> 00:05:58,130 Well, that's surprising. 86 00:05:58,430 --> 00:06:02,360 So what I'll do is I'll just say this bank that would draw. 87 00:06:04,120 --> 00:06:05,800 I'm going to create a new transaction. 88 00:06:08,650 --> 00:06:09,610 Type would draw. 89 00:06:17,020 --> 00:06:19,200 We can give this any time stamp, it doesn't matter. 90 00:06:21,560 --> 00:06:26,690 The transaction is going to take place inside of this checking account, so we'll copy the idea of this 91 00:06:26,690 --> 00:06:29,270 checking account over here. 92 00:06:31,180 --> 00:06:35,110 And I'm going to make a transaction of a million dollars that shouldn't work. 93 00:06:36,990 --> 00:06:41,140 And now, if you run this test, I believe it should already work because we've already set up the statement. 94 00:06:42,160 --> 00:06:43,090 And perfect. 95 00:06:46,250 --> 00:06:51,470 Before we continue, I'm going to go inside Bank Dot Java, and I want to make these two methods private. 96 00:06:51,920 --> 00:06:55,340 I don't want them being accessed from anywhere except the bank class. 97 00:06:56,510 --> 00:06:59,130 And then what I'm going to do is create a public method. 98 00:06:59,720 --> 00:07:03,140 Public void called execute transaction. 99 00:07:05,080 --> 00:07:07,210 They receives a transaction object. 100 00:07:09,590 --> 00:07:14,360 And depending on what that transaction is, so we'll say switch transaction. 101 00:07:15,730 --> 00:07:16,360 Get type. 102 00:07:18,920 --> 00:07:25,340 If the transaction ends up being of type would draw, then we're going to call it withdraw transaction. 103 00:07:26,740 --> 00:07:29,380 And if it happens to be a tape deposit. 104 00:07:30,450 --> 00:07:32,460 Then we'll call deposit transaction. 105 00:07:34,040 --> 00:07:34,490 OK. 106 00:07:34,520 --> 00:07:40,040 And now if you go back the bank tasked Java, I can just say this that bank execute transaction. 107 00:07:44,530 --> 00:07:45,460 Copy that over. 108 00:07:47,840 --> 00:07:49,550 Can you rerun all of our tests? 109 00:07:55,790 --> 00:07:59,810 We made a mistake expected to, but was three up. 110 00:08:00,410 --> 00:08:02,480 I made a very silly mistake. 111 00:08:02,810 --> 00:08:06,440 Never forget the break your word when you're using a switch. 112 00:08:08,970 --> 00:08:10,620 We're running our tests. 113 00:08:11,450 --> 00:08:15,270 See, that's why our unit tests are so useful, because they'll catch your bug just like that. 114 00:08:19,820 --> 00:08:24,080 OK, that's two out of three unit tests that were done, the final piece of behavior we need to test 115 00:08:24,080 --> 00:08:27,170 is the bank deducting taxes from taxable accounts. 116 00:08:27,500 --> 00:08:33,650 So what I'm going to do is go back to bank tests and I'll create a unit test test. 117 00:08:35,010 --> 00:08:39,419 Public void and the unit test is going to be called tax deduction. 118 00:08:43,030 --> 00:08:47,470 And here what I'm going to do is only execute a single transaction for one account. 119 00:08:47,860 --> 00:08:51,370 So we'll go back here and that transaction is the following. 120 00:08:59,620 --> 00:09:06,370 Here we're making a single transaction of $4000 to a checking account, and the taxable income, I believe, 121 00:09:06,370 --> 00:09:07,320 was 3000. 122 00:09:07,630 --> 00:09:10,120 So our account should definitely get taxed. 123 00:09:10,750 --> 00:09:11,770 Going back here. 124 00:09:14,190 --> 00:09:20,310 The unit test is calling a deduct taxes method, and somehow this is going to deduct taxes from every 125 00:09:20,310 --> 00:09:22,830 single checking account inside of the bank object. 126 00:09:23,130 --> 00:09:25,590 And for this particular account? 127 00:09:29,190 --> 00:09:32,760 The remaining balance after they've been taxed should be the following. 128 00:09:33,120 --> 00:09:36,360 So what we're going to do is write code to make the test pass. 129 00:09:38,200 --> 00:09:43,960 Go over here and I'll create a public method void, deduct taxes. 130 00:09:48,910 --> 00:09:54,220 And I'm going to create a for each loop that runs through every single account in the accounts array 131 00:09:54,220 --> 00:09:54,640 list. 132 00:09:55,790 --> 00:09:59,900 And how do I check if an account is taxable or not? 133 00:10:00,140 --> 00:10:03,080 How do I check if something implements the taxable interface? 134 00:10:03,530 --> 00:10:06,250 Well, there is a very cool syntax that we can use. 135 00:10:06,260 --> 00:10:14,050 I left it for you on there in the parts we can check if taxable doc class that is assignable from account, 136 00:10:14,330 --> 00:10:15,110 get class. 137 00:10:16,330 --> 00:10:20,890 Basically, we're checking if this particular account implements the taxable interface. 138 00:10:23,660 --> 00:10:31,100 Then you can typecast our account to taxable taxable, taxable is equal to or typecast it. 139 00:10:34,640 --> 00:10:41,000 And here what we're going to do is tax the account, we know that taxable objects should implement a 140 00:10:41,000 --> 00:10:41,930 tax method. 141 00:10:42,320 --> 00:10:44,630 So back here, we'll say. 142 00:10:45,870 --> 00:10:46,590 Taxable. 143 00:10:47,590 --> 00:10:55,330 Don't tax the tax method, expect an income, so we'll get the income for this taxable accounts. 144 00:10:56,410 --> 00:10:59,230 OK, now how do we get the income of our accounts? 145 00:11:01,420 --> 00:11:02,740 I'm going to create a private. 146 00:11:05,360 --> 00:11:11,480 Double method called Get Income Notice that I'm making this method private because I only want to access 147 00:11:11,480 --> 00:11:15,100 it inside this class, I don't want the caller to have access to this method. 148 00:11:16,640 --> 00:11:19,460 It'll take one argument or one parameter, I should say. 149 00:11:19,490 --> 00:11:20,720 Taxable accounts. 150 00:11:22,690 --> 00:11:28,570 And inside what I'm going to do is get every single transaction that belongs to this account. 151 00:11:28,660 --> 00:11:37,400 So I'll say transaction transactions is equal to get transactions for this particular account account 152 00:11:37,420 --> 00:11:38,110 dot. 153 00:11:39,250 --> 00:11:41,040 Should be get I.D.. 154 00:11:42,720 --> 00:11:48,240 However, the taxable interface doesn't have a get ID method or anything, except this one method over 155 00:11:48,240 --> 00:11:48,510 here. 156 00:11:48,720 --> 00:11:51,780 So what we need to do is typecast as the checking. 157 00:11:54,990 --> 00:11:56,640 Put a bracket around both. 158 00:12:04,230 --> 00:12:05,800 And I think we need to impart this. 159 00:12:08,250 --> 00:12:08,880 And now we're good. 160 00:12:09,450 --> 00:12:14,010 All right, and so this basically gets every single transaction for this account. 161 00:12:14,020 --> 00:12:19,350 What we could do is create a for a loop that runs through every single transaction and take the sum. 162 00:12:19,740 --> 00:12:22,580 But if you can use a stream, you should use a stream. 163 00:12:22,590 --> 00:12:26,820 So what I'll do is return or raise dot stream. 164 00:12:28,170 --> 00:12:31,440 We'll create a stream of elements out of the transactions array. 165 00:12:33,900 --> 00:12:35,850 And then we're going to map the area to double. 166 00:12:36,420 --> 00:12:41,370 So we're basically going to map every single element in the transactions array to a double value will 167 00:12:41,370 --> 00:12:43,260 map every single transaction. 168 00:12:49,260 --> 00:12:51,480 To a double value, so you could just say. 169 00:12:53,110 --> 00:12:55,360 Transaction get amount. 170 00:12:56,530 --> 00:12:57,670 But that would be a mistake. 171 00:12:58,270 --> 00:12:58,900 Think about it. 172 00:12:59,320 --> 00:13:05,350 What if the transaction is off type withdraw, then the amount we return should be a negative, not 173 00:13:05,350 --> 00:13:05,980 positive. 174 00:13:06,400 --> 00:13:11,440 So for once, instead of just having this one liner, we're going to have an entire block of code where 175 00:13:11,440 --> 00:13:14,260 we compare the transaction type. 176 00:13:16,800 --> 00:13:18,780 Against two possible cases. 177 00:13:20,290 --> 00:13:21,280 Case would draw. 178 00:13:24,070 --> 00:13:28,480 We want to return negative transaction, get amount. 179 00:13:30,490 --> 00:13:35,200 And Case Dep., we want to return transaction. 180 00:13:36,420 --> 00:13:37,440 Don't get him out. 181 00:13:39,100 --> 00:13:41,230 Default returns zero. 182 00:13:42,720 --> 00:13:43,050 OK. 183 00:13:43,680 --> 00:13:49,620 And so at this point, our stream of transaction elements is going to be mapped to a stream of double 184 00:13:49,620 --> 00:13:50,190 values. 185 00:13:50,490 --> 00:13:54,360 So here we'll just take the sum of every single element and return that. 186 00:13:56,040 --> 00:14:02,070 This terminal operation is going to return the sum of every single element in the stream, and now we 187 00:14:02,070 --> 00:14:02,790 should be good. 188 00:14:03,390 --> 00:14:06,510 We'll go back to bank test each Java run the unit test. 189 00:14:08,790 --> 00:14:09,540 Perfect. 190 00:14:13,790 --> 00:14:16,160 Now we're going to confirm that all of our tests pass. 191 00:14:20,240 --> 00:14:20,750 And they do. 192 00:14:21,650 --> 00:14:26,480 This gives me comfort that all of the meaningful logic we implemented doesn't have any bugs and that 193 00:14:26,480 --> 00:14:27,200 can continue. 194 00:14:27,530 --> 00:14:28,820 See you in part eight.