>> ROB BOWDEN: Hi, I'm Rob.
And I hope you're charged up for credit.
So first thing we need to do is request the credit card
number from the user.
Here, we're using getLongLong.
You could have also used getString, but in that case, you'd need to check
that there were no non-numeric characters in the string.
So we'll use getLongLong.
>> Remember that you can't use something like getInt, since the number will be
too big to fit in an integer.
Once we have that number, we see here this while loop.
So this while loop is implementing Luhn's algorithm that you
see in the pset spec.
>> And it's actually going to be a bit clever.
So in the pset spec, notice that Steps One and Two are separate.
We first go over the entire credit card number, looking for every other
character starting from the second to last character, and multiplying them
and adding all the digits.
Then after that, we add in all of the other digits.
>> So instead of doing those in two separate steps, we're going to combine
them into one iteration over the entire credit card number.
Here, we see int cur digit equals credit card number, mod 10.
What is credit card number mod 10 doing?
It's giving us the last digit in the whole number.
So remember that if we divided the number up by 10, then the remainder
would be whatever that last digit is.
23 divided by 10, the remainder will be 3.
>> So the last digit, now here, we see we're branching on mult by 2.
So what we're going to be using mult by 2 for is differentiating between
one of the "every other numbers from the second digit" numbers.
Mult by 2 is going to start out as false, since the last digit should not
be considered from the second to last digit.
>> So then at the end of this for loop, we see that we're going to change this
from false to true.
On the next iteration of the for loop, it's going to considered true until
the end, when we change it from true to false.
Because then we'll be on the third to last digit, which isn't one of the
digits that we should multiply by 2.
>> So if we happen to be on one of those digits that we want to multiply by 2,
we see we're adding to our checksum.
And here, we're using the ternary operator to once
again be a bit clever.
So if cur digit is less than 5, then we can just do cur digit times 2.
That's simple.
If it's 1, then we want to add 2.
If it's 2, we want to add 4.
If it's 4, we want to add 8.
>> So what's special about 5?
Well, 5 times 2 is 10.
And remember from the pset spec that we want to add the digits of the
number times 2, and not the number times 2 itself.
So if the original number is 7, 7 times 2 is 14.
We want to add 1 plus 4 to the number, not 14.
>> So here, if the number is 5 or greater, what we're doing is cur digit
times 2 minus 9.
And if you think about that, 5 times 2 is 10.
And so we're adding 1, which is 10 minus 9.
And 6 times 2 is 12.
So we're adding 3, which is 12 minus 9.
And that works for all numbers.
>> So that's what we're adding to our checksum.
And this else is what's handling Step Two of Luhn's algorithm, which is just
adding the digit if it doesn't happen to be one of the every other digits.
So once we have that, this is keeping track of the first two characters of
the credit card number, the first two digits, since we're eventually going
to want to use that down here to verify, all right, a Visa has to start
with this, and an American Express needs to start with this, and so on.
>> Finally, we do credit card number equals credit card
number divided by 10.
Why do we do that?
Well, we just handled the last digit.
Dividing by 10 will shift the entire number over.
So now when we loop back, we're going to be handling the
second to last digit.
Then when we hit this again, we're going to cut off the second to last
digit, loop back, and handle the third to last digit, and so on, until the
number reaches 0, at which point we break out of the while loop.
>> We're also keeping track of the credit card number length, since that's
important to distinguish whether it's a valid credit card number.
So now, once we've calculated the checksum, we can determine whether it
is a valid card.
The checksum mod 10 is part of Luhn's algorithm.
If checksum mod 10 returns something non-zero, then this will return true,
in which case, the number must be invalid.
>> Otherwise, if checksum mod 10 is 0, then we can continue.
This big else if is saying, if the first two digits are equal to AMEX 1,
where up here, we see that AMEX 1, as per the spec, is 34.
And we'll also compare it to AMEX 2, which is 37.
And the credit card number length is equal to the expected American Express
card length, then we can print American Express.
>> We'll do a similar thing with Visa.
The first two digits need to be greater than or equal to 40, or less
than or equal to 49.
Those represent valid Visa cards.
And the length needs to be equal to Visa Length 1 or Visa Length 2.
And so the length must be either 13 or 16 digits long.
>> And finally with MasterCard, it's similar to Visa, that the first two
digits need to be in a certain range, and the length must
be exactly 16 digits.
So if any of those cases hold, then in the first case, we'll print AMEX.
If this case holds, we'll print Visa.
If this case holds, we'll print MasterCard.
>> But if none of those hold, even if the checksum was valid,
we still print invalid.
Because it's not one of those types of cards.
My name is Rob, and I hope you found credit interesting.