1 00:00:00,000 --> 00:00:13,290 2 00:00:13,290 --> 00:00:14,570 >> ROB BOWDEN: Hi, I'm Rob. 3 00:00:14,570 --> 00:00:17,610 And I hope you're charged up for credit. 4 00:00:17,610 --> 00:00:20,710 So first thing we need to do is request the credit card 5 00:00:20,710 --> 00:00:22,710 number from the user. 6 00:00:22,710 --> 00:00:25,060 Here, we're using getLongLong. 7 00:00:25,060 --> 00:00:29,070 You could have also used getString, but in that case, you'd need to check 8 00:00:29,070 --> 00:00:32,340 that there were no non-numeric characters in the string. 9 00:00:32,340 --> 00:00:34,560 So we'll use getLongLong. 10 00:00:34,560 --> 00:00:38,070 >> Remember that you can't use something like getInt, since the number will be 11 00:00:38,070 --> 00:00:40,650 too big to fit in an integer. 12 00:00:40,650 --> 00:00:44,480 Once we have that number, we see here this while loop. 13 00:00:44,480 --> 00:00:48,210 So this while loop is implementing Luhn's algorithm that you 14 00:00:48,210 --> 00:00:50,980 see in the pset spec. 15 00:00:50,980 --> 00:00:53,830 >> And it's actually going to be a bit clever. 16 00:00:53,830 --> 00:01:00,800 So in the pset spec, notice that Steps One and Two are separate. 17 00:01:00,800 --> 00:01:05,160 We first go over the entire credit card number, looking for every other 18 00:01:05,160 --> 00:01:09,775 character starting from the second to last character, and multiplying them 19 00:01:09,775 --> 00:01:11,750 and adding all the digits. 20 00:01:11,750 --> 00:01:16,150 Then after that, we add in all of the other digits. 21 00:01:16,150 --> 00:01:20,660 >> So instead of doing those in two separate steps, we're going to combine 22 00:01:20,660 --> 00:01:24,430 them into one iteration over the entire credit card number. 23 00:01:24,430 --> 00:01:29,710 Here, we see int cur digit equals credit card number, mod 10. 24 00:01:29,710 --> 00:01:32,050 What is credit card number mod 10 doing? 25 00:01:32,050 --> 00:01:35,750 It's giving us the last digit in the whole number. 26 00:01:35,750 --> 00:01:39,340 So remember that if we divided the number up by 10, then the remainder 27 00:01:39,340 --> 00:01:42,180 would be whatever that last digit is. 28 00:01:42,180 --> 00:01:46,560 23 divided by 10, the remainder will be 3. 29 00:01:46,560 --> 00:01:53,760 >> So the last digit, now here, we see we're branching on mult by 2. 30 00:01:53,760 --> 00:01:57,630 So what we're going to be using mult by 2 for is differentiating between 31 00:01:57,630 --> 00:02:02,110 one of the "every other numbers from the second digit" numbers. 32 00:02:02,110 --> 00:02:08,310 Mult by 2 is going to start out as false, since the last digit should not 33 00:02:08,310 --> 00:02:11,750 be considered from the second to last digit. 34 00:02:11,750 --> 00:02:16,760 >> So then at the end of this for loop, we see that we're going to change this 35 00:02:16,760 --> 00:02:18,870 from false to true. 36 00:02:18,870 --> 00:02:22,520 On the next iteration of the for loop, it's going to considered true until 37 00:02:22,520 --> 00:02:25,090 the end, when we change it from true to false. 38 00:02:25,090 --> 00:02:28,290 Because then we'll be on the third to last digit, which isn't one of the 39 00:02:28,290 --> 00:02:32,210 digits that we should multiply by 2. 40 00:02:32,210 --> 00:02:37,410 >> So if we happen to be on one of those digits that we want to multiply by 2, 41 00:02:37,410 --> 00:02:40,610 we see we're adding to our checksum. 42 00:02:40,610 --> 00:02:43,640 And here, we're using the ternary operator to once 43 00:02:43,640 --> 00:02:45,470 again be a bit clever. 44 00:02:45,470 --> 00:02:50,170 So if cur digit is less than 5, then we can just do cur digit times 2. 45 00:02:50,170 --> 00:02:50,690 That's simple. 46 00:02:50,690 --> 00:02:52,770 If it's 1, then we want to add 2. 47 00:02:52,770 --> 00:02:54,090 If it's 2, we want to add 4. 48 00:02:54,090 --> 00:02:55,530 If it's 4, we want to add 8. 49 00:02:55,530 --> 00:02:57,400 >> So what's special about 5? 50 00:02:57,400 --> 00:03:00,290 Well, 5 times 2 is 10. 51 00:03:00,290 --> 00:03:05,920 And remember from the pset spec that we want to add the digits of the 52 00:03:05,920 --> 00:03:09,300 number times 2, and not the number times 2 itself. 53 00:03:09,300 --> 00:03:13,920 So if the original number is 7, 7 times 2 is 14. 54 00:03:13,920 --> 00:03:18,930 We want to add 1 plus 4 to the number, not 14. 55 00:03:18,930 --> 00:03:24,050 >> So here, if the number is 5 or greater, what we're doing is cur digit 56 00:03:24,050 --> 00:03:26,470 times 2 minus 9. 57 00:03:26,470 --> 00:03:29,940 And if you think about that, 5 times 2 is 10. 58 00:03:29,940 --> 00:03:33,130 And so we're adding 1, which is 10 minus 9. 59 00:03:33,130 --> 00:03:35,490 And 6 times 2 is 12. 60 00:03:35,490 --> 00:03:38,380 So we're adding 3, which is 12 minus 9. 61 00:03:38,380 --> 00:03:40,250 And that works for all numbers. 62 00:03:40,250 --> 00:03:43,330 >> So that's what we're adding to our checksum. 63 00:03:43,330 --> 00:03:49,970 And this else is what's handling Step Two of Luhn's algorithm, which is just 64 00:03:49,970 --> 00:03:55,010 adding the digit if it doesn't happen to be one of the every other digits. 65 00:03:55,010 --> 00:04:01,440 So once we have that, this is keeping track of the first two characters of 66 00:04:01,440 --> 00:04:05,220 the credit card number, the first two digits, since we're eventually going 67 00:04:05,220 --> 00:04:08,980 to want to use that down here to verify, all right, a Visa has to start 68 00:04:08,980 --> 00:04:14,440 with this, and an American Express needs to start with this, and so on. 69 00:04:14,440 --> 00:04:16,850 >> Finally, we do credit card number equals credit card 70 00:04:16,850 --> 00:04:18,730 number divided by 10. 71 00:04:18,730 --> 00:04:19,829 Why do we do that? 72 00:04:19,829 --> 00:04:22,070 Well, we just handled the last digit. 73 00:04:22,070 --> 00:04:24,880 Dividing by 10 will shift the entire number over. 74 00:04:24,880 --> 00:04:27,150 So now when we loop back, we're going to be handling the 75 00:04:27,150 --> 00:04:28,540 second to last digit. 76 00:04:28,540 --> 00:04:31,060 Then when we hit this again, we're going to cut off the second to last 77 00:04:31,060 --> 00:04:35,060 digit, loop back, and handle the third to last digit, and so on, until the 78 00:04:35,060 --> 00:04:40,120 number reaches 0, at which point we break out of the while loop. 79 00:04:40,120 --> 00:04:43,560 >> We're also keeping track of the credit card number length, since that's 80 00:04:43,560 --> 00:04:48,440 important to distinguish whether it's a valid credit card number. 81 00:04:48,440 --> 00:04:53,560 So now, once we've calculated the checksum, we can determine whether it 82 00:04:53,560 --> 00:04:55,180 is a valid card. 83 00:04:55,180 --> 00:04:58,010 The checksum mod 10 is part of Luhn's algorithm. 84 00:04:58,010 --> 00:05:03,360 If checksum mod 10 returns something non-zero, then this will return true, 85 00:05:03,360 --> 00:05:06,650 in which case, the number must be invalid. 86 00:05:06,650 --> 00:05:12,590 >> Otherwise, if checksum mod 10 is 0, then we can continue. 87 00:05:12,590 --> 00:05:18,360 This big else if is saying, if the first two digits are equal to AMEX 1, 88 00:05:18,360 --> 00:05:23,640 where up here, we see that AMEX 1, as per the spec, is 34. 89 00:05:23,640 --> 00:05:26,595 And we'll also compare it to AMEX 2, which is 37. 90 00:05:26,595 --> 00:05:30,360 91 00:05:30,360 --> 00:05:34,210 And the credit card number length is equal to the expected American Express 92 00:05:34,210 --> 00:05:37,910 card length, then we can print American Express. 93 00:05:37,910 --> 00:05:41,920 >> We'll do a similar thing with Visa. 94 00:05:41,920 --> 00:05:51,940 The first two digits need to be greater than or equal to 40, or less 95 00:05:51,940 --> 00:05:54,290 than or equal to 49. 96 00:05:54,290 --> 00:05:57,180 Those represent valid Visa cards. 97 00:05:57,180 --> 00:06:01,530 And the length needs to be equal to Visa Length 1 or Visa Length 2. 98 00:06:01,530 --> 00:06:07,320 And so the length must be either 13 or 16 digits long. 99 00:06:07,320 --> 00:06:12,240 >> And finally with MasterCard, it's similar to Visa, that the first two 100 00:06:12,240 --> 00:06:15,340 digits need to be in a certain range, and the length must 101 00:06:15,340 --> 00:06:19,440 be exactly 16 digits. 102 00:06:19,440 --> 00:06:24,390 So if any of those cases hold, then in the first case, we'll print AMEX. 103 00:06:24,390 --> 00:06:26,310 If this case holds, we'll print Visa. 104 00:06:26,310 --> 00:06:28,400 If this case holds, we'll print MasterCard. 105 00:06:28,400 --> 00:06:32,170 >> But if none of those hold, even if the checksum was valid, 106 00:06:32,170 --> 00:06:33,900 we still print invalid. 107 00:06:33,900 --> 00:06:37,050 Because it's not one of those types of cards. 108 00:06:37,050 --> 00:06:40,030 My name is Rob, and I hope you found credit interesting. 109 00:06:40,030 --> 00:06:46,272