Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we understand math operations, let's see if we can calculate a total price for the order.
Cat Food Store Features
Display welcome messageAsk for quantity- Calculate total
- Discount for large orders
Now that we understand math operations, let's see if we can calculate a total price for the order. At the base rate, each can is $2, so we need to multiply the number of cans by 2. We'll worry about applying discounts for larger orders later.
- We'll update the call to
Console.WriteLine
to print the number of cans entered, and then the same number multiplied by 2.
Console.WriteLine($"For {entry} cans, your total is: ${entry * 2}");
- But this generates an error:
Operator '*' cannot be applied to operands of type 'string' and 'int'
- The value returned from
Console.ReadLine
in theAsk
method is astring
. - So the value returned from
Ask
is also astring
. - And you can't multiply a string by a number; it just doesn't make sense.
- We need to convert the string to an actual number before multiplying it.
- We can't use a conversion, though:
int dozen = (int)"12";
- Those only work on numeric types. We'll get an error if we try to use one on a string: "Cannot convert type 'string' to 'int'"
- Instead, all of the built-in types have a static
Parse
method that we can call to convert string values.- I can write the type name I want to convert to,
int
, a dot, the name of theParse
method, parentheses, and the string value I want to convert:int dozen = int.Parse("12");
- That will get me a value of the
int
type. - Remember,
int
is just a shorthand for theSystem.Int32
type; that's why it showsSystem.Int32
here.
- I can write the type name I want to convert to,
int dozen = int.Parse("12");
Console.WriteLine(dozen.GetType()); // => System.Int32
Console.WriteLine(dozen); // => 12
- Be sure that you use a string that will actually convert to the type you want.
- For example, what if I changed this string to hold a floating-point number?
int dozen = int.Parse("12.42398");
- I'll get an error, because it's expecting an integer: "System.FormatException: Input string was not in a correct format."
- And of course, using a string that doesn't even contain a number will fail as well:
int dozen = int.Parse("ADSFAFDS");
- For example, what if I changed this string to hold a floating-point number?
- As I mentioned, every built-in type has a
Parse
method.- You just have to be sure to provide a string that will convert to a value of that type.
double pi = double.Parse("3.1415");
Console.WriteLine(pi.GetType());
Console.WriteLine(pi);
bool flag = bool.Parse("true");
Console.WriteLine(flag.GetType());
Console.WriteLine(flag);
char letter = char.Parse("A");
Console.WriteLine(letter.GetType());
Console.WriteLine(letter);
int number = int.Parse(entry);
Console.WriteLine($"For {number} cans, your total is: ${number * 2}");
Now that we understand math operations,
0:00
let's see if we can calculate
a total price for the order.
0:02
At the base rate, each can is $2, so we
need to multiply the number of cans by 2.
0:06
We'll worry about applying discounts for
larger orders later.
0:12
We'll update the call, the
Console.WriteLine to print the number of
0:15
cans entered, and
then the same number multiplied by 2.
0:19
But this generates an error.
0:29
Operator cannot be applied to
operands of type string and int.
0:31
The value returned from Console.ReadLine
in the ask method is a string.
0:39
So the value returned from
ask is also a string.
0:44
And you can't multiply a string by
a number, it just doesn't make sense.
0:47
We can't use a conversion though,
those only work on numeric types.
0:52
We'll get an error if we
try to use one on a string.
0:57
Instead, all of the built-in types have
a static parse method that we can call
1:01
the convert string values.
1:06
I can write the type name I
want to convert to, int, a dot,
1:08
the name of the parse method, parenthesis,
and the string value I want to convert.
1:12
That will get me a value of the int type.
1:17
Remember, int is just a shorthand for
the System.Int32 type,
1:25
that's why it shows System.Int32 here.
1:29
Be sure that you use a string that will
actually convert to the type you want.
1:32
For example, what if I changed this
string to hold a floating point number?
1:37
I'll get an error because
it's expecting an integer.
1:42
Input string was not in a correct format.
1:48
And of course, using a string that doesn't
even contain a number will fail as well.
1:57
I'll just change the string back to 12.
2:01
As I mentioned, every builtin type
has a parse method, you just have to
2:10
be sure to provide a string that will
convert to a value of that type.
2:14
So the double type has a parse method
that will convert floating point values.
2:18
The bool type has a parse method
that will convert Boolean values.
2:22
And the char type has a parse method
that will convert single letters.
2:29
And here is the results
of all the code we added.
2:40
Double.parse gives us say
results of type double.
2:44
Bool.parse gives us
a result of type Boolean.
2:47
And Char.parse gives us
a result of type char.
2:50
So we can use the int.Parse method
to fix our cat food store program.
2:56
We'll just take the string entry that
we got back from the ask method and
3:02
pass it to int.Parse,
that will give us a result of type int
3:07
Then instead of multiplying
the string entry variable,
3:13
we multiply the integer number variable.
3:17
Let's save this and try running it.
3:20
We're asked, how many cans we
want to order, we'll say 10.
3:26
And the result gets multiplied by
2 to give us a total value of 20.
3:28
In this stage we've covered the basics
of working with numbers in C#.
3:33
Check the teacher's notes for ways you
can get more practice with numbers and
3:38
math operations.
3:42
Now that you know the basics
of math operations and C#,
3:43
calculating a total price is easy.
3:46
Nice work, you've implemented three of
the four features our program needs,
3:48
just one more to go.
3:53
Our last requirement is to offer
discounts for large orders.
3:54
If they're ordering 50
we charge $1 75 a piece.
3:58
If they're ordering 100
we charge $1 50 a piece.
4:01
We don't really know a way to change
our program's behavior under different
4:05
conditions yet.
4:09
In the final stage of this course,
we'll show you how.
4:10
You need to sign up for Treehouse in order to download course files.
Sign up