3.3 Fare Calculator Part 2
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
4 hours 19 minutes
hi and welcome back to the course. Now, if you noticed in the last video lecture or lab, I should say we went over creating the first part of our fair calculator where we defined a certain set of variables, one of them being the age and another one being tax and in standard price, total price and total tax.
Then we sent we set the total price to zero and the tax rate
to 13% or 0.13 in decimal numbers. We also made use of the scanner class and which allowed us to accept user input from the user, which in this case is an age. So an inter juror number
and know what we're gonna do is actually implement our
concept that we learned in the last video lecture the video Lecter before that which was related to and based on if statements.
So let's say I have a user whose age is less than or equal to 10 and I want to set a standard price that I will charge them to $5.
And then I'd also like to add tax on top of that and then include that in my total price. How would I be doing that?
So, first off,
make use of that if word. So you were typing. If and then since we have an age of arable, which is what we're storing from the user input,
I would put it if age is less than or equal to. And this year is the lesson or equal to
sign your operator
less than a miracle to 10. Then I would like to set the standard price too.
And now, I would like to calculate the total tax
in order for me to calculate the total attacks.
Think about what it is that you need to do.
So if you're thinking along the lines of multiplying the tax with the standard price, that is exactly correct. And that is what you need to do. So I would do tax times
is it that that I would have to do or go through in order to calculate my total price?
Now, this may seem pretty trivial, but what we need to go through is we tow ad,
um, the standard price and the total tax, which will essentially give us our total price,
so we would be doing standard. Apply a price on and plus total tax.
Now, this is just one condition for someone whose age is less than or equal to 10. But let's say we wanted to have another condition where we are checking if the user's age is greater than 10 or it is less than or equal to 18.
And if that happens to be the case, then I would like to set the standard price to $10
and then essentially, have these two lines repeat, since they complete the job of calculating the total tax in the total price.
So let's go ahead and add that into our program so I would be doing else if
age is creator than 10
is less than or equal to 18.
It's a simple as that you
take your age and you check if it's greater than 10 or if it's a lesson or equal to
And this over here is a symbol of or if you are not familiar with it. And we discussed that this is a center for symbol for less than or equal to.
Now what is it that I would like to do if this happens to be the case. So, as I mentioned previously, I would like to set the standard price to $10.
And then these two lines over here would repeat.
So I'm saying Go ahead and copy them control, see?
And then control Beat to face them.
Now, after doing all these calculations, there is no use of them unless we actually print something to the consul.
So I'd like to tell the user that this is their total price to the fair. So in order to do that,
I would say something like the total
for entry to the
and then total costs or a total price.
Perfect. Now what that would do is print out the result of our calculations. No, it's actually go ahead and run our program.
And what this will do once we click run is it'll compiler program. Build it and then
we will be asked for an age. So let's go ahead and start with 10.
Right? So no, it's telling us the total cost for entry to the festival is $5.65 to confirm that we can do it manually.
So let's just go ahead and follow the formula or the calculations we set up here. So the ticket is $5. For a standard price,
multiply that by 0.13
which gives you 0.65
and then add $5 on to that
perfect. That gives you $5.65. And that is exactly what we got as an output here.
Now let's say the age of the user is 18.
Then let's type in 18.
So now it is saying the total cost for the entry to the festival is $11.30
so I would go back to my calculator and then
I'm set. First, start with the standard price, which is $10 for someone whose age is greater than 10 or less than or equal to 18
and then essentially multiply so not add. But multiply that by 0.13
and then add 10 to that, which gives me 11.3, which is exactly the number that we calculated over here.
Perfect. So that's essentially the fair calculator that we wanted to build too sweet and simple program for you to grasp the concept of
if and else if statements now going back to our,
We covered in this video a summary implementing if statements by creating a fair calculator program and in the next video that's coming up next, we will be going over four loops.
Now, as always, if you have any questions at all, feel free to reach out to me of your Lincoln, and I would be more than happy to end answer any of your questions, and I look forward to seeing you in the next of the electorate. Thank you very much for watching.