简体   繁体   中英

How can I modify my credit card validator program to check only for valid numbers in my last function that check the card type?

public class prog {

// Global variables to be use in the whole program
public static int numOfInvalids = 0 , visaNum=0 ; 



public static void main(String[] args) 
{
    
    Scanner userInput = new Scanner(System.in) ;
    

    System.out.println("How many credit card you want to check?");
    int numOfCredit = userInput.nextInt() ;
    
    int creditnumbers[] = new int[numOfCredit] ;
    
    for (int i = 0 ; i<numOfCredit ; i++)
    {
        System.out.println("Enter credit card number "+(i+1)+": ") ;
        String creditNumber = userInput.next() ;
        
        // checks if number is a credit number or not
        
        if (creditNumber.length()<16 || creditNumber.length()>19)            
        {
            System.out.println("Not a credit card number!") ;
            prog.numOfInvalids ++ ;  // count number of invalid card numbers
            
            System.out.println();
            continue ;
        }
        
        checkValid(creditNumber);
        checkType(creditNumber) ;
        // calls function 

    }
    System.out.println("Number of invalid: "+prog.numOfInvalids) ;
    System.out.println("Number of visa credit card: "+prog.visaNum) ;
    
    
}



private static boolean checkValid(String cardNumber)        // function to check if card number is valid or not 
{
    int cardlength = cardNumber.length();
    int evenSum = 0, oddSum = 0, sum;
    
    
    for (int i = cardlength - 1; i >= 0; i--)  //looping from the right 
    {
        int digit = Character.getNumericValue(cardNumber.charAt(i));  // Character.getNumericValue = convert char to integer
        
        if (i % 2 == 0)  // taking every 2nd digits from the right e.g 2nd..4th..6th..8th etc.
        {
            int multiplyByTwo = digit * 2;     // multiply each 2nd digits by 2
            
            if (multiplyByTwo > 9)  // Add two digits to handle cases that make two digits after doubling  e.g (6 * 2 = 12) = 1 + 2
            {
                
                String doubleDigits = String.valueOf(multiplyByTwo);  // convert double digit to String for use of charAt function
                multiplyByTwo = Character.getNumericValue(doubleDigits.charAt(0)) + Character.getNumericValue(doubleDigits.charAt(1));   //convert each character to integer and add them together
            }
            
            evenSum += multiplyByTwo;  // add product digits together
            
            } 
        
        else 
        {
            oddSum += digit;      // Add sum of digits that weren’t multiplied by 2
        }
    }
    
    sum = evenSum + oddSum;   //Adding both sums together
    
    if (sum % 10 == 0)       //if sum is multiple of 10 , it is valid 

    {
        
        System.out.println("valid card");
        System.out.println();
        return true;    
       
    } 
    
    else
    {
        System.out.println("invalid card") ; 
        prog.numOfInvalids ++ ; // count the number of invalid card numbers
        System.out.println();
        return false;
        
    }
}

private static boolean checkType(String cardNumber) // function to check card type 
{
    


    if ( (cardNumber.charAt(0)=='4') && cardNumber.length()==13 || cardNumber.length()==16 )  //checking first character and length of card number
            {
                System.out.println("VISA");
                visaNum ++ ; // counting number of visa cards 
                return true;    
            } 
            
            else
            {
           
                return false;
            }

} }

I want to check the card type for valid cards only from the first function "check Valid" into my last function "Check Type "

Is there any way I can save the valid numbers from the first function and use them in the "Check Type " function?

I want to check the card type for valid cards only from the first function "check Valid" into my last function "Check Type "

Is there any way I can save the valid numbers from the first function and use them in the "Check Type " function?

Assumptions based on the question

  1. Need to infer card type only for valid cards
  2. Function return type can not be changed

Possible approach

  1. Add a if check on the response of checkValid and if its true, then call checkType
   if (checkValid(creditNumber) {
        checkType(creditNumber);
   }

Suggestion

  1. Irrespective of this solution, please improve the formatting while coding and asking questions as it will help others to better understand the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM