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 problem, 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 detect fake credit card numbers!

To check that the credit card number is valid, verify that it conforms to the following rules:

  • The first digit must be a 4.
  • The fourth digit must be one greater than the fifth digit
  • The product of the first, fifth, and ninth digits must be 24
  • The sum of all digits must be evenly divisible by 4
  • The sum of the first four digits must be one less than the sum of the last four digits
  • 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.

Hint: valid card numbers according to this set of rules include 480760521766 and 409434602754. You should use these for testing out your program.

Your job is to create a CreditCard class that represents a card number as a String. Hence you will have an instance variable of type String to store this. In addition the CreditCard class must have a mutator method called check( ) that determines if the credit card is valid or not. This information (whether the card is valid or not) should be stored in an instance variable of type boolean. Your class should also have an accessor method called isValid( ) that returns the value of this boolean. Your class CreditCard should also have an instance variable of type int called errorCode. This variable should be initialized to 0 and changed to an integer between 1 and 6 if the CreditCard object fails one of the tests above. That is, the check( ) method should change errorCode to the number corresponding to the first test the number failed. If the number does not fail any tests then you can leave the errorCode variable at 0. Finally, you should have an accessor method called getErrorCode( ) that returns the value of the variable errorCode. You might find this method useful when you are verifying that your check( ) method works correctly.

Attached you will find a test class called CreditCardTester. Your CreditCard class must work with the test class provided without modification. You must NOT alter the test class.

To complete this problem, you will need to use various methods of the String class, described in Horstmann and in the Java online documentation for String. In particular, you should use methods for getting the character at a certain index and for converting characters to Strings. You will also need to use the Integer.parseInt method to convert a String to an int.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.