1 00:00:05,550 --> 00:00:09,550 In this video, we'll apply some of the concepts that we've learned in this section 2 00:00:09,550 --> 00:00:10,850 to solve a problem. 3 00:00:11,050 --> 00:00:14,650 What we'll do first is we'll solve the problem using literal constants 4 00:00:14,650 --> 00:00:18,090 and then what we'll do is we'll refactor it to use declared constants. 5 00:00:18,090 --> 00:00:21,390 And along the way you'll see how much better it is to use declared constants. 6 00:00:21,790 --> 00:00:23,990 Okay. So here's the problem we're going to solve. 7 00:00:23,990 --> 00:00:27,590 Frank has a carpet cleaning service that charges $30 per room 8 00:00:27,590 --> 00:00:29,890 and there's a sales tax rate of 6%. 9 00:00:30,390 --> 00:00:34,290 All the estimates are valid for 30 days. So our program is really simple. 10 00:00:34,290 --> 00:00:37,490 We're going to prompt the user for the number of rooms they would like cleaned. 11 00:00:37,890 --> 00:00:39,490 And we're going to provide an estimate. 12 00:00:39,490 --> 00:00:41,290 The estimate looks something like this. 13 00:00:41,290 --> 00:00:43,290 You can see here starting at line 13. 14 00:00:43,590 --> 00:00:46,490 Estimate for carpet cleaning service: number of rooms, 15 00:00:46,490 --> 00:00:48,690 2. That's the number that the user entered. 16 00:00:49,190 --> 00:00:53,390 Price per room $30, cost $60. 17 00:00:53,390 --> 00:00:56,390 Obviously, that would be $30 per room times x2 rooms. 18 00:00:57,290 --> 00:01:00,590 The tax which would be 6% based on the $60, 19 00:01:00,590 --> 00:01:02,590 which would be $3.60. 20 00:01:03,090 --> 00:01:06,990 Then the total estimate would be $63.60, and then you print out. 21 00:01:06,990 --> 00:01:08,790 This estimate is valid for 30 days. 22 00:01:08,790 --> 00:01:12,390 Now I'm using dollars. You could use euros. You could use any currency you like. 23 00:01:12,390 --> 00:01:14,390 Also I know that the formatting isn't right. 24 00:01:14,390 --> 00:01:18,290 Obviously, $3.60 would be 3.60. 25 00:01:18,290 --> 00:01:21,890 Don't worry about that right now. Let's worry about solving this problem and using 26 00:01:21,890 --> 00:01:24,890 literal constants and using declared constants. 27 00:01:24,890 --> 00:01:27,890 We'll worry about output formatting in a different section, 28 00:01:27,890 --> 00:01:31,290 and we'll go over a bunch of different ways to do formatting, including 29 00:01:31,290 --> 00:01:33,990 currency formatting based on your locale. 30 00:01:33,990 --> 00:01:36,990 All right. So let's think about how we're going to solve this problem. 31 00:01:37,590 --> 00:01:40,390 Programmers often use something called pseudocode. 32 00:01:40,390 --> 00:01:44,190 Pseudocode is like an English-like or your own natural spoken language- 33 00:01:44,190 --> 00:01:48,390 like representation of the algorithm or the steps necessary 34 00:01:48,390 --> 00:01:49,590 to solve the problem. 35 00:01:50,190 --> 00:01:52,490 In this example, I wrote down some pseudocode. 36 00:01:52,490 --> 00:01:56,480 And pseudocode, don't let anybody tell you that there's a right way and a wrong way to do 37 00:01:56,480 --> 00:02:00,680 pseudocode,there really isn't -- it's not a programming language. 38 00:02:00,880 --> 00:02:05,480 This is something that organizes your thoughts and allows you to solve the problem. 39 00:02:05,480 --> 00:02:08,780 Remember, if you can't solve the problem in pseudocode, 40 00:02:08,780 --> 00:02:11,140 there's no way you're going to solve it in c or 41 00:02:11,140 --> 00:02:13,340 in java or in any other programming language. 42 00:02:13,340 --> 00:02:15,140 So in this case, this is my pseudocode. 43 00:02:15,140 --> 00:02:18,140 These are the steps that I want to take to solve this problem. 44 00:02:18,140 --> 00:02:21,440 The first thing i want to do and you can see here on line 23 45 00:02:21,440 --> 00:02:23,540 is prompt the user to enter the number of rooms. 46 00:02:24,240 --> 00:02:27,240 Great. I want to display the number of rooms to the user, 47 00:02:27,240 --> 00:02:31,390 display the price per room then I want to display the cost which is 48 00:02:31,390 --> 00:02:33,990 the number of rooms, times, the price per room. 49 00:02:34,590 --> 00:02:36,390 I want to display the tax, 50 00:02:36,390 --> 00:02:39,990 which is the number of rooms, times, the price per room, times the tax rate. 51 00:02:41,190 --> 00:02:44,690 Then I want to display the total estimate. The total estimate of course is 52 00:02:44,690 --> 00:02:46,190 the cost plus the tax. 53 00:02:46,490 --> 00:02:50,090 And then I want to display at the end, this estimate is valid for 30 days. 54 00:02:50,090 --> 00:02:53,890 So what we want to do is we want to mimic this sample output right here. 55 00:02:54,690 --> 00:02:56,390 Okay. So let's get started. 56 00:02:56,390 --> 00:02:59,790 What we'll do first is we'll do this using literal constants, 57 00:02:59,790 --> 00:03:02,990 and then we'll refactor it using declared constants. 58 00:03:02,990 --> 00:03:07,390 So the first thing I'm saying is "Hello welcome to Frank's Carpet Cleaning Service". 59 00:03:07,390 --> 00:03:10,640 That's my little introduction. Now I need to ask the user, 60 00:03:10,640 --> 00:03:12,140 again look at the pseudocode, 61 00:03:12,140 --> 00:03:14,640 ask the user how many rooms they'd like to be cleaned. 62 00:03:14,640 --> 00:03:16,940 Okay. So let's do that. We'll say cout 63 00:03:17,940 --> 00:03:22,240 and we'll say "How many rooms would you like cleaned?" 64 00:03:27,140 --> 00:03:30,700 And I'm going to type this along and if I make typos and errors that's fine. 65 00:03:30,700 --> 00:03:34,500 I'll fix them along the way, and you'll probably make the same kinds of errors, 66 00:03:34,500 --> 00:03:37,500 so you'll see how to fix them. Semicolon at the end 67 00:03:37,900 --> 00:03:41,600 and there's a typo right here. So how many rooms would you like cleaned? 68 00:03:41,600 --> 00:03:45,600 Now let's run this, and what I'm doing is I'm pressing ctrl F5, 69 00:03:45,600 --> 00:03:47,600 which is build and run. 70 00:03:49,200 --> 00:03:52,700 And you can see here "Hello welcome to Frank's Carpet Cleaning Service. 71 00:03:52,700 --> 00:03:55,060 How many rooms would you like cleaned?" Perfect. 72 00:03:55,060 --> 00:03:59,560 Now I'd like to put a space between the first line and the second line of output. 73 00:03:59,860 --> 00:04:01,860 Now there's a couple of ways I can do that. 74 00:04:01,860 --> 00:04:04,460 I can come down here and I can just add another end line, 75 00:04:05,660 --> 00:04:08,960 and that'll print two new lines at the end, which would be fine. 76 00:04:09,760 --> 00:04:12,120 I could also just say cout 77 00:04:13,120 --> 00:04:17,320 end line here, which prints out a line. Let's run that. I'll show you what that looks like. 78 00:04:17,920 --> 00:04:21,420 Now you can see that there's a line between the the first line and the second line. 79 00:04:22,019 --> 00:04:26,020 And I can also use a character literal 80 00:04:26,020 --> 00:04:29,680 constant right here. And in front of the how I can put a \n, a backslash n 81 00:04:29,680 --> 00:04:33,340 That's a new line. Now I can get rid of this. 82 00:04:34,000 --> 00:04:36,700 And it has the same effect. So what's going to happen here is 83 00:04:36,700 --> 00:04:39,700 it's going to print a new line before it prints the how. 84 00:04:39,700 --> 00:04:41,200 So if I run this now, 85 00:04:42,400 --> 00:04:44,300 you can see I get the same effect. 86 00:04:44,300 --> 00:04:48,300 So this is an example of embedding those escape codes as I mentioned 87 00:04:48,300 --> 00:04:51,660 in the PowerPoint slides, into string literals. 88 00:04:51,660 --> 00:04:52,960 So now we're here. 89 00:04:53,660 --> 00:04:57,260 Next step is to read in the number of rooms from the user. 90 00:04:57,260 --> 00:04:58,560 So obviously, we need 91 00:04:58,560 --> 00:05:02,060 to use the extraction operator with the cin object, 92 00:05:02,060 --> 00:05:06,660 but we need to store that somewhere. So let's create an integer. We'll create a variable here, 93 00:05:06,660 --> 00:05:08,540 and we'll call it number of rooms. 94 00:05:11,740 --> 00:05:15,540 Best practice is to initialize it. So I'm going to initialize it to 0. 95 00:05:16,340 --> 00:05:20,440 And cin, and let's get the number of rooms 96 00:05:21,440 --> 00:05:22,440 from cin. 97 00:05:23,240 --> 00:05:25,440 Okay. So again, let's see where we're at. 98 00:05:25,690 --> 00:05:29,990 We've outputted our prompt, and the user has entered the number of rooms. 99 00:05:30,240 --> 00:05:32,740 Now we've got all the information that we need 100 00:05:32,740 --> 00:05:34,640 to do our calculations. 101 00:05:34,640 --> 00:05:36,640 We know that it's $30 per room, 102 00:05:37,140 --> 00:05:40,020 and we know that it's a 6% tax rate, that's all I need. 103 00:05:40,020 --> 00:05:42,120 So let's create some output statements, and I'm going 104 00:05:42,120 --> 00:05:44,420 to close this down here just to create a little bit more room. 105 00:05:45,320 --> 00:05:48,920 All right. So let's go here, and we're going to say 106 00:05:50,520 --> 00:05:51,420 cout. 107 00:05:51,420 --> 00:05:54,820 And let's put a \n here too because I'd like a space right here. 108 00:05:54,820 --> 00:05:59,520 So I'm going to say estimate for Carpet Cleaning Service. 109 00:06:05,120 --> 00:06:06,420 We'll create a new line here. 110 00:06:08,620 --> 00:06:11,120 Now we want to display the number of rooms to the user. 111 00:06:14,520 --> 00:06:16,020 I'll say number of rooms 112 00:06:16,900 --> 00:06:18,670 and I'll put a dollar sign in here. 113 00:06:18,670 --> 00:06:22,570 I'm not too worried about the formatting, I'm just using dollars so I might as well put the dollar sign here. 114 00:06:23,070 --> 00:06:25,070 And I want to display the number of rooms. 115 00:06:25,070 --> 00:06:27,570 Where do I get that? Well, I get that right from here. 116 00:06:27,570 --> 00:06:31,470 That variable right there the user just entered a number and I stored it in there. 117 00:06:31,470 --> 00:06:33,970 So that's going to contain whatever they entered. 118 00:06:34,570 --> 00:06:37,870 So there's number of rooms and then end line. 119 00:06:39,270 --> 00:06:42,270 All right. Let's run this. Control F5 120 00:06:43,370 --> 00:06:45,570 and I've got an error that says 121 00:06:47,070 --> 00:06:50,570 I forgot to put an l at the end of the end line. There we go. 122 00:06:51,070 --> 00:06:52,370 So let's run this again. 123 00:06:54,070 --> 00:06:56,840 How many rooms would you like cleaned? Let's say, I want two rooms cleaned. 124 00:06:57,640 --> 00:07:00,800 Estimate for Carpet Cleaning Services, number of rooms 2. 125 00:07:00,800 --> 00:07:02,160 I don't want a dollar there. 126 00:07:03,160 --> 00:07:06,360 I want dollar for prices. So that- I'll fix that error, 127 00:07:08,160 --> 00:07:10,260 2 rooms, perfect. 128 00:07:10,260 --> 00:07:12,960 Okay. So now let's start doing some of the calculations. 129 00:07:12,960 --> 00:07:15,560 But first, we need to print out the price per room, 130 00:07:16,960 --> 00:07:19,840 and that's a given. That was given to us as $30. 131 00:07:19,840 --> 00:07:21,340 So we'll say price per room. 132 00:07:22,600 --> 00:07:24,100 Here we'll put a dollar sign, 133 00:07:25,300 --> 00:07:27,000 and we'll say 30. 134 00:07:29,000 --> 00:07:33,500 We'll use the literal right in there, and we'll say end line. 135 00:07:34,200 --> 00:07:37,700 All right. So that's it. Then we'll say what's the cost? 136 00:07:37,700 --> 00:07:39,700 Well, we'll have to do a calculation. 137 00:07:40,800 --> 00:07:42,300 So we'll say cout would be cost. 138 00:07:43,800 --> 00:07:45,800 And again, I'll put a dollar sign in here. 139 00:07:48,300 --> 00:07:52,200 And what's the cost going to be? Well, the cost is going to be 140 00:07:53,700 --> 00:07:55,700 30 times the number of rooms. 141 00:07:56,500 --> 00:07:59,500 Right, $30 per room times the number of rooms. 142 00:08:01,000 --> 00:08:01,800 And 143 00:08:02,400 --> 00:08:04,000 we'll put a new line at the end. 144 00:08:04,500 --> 00:08:06,900 Let's test this out as we go. So let's run it. 145 00:08:08,600 --> 00:08:12,600 I want two rooms cleaned so I expect the cost at this point to be $60. 146 00:08:13,500 --> 00:08:17,700 So 2 rooms, $30 per room, cost $60. So we're good. 147 00:08:18,360 --> 00:08:19,660 All right so let's continue, 148 00:08:20,460 --> 00:08:23,160 and we'll say cout, what's my tax 149 00:08:24,660 --> 00:08:26,360 or my sales tax is -- 150 00:08:27,560 --> 00:08:28,660 and again, 151 00:08:29,860 --> 00:08:32,159 I'll use dollar and my sale tax is going to be 152 00:08:32,159 --> 00:08:36,059 the same calculation right, 30 times the number of rooms. 153 00:08:37,159 --> 00:08:40,360 And I've got an extra quote character right here. Let me get rid of it. 154 00:08:43,260 --> 00:08:45,860 30x the number of rooms times 155 00:08:45,860 --> 00:08:48,560 0.06, which is the tax rate, 156 00:08:51,060 --> 00:08:53,960 end line. I'll save this. Let's run one more time. 157 00:08:54,960 --> 00:08:59,160 2 rooms cleaned, $30, it's $60, 158 00:08:59,960 --> 00:09:03,360 6% of 60 is $3.60. 159 00:09:03,960 --> 00:09:05,260 Okay. So we're good. 160 00:09:07,160 --> 00:09:11,360 Now we're going to calculate the total estimate, which is going to be the cost plus the tax. But first 161 00:09:11,360 --> 00:09:14,460 I just want to print out a nice little line that separates 162 00:09:15,260 --> 00:09:16,260 everything together. 163 00:09:16,260 --> 00:09:20,460 That's long enough. And I'll put an end line at the end. 164 00:09:22,560 --> 00:09:26,660 All right. So now let's say what the total estimate is and then we just need to say the estimate is valid 165 00:09:26,660 --> 00:09:29,020 for 30 days, and we're done. So cout, 166 00:09:30,020 --> 00:09:31,620 we'll say total estimate. 167 00:09:33,820 --> 00:09:36,320 And again, in dollars is going to be -- okay, 168 00:09:37,420 --> 00:09:41,020 we need to do the calculation again. So we're going to say 30 times 169 00:09:42,620 --> 00:09:44,320 number of rooms, 170 00:09:45,420 --> 00:09:47,720 which gives us the cost, right. 171 00:09:47,720 --> 00:09:50,920 And I'm going to put some parens around that because I need to add to that 172 00:09:53,920 --> 00:09:57,910 plus 30 times 173 00:09:59,210 --> 00:10:03,310 the number of rooms times 0.06, 174 00:10:03,310 --> 00:10:07,310 which is my sales tax. So that's my cost plus my sales tax 175 00:10:08,310 --> 00:10:10,180 and a new line at the end. 176 00:10:10,780 --> 00:10:12,980 Last thing is, let's say, cout. 177 00:10:14,580 --> 00:10:17,980 This estimate is valid 178 00:10:19,180 --> 00:10:20,480 for, 179 00:10:21,180 --> 00:10:23,780 however many days and we said 30. 180 00:10:26,580 --> 00:10:29,780 And I'll put a space in front of the D just so we have a little room. 181 00:10:29,780 --> 00:10:33,030 Another line here, and I think we're good. 182 00:10:33,030 --> 00:10:36,330 Let me clean this up a little bit down here. So it all fits a little better. 183 00:10:36,930 --> 00:10:39,230 All right. So let's run it and then we'll go over it real quick. 184 00:10:39,730 --> 00:10:42,030 I'm going to save it and run it with control F5. 185 00:10:43,230 --> 00:10:45,430 How many rooms would you like cleaned, 2 rooms. 186 00:10:47,090 --> 00:10:49,890 Number of rooms is 2. The price per room is $30. 187 00:10:49,890 --> 00:10:52,590 The cost is $60. The tax is $3.60. 188 00:10:52,590 --> 00:10:56,690 So you would expect the total estimate to be $63.60, which it is. 189 00:10:56,690 --> 00:10:58,790 This estimate is valid for 30 days. 190 00:10:59,190 --> 00:11:03,290 Cool. Let's try it again. Let's run it again with 1 room this time. 191 00:11:04,490 --> 00:11:08,940 One room is $30. The cost is $30. The tax is $1.80. 192 00:11:08,940 --> 00:11:12,340 Total estimate $31.80. So it looks good. 193 00:11:12,340 --> 00:11:13,940 Don't forget to test with 0. 194 00:11:16,840 --> 00:11:20,540 So 0 rooms cleaned, 0 cost, 0 tax, 0 estimate. 195 00:11:21,540 --> 00:11:26,090 Great. Don't worry about testing with illegal values. We'll learn how to deal with illegal values later. 196 00:11:26,090 --> 00:11:30,490 So for example, expected to use is going to type 0 or something positive in 197 00:11:30,490 --> 00:11:31,640 the number of rooms. 198 00:11:31,640 --> 00:11:35,140 They're not going to type in -1 or Joe or Frank or anything like that. 199 00:11:35,140 --> 00:11:38,680 As I said, we'll deal with exceptions as the course progresses. 200 00:11:39,180 --> 00:11:42,480 So that's it. That's the program. It works. 201 00:11:42,480 --> 00:11:45,380 It's pretty easy to understand. It follows our pseudocode, 202 00:11:45,380 --> 00:11:48,380 but there's a couple of issues here. 203 00:11:48,980 --> 00:11:53,080 First, suppose that I 204 00:11:53,580 --> 00:11:57,080 want to increase my cost per room or my price per room to 205 00:11:57,080 --> 00:11:59,080 $32.50, 206 00:11:59,880 --> 00:12:00,880 what do I do? 207 00:12:01,240 --> 00:12:05,140 Well, I could do a search and replace, right. Let's do that. 208 00:12:05,640 --> 00:12:08,640 I'll do a search, find and replace 209 00:12:11,640 --> 00:12:16,340 and I want to look for 30 and I want to replace it with $32.50 and I want to do it everywhere, 210 00:12:16,540 --> 00:12:20,640 replace all. Done. I'll close this up, 211 00:12:20,640 --> 00:12:21,640 and let's run it. 212 00:12:24,520 --> 00:12:26,220 So I want to do 2 rooms, 213 00:12:27,520 --> 00:12:32,510 2 times $32.50 is $65, that's correct. The tax is $33.90, 214 00:12:32,510 --> 00:12:34,510 that's correct. The total estimate is correct. 215 00:12:34,910 --> 00:12:37,530 Looks good, right? Except for one little thing: 216 00:12:37,530 --> 00:12:40,530 This estimate is valid for 32.5 days. 217 00:12:40,530 --> 00:12:43,530 You can see that the 30 was replaced with 32.5. 218 00:12:44,330 --> 00:12:48,770 I didn't want to do that. So I need to go back to my code, and let's change that to 30. 219 00:12:50,270 --> 00:12:51,470 And now we're okay. 220 00:12:51,830 --> 00:12:54,630 This is easy to fix in this example because it's really small. 221 00:12:54,630 --> 00:12:56,830 But if you've got thousands of lines of code 222 00:12:56,830 --> 00:12:59,330 and you're doing search and replaces like this, 223 00:12:59,330 --> 00:13:03,730 it's real possible that that 30 is going to be in different places and it doesn't represent 224 00:13:03,730 --> 00:13:06,230 price per room. It represents something else. 225 00:13:06,230 --> 00:13:08,830 And you just really messed up your program. 226 00:13:08,830 --> 00:13:11,830 You made a lot of changes you didn't intend to change so 227 00:13:11,830 --> 00:13:16,230 you -- hopefully, you'll see what I mean. I'm going to undo this 30. 228 00:13:17,330 --> 00:13:21,130 And I hope I'm back to where I was. Yep, I'm back to where I was with 30. 229 00:13:21,130 --> 00:13:24,630 What I want to do here is I want to use declared constants 230 00:13:24,630 --> 00:13:28,130 rather than these literal constants. So let's do that. 231 00:13:28,130 --> 00:13:30,730 First, let's just create the variable and then we'll make it a constant. 232 00:13:30,730 --> 00:13:33,730 So we need something for the price per room. 233 00:13:33,730 --> 00:13:37,930 So let's create that. Let's say int price per room, 234 00:13:38,530 --> 00:13:40,730 and we can initialize it to 30. 235 00:13:41,430 --> 00:13:44,330 And then the idea being instead of using 30 here, 236 00:13:44,330 --> 00:13:46,330 we'll use that name price per room. 237 00:13:46,630 --> 00:13:49,630 Okay. That's okay but why an integer, 238 00:13:49,630 --> 00:13:52,990 that's really doesn't make too much sense, right? It makes sense with 30. 239 00:13:52,990 --> 00:13:55,790 But if I change the price per room to $32.50 240 00:13:55,790 --> 00:13:59,590 or some real number like that a floating point number, this won't work very well. 241 00:13:59,590 --> 00:14:02,790 So this really should be a double, and I'm going to change that right there. 242 00:14:03,590 --> 00:14:05,290 That gives us a floating point number. 243 00:14:05,890 --> 00:14:08,190 And the last thing I want to do is I want to make it const. 244 00:14:09,190 --> 00:14:12,190 I don't want that to change in my program. 245 00:14:12,190 --> 00:14:15,190 I'll always want it to be 30 while this program is running. 246 00:14:16,440 --> 00:14:20,800 Okay. Now if I try to change that right here, I'll just show you real quick. 247 00:14:20,800 --> 00:14:23,500 Price per room, let's say, I want to change that to a $100. 248 00:14:24,700 --> 00:14:26,500 And I'll just compile over here. 249 00:14:27,700 --> 00:14:30,060 I'll get a compiler error that says "error: 250 00:14:30,060 --> 00:14:33,260 assignment of read-only variable price per room" 251 00:14:33,260 --> 00:14:36,560 You just told the compiler that price per room is constant 252 00:14:36,560 --> 00:14:40,260 and here, you tried to change it. The compiler's saying no way. 253 00:14:40,260 --> 00:14:42,460 You just told me that this should not be allowed. 254 00:14:42,960 --> 00:14:46,760 Okay. So that's one of the the benefits of constants, as I mentioned in the lecture. 255 00:14:46,760 --> 00:14:47,960 So let me get rid of that. 256 00:14:48,760 --> 00:14:52,060 And so that's one of the constants we need. We also need another constant. 257 00:14:52,060 --> 00:14:55,660 I'll put a 30.0 in here to be more accurate because it is a real number. 258 00:14:56,460 --> 00:14:59,460 I need another one for the sales tax. So I could say const, 259 00:15:00,760 --> 00:15:01,860 again double. 260 00:15:03,060 --> 00:15:06,160 And I can say sales tax or sales rate or whatever you want to call it. 261 00:15:06,660 --> 00:15:08,860 And in this case is 0.06. 262 00:15:11,060 --> 00:15:14,360 And then finally, let's create another one for the 263 00:15:15,360 --> 00:15:17,800 estimate expiry, which is the 30 days. 264 00:15:17,800 --> 00:15:21,600 Const - this case it could be an integer. And we'll just say estimate 265 00:15:22,400 --> 00:15:23,500 expiry, 266 00:15:23,800 --> 00:15:25,800 and we'll initialize that to 30. 267 00:15:26,200 --> 00:15:29,200 And this would be a good place for a comment days. 268 00:15:31,200 --> 00:15:35,000 Okay. So now we've got those three constants. What we can do now is 269 00:15:35,000 --> 00:15:38,900 let's go through our code and we'll replace that 30, for example, 270 00:15:38,900 --> 00:15:42,900 with price per room. That 0.06 with sales 271 00:15:42,900 --> 00:15:44,150 tax and so forth. 272 00:15:44,150 --> 00:15:46,350 This is called refactoring your code. 273 00:15:47,150 --> 00:15:50,750 Refactoring means changing your code to make it better 274 00:15:50,750 --> 00:15:53,650 without changing behavior that's real important. 275 00:15:54,150 --> 00:15:58,050 Refactoring does not change the behavior of your code, it just makes your code better, 276 00:15:58,050 --> 00:16:02,550 more modifiable, easier to read and so forth. So let's do that. 277 00:16:02,700 --> 00:16:05,700 I'm going to use price per room here. I'm just going to copy it, 278 00:16:06,900 --> 00:16:10,900 and I'll paste it in here. I'll paste it in here. I'll paste it in here, 279 00:16:11,900 --> 00:16:16,700 in here, in here and not in here because that's my expiry. 280 00:16:17,360 --> 00:16:18,860 I'll take sales tax. 281 00:16:20,720 --> 00:16:24,320 I'll copy that, and I'll paste it here 282 00:16:25,320 --> 00:16:26,680 as well as over here. 283 00:16:27,680 --> 00:16:30,480 And then finally, we'll do the estimate expiry 284 00:16:31,980 --> 00:16:32,980 right here. 285 00:16:34,980 --> 00:16:37,280 All right. So now let's run this again. 286 00:16:39,180 --> 00:16:42,480 And the behavior should not change. I want 2 rooms cleaned, 287 00:16:43,080 --> 00:16:47,280 2 rooms at $30 is $60, $3.60 sales tax, 288 00:16:47,280 --> 00:16:51,180 $63.60 is a total estimate and it's valid for 30 days. 289 00:16:51,180 --> 00:16:51,980 Perfect. 290 00:16:51,980 --> 00:16:56,080 As you can see this code is much more readable now, you can actually see what's going on. 291 00:16:56,080 --> 00:17:00,880 And more importantly, if I want to change the price per room to $32.50, 292 00:17:01,130 --> 00:17:03,430 I only have to do it in one place right up here. 293 00:17:03,730 --> 00:17:05,730 So I can make this $32.50, 294 00:17:07,030 --> 00:17:07,630 save it, 295 00:17:08,329 --> 00:17:09,430 build and run again. 296 00:17:11,630 --> 00:17:15,230 2 rooms $32.50 which is $65, 297 00:17:15,230 --> 00:17:20,220 that's the cost, $3.90 tax, $68.90. And it's still valid for 30 days. 298 00:17:20,220 --> 00:17:23,520 So you can see the benefits of using declared constants. 299 00:17:23,520 --> 00:17:25,520 You can just make changes in one place. 300 00:17:25,920 --> 00:17:29,720 You're guaranteed that they're not going to change even if you made a mistake and it was an accident. 301 00:17:30,320 --> 00:17:33,320 And it makes your code so much more readable and modifiable. 302 00:17:35,320 --> 00:17:38,680 Now although this code is much better now after refactoring, 303 00:17:38,680 --> 00:17:40,180 it still has a few problems, 304 00:17:40,180 --> 00:17:43,480 and those problems are going to address as the course progresses. 305 00:17:43,680 --> 00:17:47,280 You hopefully have seen that right about here we're doing a calculation. 306 00:17:48,180 --> 00:17:51,180 And then right here, we're doing the same calculation again. 307 00:17:51,980 --> 00:17:54,480 And here, we're doing the same calculation again. 308 00:17:55,140 --> 00:17:57,140 And here, we're doing it again. Okay. 309 00:17:57,140 --> 00:18:00,640 So that's four places that we're doing the same calculation. 310 00:18:00,640 --> 00:18:02,640 That's four places we could mess up. 311 00:18:02,640 --> 00:18:06,960 We really don't want to do that. We want to do that calculation exactly in one place. 312 00:18:06,960 --> 00:18:10,660 And what we'll do is when we learn about assignment statements a little bit more about mathematical 313 00:18:10,660 --> 00:18:13,860 expressions, we'll do that there. And then a little bit later in the course, 314 00:18:13,860 --> 00:18:17,060 we'll use functions to do that in exactly one place. 315 00:18:17,060 --> 00:18:20,860 So if you notice that this isn't quite right you see a lot of duplication of code, 316 00:18:20,860 --> 00:18:23,860 you're absolutely right. And we're going to handle that as we go.