Android Credit Card Check FormatAmericanExpress( String UnformattedAccountNumber, String Seperator)

Here you can find the source of FormatAmericanExpress( String UnformattedAccountNumber, String Seperator)

Description

Format American Express

Declaration

public static String FormatAmericanExpress(
            String UnformattedAccountNumber, String Seperator) 

Method Source Code

//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final String DEFAULT_SEPERATOR = "-";

    public static String FormatAmericanExpress(
            String UnformattedAccountNumber, String Seperator) {
        //Sample format: 3714-496353-98431 [4 - 6 - 5 for a total of 15 digits] 
        if (UnformattedAccountNumber == null) {
            return "";
        }//from   ww w. j  a v  a  2  s .c o m

        UnformattedAccountNumber = UnFormatAccountNumber(UnformattedAccountNumber);

        UnformattedAccountNumber = UnformattedAccountNumber.trim();
        if (UnformattedAccountNumber.length() == 0) {
            return "";
        }

        if (Seperator == null) {
            Seperator = DEFAULT_SEPERATOR;
        }

        String formattedAccountNumber = "";
        StringBuilder sb = new StringBuilder();
        if (UnformattedAccountNumber.length() == 15) {

            sb.append(UnformattedAccountNumber.substring(0, 4));
            sb.append(Seperator);
            sb.append(UnformattedAccountNumber.substring(4, 10));
            sb.append(Seperator);
            sb.append(UnformattedAccountNumber.substring(10, 15));
            formattedAccountNumber = sb.toString();

        } else {
            formattedAccountNumber = FormatAccountNumber(
                    UnformattedAccountNumber, 4, Seperator);
        }
        return formattedAccountNumber;
    }

    public static String UnFormatAccountNumber(String AccountNumber) {
        /*      StringBuilder sb = new StringBuilder();
              Pattern p = Pattern.compile("(\\d+)");
              Matcher m = p.matcher(AccountNumber);
              while (m.find())
              {
                 sb.append(m.group(1));
              }
              return sb.toString();*/

        String result = AccountNumber;
        if (containsNonNumericCharacters(AccountNumber)) {
            StringBuilder sb = new StringBuilder();
            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(AccountNumber);
            while (m.find()) {
                sb.append(m.group(1));
            }
            result = sb.toString();
        }
        return result;
    }

    public static String FormatAccountNumber(
            String UnformattedAccountNumber, int subGroupLength,
            String Seperator) {

        if (UnformattedAccountNumber == null) {
            return "";
        }

        UnformattedAccountNumber = UnFormatAccountNumber(UnformattedAccountNumber);

        UnformattedAccountNumber = UnformattedAccountNumber.trim();
        if (UnformattedAccountNumber.length() == 0) {
            return "";
        }

        if (Seperator == null) {
            Seperator = DEFAULT_SEPERATOR;
        }

        if (subGroupLength < 1) {
            subGroupLength = 4;
        }

        int end = 0;
        StringBuilder result = new StringBuilder();
        for (int start = 0; start < UnformattedAccountNumber.length(); start += subGroupLength) {
            end = start + subGroupLength;
            if (end > UnformattedAccountNumber.length()) {
                end = UnformattedAccountNumber.length();
            }
            result.append(UnformattedAccountNumber.substring(start, end));
            result.append(Seperator);
        }
        // remove the last Seperator
        String formattedAccountNumber = result.toString();
        formattedAccountNumber = formattedAccountNumber.substring(0,
                formattedAccountNumber.length() - Seperator.length());
        return formattedAccountNumber;

    }

    public static boolean containsNonNumericCharacters(String rawInput) {
        Pattern p = Pattern.compile("(\\D+)");
        Matcher m = p.matcher(rawInput);
        return m.find();
    }
}

Related

  1. IsJCBCreditCard(String UnformattedAccountNumber)
  2. IsMasterCardCreditCard( String UnformattedAccountNumber)
  3. IsVISACreditCard(String UnformattedAccountNumber)
  4. FindCreditCard(String AccountNumber)
  5. FormatAccountNumber( String UnformattedAccountNumber, int subGroupLength, String Seperator)
  6. FormatDinersClub(String UnformattedAccountNumber, String Seperator)