Now as we start using objects and their attributes and methods more and more,
we're going to start to see why it is that they are so useful and so loved by
programmers. Now,
I think one of the non-programmers that explained OOP the best is actually the late
Steve Jobs.
And he has this brilliant example that makes it really clear why objects are so
great. In his story
he talks about this concept where let's say you were traveling to Japan and
after a long flight,
you finally land and you go outside and there's beautiful cherry blossoms, Mt.
Fuji is looking wonderful. But unfortunately on the flight,
you had soiled your T-shirt.
You were overly eager with the tomato juice and you spilled everything upon
yourself. So what do we do? Well,
you're in this unfamiliar city and you have no idea
even whether the hell you are let alone where to find the nearest
dry cleaners or launderette. And plus on top of that,
you don't have the local currency. They take yen,
but you only have pounds and you don't even speak Japanese. So what do you do?
It's going to be a real struggle trying to solve this problem. Well,
you staying at a hotel, right? And the hotel staff probably speak English.
So you go the receptionist and you tell it the hotel that, Hey,
my t-shirt is soiled and they say, Hey, no problems. We'll take that from you.
And we'll go and handle all of that messy stuff.
We know how to find the dry cleaners,
we can pay them in the local currency and we speak the local language.
And what you get back is just a nice clean t-shirt.
So this is similar to the process of working with objects. Essentially,
if you have this hotel object
which has a blueprint for how to handle dry cleaning
some sort of clothing, then all you have to do is just get the object hotel and
then call its associated method dry clean,
and then it will do all of the messy stuff for you.
And you don't have to worry about the implementation of this function at all.
So now you can work with multiple objects
getting them to do different things and trust that they know how to manage their
data and how to perform the necessary functionality.
Coming back to a problem that we've already solved
creating that coffee machine with all of the program requirements. Well,
it's time to put our Object Oriented Programming knowledge to the test because
we're going to be making a OOP version of our coffee machine.
Now the program still has the same requirements
as before. You have to print reports for
all the resources, you have to check if the resources are sufficient,
process coins, check transaction is successful, and finally make coffee.
But in this case, you're going to be working with objects.
In the course resources,
you'll find a link to the starting project for the OOP coffee machine.
So if you head over to this link,
you can see that there's a whole bunch of code that we've already written for
you. Now,
the idea here is you're not going to touch any of these other files,
the money machine, the menu, or the coffee maker. All that code has already
been written for you
and it's almost like you're working with an external library.
You're just going to trust that they do what they say they do.
And you'll find a link to this coffee machine documentation that I've created
which describes in detail what each of these classes or blueprints allow you to
do. So once you construct an object from the menu item class,
then you have access to the name attribute, the cost attribute,
ingredient attributes, and there's also descriptions of what it should do
and what an example might be.
You have access to all of these classes and you can build objects from each of
these classes to solve this project.
In order to work with PyCharm, all you need to do is to download the zip
file it with all the starting code from our course resource website.
Alternatively,
if you are already logged in on Repl.it, simply fork the project and then click
on these three dots to download the zip file here. Now,
once you found that in your downloads folder,
all you have to do is uncompressed or unzip it.
And then you can go into PyCharm and open it here.
So click on the open button and then navigate to where you had that folder
downloaded. Remember it needs to be unzipped.
If you're on windows, right-click the zip file and choose extract all.
Then click extract.
You should see an unzipped folder popup next to your zip file.
If you're on Mac then all you need to do is just double-click on the .zip file.
Now, if we click open on that folder,
then it will open up this project. And when you open up the project folder,
you'll see all of the same files.
Now you're only going to be working from the main.py. In fact,
I don't even want you to care that much about the code that's in the other
files.
It has class definitions and a whole bunch of other things that we're going to
learn about in the next lesson.
But for now, I want you to treat it as if it is an external library
which you're just going to use. At the very beginning of the main.py file,
I've already imported all four classes that you are going to be using; menu,
menu item, coffee maker, and money machine.
Now all of these classes are documented in the coffee machine documentation
website.
All you have to do is head over to this website and read through what each of
the classes do. Some of them have attributes and methods,
others just have methods.
So each of these classes have already pre-written code that do a lot of the
heavy lifting and all of the code that we had to write when we wrote the coffee
machine code from scratch.
But this time you're only going to be using these four classes and the objects
created from them in order to achieve exactly the same goals as before.
So while the goals are fresh in your mind from the previous day,
you should review the code from the previous day and then have a read of the
documentation and see how you would implement exactly the same project
but this time using only the objects created from these classes.
You'll need to spend some time on this because you'll need to think through how
to create objects from these blueprint classes and then how to use the objects,
how to call the methods and the logic that's required to put everything
together. Pause the video
now, have a think about this problem, and try to make the coffee machine do
exactly the same and have all the same functionality as the coffee machine that
you created in yesterday's project.