Welcome back now we're going to wrap up the remaining tasks.
Let's read a unit test for this piece of behavior for a loan accounts, every withdrawal is charged,
a fixed interest rate of two percent.
So right here we'll create a unit test called withdrawal interest.
I'll give you some time to write that out.
OK, and now from the loan accounts, from the account and index two, we're going to withdraw.
Twenty four, $434 point thirty one cents and the alone account is already twenty five hundred dollars
in debt, so we can assert.
A third equals.
That after this withdrawal, the debt becomes five thousand twenty dollars point thirty one cents after
we get the account's current balance counts at indexed to get balance.
OK.
Run the test.
And it fails, but now we can write code to make it pass.
So here I'm going to return true by default.
We're going to update the balance super balance by getting the current balance super delegate balance,
which for a loan account represents debt.
So whatever the user withdraws will increase the debt and the debt equals whatever the amount is that
they would draw.
Plus, the interest, which is going to be that same amount.
Times zero point zero two.
And as always, don't forget to round to two decimal places, so here we're going to say super round
and round our results.
Now, do you see anything slightly wrong with this interest rate is an important value?
So we're going to make this a constant.
We're going to say private static.
Vinyl double interest rate is equal to zero point zero two.
And we're going to use the constant instead.
If I rerun my unit test.
Everything works now, our account is five thousand twenty dollars in debt.
OK, we're almost done.
We're in a writing test for this piece of behavior.
So for a loan accounts, there's a withdrawal limit of $10000.
So here we'll say well will create a unit test.
And toll caps, I'm going to write the unit test would draw a limit.
And from the savings account, which is that indexed to accounts at indexed two, I'm going to withdraw
7000.
$463 point 69 cents.
But looking back, the loan account is already $2500 in debt.
And the account isn't allowed to exceed $10000 in debt, so here I'm going to assert that nothing changes
assert equals.
The balance remains the same at twenty five hundred thirty seven point thirty one cents after we get
that account balance.
Counts to get balance.
OK.
Run the tests.
And it fails, it does make the withdrawal.
That's not good.
So back here.
We'll see if the balance, if the current dipped.
Plus the amount.
If it's bigger than the maximum depth, if it's bigger than 10000.
If it's bigger than the maximum amount of debt a loan account is allowed to have, then we're going
to return false.
Nothing will happen.
And instead of putting the rally 10000 directly here, I'm going to create another static constant private
static final double max debt and set that equal to 10000 and use the constant instead.
OK.
This does remind me of something and checking together.
We don't need to return true twice here.
We can just return true at the end.
Because if this executes, it's going to break the entire function.
And if one of these executed, then it'll return true.
So anyways, not a very big deal, but back here looks good.
I think we can just run our test.
And beautiful.
OK, back in requirements text, it seems that we're done writing the logic for withdrawal.
Now we're going to test the behavior for deposits.
Now the logic for checking and savings deposits should be identical because you're just adding money
to an account.
So I'm going to create a unit test named deposit test.
Public void.
Deposit.
And I'll deposit $5000 into the checking account, don't deposit 5000.
And then I'm going to assert.
But the resulting balance is six thousand five hundred and twenty four point five one.
After we get the updated balance.
OK, now we can write code to make it pass.
So we'll go to checking.
He will say super dad set balance will update the balance to be the current balance, super balance,
plus the amount being deposited, and just don't forget to use Super Dot Round so we can ensure that
we have two decimal places for money.
OK.
And we should be able to run the test now.
And perfect now the deposit method for a savings should be identical to checking so we can just go ahead
in savings and replicate the same logic.
We'll go here, we'll say super set balance, super dot around, super dark get balance plus amount.
All right, now, we got a ready unit test for loan deposits.
The difference is when you deposit money into a loan account, your loan balance is decreasing.
Think of it like paying your credit card in any case.
We'll go back here and I'll create a unit tests.
Public void named loan deposit.
Inside, I'm going to make a $1000 deposit to the loan accounts.
Accounts and index to.
That deposit $1000.
And after we're going to assert the resulting loan balance as third equals is one thousand five hundred
and thirty seven point three one.
After we get the updated loan account balance accounts at indexed to.
Does it get balance?
OK, the test should fail.
Nice.
Now we get to write code to make it pass.
So right here.
We'll just say super dad set balance.
Well, around whatever calculation we do to two decimal places will get the current balance, super
balance and subtract the amount that's being deposited, thus reducing the person's loan.
OK, not to run the test now.
Perfect.
Now we're going to run every single test.
They all passed, and that gives me great confidence that we implemented all of the meaningful logic
without any bugs.
That's perfect.
We unit tested every single piece of behavior from the requirements and as long as every single test
passes, I can rest assured every method is working the way it should.
All right, let's move on to part four.