In this video, we'll apply some of the concepts that we've learned in this section
to solve a problem.
What we'll do first is we'll solve the problem using literal constants
and then what we'll do is we'll refactor it to use declared constants.
And along the way you'll see how much better it is to use declared constants.
Okay. So here's the problem we're going to solve.
Frank has a carpet cleaning service that charges $30 per room
and there's a sales tax rate of 6%.
All the estimates are valid for 30 days. So our program is really simple.
We're going to prompt the user for the number of rooms they would like cleaned.
And we're going to provide an estimate.
The estimate looks something like this.
You can see here starting at line 13.
Estimate for carpet cleaning service: number of rooms,
2. That's the number that the user entered.
Price per room $30, cost $60.
Obviously, that would be $30 per room times x2 rooms.
The tax which would be 6% based on the $60,
which would be $3.60.
Then the total estimate would be $63.60, and then you print out.
This estimate is valid for 30 days.
Now I'm using dollars. You could use euros. You could use any currency you like.
Also I know that the formatting isn't right.
Obviously, $3.60 would be 3.60.
Don't worry about that right now. Let's worry about solving this problem and using
literal constants and using declared constants.
We'll worry about output formatting in a different section,
and we'll go over a bunch of different ways to do formatting, including
currency formatting based on your locale.
All right. So let's think about how we're going to solve this problem.
Programmers often use something called pseudocode.
Pseudocode is like an English-like or your own natural spoken language-
like representation of the algorithm or the steps necessary
to solve the problem.
In this example, I wrote down some pseudocode.
And pseudocode, don't let anybody tell you that there's a right way and a wrong way to do
pseudocode,there really isn't -- it's not a programming language.
This is something that organizes your thoughts and allows you to solve the problem.
Remember, if you can't solve the problem in pseudocode,
there's no way you're going to solve it in c or
in java or in any other programming language.
So in this case, this is my pseudocode.
These are the steps that I want to take to solve this problem.
The first thing i want to do and you can see here on line 23
is prompt the user to enter the number of rooms.
Great. I want to display the number of rooms to the user,
display the price per room then I want to display the cost which is
the number of rooms, times, the price per room.
I want to display the tax,
which is the number of rooms, times, the price per room, times the tax rate.
Then I want to display the total estimate. The total estimate of course is
the cost plus the tax.
And then I want to display at the end, this estimate is valid for 30 days.
So what we want to do is we want to mimic this sample output right here.
Okay. So let's get started.
What we'll do first is we'll do this using literal constants,
and then we'll refactor it using declared constants.
So the first thing I'm saying is "Hello welcome to Frank's Carpet Cleaning Service".
That's my little introduction. Now I need to ask the user,
again look at the pseudocode,
ask the user how many rooms they'd like to be cleaned.
Okay. So let's do that. We'll say cout
and we'll say "How many rooms would you like cleaned?"
And I'm going to type this along and if I make typos and errors that's fine.
I'll fix them along the way, and you'll probably make the same kinds of errors,
so you'll see how to fix them. Semicolon at the end
and there's a typo right here. So how many rooms would you like cleaned?
Now let's run this, and what I'm doing is I'm pressing ctrl F5,
which is build and run.
And you can see here "Hello welcome to Frank's Carpet Cleaning Service.
How many rooms would you like cleaned?" Perfect.
Now I'd like to put a space between the first line and the second line of output.
Now there's a couple of ways I can do that.
I can come down here and I can just add another end line,
and that'll print two new lines at the end, which would be fine.
I could also just say cout
end line here, which prints out a line. Let's run that. I'll show you what that looks like.
Now you can see that there's a line between the the first line and the second line.
And I can also use a character literal
constant right here. And in front of the how I can put a \n, a backslash n
That's a new line. Now I can get rid of this.
And it has the same effect. So what's going to happen here is
it's going to print a new line before it prints the how.
So if I run this now,
you can see I get the same effect.
So this is an example of embedding those escape codes as I mentioned
in the PowerPoint slides, into string literals.
So now we're here.
Next step is to read in the number of rooms from the user.
So obviously, we need
to use the extraction operator with the cin object,
but we need to store that somewhere. So let's create an integer. We'll create a variable here,
and we'll call it number of rooms.
Best practice is to initialize it. So I'm going to initialize it to 0.
And cin, and let's get the number of rooms
from cin.
Okay. So again, let's see where we're at.
We've outputted our prompt, and the user has entered the number of rooms.
Now we've got all the information that we need
to do our calculations.
We know that it's $30 per room,
and we know that it's a 6% tax rate, that's all I need.
So let's create some output statements, and I'm going
to close this down here just to create a little bit more room.
All right. So let's go here, and we're going to say
cout.
And let's put a \n here too because I'd like a space right here.
So I'm going to say estimate for Carpet Cleaning Service.
We'll create a new line here.
Now we want to display the number of rooms to the user.
I'll say number of rooms
and I'll put a dollar sign in here.
I'm not too worried about the formatting, I'm just using dollars so I might as well put the dollar sign here.
And I want to display the number of rooms.
Where do I get that? Well, I get that right from here.
That variable right there the user just entered a number and I stored it in there.
So that's going to contain whatever they entered.
So there's number of rooms and then end line.
All right. Let's run this. Control F5
and I've got an error that says
I forgot to put an l at the end of the end line. There we go.
So let's run this again.
How many rooms would you like cleaned? Let's say, I want two rooms cleaned.
Estimate for Carpet Cleaning Services, number of rooms 2.
I don't want a dollar there.
I want dollar for prices. So that- I'll fix that error,
2 rooms, perfect.
Okay. So now let's start doing some of the calculations.
But first, we need to print out the price per room,
and that's a given. That was given to us as $30.
So we'll say price per room.
Here we'll put a dollar sign,
and we'll say 30.
We'll use the literal right in there, and we'll say end line.
All right. So that's it. Then we'll say what's the cost?
Well, we'll have to do a calculation.
So we'll say cout would be cost.
And again, I'll put a dollar sign in here.
And what's the cost going to be? Well, the cost is going to be
30 times the number of rooms.
Right, $30 per room times the number of rooms.
And
we'll put a new line at the end.
Let's test this out as we go. So let's run it.
I want two rooms cleaned so I expect the cost at this point to be $60.
So 2 rooms, $30 per room, cost $60. So we're good.
All right so let's continue,
and we'll say cout, what's my tax
or my sales tax is --
and again,
I'll use dollar and my sale tax is going to be
the same calculation right, 30 times the number of rooms.
And I've got an extra quote character right here. Let me get rid of it.
30x the number of rooms times
0.06, which is the tax rate,
end line. I'll save this. Let's run one more time.
2 rooms cleaned, $30, it's $60,
6% of 60 is $3.60.
Okay. So we're good.
Now we're going to calculate the total estimate, which is going to be the cost plus the tax. But first
I just want to print out a nice little line that separates
everything together.
That's long enough. And I'll put an end line at the end.
All right. So now let's say what the total estimate is and then we just need to say the estimate is valid
for 30 days, and we're done. So cout,
we'll say total estimate.
And again, in dollars is going to be -- okay,
we need to do the calculation again. So we're going to say 30 times
number of rooms,
which gives us the cost, right.
And I'm going to put some parens around that because I need to add to that
plus 30 times
the number of rooms times 0.06,
which is my sales tax. So that's my cost plus my sales tax
and a new line at the end.
Last thing is, let's say, cout.
This estimate is valid
for,
however many days and we said 30.
And I'll put a space in front of the D just so we have a little room.
Another line here, and I think we're good.
Let me clean this up a little bit down here. So it all fits a little better.
All right. So let's run it and then we'll go over it real quick.
I'm going to save it and run it with control F5.
How many rooms would you like cleaned, 2 rooms.
Number of rooms is 2. The price per room is $30.
The cost is $60. The tax is $3.60.
So you would expect the total estimate to be $63.60, which it is.
This estimate is valid for 30 days.
Cool. Let's try it again. Let's run it again with 1 room this time.
One room is $30. The cost is $30. The tax is $1.80.
Total estimate $31.80. So it looks good.
Don't forget to test with 0.
So 0 rooms cleaned, 0 cost, 0 tax, 0 estimate.
Great. Don't worry about testing with illegal values. We'll learn how to deal with illegal values later.
So for example, expected to use is going to type 0 or something positive in
the number of rooms.
They're not going to type in -1 or Joe or Frank or anything like that.
As I said, we'll deal with exceptions as the course progresses.
So that's it. That's the program. It works.
It's pretty easy to understand. It follows our pseudocode,
but there's a couple of issues here.
First, suppose that I
want to increase my cost per room or my price per room to
$32.50,
what do I do?
Well, I could do a search and replace, right. Let's do that.
I'll do a search, find and replace
and I want to look for 30 and I want to replace it with $32.50 and I want to do it everywhere,
replace all. Done. I'll close this up,
and let's run it.
So I want to do 2 rooms,
2 times $32.50 is $65, that's correct. The tax is $33.90,
that's correct. The total estimate is correct.
Looks good, right? Except for one little thing:
This estimate is valid for 32.5 days.
You can see that the 30 was replaced with 32.5.
I didn't want to do that. So I need to go back to my code, and let's change that to 30.
And now we're okay.
This is easy to fix in this example because it's really small.
But if you've got thousands of lines of code
and you're doing search and replaces like this,
it's real possible that that 30 is going to be in different places and it doesn't represent
price per room. It represents something else.
And you just really messed up your program.
You made a lot of changes you didn't intend to change so
you -- hopefully, you'll see what I mean. I'm going to undo this 30.
And I hope I'm back to where I was. Yep, I'm back to where I was with 30.
What I want to do here is I want to use declared constants
rather than these literal constants. So let's do that.
First, let's just create the variable and then we'll make it a constant.
So we need something for the price per room.
So let's create that. Let's say int price per room,
and we can initialize it to 30.
And then the idea being instead of using 30 here,
we'll use that name price per room.
Okay. That's okay but why an integer,
that's really doesn't make too much sense, right? It makes sense with 30.
But if I change the price per room to $32.50
or some real number like that a floating point number, this won't work very well.
So this really should be a double, and I'm going to change that right there.
That gives us a floating point number.
And the last thing I want to do is I want to make it const.
I don't want that to change in my program.
I'll always want it to be 30 while this program is running.
Okay. Now if I try to change that right here, I'll just show you real quick.
Price per room, let's say, I want to change that to a $100.
And I'll just compile over here.
I'll get a compiler error that says "error:
assignment of read-only variable price per room"
You just told the compiler that price per room is constant
and here, you tried to change it. The compiler's saying no way.
You just told me that this should not be allowed.
Okay. So that's one of the the benefits of constants, as I mentioned in the lecture.
So let me get rid of that.
And so that's one of the constants we need. We also need another constant.
I'll put a 30.0 in here to be more accurate because it is a real number.
I need another one for the sales tax. So I could say const,
again double.
And I can say sales tax or sales rate or whatever you want to call it.
And in this case is 0.06.
And then finally, let's create another one for the
estimate expiry, which is the 30 days.
Const - this case it could be an integer. And we'll just say estimate
expiry,
and we'll initialize that to 30.
And this would be a good place for a comment days.
Okay. So now we've got those three constants. What we can do now is
let's go through our code and we'll replace that 30, for example,
with price per room. That 0.06 with sales
tax and so forth.
This is called refactoring your code.
Refactoring means changing your code to make it better
without changing behavior that's real important.
Refactoring does not change the behavior of your code, it just makes your code better,
more modifiable, easier to read and so forth. So let's do that.
I'm going to use price per room here. I'm just going to copy it,
and I'll paste it in here. I'll paste it in here. I'll paste it in here,
in here, in here and not in here because that's my expiry.
I'll take sales tax.
I'll copy that, and I'll paste it here
as well as over here.
And then finally, we'll do the estimate expiry
right here.
All right. So now let's run this again.
And the behavior should not change. I want 2 rooms cleaned,
2 rooms at $30 is $60, $3.60 sales tax,
$63.60 is a total estimate and it's valid for 30 days.
Perfect.
As you can see this code is much more readable now, you can actually see what's going on.
And more importantly, if I want to change the price per room to $32.50,
I only have to do it in one place right up here.
So I can make this $32.50,
save it,
build and run again.
2 rooms $32.50 which is $65,
that's the cost, $3.90 tax, $68.90. And it's still valid for 30 days.
So you can see the benefits of using declared constants.
You can just make changes in one place.
You're guaranteed that they're not going to change even if you made a mistake and it was an accident.
And it makes your code so much more readable and modifiable.
Now although this code is much better now after refactoring,
it still has a few problems,
and those problems are going to address as the course progresses.
You hopefully have seen that right about here we're doing a calculation.
And then right here, we're doing the same calculation again.
And here, we're doing the same calculation again.
And here, we're doing it again. Okay.
So that's four places that we're doing the same calculation.
That's four places we could mess up.
We really don't want to do that. We want to do that calculation exactly in one place.
And what we'll do is when we learn about assignment statements a little bit more about mathematical
expressions, we'll do that there. And then a little bit later in the course,
we'll use functions to do that in exactly one place.
So if you notice that this isn't quite right you see a lot of duplication of code,
you're absolutely right. And we're going to handle that as we go.