tags:

views:

167

answers:

2

Ok. This is my HW and I am kind of lost. I appreciate if you can help me:

Credit card companies use built-in "checksums" as added security measures when creating the account numbers on credit cards. This means that there are only certain valid credit card numbers, and validity can instantly be detected by using an algorithm that may involve adding up parts of the numbers or performing other checks.

In this assignment, you will implement a security algorithm on a credit card number, and your program will indicate whether or not the card number is valid, according to the security check. Note that this algorithm is purely made-up; don't try to use it to create fake credit card numbers!

In your program, you must prompt the user to input a credit card number in the form ####-####-#### as a String, including the dashes. You can assume that the input is correctly formatted, but if you add code to check for correct formatting and terminate if there is an error, you will receive 5 points extra credit.

Your program must then check that the credit card number conforms to the following rules:

  1. The first digit must be a 4.

  2. The fourth digit must be one greater than the fifth digit

  3. The product of the first, fifth, and ninth digits must be 24

  4. The sum of all digits must be evenly divisible by 4

  5. The sum of the first four digits must be one less than the sum of the last four digits

  6. If you treat the first two digits as a two-digit number, and the seventh and eight digits as a two digit number, their sum must be 100.

+2  A: 

The bonus formatting check is a good opportunity to use regular expressions, though there are obviously some other ways to do so. The correct expression could be something like "(\d{4}-){2}\d{4}" - take a look at the Pattern class.

For the rest, the easy way is to get the input string, delete the dashes from that string (using the built-in replace method is a good option), break it up into individual pieces (toCharArray is a good built-in option), use the Integer.valueOf to convert the appropriate individual pieces into the integers you need, then perform the various comparisons. Most of them are just direct comparisons with "==", though you might have to learn something about integer division for 4.

Basically, read through the java doc about the methods for the String and Integer classes; those two alone, plus some creative thinking, have just about everything you need.

Carl
A: 

I would convert the string to Integers and store them in an array (make sure skip the dashes since they are not needed). After that everything should be pretty easy. You just need to make each of the checks and return true or false.

For the division by 4 you just use the mod (%) operator. If you get 0 then it is divisible by 4 otherwise it is not.

if( (all_digit_sum % 4) == 0 )
{
    //IS DIVISIBLE BY 4
}

Is there a specific part you are having trouble with?

Josh Curren