WEBVTT

0
00:00.450 --> 00:07.140
If you got stuck or if you had any problems completing this project, the first port of call is to review

1
00:07.140 --> 00:08.380
the lessons you learned today.

2
00:08.760 --> 00:14.280
It might just be that you need to see some of the theory a few more times and listen to it a few more

3
00:14.280 --> 00:19.410
times and practice it a few more times before you're ready to tackle this final challenge.

4
00:20.070 --> 00:24.660
But if you have completed it and you just want to see the solution, then I'm going to go through it

5
00:24.660 --> 00:25.380
with you right now.

6
00:26.400 --> 00:30.210
Again, we're going to complete all of the program requirements step by step.

7
00:30.570 --> 00:38.610
So the first thing is to print a report for all of the current resources in the coffee machine. Back

8
00:38.610 --> 00:39.090
in PyCharm,

9
00:39.330 --> 00:44.310
I'm actually going to collapse this entire project folder because I'm not going to be writing any code

10
00:44.310 --> 00:46.140
other than inside main.py.

11
00:47.040 --> 00:54.270
Now you can see up here, we've got access to four classes. And if we take a look inside the documentation,

12
00:54.480 --> 01:00.990
you can see that both the coffeemaker class and the money machine class have a method called report.

13
01:01.560 --> 01:06.690
And this is going to print a report of all of the resources like water, milk, coffee,

14
01:06.990 --> 01:09.810
and this is going to print a report of the current profit.

15
01:10.260 --> 01:16.290
So our final report is going to be both a report from the money machine as well as the coffee maker.

16
01:17.310 --> 01:22.380
Let's go ahead and start off by creating some objects from these classes.

17
01:22.950 --> 01:27.840
For example, the money machine is going to be just called money_machine

18
01:27.870 --> 01:30.250
or you could call it my_money_machine.

19
01:30.270 --> 01:33.090
You can actually name your objects anything you like.

20
01:33.720 --> 01:41.160
But I'm just going to use the default Python snake case to create a new variable money_machine, which

21
01:41.160 --> 01:46.980
is going to hold my object. And my object is going to be created, of course, from the money machine

22
01:46.980 --> 01:47.520
class.

23
01:48.090 --> 01:51.900
And the construction happens when I add the parentheses.

24
01:52.500 --> 01:58.440
So now I have a object created and stored inside this variable money_machine.

25
01:59.280 --> 02:06.870
Now, if I want this money machine to create a report, then all I have to do is tap into the object

26
02:07.260 --> 02:12.360
and then write a dot and then call the method that I need, which is report.

27
02:13.020 --> 02:15.630
So now if I go ahead and run my code,

28
02:18.360 --> 02:22.110
you can see it prints out the current amount of money in the machine.

29
02:22.890 --> 02:25.710
Now let's do the same thing for our coffee maker.

30
02:26.630 --> 02:31.010
Again, I'm going to call it coffee_maker, and you see this frequently.

31
02:31.020 --> 02:39.840
The object naming tends to be the lower case and snake case, so separated by underscores, version of the

32
02:39.840 --> 02:40.740
name of the class.

33
02:40.950 --> 02:47.580
So the name of the class is CoffeeMaker, and usually you'll see people name the object exactly the

34
02:47.580 --> 02:49.290
same, but in lowercase.

35
02:50.280 --> 02:57.900
And now that I have my coffee _maker, then I can go ahead and call the report method on that object

36
02:57.900 --> 02:58.410
as well.

37
02:58.800 --> 03:04.980
And now, if I run my code once more, you can see that I've got everything now being reported and printed

38
03:04.980 --> 03:05.190
out.

39
03:06.060 --> 03:12.870
So in this case, we don't actually have to care how report is implementing all of this functionality.

40
03:13.200 --> 03:19.470
All we have to do is read the documentation, find the method that does the thing that we want, and

41
03:19.470 --> 03:23.640
then trust that it will carry out the functionality as described.

42
03:24.870 --> 03:30.480
Now that we've completed step 1, the next one is to check that resources are sufficient.

43
03:31.080 --> 03:35.370
My first port of call is going to be going over to the documentation.

44
03:35.820 --> 03:42.240
And you can see that the coffeemaker class actually has a method called is _resource_efficient.

45
03:42.600 --> 03:50.130
And all we have to do is pass in the drink, which is a menu item, and then it's going to check and

46
03:50.130 --> 03:56.640
give us true, if that drink order can be made, and false if the ingredients are insufficient.

47
03:57.840 --> 04:03.930
So how do we get hold of this drink menu item to pass over to this method?

48
04:04.650 --> 04:11.700
Well, if we take a look at the menu class, you can see it has a method called get_items and this is

49
04:11.700 --> 04:15.030
where you really see reading the documentation properly,

50
04:15.450 --> 04:19.800
actually absorbing everything that's in here becomes really, really helpful.

51
04:20.460 --> 04:24.570
This menu class has two methods that are really useful.

52
04:25.050 --> 04:30.870
One, It's able to get a hold of the names of all the items that are available on the menu.

53
04:31.350 --> 04:35.370
And it returns it as a string separated by forward slashes.

54
04:36.120 --> 04:40.560
It's also able to find a drink based on the order name.

55
04:40.590 --> 04:47.550
So if we passed in one of these names to this function find_drink, then it's going to return a menu

56
04:47.550 --> 04:49.770
item object if it actually exists.

57
04:50.040 --> 04:51.990
Otherwise, it's going to return none.

58
04:52.620 --> 04:56.670
We're going to need both of these in order to ask the user what they want.

59
04:57.530 --> 05:02.520
Let's go ahead and set up our while loop. As we had previously

60
05:02.540 --> 05:08.660
I'm going to create a new variable called is_on and it's going to start off being true.

61
05:09.230 --> 05:16.880
And then while coffee machine is on, well then we're going to get hold of all the options that we

62
05:16.880 --> 05:22.850
can offer the user. And we get that by calling that method get_items.

63
05:23.300 --> 05:28.580
But of course, because this is a method, it's associated with an object.

64
05:28.670 --> 05:32.240
So we have to create an object from this menu blueprint.

65
05:33.440 --> 05:36.770
So let's do that next to all the other objects being created.

66
05:37.190 --> 05:42.050
So let's create a menu object and it's going to be created from the menu class,

67
05:42.050 --> 05:43.640
and then we add the parentheses.

68
05:44.030 --> 05:47.840
And now we have access to that object by the name menu.

69
05:48.380 --> 05:54.800
The options are going to be equal to the menu object and then we're going to call the get_items method.

70
05:55.340 --> 06:01.580
And then when this method returns, it's going to save the string and all the options into this variable

71
06:01.580 --> 06:02.390
called options.

72
06:02.990 --> 06:09.800
Now we can get the user's choice by asking them for what would you like?

73
06:11.170 --> 06:17.650
And then inside a set of parenthesis we'll offer them all the options. And then, of course, I need

74
06:17.650 --> 06:19.480
to turn this into an fstring.

75
06:19.630 --> 06:24.070
And the code highlighting immediately tells me that this is actually now working.

76
06:24.640 --> 06:32.740
So once I've got the user's choice, then I can incorporate the reporting behaviour into this choice.

77
06:33.130 --> 06:42.310
Remember, previously we said if the choice was equal to off, then that means the is_on is going

78
06:42.310 --> 06:45.390
to be equal to false, and elif

79
06:45.400 --> 06:47.860
the choice is equal to report,

80
06:48.310 --> 06:53.110
well, in this case, we're going to get the coffeemaker and the money machine to make their reports

81
06:53.110 --> 06:54.040
into the console.

82
06:54.970 --> 07:03.040
Finally, we're ready to tackle the next problem, which is how do we check that we have enough resources?

83
07:04.100 --> 07:10.520
And this is going to require us to find the drink given the order name that the user chose from the

84
07:10.520 --> 07:17.440
choice and then get hold of the menu item which comes as the output from this method call.

85
07:18.430 --> 07:26.110
So else, we're going to save our drink as a variable called drink, and then we're going to tap into

86
07:26.110 --> 07:35.500
the menu.find_drink. And find_drink, you'll notice, takes a order name as the input.

87
07:35.980 --> 07:42.370
So this is a string and this is going to be equal to whatever the user chose inside this choice.

88
07:42.940 --> 07:45.910
So let's put the choice in here as the input.

89
07:46.450 --> 07:51.190
And now, once we've got hold of this drink, let's go ahead and print it.

90
07:51.820 --> 07:55.930
Remember, we're expecting a menu item object at this stage.

91
07:56.500 --> 08:01.480
So let's click run, and let's make sure that we can hit report to get the report,

92
08:01.870 --> 08:11.440
we can say off to switch off the machine and exit the code, and we can also get hold of a latte by

93
08:11.440 --> 08:14.530
passing in latte as the drink that we want.

94
08:15.040 --> 08:21.940
And we now get printed a menu item object at this particular location in the computer's memory.

95
08:22.450 --> 08:23.040
Perfect.

96
08:23.050 --> 08:25.330
Everything is working as expected.

97
08:26.140 --> 08:32.590
Now, instead of printing the drink, I'm going to tackle this step 2, which is to check that the

98
08:32.590 --> 08:34.330
resources are sufficient.

99
08:35.440 --> 08:42.400
Again, that's going to require us to look through our documentation. And you'll see that the coffee

100
08:42.400 --> 08:43.600
maker class has

101
08:43.600 --> 08:50.260
that method is_resource _sufficient, and it expects a menu item object as the input.

102
08:50.710 --> 08:54.970
And then it's going to return true when it can be made and false if it can't.

103
08:55.960 --> 09:03.550
So let's get hold of our coffee maker object and then get it to check if the resources are sufficient

104
09:03.940 --> 09:07.000
to make the current drink that we're interested in.

105
09:07.870 --> 09:11.770
And let's go ahead and print the results here

106
09:12.820 --> 09:14.350
and run our code.

107
09:15.190 --> 09:23.290
Let's say that we wanted to make a latte. That's going to be resource sufficient, so true. Espresso,

108
09:23.620 --> 09:27.560
also true, and a Cappuccino is also true.

109
09:27.580 --> 09:29.740
So we can make all three drinks, basically.

110
09:30.070 --> 09:37.480
Now, instead of printing this, what we want to do is we want to check if this is true and then proceed

111
09:37.480 --> 09:40.210
to the next step if there are enough resources.

112
09:40.870 --> 09:47.830
Now the next step is to take payment from the user and to process the coins and check the transaction

113
09:47.830 --> 09:48.700
is successful.

114
09:49.630 --> 09:56.980
Notice how in our coffee machine documentation for the money machine, we don't actually have a way

115
09:56.980 --> 10:05.050
of processing the coins. But it what it does have is it allows us to make payments passing in the cost

116
10:05.050 --> 10:11.530
of the drink and then it will return true when the payment is accepted or false, if insufficient.

117
10:12.160 --> 10:15.650
So let's see what happens if we actually try to make payment.

118
10:16.510 --> 10:23.110
Let's tap into our money machine object and then call the make_payment method and we have to pass in

119
10:23.110 --> 10:26.170
the cost of the drink that the user ordered.

120
10:26.710 --> 10:34.480
So we've got the drink object here, which remember is a menu item, and each of the menu items have

121
10:34.480 --> 10:39.880
three attributes that we care about: the name of the drink, the cost of the drink and the ingredient

122
10:39.880 --> 10:40.240
list.

123
10:40.630 --> 10:49.360
So this is what we're interested in at the moment. So we can pass in the drink.cost which is the

124
10:49.360 --> 10:52.270
attribute that's associated with the drink object,

125
10:52.750 --> 10:59.110
and that cost is going to be processed by this make_payment method from the money machine.

126
10:59.740 --> 11:05.020
And then finally, it's going to tell us whether if this was successful or not.

127
11:05.410 --> 11:08.200
So let's see what happens when we run this code as it is.

128
11:09.230 --> 11:11.230
Let's say I wanted a latte,

129
11:12.170 --> 11:14.820
notice how it's asking me to insert coins:

130
11:14.840 --> 11:22.340
how many quarters, how many dimes, how many nickels and how many pennies. A the coffee machine,

131
11:22.700 --> 11:28.490
I don't actually need to care about how the money and the coins are processed.

132
11:29.090 --> 11:35.180
All I had to do is call make_payment and it deals with all of this automatically.

133
11:36.170 --> 11:43.430
Finally, it gives me the change and it returns true, telling me that this make_payment was indeed

134
11:43.430 --> 11:44.240
successful.

135
11:45.140 --> 11:52.610
If you actually look inside the money_machine code, then you can see that it has a method called process_

136
11:52.610 --> 11:56.790
coins, but it's called when make payment is triggered.

137
11:57.290 --> 12:02.960
This class is going to process coins, and then it's going to decide whether if it was successful

138
12:03.170 --> 12:07.820
or not. And if it was successful, it's going to add the cost to the profit,

139
12:08.120 --> 12:13.130
and if not, it's going to tell you that that was not enough money and your money is refunded.

140
12:14.980 --> 12:23.620
Coming back over here, if this is successful, then that means the user has managed to pay and we fulfilled

141
12:23.680 --> 12:27.850
step 3 and 4 just using one method.

142
12:28.960 --> 12:32.440
The final thing we have to do is to actually make the coffee.

143
12:33.160 --> 12:35.830
And that, of course, happens right at the end here.

144
12:36.430 --> 12:42.730
And all we have to do to make the coffee is get hold of the coffee maker object and then call the make_

145
12:42.730 --> 12:48.220
coffee method and we can pass in the order, which is our drink.

146
12:48.700 --> 12:54.850
Because remember that the make_coffee method expects a menu item as the input parameter.

147
12:55.990 --> 13:03.280
We can actually simplify this code even more by saying, well, if the coffee maker is_resource_sufficient

148
13:04.030 --> 13:11.620
and the money machine was able to take payment, well then we can get the coffee_maker to make coffee.

149
13:12.310 --> 13:14.320
So now let's go ahead and run our code.

150
13:16.530 --> 13:21.930
And let's report to begin with, to see how much resources we have to begin with.

151
13:22.380 --> 13:24.780
And then let's order a coffee.

152
13:26.930 --> 13:30.890
Insert lots of money and we've got our coffee back.

153
13:31.400 --> 13:39.800
So now let's go ahead and order a different coffee, latte, and let's go ahead and insert lots of money.

154
13:40.370 --> 13:42.380
And you can see, here's my latte.

155
13:42.770 --> 13:49.910
But now, if I hit report, you can see that the objects, the coffee maker has been managing our resources

156
13:50.420 --> 13:53.240
and the money_machine has been managing our profit.

157
13:53.960 --> 13:59.900
Now, if I decided to order another latte, then it should actually tell me, sorry, there's not enough

158
13:59.900 --> 14:04.520
water and there's not enough milk. So it can't make me that drink.

159
14:05.150 --> 14:12.650
And we've managed to achieve all of this functionality by writing very few lines of code, and the code

160
14:12.650 --> 14:16.130
itself is actually very, very easy to understand.

161
14:16.370 --> 14:22.840
Even if you came to this completely fresh, you've never seen how each of the methods are implemented,

162
14:22.850 --> 14:24.440
you don't know how the coffee maker works,

163
14:24.710 --> 14:26.240
you don't know how the money machine works,

164
14:26.540 --> 14:29.000
you can see quite easily what it's trying to do.

165
14:29.960 --> 14:35.660
And so if you only have to write this kind of code, then it would make it so much easier to create

166
14:35.660 --> 14:39.440
even more complex functionality in your coffee machine, right?

167
14:40.430 --> 14:43.130
So have a play around with these classes:

168
14:43.580 --> 14:50.270
the coffeeMaker, the moneyMachine, the menu, the menu item, etc. And see what else it is that

169
14:50.270 --> 14:51.590
you can do with this code

170
14:51.860 --> 14:55.880
while not touching any of the blueprints for each of these classes.

171
14:57.200 --> 15:02.660
Now, in the next lesson, I'm going to be showing you how to create your own classes so that you can

172
15:02.660 --> 15:07.880
actually start structuring your code using full object-oriented programming.

173
15:08.390 --> 15:14.420
And we're going to learn about these things like the initialization and how classes are declared and

174
15:14.420 --> 15:16.910
why there's all this self all over the place.

175
15:17.630 --> 15:20.480
That's all yet to come on tomorrow's lessons.