7.2 Bank Account Part 1
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
Hi and welcome back to the course. I hope you guys are doing well. And if you remember in the last video lecture, we discussed one of the components of O. P. Called Abstraction. And in this video lecture, we will be implementing that concept of obstruction by creating a bank account program.
00:19
Now, at this point, I like you to go ahead and open up Intel, Ajay, and in intelligence, I'd like you to go ahead and create four classes, one of them called bank Account, as you can see here than checking account savings account and then a tester class.
00:36
No.
00:38
Before we begin, I've listed out some comments that we can use as a sort of guide or guidance to help us through this lab.
00:47
So in this program, I'd liketo have, um, a simple bank account in which I can display, uh, the users,
00:55
um, balance of their checking account, and they're saving account. And I want to be able to deposit and withdraw money. It's a few small functionalities. So if we begin, we can see that the first comment is stating that we need to store the customer's name.
01:15
So when it comes to storing a customer's name.
01:18
We are going to be using a variable to do so because storing almost always refers that we need to use a variable to store that data now before we actually continue. One thing that I do not want to forget is that
01:32
in order to create an upset class, you need to have the key word public and then abstract, which is the word that defines it as a the keyword that defines it as a abstract class. If you do not have that, then your class is not going to meet an abstract class.
01:49
All right, so going back to
01:52
our customer name. So one more thing I'd like to introduce is that whenever we make variables, especially when it comes to programs like these where we're making a bank account program, we do not want to make the customer's name public. And we want to use encapsulation, which we discussed in the previous module.
02:12
Two actually
02:13
hide data. So in this case, I would make a private string
02:20
and then we could call it
02:23
cost name.
02:24
Okay, Perfect.
02:25
Then it says store the balance now, since balance is also something that we would like to keep private. I will say private end, since balance is also a integer and then
02:39
a colon.
02:40
Now we need to have a constructor. And in order to do that, you would write public bank account,
02:50
and then inside bank account, I would say String knew this is for a new customer name. You cost name,
03:00
and then we would have into new balance.
03:07
Now, this is for the initialization of these attributes.
03:13
Now I'm gonna set our custom name two. Or let's just get cussed Name too
03:19
new Coast name.
03:21
And then I'm gonna set balance to new balance.
03:27
New
03:28
balance. Perfect.
03:30
All right, so here we've made our constructor.
03:32
Now we want to make a get method.
03:36
We want to make it getters, or simply put a get method for our customer name and for our balance. How do we do that? So if you remember in, uh, encapsulation Sze lab in the previous model, we had quite a few getters and centers, and that was as simple as this. So you would type in public.
03:55
Sorry about that. So public. And then since I'm trying to get the customer's name, it is of the type string, which is what I'm getting or what I'm returning
04:06
and then get cost Name
04:11
a set of parentheses, curly braces and what am I returning? I'm returning the customer's name, which is a string, so that would then be
04:20
cussed name.
04:23
Perfect. Now I am doing the same with balance. But instead of returning a, um,
04:30
string on, we're gonna be returning.
04:32
Be balance. So return balance.
04:36
Perfect.
04:38
No, we want to be able to deposit money into the count.
04:44
Now, in order to deposit money into the count,
04:46
let's say let's take a real life scenario. Let's say I go to an A t m for any bank and I deposit money, that money, whether in my whether I have $0 or thousands of dollars in my account, that money is going to be added on to the current amount that I have.
05:05
So we need to keep that in mind when we make our method.
05:09
So first off, my method is gonna be public and it's gonna be able to type. And since I am returning of the balance at the end and I'll call a deposit
05:18
and then curly breed, sorry, parentheses
05:23
and then what I'd like to do is I'd like to pass in a parameter called amount.
05:30
Then we have our set of curly braces.
05:33
Now I want Thio increment s. I want to increment my balance by adding my amount variable to my current balance. Now, for our current balance, we have our balance variable. So for that I will do balance
05:50
plus equals, which is the same as a balance equals
05:54
imbalance
05:55
plus amount
05:58
that I can I can also do like that or I could just would plus equals to avoid writing balance twice.
06:02
So plus equals balance Now what that will do is it will add the amount
06:08
that I specify when I want to deposit a certain amount of money to my current balance. And then I like to return that balance. Return balance.
06:16
Perfect.
06:18
No, let's say we want to withdraw money. So for that, my method would also be public
06:26
end. And let's call it with draw.
06:30
No, this method I'm also going to pass in a amount that I'd like to withdraw and then a set of curly braces.
06:36
Now, for this method, I need to check if my balance is greater than zero. Because if it is not greater than zero, then it doesn't make sense to withdraw money from an account that has no money at all or hasn't balance of
06:53
zero.
06:55
All right, so for that, I'm gonna have a statement that says if balance is greater than zero,
07:03
then what I like to do is I would like to
07:08
balance.
07:10
So for that, I'm gonna do minus minus equals, which is just balance equals balance minus amount. But I'm just avoiding writing balance twice.
07:19
And then I'm going to be returning
07:23
my balance
07:25
Bonds. Okay,
07:26
Perfect. So what this method does over here and the reason I'm going over it carefully said so that you understand. So first we have our modifier, which is public.
07:40
And then I have the return time, which is in't, then the name of the method. I'm passing in an amount that I would like to withdraw. And then I'm doing a crucial check for whether I have more than $0 in my account. If I have more than $0 in my account, then I would like to subtract
08:00
from that balance. So subtract the amount that I'm passing in. That I would like to withdraw from that current balance. And as I mentioned, this is the same as doing or as writing balance equals
08:13
balance
08:16
minus amount.
08:18
Okay,
08:20
are Let's just leave that there. Okay? So I'm gonna pause this'll video right here. We're gonna continue on with the rest of the lab in the part two of this lab. Thank you very much for watching. And I look forward to seeing you in part two.
Up Next
Similar Content