1 00:00:00,000 --> 00:00:02,561 Now that we understand math operations, 2 00:00:02,561 --> 00:00:06,153 let's see if we can calculate a total price for the order. 3 00:00:06,153 --> 00:00:12,174 At the base rate, each can is \$2, so we need to multiply the number of cans by 2. 4 00:00:12,174 --> 00:00:15,950 We'll worry about applying discounts for larger orders later. 5 00:00:15,950 --> 00:00:19,759 We'll update the call, the Console.WriteLine to print the number of 6 00:00:19,759 --> 00:00:22,882 cans entered, and then the same number multiplied by 2. 7 00:00:29,288 --> 00:00:31,865 But this generates an error. 8 00:00:31,865 --> 00:00:35,891 Operator cannot be applied to operands of type string and int. 9 00:00:39,678 --> 00:00:44,685 The value returned from Console.ReadLine in the ask method is a string. 10 00:00:44,685 --> 00:00:47,975 So the value returned from ask is also a string. 11 00:00:47,975 --> 00:00:52,588 And you can't multiply a string by a number, it just doesn't make sense. 12 00:00:52,588 --> 00:00:57,619 We can't use a conversion though, those only work on numeric types. 13 00:00:57,619 --> 00:01:01,624 We'll get an error if we try to use one on a string. 14 00:01:01,624 --> 00:01:06,442 Instead, all of the built-in types have a static parse method that we can call 15 00:01:06,442 --> 00:01:08,195 the convert string values. 16 00:01:08,195 --> 00:01:12,185 I can write the type name I want to convert to, int, a dot, 17 00:01:12,185 --> 00:01:17,986 the name of the parse method, parenthesis, and the string value I want to convert. 18 00:01:17,986 --> 00:01:20,850 That will get me a value of the int type. 19 00:01:25,478 --> 00:01:29,931 Remember, int is just a shorthand for the System.Int32 type, 20 00:01:29,931 --> 00:01:32,963 that's why it shows System.Int32 here. 21 00:01:32,963 --> 00:01:37,309 Be sure that you use a string that will actually convert to the type you want. 22 00:01:37,309 --> 00:01:41,216 For example, what if I changed this string to hold a floating point number? 23 00:01:42,995 --> 00:01:46,062 I'll get an error because it's expecting an integer. 24 00:01:48,714 --> 00:01:51,350 Input string was not in a correct format. 25 00:01:57,464 --> 00:02:01,852 And of course, using a string that doesn't even contain a number will fail as well. 26 00:02:01,852 --> 00:02:04,446 I'll just change the string back to 12. 27 00:02:10,032 --> 00:02:14,142 As I mentioned, every builtin type has a parse method, you just have to 28 00:02:14,142 --> 00:02:18,059 be sure to provide a string that will convert to a value of that type. 29 00:02:18,059 --> 00:02:22,839 So the double type has a parse method that will convert floating point values. 30 00:02:22,839 --> 00:02:26,956 The bool type has a parse method that will convert Boolean values. 31 00:02:29,198 --> 00:02:32,961 And the char type has a parse method that will convert single letters. 32 00:02:40,920 --> 00:02:44,125 And here is the results of all the code we added. 33 00:02:44,125 --> 00:02:47,977 Double.parse gives us say results of type double. 34 00:02:47,977 --> 00:02:50,924 Bool.parse gives us a result of type Boolean. 35 00:02:50,924 --> 00:02:54,158 And Char.parse gives us a result of type char. 36 00:02:56,566 --> 00:03:02,955 So we can use the int.Parse method to fix our cat food store program. 37 00:03:02,955 --> 00:03:07,533 We'll just take the string entry that we got back from the ask method and 38 00:03:07,533 --> 00:03:11,369 pass it to int.Parse, that will give us a result of type int 39 00:03:13,847 --> 00:03:17,625 Then instead of multiplying the string entry variable, 40 00:03:17,625 --> 00:03:20,467 we multiply the integer number variable. 41 00:03:20,467 --> 00:03:22,503 Let's save this and try running it. 42 00:03:26,111 --> 00:03:28,932 We're asked, how many cans we want to order, we'll say 10. 43 00:03:28,932 --> 00:03:33,913 And the result gets multiplied by 2 to give us a total value of 20. 44 00:03:33,913 --> 00:03:38,026 In this stage we've covered the basics of working with numbers in C#. 45 00:03:38,026 --> 00:03:42,226 Check the teacher's notes for ways you can get more practice with numbers and 46 00:03:42,226 --> 00:03:43,267 math operations. 47 00:03:43,267 --> 00:03:46,617 Now that you know the basics of math operations and C#, 48 00:03:46,617 --> 00:03:48,762 calculating a total price is easy. 49 00:03:48,762 --> 00:03:53,252 Nice work, you've implemented three of the four features our program needs, 50 00:03:53,252 --> 00:03:54,399 just one more to go. 51 00:03:54,399 --> 00:03:58,049 Our last requirement is to offer discounts for large orders. 52 00:03:58,049 --> 00:04:01,578 If they're ordering 50 we charge \$1 75 a piece. 53 00:04:01,578 --> 00:04:05,001 If they're ordering 100 we charge \$1 50 a piece. 54 00:04:05,001 --> 00:04:09,405 We don't really know a way to change our program's behavior under different 55 00:04:09,405 --> 00:04:10,429 conditions yet. 56 00:04:10,429 --> 00:04:13,230 In the final stage of this course, we'll show you how.