Hey, welcome back.
In this video, you'll learn how to add new classes to existing programs without breaking the program.
So let's simulate a situation.
The client, whom we built this app for is complaining that customers are booking a hotel and then they
are not showing up.
So the client now is asking that we should add a feature to the program which asks the customers to
enter their credit card details and pay the booking when they do the booking through the app.
So as a programmer now we need to extend the program to satisfy that new requirements of the client.
So we need to add a new feature, a new credit card feature.
And when your program is in OP, meaning that it's organized in classes, then all you need to do is
add a new class without breaking the existing classes.
And that is actually one of the principles of o p.
You shouldn't change the existing classes, just try to add new ones so we can start the logic by looking
at the program main loop here.
So we print out the list of hotels here and then we get the ID of the hotel the user want to book.
So just to refresh your memory, so the users see this and they enter something like 188 for this row.
So they want to book that.
But then let me stop this and then we check if the hotel is available and then we let the user book
the hotel.
But now, before we let the user book the hotel, we first want to tell the user, Oh, first let's
create a credit card instance using a class, which we're going to have to create in here.
So let's start to create a class class credit card.
Let's pass for now.
So in that credit card class, the user will enter some data.
Or perhaps we need to ask the user first, like, you know, card number.
We can also user through an input function.
If this was a web app, then we would show the user some text boxes where they can enter the credit
card number and the expiration date of the credit card number, the name or the credit card, and also
the CVC number, which is like a three digit number in Visa and MasterCards.
So these are the data that the app would need to validate that credit card.
So we can ask the user for these three like, Oh.
Enter the credit card number and then another one would be expiration date.
You know, we could ask again for input, but I would want to.
Just assume that we got these from the user.
For example, we got.
The number se.
One, two, three, four, five, six.
Cards have something like 16 digits card numbers.
So let's assume that the user entered this number.
So that's ten digits.
One, two, three, four, five, six.
That's 16 digits.
So the expiration date would be 12.
26 of December 2026.
That's the year.
And then the holder, the name of the holder, let's say, John Smith.
And then finally, the CVC number, which is a three digit number, let's say 1 to 3.
So let's suppose that we got this from the users using input functions and then those variables we pass
them in here.
So we're skipping the input function step just for simplicity.
So once you have created a credit card instance, then you want to say, if credit card don't validate,
then you want to book the hotel if this basically is equal to true.
So that's that would be some a similar method like this.
So return true if we find that in this card, that CSV file which you can find the touch in the lecture
resources if we find that that card that the user entered here with this data, if that card is an existing
records in this database, which is just a CSB file for now, but in real life, this would be some
remote databases where your program has access to through an API and the program can check if a code
exists in that database.
In this example, we want to keep things simple and have those data in a C zip file.
So if we validate the credit card instance, this credit card instance, then we book the hotel, then
we ask the user for their name, then we create a reservation ticket, we print out the reservation
ticket, and we leave this as it is.
So this else block here is part of this.
So if the hotel is available, this will happen, right?
Otherwise, if we are stopped at this point, if the hotel is not available, which is the case when
this here is.
No.
The availability, the available field.
In that case we print out the hotel is not free.
If you like here you can add an else statement which belongs to this group.
If else.
Right.
Else.
Print.
There was a problem with your payment.
So if for some reason we couldn't validate the credit card, we showed the use of this message.
So they know that there's something wrong with their credit card.
So with that, we can go up to the credit card class here and we start to add methods to this credit
card class.
The first method is underscore, underscore, init, underscore, underscore self.
Now the question is the big question always is what parameters should in it take.
The way we instantiate its credit cards here suggests that this should take these one, two, three
four parameters.
Right so number.
Expiration.
Holder and CVC number.
However, this is debatable because some of these parameters you can also have them in the other method
validate.
Right.
So perhaps you could have only the number there and these three in here.
And then in here you only provide the number when you create an instance of the credit card and you
move these three to the validate method because they are now part of the validate method.
Right.
Remove this comma as well.
So the big question is what is better?
Is it the first version where we had all the parameters in here, or is it this version better?
Well, to answer this question, you should think of what is the minimum set of parameters you need
when you instantiate when you create an instance of a credit card.
So can a credit card be defined using only its number?
Well, the answer is yes, it can be defined, but it cannot be validated.
Right?
Because to validate a card, to make a payment, you need all these data expiration date, the holder
and the CVC.
So it does make sense to have only the number here and to have all this other parameters part of the
validate method.
So think of this like, Oh, this is like the user name of this object and these are like the passwords.
So I think it does make sense to have it like this, although some programmers would think otherwise.
So there's no concrete way a definite technique to do this.
So let's keep it like this and I'll do self number is equal to number.
I did this because we're going to need the number and this method, right?
So we need to access that number.
And the way to access it is by assigning number as an attribute of the instance.
So now we can access that as self, that number, right.
So how do we validate the credit card?
Well, first of all, you need to load the data frame out of this CSB file.
So let's do that.
We can do that outside here.
So you can say DF for four cards is equal to pandas.
Dot three C is V cards dot C as V?
That's the name of that file.
I'll do D type is equal to SDR.
So I want to load all the fields as strings because none of them has any mathematical meaning.
Right.
So this is a number, but it just an idea.
This is also just an expiration date which can be treated as a string.
The CVC field doesn't also have any mathematical meaning.
We're not doing mathematical operations, so it's fine to load all these as strings.
Now, I would like to load this dataframe actually, as a dictionary.
Why?
Well, I'll explain you why.
So let me import pandas in a python console to show you how this dictionary will look like.
Import pandas DF cards.
DF cards looks like this.
So a dictionary?
Sorry.
A list of dictionaries and a dictionary represents one row of the cards data.
So.
One, two, three, four.
That role that I selected here reflects that dictionary here.
I did convert the dataframe into a dictionary because it's easier to check if all these.
Data are part of that list of dictionaries.
Right.
You'll see how it works.
Exactly.
So the idea is that now I can I can make a dictionary with.
The data which should look like this, let's say.
We have the number first rate as a string number.
We just sell that number and then we have expiration.
So that's the key.
The value would be expiration.
The variable, this parameter which comes directly in the validate method.
Holder.
CVC.
CVC let me split this because it's too long, just for readability and following the PEP eight style
guide.
So once I have these data as dictionary, so I get the data from the user, right?
I get the number in here, I get the expiration holder and CVC in here and then these are transported
to here.
And then we create a dictionary and then we say if cart data in DF cards.
So that data frame actually it's not a data frame, as you see, it's a list of dictionaries.
So we're checking if that dictionary is one of those dictionaries in the DF cards list.
Pretend to.
Right.
And you can say ls return false, although it will work even if you don't do that because this will
return none by default.
Same goes with this available method of the hotel class.
But if you want to be explicit and make the code more readable, you can do the return false.
So some people prefer this method of doing things more explicit.
So that means that this validate methods in here will return.
True.
And that means this will be evaluated to true.
And that means we book the hotel and so on.
So, yeah, that's it.
Let's run the program.
So run it.
Now, all these hotels are currently not available, so if we do a 188, we're going to get hotel is
not free as expected.
So this was executed, this line.
Now let's go to hotels as we change the second one snow palace in New Delhi to yes, they do have some
availability currently because it's not snowing in Delhi.
So they don't have lots of customers.
Usually it snows there and anyway, it's run and enter the idea.
So we said Snow palace, which is.
Yes.
Now 188 press enter.
There was a problem with your payment.
So something is not going as smoothly.
That's because the number here doesn't reflect the number there.
So let me just add it.
The number of the cards here plays that there are.
Try again.
So Snow Palace is still available.
So 188 enter.
Oh, enter your name.
Now we're being asked for our name ordered.
And thank you for your reservation.
Here are your booking data or this hotel name and so on.
So now our card was validated and we successfully booked the hotel.
Of course, you can have this database more complex, so you could add like a balance here and then
have.
How much money each of these cards have.
Right.
And then you could.
Extend the class credit card by adding another methods here like define pay.
And then this pay method would modify this data frame, right, and subtract the payment the price of
the hotel.
If the hotel had a price, which also is not the case because our hotel's database was also simple,
but you get the idea so you can add more methods to a class when you want to extend the class.
So it's better to add the methods here than to modify this validate methods.
So each method should be responsible for one and one thing only.
So this method validates the card and that's its duty.
The other method would pay, which means subtracts something from that hotel.
Right.
And so on.
Anyway, I don't want to go into the rabbit hole with so many details.
I want to keep the focus on P.
So that's the cards.
And yeah, that's how you can extend an existing program.
So you add a new class.
And then you modify the main loop.
Where do you create instances of the classes and where the logic of how the program will run is?
So I hope this is helpful for you to understand classes even better.
And yeah, we have some more videos today, so I'll see you in the next one.