Hello and welcome, I'm Dave. Today we will complete an object-oriented programming practice project in Python.
And I'll provide links to all resources in the description below.
I've got VS Code Open. I have a folder for Lesson 20 over here on the left.
And two lessons ago, we covered Python, classes, and objects.
And in the last lesson, we learned about Python exception handling.
So today, we'll take what we learned from both of those lessons and complete a Python-pack practice project.
Try saying that three times fast, Python-packed this project, using object-oriented programming or OOP, if you will.
So let's start by creating a bank accounts file.
So I'll have a new file, I'll name this bank underscore accounts.py.
Now this file is going to hold the classes for several different types of bank accounts.
But let's start by just creating our first class, which will be bank accounts.
So I'll type class and then bank account.
And now inside of this bank account class, we'll start with our init function.
And remember that's two underscores and then init and two more underscores.
And it will receive self and then it needs to receive an initial amount that we're opening our bank account with.
So I'll type initial amount here.
And then also let's give this an account name that it receives.
And then inside this initializer function will say self dot balance equals the initial amount.
And then we'll also say self dot name equals the account name that it receives.
And then we're going to print some output here just to get some output when we open our bank account.
So we'll say print, let's make this an F string.
And inside the string, we'll start with a line break.
So we start on a new line and I'll say account.
And then inside of this, we're going to wrap this in single quotes.
We'll say self dot name.
And then another single quote.
And then we'll say created.
But a period, another new line.
And now let's say balance equals and a dollar sign.
Because this will be in US dollars and we'll say self dot.
Balance and I'm going to press all Z to wrap this down to the next line as it starts to get longer.
And then we'll format this.
So we put a colon and we'll put a dot to F.
So that means we'll have two decimal points in our self dot balance.
And that would represent the sense if there were any sense to go along with our dollars.
And that's all we need to open an account.
So let's save this file.
And now in our file tree, let's create another file.
And let's call this OOP underscore project dot pie.
And in this file, we're going to import everything from our bank accounts file.
So we'll do that with from bank underscore accounts.
Import and then we'll put an asterisk, which stands for all.
And then I'm going to create an instance of a bank account.
So I'll say Dave equals bank account.
And we'll start our bank account with $1,000.
And we'll name the bank account Dave.
And let's go ahead and create a second bank account.
So I'll do shift alt and the down arrow to copy that down.
I'll double click Dave, which selects the first one here online for.
And then control D. Now I've selected both.
And I'll just type Sarah and it will change Sarah with an a at the end.
And it will just change the name on both.
Now let's go ahead and change Sarah's deposit amount here that she starts her account with.
And she'll get $2,000 to start her account.
So now let's save this file.
And let's go ahead and from the drop down menu, choose run python file.
And you can see we've got a message here that says account Dave created balance equals 1,000.
And then account Sarah created balance equals 2,000.
Now I want to change this view so this appears on the right.
So we should be able to go to view appearance.
And as I have done in previous tutorials, choose oh, it's panel position.
And then choose right.
And now this is over here on the right.
Our code in the middle.
And we see our file tree on the left.
And we're not going to add any more files.
So we can go ahead and press control B.
I hide that file tree and have a little more room for our code.
And we can switch between the files at the tabs here at the top.
So now let's go back to bank accounts and let's add a method to our class.
And let's call this method get balance.
And it's going to receive self.
And that's all it needs to have because it's just going to output the balance of the account.
So we'll say print.
Make this another F string.
And inside this, we'll once again start with a new line.
And then we'll say account.
And we'll say self dot name.
So it names the account.
And of course another single quote.
Oh, we need a single quote to start with.
I said another and we didn't put the first one.
There we go.
So we're just wrapping that in single quotes in the output.
As we see over here, where it says account,
Dave and account Sarah.
So after account and the name,
then we'll just say balance equals.
We'll have a dollar sign.
And now we'll essentially have the same thing that we had up here.
So we can just copy this where we had self dot balance.
And we format it to have two decimals.
So I did copy C and now our control C and then control V to paste.
And so that is our method get balance.
Now let's go ahead and call that over in our OOP project file.
So just underneath where we opened our accounts,
I'm going to type Dave dot get balance and call that.
I'll copy that down with shift alt in the down arrow.
And we'll also get the balance or Sarah's account.
Now let's save the file and let's once again run this file.
And now I need this just a little bit wider.
So it doesn't wrap down.
There we go.
So now we create both accounts.
And then we get the balance for each account with our get balance method.
Now let's go back to the bank accounts class once again.
And let's add a deposit method.
So we'll save death.
A deposit is going to receive self.
And it's also going to receive an amount.
So whatever the amount of the deposit is.
Here we'll say self dot balance.
And we'll set that equal to self dot balance.
Plus the amount that we've received.
And now I'm going to copy our print statement from above where we get the balance.
And I'll paste it below once again control C and then control V.
But I want to add just a little bit.
So before we have the account balance,
I'm also going to say.
The deposit.
Complete.
And then go ahead and save.
Now essentially we're getting the balance once again.
So if you wanted to just have a print statement that said deposit complete.
And then we call get balance.
We could do that also.
So how about we do that?
Since it looks like the same thing from above.
Let's go ahead and delete what we were going to print.
We can get this all on one line.
And it doesn't really even need to be an F string here because we're not inserting anything.
And it looks like I need a quote right here at the end of our print statement.
But after that, we could go ahead and call get balance as well.
And here we're going to do this with self.
Get balance.
Let's go ahead and save this method.
And now let's run the file with our OP project.
But we first, of course, need to call the method.
So I'm going to say Sara.
And now let's deposit.
And now we need to pass in a value we're going to deposit.
So let's deposit an extra $500 in the Sara's account.
We'll save that.
Let's go ahead and run the Python file.
And now we have deposit complete.
And it went ahead and called the get balance method once again.
So we can see Sara now has $2,500 in her account.
Now we need to go back to the bank accounts file.
And we need to add a withdraw method.
However, this gets a little more complex because any time you withdraw money,
you need to do a balance check to see if you have enough money or enough funds
to complete the transaction.
So for this, we're going to need to add a separate method called Viable
Transaction, which will make sure the transaction can be completed
before we go ahead and complete it.
And because of this, we're also going to want to create a custom error
that I'm going to name balance exception.
So let's scroll to the top of our file and just above our class bank account.
Let's create another class that I'll call balance exception.
And it's going to receive an exception.
And then inside this class, I'm just going to say pass because we won't put anything
extra here.
But this is a very descriptive exception because we're going to have a problem with the balance
if we don't have enough funds to complete the transaction.
So now let's scroll back down underneath the deposit method.
And let's add our Viable transaction method that we're going to need to use
inside of our withdrawal methods.
So I'm going to call this Viable transaction.
And then it's going to receive self and the amount.
And then because of this, we'll say if self got balance is greater than
or equal to the amount, then we can go ahead and complete the transaction.
So we'll just return.
But otherwise, we'll just have an else.
We're going to raise the exception.
So this will be the balance exception.
And inside of this, I'm going to put an F string.
Let me go ahead and close the terminal now.
So we have a little more room to read this F string.
Say F.
And then inside of this slash in for a new line and sorry.
Account.
And now we can put self name.
But I want that in single quotes again.
Self dot name.
Another single quote.
And after that, only as a balance of.
And now let's put in our self.
Balance and our format here of two decimals.
And it looks like we're pretty much complete.
We just need our closing parentheses there.
No, we actually have the closing parentheses down below.
So I just put that on separate lines.
So that is our balance exception and the message that goes along with it.
Which remember, we created the class above.
And now we're just passing in the message with the exception here.
As we raise it inside of the viable transaction method.
And we're going to use this method in our with draw method.
So now let's create the with draw method.
So just underneath this, we'll say deaf.
And have our with draw.
And it's going to receive self and an amount as well.
I'm going to scroll for just a little more room here.
And now inside this method, we're going to start with a try.
Because we also want to catch an error.
If we actually raise that exception.
So we'll have self dot viable transaction.
And we'll pass in the amount.
And here's where an exception could be raised.
And we'll want to catch that below if we have that.
Accur.
So we will handle that in just a moment.
But now let's go ahead and say self dot balance.
It's going to equal.
Self dot balance.
Minus.
The amount.
So we're beginning the withdrawal here.
Because if we make it past.
This self dot viable transaction.
And an error is not raised or an exception is not raised.
Then we will make it onto this line.
Because then it will be okay to go ahead and complete the withdrawal.
So now let's say print.
And we'll have a new line here.
And we'll say withdraw.
Complete.
Now on the next line.
We can go ahead and say self dot get balance once again.
To show the balance after with the withdrawal is complete.
But now after our try we can have our accept.
This will be a balance exception.
As error.
Now inside this accept block.
We'll just print.
And this can be an F string.
And here we'll just say.
With draw.
Interrupted.
There we go spelled that correctly.
And we can go ahead and pass in.
The error message right here as well.
Which of course already starts on a new line.
So I didn't put a new line there to go with it.
So this will print out with draw interrupted.
And then of course fill in this information right here.
That goes along with the balance exception.
Now let's head back to our OOP project file.
And underneath the deposit for Sarah.
Let's go ahead and say Dave.
With draw and let's withdraw an amount.
We don't have like $10,000 here.
Go ahead and run this file once again.
And we can see.
Just bring this over just a little bit.
But sorry, the withdraw is interrupted there.
It says sorry.
Account Dave only has a balance of $1,000.
Maybe I could bring this in a little bit.
So it reads better.
It's not wrapping too well.
There we go.
So sorry.
Account Dave only has a balance of $1,000.
So we raised our error and it did work as expected.
Now let's go ahead and add a withdraw that will work.
So Dave withdraw.
And let's just withdraw $10.
Now.
And let's run this file once again.
And now it says withdraw complete.
And Account Dave has a balance of $990.
Because if you remember above, we opened the account with $1,000.
Now let's finish our first class here, our bank account class.
Let's finish this with a transfer method,
where we could transfer money from one account to another.
And this method is going to also check for a viable transaction.
Because we'll need to withdraw money from one account and then deposit it into another.
So it's also going to call those methods.
So I'm going to once again scroll up for a little more room here.
And we'll start our transfer method.
So we'll say death transfer.
It's going to receive self.
It's going to receive the amount.
And it's going to receive the account that we want to transfer money to.
Once again start with a tri block.
And I'm going to print and I'm going to start a new line.
And I'm going to make it rather obvious that we're starting a transfer here.
So 4, 5, 6, 7, 8, 9, 10.
I'll put 10 asterix.
And then I'll put two new lines.
And I'm going to say beginning transfer couple of dots.
And then I'm on windows.
You can pull up an emoji.
However, you want to on your operating system.
But on windows, I can press the windows key and the period.
And my emoji selector comes up.
And I'm going to type rocket.
And I'll just put a rocket here for our beginning message.
Now, after the transfer begins, then I'm going to say self dot viable transaction and pass in the amount.
So we make sure that it is a viable transaction that can proceed.
And we'll say self dot withdraw.
And we'll pass in the amount once again.
And then we're going to say account.
This is the account we passed in that we're transferring money to.
And we'll call the deposit method on that account.
And we'll pass in the same amount there that has been withdrawn from the other account.
Then we're going to print.
And here we'll say slash in with a new line once again.
And then transfer complete.
And I'll put an exclamation mark.
Once again, bringing up my emojis.
I want to use a check mark here as well.
So now we have our full transfer complete message.
Let's go ahead and put some more asterix at the end of this too.
So I'll do a couple of new lines.
And then 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Once again, just to match that beginning message.
So there's our full try.
And then we have our accept as well.
And this will be.
Accept.
Balance exception.
As air.
And inside the accept block, we will print.
I have an F string here.
And then we'll say.
Transfer.
Interrupted.
And I'll once again pull up my emojis.
And I want an X.
I'm just going to use a red X there.
So it's obvious.
Itches our attention.
And I'll close my emoji selector space over.
And we're going to.
Ask the error into the string as well.
So we get the message from the air.
Now let's go back to our OP project.
And then let's transfer some money.
So I'm going to say Dave dot transfer.
And first I'm going to try to transfer.
$10,000 once again, which should trigger our transfer error.
Now not the withdrawal interrupted, but we should see transfer interrupted.
Let's try that out.
So we'll run the file.
And clearly, I forgot to pass in what else was needed, which is the second account.
I need to actually transfer this to.
Sarah.
So I'm transferring $10,000 to Sarah's account.
So we had an error when we didn't pass in.
The other.
That was needed here.
So now let's go ahead and save this file once again.
Run this.
And yes, we had beginning transfer.
But then the transfer was interrupted.
And it says, sorry, account Dave only has a balance of $990.
So no way we could transfer in $1000.
Let me do shift alt in the down arrow and change that transfer to $100 that we're going to send to Sarah.
So we'll see the beginning transfer and the transfer interrupted again.
But then after that, we're going to look for a successful transfer.
Let's run the file again.
And yes, so above the transfer was interrupted.
But now here is a full transfer.
So we have beginning transfer.
Then the withdrawal is complete because we call the withdrawal method.
And now it shows Dave has a balance of $890.
And then it says deposit complete.
And it shows Sarah has a balance of $2600.
And finally, it says transfer complete.
And we have completed our main bank account class.
But now let's go back to the bank accounts file.
And let's start a new class.
So underneath this, we're going to create another class.
And this one is going to use inheritance from the bank account class.
And this is going to be an interest rewards account.
And I'll abbreviate account like that.
And then we're going to pass in bank account, which is the parent that we're going to inherit from.
Now there will be no new properties.
So we don't need to create a new initializer at the top or a knit function.
But we are going to overwrite the deposit method.
Because any deposit in this account gets an added 5% to the amount that's going in as the deposit.
That's the rewards.
So here, let's say, deaf deposit once again.
And we can see visual studio code wanted to fill everything out for us.
Already returning and calling super as the deposit amount.
That's very nice.
But that's not exactly what we want.
So I'm going to delete this second line that VS code added from me automatically.
Still I'm going to say self.
Balance is going to equal self.
Balance.
And then I'm going to say plus the amount that we're passing in.
But if we want to add 5% to the amount, we don't want to just add the amount.
So I didn't put a parentheses here just to indicate our order of operations.
And we'll say amount times 1.05, which would actually add that 5%.
I'll close the terminal window over here again.
So this can stay on one line.
So we're not just adding the amount to the balance.
But we're adding an additional 5% to that amount.
After we do that, we can say print.
And then we'll just, we don't need an F string.
We'll just have a line here that says deposit.
Complete.
And then on the next line, we should be able to go ahead and say self.
Got to get balance once again.
And this is really all we need for our new class, because it should inherit everything else
from the parent class bank account.
So we were just overwriting the deposit.
So now it has the interest rewards added.
Let's go back to the OOP project file and let's try this out.
So first, let's start by creating a new interest rewards account for Jim.
So I need to say interest rewards account.
Now we're going to open this with a thousand dollars for Jim also.
And we'll name the account Jim.
I'll scroll up for some more room.
Underneath opening the new account.
Let's go ahead and call Jim get balance.
So we can check that method out.
Let's also say Jim dot deposit, which remembers also going to use get balance in there.
We'll just pass in an extra hundred dollars to Jim.
So we should have a eleven hundred dollars, but also another five percent of one hundred dollars.
So at this point, when we check the result, we should be looking for one thousand one hundred and five dollars
in Jim's account after that deposit, because he would get a reward.
Then let's go ahead and say Jim got transfer and Jim will transfer one hundred dollars to Dave.
Now the transfer method, if you remember, not only uses the deposit method, but it also uses the withdraw method.
So that should also indicate that those methods are working.
So let's save this and run our file once again.
Okay, let's scroll up so we can see where everything starts here.
And we have account Jim created with one thousand dollars.
And now we got the balance, which was one thousand dollars.
And then Jim deposits another one hundred dollars.
And with his rewards account, he gets an extra five dollars, which is an extra five percent of that one hundred.
So now Jim has one thousand one hundred and five dollars.
Then Jim transfers one hundred dollars to Dave.
So we begin the transfer.
It says the withdraw is complete.
And then account Jim has one thousand and five dollars instead of one thousand one hundred and five dollars.
And now the deposit is complete for Dave.
Dave doesn't have a rewards account.
So he just gets the one hundred dollars on that transfer deposit.
And it's back up to nine hundred and ninety dollars.
And then the transfer is complete.
So everything with our interest rewards account worked as expected.
And you can see it has all of the methods that it inherited from the bank account class.
Now let's go back to the bank accounts page one more time.
And we have our interest rewards account class here.
And underneath this we're going to create one last class.
So let's call this class.
And here I'm going to say savings account.
Now savings account is going to inherit from an interest rewards account.
I'm going to scroll up for a little more room here.
Now our savings account is going to add another property to the class called
fee.
And so in order to do this, we are going to need to define that init method for the class again.
And then we'll use super to bring in everything from the parent class.
And the parent class as we mentioned is an interest rewards account.
So let's start here.
Just underneath and we'll say,
Def and then we'll put in our init method,
which we'll receive self.
It will receive an initial amount.
As our parent class bank account did,
which is actually a grandparent going up two levels there.
And then underneath here,
we'll go ahead and call at super function.
And then we'll have a knit after it.
And now we put in initial amount once again.
And after initial amount, we'll also put in a count name.
Now, no, no colon after that.
I accidentally typed that for a second.
But no colon there.
And then we'll add our new property,
which is self.
And there's going to be a five dollar fee for any withdraw from this account.
Now, this is inheriting from the interest rewards account.
And it's a savings account.
So also, any deposit will get that additional 5%.
Just like Jim's account did when he had an interest rewards account.
However, it's a savings account.
So anything with drawn from this account is going to have a five dollar fee on every withdraw.
So to do that, we need to go ahead and override the withdraw method by adding a new withdraw method here.
So this is going to receive self and amount.
And now inside the withdraw method,
we're going to have a try.
We'll start with checking to see if we once again have a viable transaction.
So we're going to have the amount plus self dot fee,
because that would be the total amount with drawn.
Let me go ahead and close our terminal once again.
So we're not just checking the amount.
So this is a little bit different than the initial withdraw method that we had above in that bank account class.
After this, we're going to set the self dot balance equal to self dot balance minus the amount plus the fee that is added on.
So that total needs to be withdrawn from the balance.
Then we can print and we'll start with a new line once again.
And we'll say withdraw complete.
And then after that, we should once again call the balance with self dot get balance.
And we'll find out what the new balance is after that withdraws complete.
But if it's not a viable transaction,
we once again need to catch the error or the exception that is raised.
So we'll say accept balance exception as error.
Now we will print and here we will have an F string.
We'll have slash in for a new line.
And then we'll say withdraw interrupted.
And then after that, oh, let's not put a period, let's put a colon.
Then we'll print out the error message as well.
Now we can save this file and check it out.
I'm going to go back to the OOP project file.
And underneath Jim, let's create a new account for blaze.
And blaze is going to have a savings account.
And let's go ahead and pass in $1,000 when blaze opens as account.
Let me make sure I abbreviated savings account right.
If I look at the class, yes, savings account.
So that needs to match up.
We did call the right class there.
So we're creating an instance of a savings account.
So it starts with a thousand dollars for blaze.
And then we'll save blaze.
And we'll once again check the get balance method there, which is also called when we make it a
deposit.
And let's go ahead and make a deposit for $100, just like we did for Jim.
And finally, we'll save blaze and transfer.
And we'll try to transfer $10,000 to Sarah.
And let's go ahead and save the file.
And we'll run the file and see what we get for output.
So drag this over just a little bit more.
There we go.
Let's scroll up here where we go ahead and create the account for blaze.
Here it is.
Blaze account blaze created.
And then the balance is $1,000.
Then we called the get balance method.
And you can see has $1,000.
And then we deposited $100.
And since he also has a savings account that inherited from the interest reward.
Just like Jim, he gets that extra 5%.
So now he has $1,105.
And then we begin the transfer, but it is interrupted because blaze had $1,105.
And he could not transfer $10,000 to Sarah.
So we get the message that blaze only has a balance of $1,105.
So instead of transferring $10,000, let's go ahead and bump that back.
Do $1,000.
And now we can save and run this again.
Well, once again, have a transfer interrupted.
But then we should have a successful transfer.
So let's check that out.
Here it was interrupted.
But now here's the successful transfer.
We get the beginning transfer.
But with draws complete.
And now blaze only has a balance of $100.
Because he had $1,105.
But he has a savings account.
So also that $5 fee applied.
And it took away the $5 with the fee.
And then he transferred $1,000 leaving blaze with $100.
Then the deposit is complete and Sarah now has a nice bank account with $3,600.
And we get the transfer complete message.
Okay, I think you're getting the idea overall.
Now we've created three classes here.
And we've used inheritance as we've created them.
And then we've also added a new property to one class.
And we have used a couple of methods more than once,
essentially overriding one to add some new functionality
as these classes needed that new functionality.
Now these could be used in a larger banking or part of a personal finance program, for example.
But this is where we're going to go ahead and stop this OOP practice project.
But I hope it has helped you review everything we've learned about Python objects
and classes as we move forward.
Remember to keep striving for progress over perfection.
And a little progress every day will go a very long way.
Please give this video a like if it's helped you.
And thank you for watching and subscribing.
You're helping my channel grow.
Have a great day and let's write more code together very soon.
Einführung in das OOP-Projekt
Erstellen der BankAccount-Klasse
Methoden in der BankAccount-Klasse
Einzahlungs- und Abhebemethoden hinzufügen
Fehlerbehandlung in Transaktionen
Überweisung von Geldern zwischen Konten
Einführung in das Zinsbelohnungskonto
Erstellen einer Sparkonto-Klasse
Abschluss des OOP-Projekts
Welche wichtigen OOP-Prinzipien werden in diesem Projekt gezeigt?
Wie verbessert die get_balance-Methode das Nutzererlebnis?
Welche Rolle spielt die individuelle Fehlerbehandlung in Banksystemen?
Wie können wir Gelder sicher zwischen Konten überweisen?
Was macht das Interest Rewards Konto so besonders bei Einzahlungen und Abhebungen?
Wie beeinflusst die Gebühr fürs Sparkonto die Transaktionen?
Welche Lektionen über Vererbung können wir lernen, wenn wir verschiedene Kontotypen erstellen?
Wie kann dieses OOP-Projekt in echten Banksystemen angewendet werden?
Der Inhalt beginnt damit, das Praxisprojekt vorzustellen, das darauf abzielt, die Prinzipien der objektorientierten Programmierung (OOP) in Python zu demonstrieren. Es wird davon ausgegangen, dass ihr bereits in vorherigen Lektionen über Klassen, Objekte und Ausnahmebehandlung gelernt habt. Der Autor erstellt dann eine neue Datei für das Bankkontensystem und definiert darin eine BankAccount-Klasse. Diese Klasse hat eine Initialisierungsfunktion, die einen Anfangsbetrag und einen Kontonamen als Parameter entgegennimmt und sie den Instanzvariablen zuweist. Die Funktion gibt auch eine Ausgabe aus, wenn das Konto eröffnet wird. Der Inhalt konzentriert sich darauf, die Anwendung der OOP-Prinzipien zu veranschaulichen, wie Kapselung (verstecken interner Daten), Vererbung (abgeleitete Klassen erben Eigenschaften von Basisklassen), Polymorphismus (Methoden führen je nach Kontext unterschiedliche Aktionen aus) und Abstraktion (verbergen von Implementierungsdetails). Indem die Zuschauer durch dieses praktische Projekt geleitet werden, zielt der Inhalt darauf ab, ihr Verständnis dieser grundlegenden Konzepte der objektorientierten Programmierung zu festigen.