Generating Credit Card Numbers

Ever notice how when you purchase something online, and you make one small mistake when typing in your credit card number, the web page immediately identifies this number as invalid? This can be done because credit card numbers have certain built-in protections against "typos".

The first six digits of a credit card number make up the issuer identification number (IIN) and -- as the name suggests -- identifies the issuer of the credit card. Here's a list of known IIN prefixes.

After that comes the account number, which can be $6$ to $12$ digits in length.

Finally, the last digit is a check digit -- a digit that can be calculated from the preceding digits and meant to detect errors (e.g., the aforementioned "typos").

As an example, suppose one's credit card number is $537722361729123$$4$. Here, the "$4$" is the check digit. This digit can be determined solely from the digits that precede it through what is called the Luhn Algorithm. If when entering this credit card number, one accidentally types a "$7$" where the right-most "$1$" should be (i.e., $537722361729$$7$$23$$4$), the check digit produced from the first $15$ digits, in accordance with the Luhn Algorithm, will now disagree with "$4$" on the end -- flagging this as an invalid credit card number.

The Luhn Algorithm works in the following way. Let's suppose that the number for which we need a check digit is $7992739871$. If we denote the checksum digit by $x$, then our final number will look like $7992739871x$.

  1. From the rightmost digit, which is the check digit, moving left, double the value of every second digit (e.g. $3 \rightarrow 3 \times 2 = 6$). If any of these doubled values have more than one digit itself, then replace the doubled value with the sum of its digits (e.g., $8 \rightarrow 8 \times 2 = 16 \rightarrow 1 + 6 = 7$).
  2. Add together the digits resulting from the doubling process with the undoubled digits from the original number.
  3. Only if the total is evenly divisible by $10$ (i.e., if the total ends in zero), will the number be "valid" according to the Luhn Algorithm. Consequently, the check digit should be chosen to make the total evenly divisible by $10$
Account number 7 9 9 2 7 3 9 8 7 1 $x$
Double every other digit 7 18 9 4 7 6 9 16 7 2 $x$
Sum digits, as necessary 7 9 9 4 7 6 9 7 7 2 $x$

In the example above, the bottom row totals $67+x$ which (recalling that $x$ is a single digit) is only evenly divisible by $10$ when $x = 3$. Hence, $3$ is the check digit we seek, and our final number is $79927398713$.

Write a class named CreditCardNumberGenerator whose main method prompts the user to enter a valid IIN, an account number length between $6$ and $12$, and some number $n$, and then prints out $n$ random, but valid credit card numbers that could have come from the issuer specified by the IIN, one per line.