Wednesday, August 5, 2020

Validate Credit Card Number

Create a function that takes a number as an argument and returns true if the number is a valid credit card number, false otherwise.

Credit card numbers must be between 14-19 digits in length, and pass the Luhn test, described below:

1 Remove the last digit (this is the "check digit").
2 Reverse the number.
3 Double the value of each digit in odd-numbered positions. If the doubled value has more than 1 digit, add the digits together (e.g. 8 x 2 = 16 ➞ 1 + 6 = 7).
4 Add all digits.
5 Subtract the last digit of the sum (from step 4) from 10. The result should be equal to the check digit from step 1.

---------------------------------------------------------------------------------------------------------------------------------------
Examples
---------------------------------------------------------------------------------------------------------------------------------------

validateCard(1234567890123456) ➞ false

// Step 1: check digit = 6, num = 123456789012345
// Step 2: num reversed = 543210987654321
// Step 3: digit array after selective doubling: [1, 4, 6, 2, 2, 0, 9, 8, 5, 6, 1, 4, 6, 2, 2]
// Step 4: sum = 58
// Step 5: 10 - 8 = 2 (not equal to 6) ➞ false

validateCard(1234567890123452) ➞ true

// Same as above, but check digit checks out.

-----------------------------------------------------------------------------------------------------------------------------
Tests
------------------------------------------------------------------------------------------------------------------------------

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class ChallengeTests {
  @Test
  public void test1() {
    assertEquals(false, Challenge.validateCard(79927398714L));
  }
   
    @Test
  public void test2() {
        System.out.println("Passes Luhn test, but too short.");
    assertEquals(false, Challenge.validateCard(79927398713L));
  }
   
    @Test
  public void test3() {
    assertEquals(true, Challenge.validateCard(709092739800713L));
  }
   
    @Test
  public void test4() {
    assertEquals(false, Challenge.validateCard(1234567890123456L));
  }
   
    @Test
  public void test5() {
    assertEquals(true, Challenge.validateCard(12345678901237L));
  }
   
    @Test
  public void test6() {
    assertEquals(true, Challenge.validateCard(5496683867445267L));
  }
   
    @Test
  public void test7() {
    assertEquals(false, Challenge.validateCard(4508793361140566L));
  }
   
    @Test
  public void test8() {
    assertEquals(true, Challenge.validateCard(376785877526048L));
  }
   
    @Test
  public void test9() {
    assertEquals(false, Challenge.validateCard(36717601781975L));
  }
}

--------------------------------------------------------------------------------------------------------------
Welcome to TryExperts- Truly the Market Place for Experts

Let me introduce you to Product TryExperts. It's a great platform for Experts & Users.

Free User Registration https://www.tryexperts.com/use-try-experts

Are you an  Expert..? Yes So Please Free Registration https://www.tryexperts.com/register-experts

Register with us and put your question. Our experts will reach out to you. And this SERVICE is ABSOLUTELY FREE.

So with TRYEXPERTS get an answer to all your Technical Queries without spending Money and also save you valuable TIME.

No comments:

Post a Comment

Hello World In Different Programming Languages

Print Hello Word in All Programming Language 1 Hello World In C // Simple C program to // display "Hello World" // Header file for...