1
00:00:00,210 --> 00:00:00,990
Hey, welcome back.

2
00:00:01,020 --> 00:00:07,510
In this video, you'll learn how to add new classes to existing programs without breaking the program.

3
00:00:07,530 --> 00:00:10,050
So let's simulate a situation.

4
00:00:10,410 --> 00:00:19,770
The client, whom we built this app for is complaining that customers are booking a hotel and then they

5
00:00:19,770 --> 00:00:21,330
are not showing up.

6
00:00:21,870 --> 00:00:30,000
So the client now is asking that we should add a feature to the program which asks the customers to

7
00:00:30,000 --> 00:00:36,330
enter their credit card details and pay the booking when they do the booking through the app.

8
00:00:36,540 --> 00:00:44,270
So as a programmer now we need to extend the program to satisfy that new requirements of the client.

9
00:00:44,280 --> 00:00:48,510
So we need to add a new feature, a new credit card feature.

10
00:00:48,510 --> 00:00:55,470
And when your program is in OP, meaning that it's organized in classes, then all you need to do is

11
00:00:55,470 --> 00:00:59,880
add a new class without breaking the existing classes.

12
00:00:59,880 --> 00:01:03,530
And that is actually one of the principles of o p.

13
00:01:03,540 --> 00:01:12,960
You shouldn't change the existing classes, just try to add new ones so we can start the logic by looking

14
00:01:12,960 --> 00:01:15,650
at the program main loop here.

15
00:01:15,660 --> 00:01:23,400
So we print out the list of hotels here and then we get the ID of the hotel the user want to book.

16
00:01:23,400 --> 00:01:32,310
So just to refresh your memory, so the users see this and they enter something like 188 for this row.

17
00:01:32,730 --> 00:01:34,200
So they want to book that.

18
00:01:34,200 --> 00:01:42,930
But then let me stop this and then we check if the hotel is available and then we let the user book

19
00:01:42,930 --> 00:01:43,770
the hotel.

20
00:01:43,770 --> 00:01:51,030
But now, before we let the user book the hotel, we first want to tell the user, Oh, first let's

21
00:01:51,030 --> 00:01:59,490
create a credit card instance using a class, which we're going to have to create in here.

22
00:01:59,490 --> 00:02:04,950
So let's start to create a class class credit card.

23
00:02:06,060 --> 00:02:07,500
Let's pass for now.

24
00:02:08,160 --> 00:02:13,470
So in that credit card class, the user will enter some data.

25
00:02:13,470 --> 00:02:18,660
Or perhaps we need to ask the user first, like, you know, card number.

26
00:02:18,660 --> 00:02:21,690
We can also user through an input function.

27
00:02:22,260 --> 00:02:28,890
If this was a web app, then we would show the user some text boxes where they can enter the credit

28
00:02:28,890 --> 00:02:37,440
card number and the expiration date of the credit card number, the name or the credit card, and also

29
00:02:37,440 --> 00:02:43,470
the CVC number, which is like a three digit number in Visa and MasterCards.

30
00:02:43,740 --> 00:02:49,720
So these are the data that the app would need to validate that credit card.

31
00:02:49,740 --> 00:02:53,160
So we can ask the user for these three like, Oh.

32
00:02:54,250 --> 00:03:01,630
Enter the credit card number and then another one would be expiration date.

33
00:03:01,790 --> 00:03:06,280
You know, we could ask again for input, but I would want to.

34
00:03:08,000 --> 00:03:12,000
Just assume that we got these from the user.

35
00:03:12,020 --> 00:03:13,850
For example, we got.

36
00:03:17,750 --> 00:03:19,190
The number se.

37
00:03:19,700 --> 00:03:22,450
One, two, three, four, five, six.

38
00:03:22,790 --> 00:03:27,140
Cards have something like 16 digits card numbers.

39
00:03:27,650 --> 00:03:30,800
So let's assume that the user entered this number.

40
00:03:31,400 --> 00:03:32,990
So that's ten digits.

41
00:03:32,990 --> 00:03:34,790
One, two, three, four, five, six.

42
00:03:34,790 --> 00:03:36,050
That's 16 digits.

43
00:03:36,050 --> 00:03:40,070
So the expiration date would be 12.

44
00:03:40,950 --> 00:03:44,410
26 of December 2026.

45
00:03:44,430 --> 00:03:45,270
That's the year.

46
00:03:45,270 --> 00:03:51,480
And then the holder, the name of the holder, let's say, John Smith.

47
00:03:51,720 --> 00:03:57,590
And then finally, the CVC number, which is a three digit number, let's say 1 to 3.

48
00:03:57,600 --> 00:04:04,860
So let's suppose that we got this from the users using input functions and then those variables we pass

49
00:04:04,860 --> 00:04:05,550
them in here.

50
00:04:05,550 --> 00:04:09,570
So we're skipping the input function step just for simplicity.

51
00:04:10,480 --> 00:04:20,769
So once you have created a credit card instance, then you want to say, if credit card don't validate,

52
00:04:21,399 --> 00:04:30,130
then you want to book the hotel if this basically is equal to true.

53
00:04:31,000 --> 00:04:36,430
So that's that would be some a similar method like this.

54
00:04:36,430 --> 00:04:43,190
So return true if we find that in this card, that CSV file which you can find the touch in the lecture

55
00:04:43,190 --> 00:04:53,230
resources if we find that that card that the user entered here with this data, if that card is an existing

56
00:04:53,230 --> 00:04:59,920
records in this database, which is just a CSB file for now, but in real life, this would be some

57
00:04:59,920 --> 00:05:07,810
remote databases where your program has access to through an API and the program can check if a code

58
00:05:07,810 --> 00:05:10,120
exists in that database.

59
00:05:10,810 --> 00:05:16,060
In this example, we want to keep things simple and have those data in a C zip file.

60
00:05:16,420 --> 00:05:24,610
So if we validate the credit card instance, this credit card instance, then we book the hotel, then

61
00:05:24,610 --> 00:05:32,020
we ask the user for their name, then we create a reservation ticket, we print out the reservation

62
00:05:32,020 --> 00:05:35,650
ticket, and we leave this as it is.

63
00:05:35,860 --> 00:05:39,910
So this else block here is part of this.

64
00:05:39,910 --> 00:05:44,140
So if the hotel is available, this will happen, right?

65
00:05:44,380 --> 00:05:51,550
Otherwise, if we are stopped at this point, if the hotel is not available, which is the case when

66
00:05:52,990 --> 00:05:54,280
this here is.

67
00:05:54,280 --> 00:05:54,970
No.

68
00:05:56,560 --> 00:05:59,350
The availability, the available field.

69
00:05:59,770 --> 00:06:02,050
In that case we print out the hotel is not free.

70
00:06:02,080 --> 00:06:07,000
If you like here you can add an else statement which belongs to this group.

71
00:06:07,000 --> 00:06:08,410
If else.

72
00:06:08,410 --> 00:06:09,070
Right.

73
00:06:09,070 --> 00:06:10,360
Else.

74
00:06:10,780 --> 00:06:11,980
Print.

75
00:06:13,000 --> 00:06:17,020
There was a problem with your payment.

76
00:06:17,650 --> 00:06:23,380
So if for some reason we couldn't validate the credit card, we showed the use of this message.

77
00:06:23,380 --> 00:06:26,560
So they know that there's something wrong with their credit card.

78
00:06:27,250 --> 00:06:36,340
So with that, we can go up to the credit card class here and we start to add methods to this credit

79
00:06:36,340 --> 00:06:37,050
card class.

80
00:06:37,060 --> 00:06:42,210
The first method is underscore, underscore, init, underscore, underscore self.

81
00:06:42,220 --> 00:06:48,610
Now the question is the big question always is what parameters should in it take.

82
00:06:49,000 --> 00:06:58,510
The way we instantiate its credit cards here suggests that this should take these one, two, three

83
00:06:58,510 --> 00:07:00,460
four parameters.

84
00:07:00,460 --> 00:07:02,590
Right so number.

85
00:07:03,920 --> 00:07:05,330
Expiration.

86
00:07:06,830 --> 00:07:09,770
Holder and CVC number.

87
00:07:09,920 --> 00:07:19,340
However, this is debatable because some of these parameters you can also have them in the other method

88
00:07:19,340 --> 00:07:20,300
validate.

89
00:07:20,300 --> 00:07:20,990
Right.

90
00:07:21,500 --> 00:07:28,730
So perhaps you could have only the number there and these three in here.

91
00:07:29,180 --> 00:07:37,220
And then in here you only provide the number when you create an instance of the credit card and you

92
00:07:37,220 --> 00:07:44,240
move these three to the validate method because they are now part of the validate method.

93
00:07:44,570 --> 00:07:45,200
Right.

94
00:07:45,410 --> 00:07:47,210
Remove this comma as well.

95
00:07:47,600 --> 00:07:50,240
So the big question is what is better?

96
00:07:50,240 --> 00:07:55,500
Is it the first version where we had all the parameters in here, or is it this version better?

97
00:07:55,520 --> 00:08:03,180
Well, to answer this question, you should think of what is the minimum set of parameters you need

98
00:08:03,230 --> 00:08:07,230
when you instantiate when you create an instance of a credit card.

99
00:08:07,250 --> 00:08:13,310
So can a credit card be defined using only its number?

100
00:08:13,700 --> 00:08:19,580
Well, the answer is yes, it can be defined, but it cannot be validated.

101
00:08:19,580 --> 00:08:20,210
Right?

102
00:08:20,240 --> 00:08:27,620
Because to validate a card, to make a payment, you need all these data expiration date, the holder

103
00:08:27,620 --> 00:08:29,300
and the CVC.

104
00:08:30,260 --> 00:08:36,679
So it does make sense to have only the number here and to have all this other parameters part of the

105
00:08:36,679 --> 00:08:38,120
validate method.

106
00:08:38,390 --> 00:08:45,050
So think of this like, Oh, this is like the user name of this object and these are like the passwords.

107
00:08:45,290 --> 00:08:51,200
So I think it does make sense to have it like this, although some programmers would think otherwise.

108
00:08:51,200 --> 00:08:56,420
So there's no concrete way a definite technique to do this.

109
00:08:56,630 --> 00:09:02,330
So let's keep it like this and I'll do self number is equal to number.

110
00:09:03,530 --> 00:09:08,090
I did this because we're going to need the number and this method, right?

111
00:09:08,090 --> 00:09:09,860
So we need to access that number.

112
00:09:09,860 --> 00:09:15,860
And the way to access it is by assigning number as an attribute of the instance.

113
00:09:15,860 --> 00:09:19,760
So now we can access that as self, that number, right.

114
00:09:19,820 --> 00:09:21,950
So how do we validate the credit card?

115
00:09:22,740 --> 00:09:28,700
Well, first of all, you need to load the data frame out of this CSB file.

116
00:09:28,710 --> 00:09:29,800
So let's do that.

117
00:09:29,820 --> 00:09:31,710
We can do that outside here.

118
00:09:31,890 --> 00:09:37,770
So you can say DF for four cards is equal to pandas.

119
00:09:37,770 --> 00:09:41,810
Dot three C is V cards dot C as V?

120
00:09:41,820 --> 00:09:43,470
That's the name of that file.

121
00:09:44,250 --> 00:09:49,440
I'll do D type is equal to SDR.

122
00:09:49,740 --> 00:09:59,850
So I want to load all the fields as strings because none of them has any mathematical meaning.

123
00:09:59,850 --> 00:10:00,060
Right.

124
00:10:00,060 --> 00:10:02,940
So this is a number, but it just an idea.

125
00:10:02,970 --> 00:10:07,320
This is also just an expiration date which can be treated as a string.

126
00:10:07,560 --> 00:10:12,720
The CVC field doesn't also have any mathematical meaning.

127
00:10:12,720 --> 00:10:18,990
We're not doing mathematical operations, so it's fine to load all these as strings.

128
00:10:19,470 --> 00:10:25,440
Now, I would like to load this dataframe actually, as a dictionary.

129
00:10:27,300 --> 00:10:28,170
Why?

130
00:10:30,030 --> 00:10:31,620
Well, I'll explain you why.

131
00:10:31,650 --> 00:10:37,890
So let me import pandas in a python console to show you how this dictionary will look like.

132
00:10:37,890 --> 00:10:40,650
Import pandas DF cards.

133
00:10:42,120 --> 00:10:44,100
DF cards looks like this.

134
00:10:44,110 --> 00:10:45,210
So a dictionary?

135
00:10:45,300 --> 00:10:45,630
Sorry.

136
00:10:45,630 --> 00:10:53,840
A list of dictionaries and a dictionary represents one row of the cards data.

137
00:10:53,850 --> 00:10:54,120
So.

138
00:10:54,120 --> 00:10:55,440
One, two, three, four.

139
00:10:56,250 --> 00:11:00,910
That role that I selected here reflects that dictionary here.

140
00:11:00,930 --> 00:11:08,730
I did convert the dataframe into a dictionary because it's easier to check if all these.

141
00:11:12,480 --> 00:11:17,550
Data are part of that list of dictionaries.

142
00:11:17,550 --> 00:11:17,910
Right.

143
00:11:18,030 --> 00:11:19,440
You'll see how it works.

144
00:11:19,440 --> 00:11:20,190
Exactly.

145
00:11:23,860 --> 00:11:28,420
So the idea is that now I can I can make a dictionary with.

146
00:11:30,160 --> 00:11:33,820
The data which should look like this, let's say.

147
00:11:34,860 --> 00:11:39,300
We have the number first rate as a string number.

148
00:11:40,710 --> 00:11:46,620
We just sell that number and then we have expiration.

149
00:11:48,450 --> 00:11:49,800
So that's the key.

150
00:11:50,190 --> 00:11:51,860
The value would be expiration.

151
00:11:51,880 --> 00:11:56,520
The variable, this parameter which comes directly in the validate method.

152
00:11:59,010 --> 00:11:59,820
Holder.

153
00:12:00,420 --> 00:12:01,410
Holder.

154
00:12:02,890 --> 00:12:04,360
CVC.

155
00:12:04,870 --> 00:12:14,440
CVC let me split this because it's too long, just for readability and following the PEP eight style

156
00:12:14,440 --> 00:12:15,100
guide.

157
00:12:15,310 --> 00:12:21,670
So once I have these data as dictionary, so I get the data from the user, right?

158
00:12:21,850 --> 00:12:28,990
I get the number in here, I get the expiration holder and CVC in here and then these are transported

159
00:12:29,590 --> 00:12:30,610
to here.

160
00:12:31,300 --> 00:12:40,420
And then we create a dictionary and then we say if cart data in DF cards.

161
00:12:40,420 --> 00:12:46,210
So that data frame actually it's not a data frame, as you see, it's a list of dictionaries.

162
00:12:46,390 --> 00:12:53,260
So we're checking if that dictionary is one of those dictionaries in the DF cards list.

163
00:12:54,530 --> 00:12:55,590
Pretend to.

164
00:12:58,940 --> 00:12:59,480
Right.

165
00:13:01,120 --> 00:13:08,410
And you can say ls return false, although it will work even if you don't do that because this will

166
00:13:08,410 --> 00:13:10,870
return none by default.

167
00:13:13,650 --> 00:13:18,960
Same goes with this available method of the hotel class.

168
00:13:19,290 --> 00:13:25,620
But if you want to be explicit and make the code more readable, you can do the return false.

169
00:13:26,880 --> 00:13:31,500
So some people prefer this method of doing things more explicit.

170
00:13:32,560 --> 00:13:40,240
So that means that this validate methods in here will return.

171
00:13:40,240 --> 00:13:41,220
True.

172
00:13:41,250 --> 00:13:44,480
And that means this will be evaluated to true.

173
00:13:44,520 --> 00:13:47,380
And that means we book the hotel and so on.

174
00:13:47,380 --> 00:13:49,010
So, yeah, that's it.

175
00:13:49,030 --> 00:13:52,240
Let's run the program.

176
00:13:54,230 --> 00:13:55,400
So run it.

177
00:13:55,610 --> 00:14:02,990
Now, all these hotels are currently not available, so if we do a 188, we're going to get hotel is

178
00:14:02,990 --> 00:14:04,790
not free as expected.

179
00:14:04,790 --> 00:14:07,910
So this was executed, this line.

180
00:14:08,480 --> 00:14:18,050
Now let's go to hotels as we change the second one snow palace in New Delhi to yes, they do have some

181
00:14:18,050 --> 00:14:21,830
availability currently because it's not snowing in Delhi.

182
00:14:21,830 --> 00:14:24,020
So they don't have lots of customers.

183
00:14:24,140 --> 00:14:32,630
Usually it snows there and anyway, it's run and enter the idea.

184
00:14:32,630 --> 00:14:35,300
So we said Snow palace, which is.

185
00:14:35,300 --> 00:14:35,990
Yes.

186
00:14:36,920 --> 00:14:40,010
Now 188 press enter.

187
00:14:40,520 --> 00:14:42,890
There was a problem with your payment.

188
00:14:43,880 --> 00:14:48,110
So something is not going as smoothly.

189
00:14:48,920 --> 00:14:52,760
That's because the number here doesn't reflect the number there.

190
00:14:52,760 --> 00:14:54,950
So let me just add it.

191
00:14:56,320 --> 00:14:59,990
The number of the cards here plays that there are.

192
00:15:00,770 --> 00:15:01,850
Try again.

193
00:15:05,580 --> 00:15:09,210
So Snow Palace is still available.

194
00:15:10,380 --> 00:15:12,780
So 188 enter.

195
00:15:12,930 --> 00:15:13,860
Oh, enter your name.

196
00:15:13,860 --> 00:15:16,380
Now we're being asked for our name ordered.

197
00:15:16,380 --> 00:15:21,030
And thank you for your reservation.

198
00:15:21,150 --> 00:15:25,480
Here are your booking data or this hotel name and so on.

199
00:15:25,500 --> 00:15:31,680
So now our card was validated and we successfully booked the hotel.

200
00:15:31,710 --> 00:15:43,200
Of course, you can have this database more complex, so you could add like a balance here and then

201
00:15:43,200 --> 00:15:43,860
have.

202
00:15:44,680 --> 00:15:48,730
How much money each of these cards have.

203
00:15:48,760 --> 00:15:49,510
Right.

204
00:15:49,750 --> 00:15:51,730
And then you could.

205
00:15:54,350 --> 00:16:01,790
Extend the class credit card by adding another methods here like define pay.

206
00:16:02,210 --> 00:16:13,850
And then this pay method would modify this data frame, right, and subtract the payment the price of

207
00:16:13,850 --> 00:16:14,960
the hotel.

208
00:16:14,990 --> 00:16:21,770
If the hotel had a price, which also is not the case because our hotel's database was also simple,

209
00:16:21,770 --> 00:16:28,430
but you get the idea so you can add more methods to a class when you want to extend the class.

210
00:16:28,430 --> 00:16:32,930
So it's better to add the methods here than to modify this validate methods.

211
00:16:32,930 --> 00:16:37,370
So each method should be responsible for one and one thing only.

212
00:16:37,730 --> 00:16:41,630
So this method validates the card and that's its duty.

213
00:16:41,630 --> 00:16:45,770
The other method would pay, which means subtracts something from that hotel.

214
00:16:45,800 --> 00:16:46,160
Right.

215
00:16:46,160 --> 00:16:47,060
And so on.

216
00:16:47,180 --> 00:16:52,130
Anyway, I don't want to go into the rabbit hole with so many details.

217
00:16:52,130 --> 00:16:55,550
I want to keep the focus on P.

218
00:16:57,760 --> 00:16:59,260
So that's the cards.

219
00:16:59,260 --> 00:17:04,180
And yeah, that's how you can extend an existing program.

220
00:17:04,180 --> 00:17:06,849
So you add a new class.

221
00:17:08,030 --> 00:17:10,550
And then you modify the main loop.

222
00:17:10,550 --> 00:17:17,270
Where do you create instances of the classes and where the logic of how the program will run is?

223
00:17:17,420 --> 00:17:21,710
So I hope this is helpful for you to understand classes even better.

224
00:17:21,800 --> 00:17:25,099
And yeah, we have some more videos today, so I'll see you in the next one.

