0
1
00:00:01,070 --> 00:00:04,820
Now, let's get back to our pizza calculator.
1

2
00:00:05,060 --> 00:00:11,750
Essentially, what we want is to be able to specify the pizza in inches that we want to buy because of some
2

3
00:00:11,750 --> 00:00:15,620
special offer or because some pizzas only come in certain sizes,
3

4
00:00:15,740 --> 00:00:16,840
whatever it may be,
4

5
00:00:17,090 --> 00:00:21,010
and we also want to specify the number of people to feed.
5

6
00:00:21,230 --> 00:00:25,010
So let's say that this was, I don't know,
6

7
00:00:25,040 --> 00:00:26,710
we had six people coming over,
7

8
00:00:26,740 --> 00:00:27,130
right?
8

9
00:00:27,290 --> 00:00:33,440
And we'll also specify another constant which is called slicesPerPerson.
9

10
00:00:33,440 --> 00:00:42,890
So according to Domino's, the world experts in pizza, the ideal slice of per person is apparently three.
10

11
00:00:42,960 --> 00:00:46,910
Now, I like to be a little bit more generous than that, especially for myself.
11

12
00:00:46,910 --> 00:00:49,610
So let's say that everybody gets five slices of pizza.
12

13
00:00:49,820 --> 00:00:57,770
So, now I want to be able to set all of these variables, and then calculate how many pizzas I should
13

14
00:00:57,770 --> 00:00:58,350
buy.
14

15
00:00:58,400 --> 00:01:02,410
So I'm going to create anotherr computed property called numberOfPizza.
15

16
00:01:02,540 --> 00:01:07,730
And this, of course, is going to be an integer because you can't buy half a pizza, at least not where I'm
16

17
00:01:07,730 --> 00:01:08,360
from.
17

18
00:01:08,360 --> 00:01:15,920
And we're going to create a getter that is going to compute how many pizzas we need to buy whenever we
18

19
00:01:15,920 --> 00:01:19,240
try to access this property numberOfPizzas.
19

20
00:01:19,370 --> 00:01:30,380
And this is pretty easy because we can say that numberOfPeopleFedPerPizza is equal to 
20

21
00:01:30,380 --> 00:01:35,990
the numberOfSlices divided by the slicesPerPerson.
21

22
00:01:35,990 --> 00:01:42,680
And then in order to figure out the number of pizza we need to buy, we can simply return
22

23
00:01:42,680 --> 00:01:44,380
the number of people to feed
23

24
00:01:44,720 --> 00:01:54,230
divided by numberOfPeopleFedPerPizza. And now our computed property is a read-only property because
24

25
00:01:54,230 --> 00:01:55,990
it only has a getter,
25

26
00:01:55,990 --> 00:02:04,010
it doesn't yet have a setter. But it means that we can simply printout numberOfPizza for the current
26

27
00:02:04,100 --> 00:02:05,060
settings.
27

28
00:02:05,060 --> 00:02:07,520
So these are 12-inch pizzas that we're buying.
28

29
00:02:07,520 --> 00:02:12,910
We've got six people in the house and everybody is going to eat 5 slices of pizza.
29

30
00:02:13,100 --> 00:02:16,750
So down here we've got the result. We need to buy six pizzas.
30

31
00:02:16,760 --> 00:02:18,240
Simple, right?
31

32
00:02:18,260 --> 00:02:23,240
So if at a later date, we decide to change some of these stats, say. we're buying larger pizzas or buying
32

33
00:02:23,240 --> 00:02:27,620
16-inch pizzas, and we've actually got a lot more people coming,
33

34
00:02:27,620 --> 00:02:33,990
we've got 12 people in the house, and everybody only wants 4 slices, because I don't know why.
34

35
00:02:34,130 --> 00:02:39,860
Now, in this case, when our code updates, we didn't have to call a method or update anything or, you know,
35

36
00:02:39,860 --> 00:02:41,930
run some piece of code again,
36

37
00:02:42,180 --> 00:02:42,980
is we've--
37

38
00:02:43,160 --> 00:02:44,500
without doing anything,
38

39
00:02:44,570 --> 00:02:51,980
when we try to access numberOfPizza, the getter gets called and our code dynamically works out how many
39

40
00:02:52,040 --> 00:02:53,800
pizzas we need to buy.
40

41
00:02:54,320 --> 00:02:56,560
Now, what about the opposite situation?
41

42
00:02:56,570 --> 00:03:01,910
Say, we have a bunch of leftover pizza in the fridge and we want to know how many friends we can invite
42

43
00:03:01,910 --> 00:03:05,470
over to play Mario Kart. So we can use a setter
43

44
00:03:05,510 --> 00:03:09,190
in this case to work out the number of people we can feed.
44

45
00:03:09,470 --> 00:03:16,200
So instead of just having a getter, so a read-only property, we're going to add a setter as well.
45

46
00:03:16,410 --> 00:03:27,830
And we're going to say let totalSlices = numberOfSlices multiplied by newValue. So, remember
46

47
00:03:27,830 --> 00:03:35,350
that previously, we learn that newValue is simply the newValue that this property is going to be set to.
47

48
00:03:35,690 --> 00:03:41,900
And this is always spelt in this way and you can't change how it's spelt or how it's written because
48

49
00:03:41,900 --> 00:03:43,760
it's predefined in the setter.
49

50
00:03:44,000 --> 00:03:52,340
So, now that we've got the total numberOfSlices, we can change the numberOfPeople to equal
50

51
00:03:52,340 --> 00:03:56,240
the totalSlices / slicesPerPerson.
51

52
00:03:56,540 --> 00:04:04,640
So, now we have to change the number of people to a var, so that it can be modified. And we can print
52

53
00:04:04,790 --> 00:04:09,040
the numberOfPeople as we change the numberOfPizza.
53

54
00:04:09,050 --> 00:04:14,540
So let's say that we've got four pizzas left at home.
54

55
00:04:14,540 --> 00:04:17,690
Now, at this stage, I'm saying number of pizza equals four.
55

56
00:04:17,690 --> 00:04:24,920
So I'm setting numberOfPizza and this block of code will trigger, and we're going to calculate the
56

57
00:04:24,920 --> 00:04:32,810
numberOfPeople that we can feed with four pizzas that are 16 inches. And that is 12 people,
57

58
00:04:32,810 --> 00:04:38,980
so I can invite 12 people over with all of my pizzas in my fridge which is pretty neat to know.
58

59
00:04:39,170 --> 00:04:45,530
So through using computed properties and getters and setters, we've been able to write this pizza calculator
59

60
00:04:45,830 --> 00:04:53,690
in a very simplified way. And you can see that our code updates dynamically whenever we change the values
60

61
00:04:53,780 --> 00:04:55,130
of its dependence.
61

62
00:04:55,130 --> 00:05:01,380
So, for example, if I change the numberOfPizza, I get a new numberOfPeople that can be fed, and this means that
62

63
00:05:01,410 --> 00:05:05,980
all I have to change are these three values up here, and everything else,
63

64
00:05:06,000 --> 00:05:08,340
I don't need to touch ever again.
64

65
00:05:08,610 --> 00:05:14,520
And that minimizes errors, makes our code more dynamic and more maintainable, and easier to understand.
65

66
00:05:14,520 --> 00:05:17,790
So that is the beauty of computed properties.
66

67
00:05:17,790 --> 00:05:24,570
And so, essentially, all that we're doing is we've basically just implemented a spreadsheet where we change
67

68
00:05:24,660 --> 00:05:31,560
any of the dependent variables, say, the pizza size, then everything else will get updated accordingly
68

69
00:05:31,590 --> 00:05:33,350
depending on this change.
