1 00:00:00,080 --> 00:00:04,893 [MUSIC] 2 00:00:04,893 --> 00:00:09,008 Our program always charges users $2 per can. 3 00:00:09,008 --> 00:00:11,830 It takes the number of cans the user wants to order, and 4 00:00:11,830 --> 00:00:15,260 multiplies it by 2 to get the total cost. 5 00:00:15,260 --> 00:00:19,360 Our last requirement is to provide a discount for large orders of can. 6 00:00:19,360 --> 00:00:22,450 Specifically, if the order is for 100 cans or more, 7 00:00:22,450 --> 00:00:25,050 the price should be $1.50 per can. 8 00:00:25,050 --> 00:00:29,617 If it's for 50 cans or more, the price should be $1.75 per can, and 9 00:00:29,617 --> 00:00:33,113 all other orders should be the full price, $2 per can. 10 00:00:33,113 --> 00:00:36,790 Our first problem here is to figure out which discount we should apply. 11 00:00:36,790 --> 00:00:40,420 Are they ordering 100 cans or more? 12 00:00:40,420 --> 00:00:41,150 50 to 99? 13 00:00:41,150 --> 00:00:42,598 Less than 50? 14 00:00:42,598 --> 00:00:45,690 To do this, we're going to need a way to compare numbers. 15 00:00:45,690 --> 00:00:49,340 This code will be a little too complicated to embed within a string. 16 00:00:49,340 --> 00:00:52,940 So first, let's move the part that calculates the price out to a method. 17 00:00:54,020 --> 00:00:57,540 We'll add a price function that takes the number of cans being ordered, and 18 00:00:57,540 --> 00:00:59,210 a parameter named quantity. 19 00:01:00,640 --> 00:01:04,490 Notice that we're having our function return a double floating point value, 20 00:01:04,490 --> 00:01:05,670 not an int. 21 00:01:05,670 --> 00:01:08,200 An integer return value would work right now, 22 00:01:08,200 --> 00:01:11,470 because we're only returning whole dollar amounts. 23 00:01:11,470 --> 00:01:15,620 But when we add discounts later, we'll be using fractional dollar amounts, 24 00:01:15,620 --> 00:01:19,050 like $1.75 or $1.50 per can. 25 00:01:19,050 --> 00:01:19,990 So we'll prepare for 26 00:01:19,990 --> 00:01:25,140 this by setting the price function up with a double floating point return type now. 27 00:01:25,140 --> 00:01:28,320 Quantity times 2 is actually an int value, but 28 00:01:28,320 --> 00:01:32,230 int is a less precise type, and double is a more precise type. 29 00:01:32,230 --> 00:01:36,750 So the conversion from int to double will be performed implicitly. 30 00:01:36,750 --> 00:01:41,740 Now that we've set the price method up, we can make a call to it in the main method. 31 00:01:41,740 --> 00:01:47,253 We pass it the number of cans and store its return value in a new total variable. 32 00:01:53,058 --> 00:01:55,945 Then we update our call to Console.WriteLine to use the new 33 00:01:55,945 --> 00:02:00,840 total variable, instead of embedding the calculation in the interpolated string. 34 00:02:00,840 --> 00:02:04,490 Also, I just noticed that we're still using our entry variable to print 35 00:02:04,490 --> 00:02:06,716 the number of cans the user entered. 36 00:02:06,716 --> 00:02:07,800 While that has worked so 37 00:02:07,800 --> 00:02:13,290 far because the string is going to contain the same content as the number variable, 38 00:02:13,290 --> 00:02:17,530 it seems safer to use the same variable that we're passing to the price function. 39 00:02:17,530 --> 00:02:21,250 So I'm gonna go ahead and change that from entry to number. 40 00:02:21,250 --> 00:02:25,626 So let me save this, and confirm that it still works in the same way. 41 00:02:27,330 --> 00:02:32,110 I'll make an order of 100 cans, and our total is $200. 42 00:02:32,110 --> 00:02:35,150 But this time the total was calculated by the price method. 43 00:02:36,490 --> 00:02:39,930 Now that we've moved our price calculation out to a separate method, 44 00:02:39,930 --> 00:02:43,720 we can work towards applying a discount for large orders. 45 00:02:43,720 --> 00:02:48,530 In order to know which discount to apply, we need to be able to tell if the order 46 00:02:48,530 --> 00:02:53,800 quantity is over 100, if it's between 50 and 100, or if it's below 50. 47 00:02:53,800 --> 00:02:58,468 C# uses comparison operators to tell whether one value is equal to, 48 00:02:58,468 --> 00:03:01,053 greater than, or less than another. 49 00:03:01,053 --> 00:03:03,790 Comparison operators return a Boolean Value. 50 00:03:03,790 --> 00:03:07,080 Boolean Values are either true or false. 51 00:03:07,080 --> 00:03:11,040 They're represented in C# code by the words true and false. 52 00:03:11,040 --> 00:03:15,550 Note, that these are not strings, so they're not surrounded by quotes. 53 00:03:15,550 --> 00:03:18,810 The most commonly used comparison operators in C#, 54 00:03:18,810 --> 00:03:23,560 are the greater than operator, the less than operator and the equality operator. 55 00:03:23,560 --> 00:03:27,810 The greater than operator will return true if the value to its left is 56 00:03:27,810 --> 00:03:29,780 greater than the value to its right. 57 00:03:29,780 --> 00:03:34,090 The less than operator will return true if the value to its left is less than 58 00:03:34,090 --> 00:03:35,870 the value to its right. 59 00:03:35,870 --> 00:03:40,090 And the equality operator will return true if the value to its left and 60 00:03:40,090 --> 00:03:42,770 the value to its right are equal. 61 00:03:42,770 --> 00:03:46,115 Let me save this and try running it so you can see the results. 62 00:03:49,015 --> 00:03:52,560 So we assign the value 75 to the quantity variable. 63 00:03:52,560 --> 00:03:55,510 And then we test whether quantity is greater than 50, 64 00:03:55,510 --> 00:03:57,590 which gives us a result of true. 65 00:03:57,590 --> 00:04:00,040 We test whether quantity is less than 50, 66 00:04:00,040 --> 00:04:04,410 which of course gives us a result of false, because quantity is 75. 67 00:04:04,410 --> 00:04:09,260 And then we test whether quantity is equal to 75, which it is. 68 00:04:09,260 --> 00:04:10,940 Now I want to warn you about something, 69 00:04:10,940 --> 00:04:15,530 it's very important not to confuse the quality operator, which is a double equal 70 00:04:15,530 --> 00:04:20,000 sign, with the assignment operator which is a single equals sign. 71 00:04:20,000 --> 00:04:22,750 So for example, this right here is a simple check 72 00:04:22,750 --> 00:04:28,330 whether quantity is equal to 99, it makes no change to the quantity variable. 73 00:04:28,330 --> 00:04:30,713 Let me save this and try running it to show you. 74 00:04:35,147 --> 00:04:38,630 You can see that first we compare quantity to 99 to see if they're equal. 75 00:04:38,630 --> 00:04:43,499 And then we print the value in quantity, and we can see that quantity is still 75. 76 00:04:44,650 --> 00:04:48,120 This will not be the case if we confuse the assignment operator and 77 00:04:48,120 --> 00:04:49,860 the quality operator. 78 00:04:49,860 --> 00:04:53,440 This code here doesn't check whether quantity is equal to 99. 79 00:04:53,440 --> 00:04:57,177 It reassigns the value 99 to quantity. 80 00:04:57,177 --> 00:05:00,096 Let me save that and show you what happens. 81 00:05:03,029 --> 00:05:06,515 So you can see that the check for a equality doesn't change the value in 82 00:05:06,515 --> 00:05:09,130 quantity, but the accidental reassignment does. 83 00:05:09,130 --> 00:05:12,780 Quantity is now set to 99 at the end of this program, 84 00:05:12,780 --> 00:05:15,410 which may not be what you wanted to do. 85 00:05:15,410 --> 00:05:18,530 It's very important not to confuse the assignment and 86 00:05:18,530 --> 00:05:21,660 the equality comparison operators. 87 00:05:21,660 --> 00:05:23,025 Let me get rid of this code here. 88 00:05:24,570 --> 00:05:27,820 And now let's talk about the greater than or equal and less than or 89 00:05:27,820 --> 00:05:29,650 equal to operators. 90 00:05:29,650 --> 00:05:32,590 The greater then or equal to operator returns true 91 00:05:32,590 --> 00:05:37,620 if the value to its left is greater than or equal to the value to its right. 92 00:05:37,620 --> 00:05:42,560 The less than or equal to operator returns true if the value to its left is less than 93 00:05:42,560 --> 00:05:44,620 or equal to the value to its right. 94 00:05:46,090 --> 00:05:48,510 So since quantity is set to 75 now, 95 00:05:48,510 --> 00:05:52,860 this will return true because 75 is greater than 50. 96 00:05:52,860 --> 00:05:59,750 And this will return false because 75 is neither less than or equal to 50. 97 00:05:59,750 --> 00:06:03,542 However, if we were then to reset the quantity variable to 50, 98 00:06:03,542 --> 00:06:08,460 both of these operations will be true because quantity isn't greater than 50 but 99 00:06:08,460 --> 00:06:10,500 it is equal to it. 100 00:06:10,500 --> 00:06:15,095 And quantity isn't less than 50, but again, it's still equal to it. 101 00:06:17,650 --> 00:06:20,270 Let me save this and run it so you can see the results. 102 00:06:23,490 --> 00:06:26,410 And you can see that both of these last operations are true. 103 00:06:27,820 --> 00:06:31,430 The last operator I wanna show you is the inequality operator 104 00:06:31,430 --> 00:06:34,960 written with an exclamation point followed by an equal sign. 105 00:06:34,960 --> 00:06:37,972 The exclamation point can be read aloud as not. 106 00:06:37,972 --> 00:06:43,120 So != can be read aloud as not equal. 107 00:06:43,120 --> 00:06:46,216 It's the opposite of the equality operator. 108 00:06:48,362 --> 00:06:53,750 So since quantity is set to 50, quantity not equal 50 will actually be false. 109 00:06:53,750 --> 00:06:58,182 This is the opposite of quantity equals 50, which is going to be true. 110 00:06:58,182 --> 00:07:01,390 Let me save this and show you the result. 111 00:07:05,952 --> 00:07:11,142 Quantity not equal to 50 gives us false, because quantity is equal to 50. 112 00:07:17,448 --> 00:07:20,810 By the way, you should never compare a string to a numeric type, 113 00:07:20,810 --> 00:07:22,005 you'll get an error. 114 00:07:25,083 --> 00:07:27,293 So here we have two string values, and 115 00:07:27,293 --> 00:07:31,517 we're attempting to compare them to two numeric values, to integers. 116 00:07:31,517 --> 00:07:33,915 Let me save this and show you the result. 117 00:07:37,301 --> 00:07:40,369 And we get compile errors for both those lines. 118 00:07:40,369 --> 00:07:44,270 You should only do a comparison if you can convert one class to match the other. 119 00:07:44,270 --> 00:07:48,966 So if we were to do an int.Parse conversion on each of 120 00:07:48,966 --> 00:07:51,970 these strings, this will work. 121 00:07:59,077 --> 00:08:02,770 Because now both of these comparisons are comparing one integer to another. 122 00:08:04,210 --> 00:08:08,140 By the way, all the comparison operators we just showed you work in pretty much 123 00:08:08,140 --> 00:08:10,140 every programming language out there. 124 00:08:10,140 --> 00:08:12,650 So remember them, because you'll be using them a lot.