>> So in our last video,
we actually introduced an exception into our code.
Let's take a closer look at that.
>> So I went and tried to compile,
and I'm being told that
bank account balance cannot be assigned to.
It's read-only.
>> Right.
>> Yes. I thought it was clever because I made balanced a getter,
but the initial balance, it won't let me set it.
> Right. Now that we're actually thinking of
this balance as something that's
directly related to a transaction,
we shouldn't actually just give people an initial balance.
We don't want to give away free money, right?
>> I see. The initial balance is the first transaction.
>> Right. So that should be a deposit.
>> Okay. Cool. Well, we didn't ever wrote deposit though.
>> Yes. Why don't we go ahead and do that.
>> Okay. So this.Balance, initial balance,
that's going to have to go away,
and then we need to go and write how to make a deposit.
There was a list of rules about certain things,
like I shouldn't be able to deposit negative money.
>> Right.
>> Stuff like that. So let's go back and
look at our instructions here,
and they've given us that code.
To save time, I'll copy
that and then we can walk through it if that's okay.
>> Sounds good.
>> So all I'm doing is I'm
selecting "Make Deposit" and "Make Withdrawal",
and copying over the top of them.
Now, it might look like a lot of code,
but let's just zoom out, just a smidge,
and let's figure this out because this
actually is cool because it
puts all the things we've learned together;
ifs, and lists and all this stuff. So what's going on here?
>> I do see something a little new.
>> Yes. So hang on.
>> They say there's exceptions. Okay, we'll get to that.
>> Yes. Well, if the amount is less than zero,
if I try to deposit negative dollars, what's a throw?
That's totally new. So throw new argument.
We know what arguments are. These are arguments,
out of range exception. That's exceptional.
>> Yes. So it looks like it's checking to make sure
that the amount needs to be above zero,
or at least equal. Yes, above the zero.
>> I guess you could deposit zero.
That's weird, but then we can figure that
out during our code review.
>> I don't think we should bother with that.
Let's do at least some amount of money.
>> So if it's less than zero,
then it's negative, and we throw an exception.
The exception is a big deal.
It's exceptional, it stops the program.
If you don't catch it and look at it.
If I try to go to a real bank,
and I said this is a good question for
you as an Object Oriented Programmer.
I go and try to model reality.
If I were to go and cash a check for negative dollars,
or deposit a check for native dollars,
wouldn't that kick off some business process
at the bank where they would be like, this is not a thing.
>> Probably, like decline.
Do we owe you money?
Why don't we just write you a check?
Why do you need a negative check? Checks don't work that way.
>> Questions will be asked.
>> Yes. So that's an exceptional moment.
>> Yes.
>> That was an exception should do.
You throw exceptions when something is completely out there.
This is an interesting one down here and make withdrawal.
What is the balance that you have,
minus the amount you want to withdrawal
would make your number of your balance go under zero?
>> That's an overdraft.
>> Right, that's an interesting one.
That's not an argument out of range.
That is in fact an invalid operation.
You can't take that money.
>> Right.
>> Then look, if you throw that does it let you continue?
Does the next line run?
It does not. They'll stop everything.
When an exception occurs,
it's like this function is over,
all things stop running at this point.
>> So we don't simply exit the if statement,
because that's what it looks like,
but since that throw is there,
it'll actually exit the program and not try to-
>> Exactly. It'll exit the function unless we caught it higher up,
and then worst-case scenario,
it'll go all the way up and injure the entire program.
But the intention is,
that it'll exactly stop the running of this particular method.
So if we look at these without the ifs,
it looks very simple.
"Make Deposit", "New Transaction",
"Add a Deposit", "Make Withdrawal",
"New Transaction", "Negative Amount", and "Add Withdrawal".
Then you add the guards around it,
and it becomes a little bit more fleshed out a little bit.
>> Yes. Maybe more secure and enforcing
the pattern that we want people to use with the system.
>> Exactly. So then the question is,
what would bank accounts constructor,
remember before I was just shoving the balance in there directly,
what would that look like in this new world
where I have a deposit?
>> Right. Well, instead of just
assigning the balance to a static number,
we should actually probably review
all transactions to see what the balance is.
>> Right. So we've done that here where we
say for each item, and that's a balance.
We started at zero,
that means that an initial deposit,
an initial balance rather should be itself a deposit.
So I will make a deposit of the initial balance.
We'll start it today.
This is cool. We haven't done dates yet, DateTime.
>> Yes. That needs a date. That makes sense.
>> Or you can say "DateTime.Now".
Then no one passed in a memo.
So I'm just going to say I'm initial balance, starting balance.
So then I would think,
we go make deposit.
It's going to add a new transaction to the list,
and then we would expect that your balance would be,
what do we give you, $10,000?
>> Yes. So I'm going to lose that probably,
where I could deposit that in the bank account.
>> Right. But see, when you construct
the Bank Account, the $10,000,
here's our constructor, The deposit
of the initial balance is $10,000 so you should not lose that.
>> Okay.
>> Then when we check the result-
>> Well, I have to give it to the bank. That's what I'm saying.
>> Yes, you do.
>> The bank is not giving it to me anymore.
>> No, that's a good point. No free money in our tiny bank.
>> Dammit.
>> There we go. Look, so now we're back where we were,
but it's more object-oriented,
and that's really interesting.
So then, now we can start simulating our bank, so let's do this.
Let's take a little bit of money out
and just see what the difference is.
So what? "Account.Make Withdrawal". What do you want to buy?
>> Let's buy a hammock.
>> How much does a hammock cost? A $120.
>> Yes. That's like a good hammock on Amazon.
>> It's a high quality, fancy. "DateTime.Now", "Hammock".
Then we're going to go and "Console.WriteLine Account.Balance".
Remember that balance is calculated from
our list of transactions. Let's see if this works.
>> Great.
>> Look at that.
>> There we go. So my withdrawal was
calculated and now it's output as my balance,
which is now $120 less than what I had.
>> Absolutely. So let's take
a little bit of a break here, and then we'll come back.
Let's actually print out the entire ledger
and make it look like a real checkbook or real bank account.
>> Like for all my transactions.
>> All your transactions.
>> All right.
>> Let's do it.