Java randomly generated code for credit card number __java

Source: Internet
Author: User
Tags generator
Import java.util.List;
Import Java.util.Stack;

Import Java.util.Vector; /** * the license below. Obviously, this isn't a Javascript credit card number * Generator.
 However, the following class is a port of a Javascript credit card * Number generator. * * @author robweber * * */public class Randomcreditcardnumbergenerator {/* Javascript credit card number GE Nerator Copyright (C) 2006-2012 Graham King * * * is free software;
     Can redistribute it and/or modify it * under the terms of the GNU general public License as published by the * Free Software Foundation;
     Either version 2 of the License, or (at your * option) any later version. * * This are distributed in the hope that it'll be useful, but * without any WARRANTY; Without even the implied warranty of * merchantability or FITNESS for A particular purpose.
     License for more details. * * You should haveReceived a copy of the GNU general public License along * and this program;
     If not, write to the free Software Foundation, Inc., * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
            * * Www.darkcoding.net/public static final string[] Visa_prefix_list = new string[] {"4539",

    "4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4"};

    public static final string[] Mastercard_prefix_list = new string[] {"51", "52", "53", "54", "55"};

    public static final string[] Amex_prefix_list = new string[] {"34", "37"};

    public static final string[] Discover_prefix_list = new string[] {"6011"};

    public static final string[] Diners_prefix_list = new string[] {"300", "301", "302", "303", "36", "38"};

    public static final string[] Enroute_prefix_list = new string[] {"2014", "2149"};

    public static final string[] Jcb_prefix_list = new string[] {"35"}; public staticFinal string[] voyager_prefix_list = new string[] {"8699"};
        static string Strrev (String str) {if (str = null) return "";
        String revstr = "";
        for (int i = Str.length ()-1; I >= 0; i--) {revstr + = Str.charat (i);
    return revstr;
     }/* ' prefix ' is the start of the CC number as a string and any number of digits. * ' Length ' is the length of the CC number to generate. typically or */static string Completed_number (string prefix, int length) {String ccnumber = Pref

        ix Generate digits while (Ccnumber.length () < (length-1)) {ccnumber + = new Double Math.floor (Ma
        Th.random ()). Intvalue ();

        }//Reverse number and convert to int String reversedccnumberstring = Strrev (Ccnumber);
        list<integer> reversedccnumberlist = new vector<integer> (); for (int i = 0; i < REVERSEDCCNUMBERSTRING.LEngth (); i++) {Reversedccnumberlist.add (new Integer (String. valueof Reversedccnumberstring.charat (
        i)));
        //calculate sum int sum = 0;

        int pos = 0;
        integer[] Reversedccnumber = reversedccnumberlist. ToArray (New Integer[reversedccnumberlist.size ());
            while (Pos < length-1) {int odd = Reversedccnumber[pos] * 2;
            if (Odd > 9) {odd = 9;

            sum + = odd;
            if (Pos!= (length-2)) {sum = + Reversedccnumber[pos + 1];
        POS + 2;  }//calculate check digit int checkdigit = new Double ((Math.floor (SUM/10) + 1) * 10
        -sum). Intvalue ();

        Ccnumber + = Checkdigit;

    return ccnumber; public static string[] Credit_card_number (string[] prefixlist, int length, int howmany) {Stac K<string> result = new stack<string> ();
                    for (int i = 0; i < Howmany i++) {int randomarrayindex = (int) Math.floor (Math.random ()
            * prefixlist.length);
            String ccnumber = Prefixlist[randomarrayindex];
        Result.push (Completed_number (ccnumber, length));
    Return Result.toarray (New String[result.size ()); public static string[] Generatemastercardnumbers (int howmany) {return Credit_card_number (mastercard_prefix
    _list, Howmany); public static String Generatemastercardnumber () {return Credit_card_number (mastercard_prefix_list, 16, 1) [
    0];

        public static Boolean IsValidCreditCardNumber (String creditcardnumber) {Boolean isValid = false;
            try {String reversednumber = new StringBuffer (creditcardnumber). Reverse (). toString ();
            int mod10count = 0; for (int i = 0; i < Reversednumber.lengTh ();
                i++) {int augend = Integer.parseint (string.valueof (reversednumber. charAt (i)));
                    if (((i + 1)% 2) = = 0) {String productstring = string.valueof (Augend * 2);
                    augend = 0; for (int j = 0; J < Productstring.length (); j + +) {augend + = Integer.parseint (string.valueof PR
                    Oductstring. CharAt (j)));
            } Mod10count + = Augend;
            } if ((mod10count%) = = 0) {IsValid = true;
    The catch (NumberFormatException e) {} return isValid;
        public static void Main (string[] args) {int howmany = 0;
        try {howmany = Integer.parseint (Args[0]); The catch (Exception e) {system.err. println ("Usage error. Need to supply a numericArgument (ex:500000) ");
        } string[] Creditcardnumbers = Generatemastercardnumbers (Howmany);
                    for (int i = 0; i < creditcardnumbers.length i++) {System.out.println (creditcardnumbers[i) + ":" + (IsValidCreditCardNumber (creditcardnumbers[i])?
        Valid ":" Invalid "));
 }}//The code fragment is from: http://www.sharejs.com/codes/java/7103

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.