Welcome to part four, going back to requirements text, a checking account is taxable.
That means you can draw a line between checking vs. savings and loan by making checking taxable.
In other words, by having, checking, implement the taxable interface.
So inside taxable, we're going to create the method signature.
For a tax method that expects an income parameter.
OK, and now from checking, we're going to implement the taxable interface implements.
Taxable.
Remember that an interface is a contract of behavior, a class that implements an interface must override
all of its methods.
So in this case, we have to override tax.
And before we do anything, we want to create a unit test for this piece of behavior.
So back inside of account tests.
We're going to create another unit test that test called income tax.
Public void income tax.
And we'll just assume that person's total income ended up being three thousand.
So we'll just deposit that.
Or, you know what, make it 4000.
The president.
Accounts at Index zero dark deposit 4000 into the checking account.
And now, if you try to say accounts at index zero tax, you're not going to get any results because
right now every element in the accounts array is behaving as an account is taking the form of an account,
and the account class does not have a tax method.
So here it's basically telling you, what are you talking about?
But we can typecast the element at index zero to taxable because we know that checking account is taxable.
And now we can tax an income of 4000.
Don't tax.
Four thousand.
OK.
Now we're going to make an assertion.
After the taxes are deducted, the resulting balance should be five thousand three hundred and seventy
four point five one.
After we get the accounts, updated balance accounts and in zero, don't get balance.
OK.
Run the tests.
And it fails now we can write code to make it pass.
Inside of checking Java.
I'm going to create a static final variable private static.
Final double.
Taxable income.
Is equal to three thousand.
And according to the requirements, the tax rate is 15 percent.
So what I'm going to do is create another private static final.
Double.
Tax rate constant is equal to zero point one five.
All right.
And here what I'll do.
Is it going to set double tax?
Is equal to math a doubt, Max.
Because either the amount that can be taxed is zero or it's the income minus the taxable income.
The idea is that if the income is smaller than what's taxable, this is going to be negative.
So Max returns zero, which means we don't want to tax them anything.
Otherwise, if the income is bigger than what's taxable, then we're going to get some number that we
can multiply by the tax rates.
And that's going to be our tax.
Now we can tax the person's account by saying super upset balance, and we'll update the balance by
rounding whatever the result is.
And that result is going to be based on us getting the current balance and deducting the tax.
OK, if we rerun our test.
Test Java.
Everything works out.
Let's rerun all of our tests.
And we're good.