Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- boolean run = false;
- do{
- System.out.println
- ("Enter Credit Card Number:");
- String asknumber = sc.next();
- int cclen = asknumber.length();
- boolean oddeven = deterOdEv(cclen);
- int[] ccnum = splitCC(asknumber, cclen);
- char[] charcc = splitSecureCC(asknumber, cclen);
- byte ctype = findType(ccnum);
- boolean valid = calcLahn(ccnum, cclen, oddeven);
- printVerdict(ctype, valid, charcc, cclen);
- run = deterRestart();
- }while(run == true);
- }
- public static boolean calcLahn(int[] ccnum, int digits, boolean oddeven){
- int[] dubccnum = new int[digits+1];
- boolean valid;
- int start;
- int ftest;
- int tadd;
- double totlnum = 0;
- if(oddeven == true){
- start = 2;
- tadd = 0;
- ftest= 1;
- }else{
- start = 1;
- tadd = 1;
- ftest= 0;
- }
- for (int i = start; i <= ccnum.length-2; i +=2){
- if (ccnum[i] <=4){
- dubccnum[i] = ccnum[i] * 2;
- }else if (ccnum[i] >4){
- int tempnu1 = ccnum[i] * 2;
- int tempnu2 = tempnu1 % 10;
- tempnu1 /= 10;
- int tempnu3 = tempnu1 % 10;
- dubccnum[i] = tempnu3;
- dubccnum[i+1] = tempnu2;
- }
- }
- for (int i = start; i <= ccnum.length-1; i++){
- System.out.print(dubccnum[i]);
- }
- for (int i = 1; i <= ccnum.length-1; i+=2){
- int tempnu = dubccnum[i + ftest] + dubccnum[i + ftest + 1] + ccnum[i + tadd];
- totlnum+=tempnu;
- System.out.println
- (totlnum);
- }
- if (totlnum % 10 == 0){
- System.out.println
- ("This is valid");
- valid = true;
- }else{
- System.out.println
- ("This is invalid");
- valid = false;
- }
- return valid;
- }
- public static int[] splitCC(String asknumber, int cclen){
- long longccnum = Long.parseLong(asknumber);
- int[] ccnum = new int[cclen + 1];
- for (int i = ccnum.length-1; i >= 1; i--){
- long digit = longccnum % 10;
- ccnum[i] = ((int)digit);
- longccnum /= 10;
- System.out.println(ccnum[i]);
- }
- System.out.println
- ("The Number Entered is: ");
- for (int i = 1; i <= ccnum.length-1; i++){
- System.out.print(ccnum[i]);
- }
- System.out.println
- ("\n");
- return ccnum;
- }
- public static char[] splitSecureCC(String asknumber, int cclen){
- char symbol = 42;
- char[] charcc = new char[cclen];
- char[] ccsec = new char[cclen];
- for(int i = 0; i < cclen; i++){
- charcc[i] = asknumber.charAt(i);
- }
- for(int i = 0; i < cclen-4; i++){
- ccsec[i] = symbol;
- }
- for(int i = cclen-4; i < cclen; i++){
- ccsec[i] = charcc[i];
- }
- return ccsec;
- }
- public static byte findType(int[] ccnum){
- int prefix = (ccnum[1]*1000) + (ccnum[2]*100) + (ccnum[3]*10) + (ccnum[4]*1);
- byte ctype;
- System.out.println
- ("Prefix is: " + prefix);
- if (prefix >= 5100 && prefix <=5500){
- System.out.println
- ("This is a MasterCard");
- ctype = 1;
- }else if (prefix >= 4000 && prefix <=4999){
- System.out.println
- ("This is a Visa");
- ctype = 2;
- }else if (prefix == 6011){
- System.out.println
- ("This is a Discover");
- ctype = 3;
- }else if (prefix >= 3400 && prefix <=3700){
- System.out.println
- ("This is an Amercan Express");
- ctype = 4;
- }else{
- System.out.println
- ("Please Enter Valid Number");
- ctype = 0;
- }
- return ctype;
- }
- public static boolean deterOdEv(int cclen){
- boolean oddeven;
- if(cclen % 2 == 0){
- oddeven = false;
- }else{
- oddeven = true;
- }
- return oddeven;
- }
- public static void printVerdict(byte ctype, boolean valid, char[] charcc, int cclen){
- System.out.println
- ("The Credit Card Number is:\n");
- try{
- for(int i = 0; i < cclen; i++){
- System.out.print(charcc[i]);
- }
- }finally{
- System.out.println("");
- }
- switch(ctype){
- case 0: System.out.println("The Card Type Does Not Match Any Stored Types"); break;
- case 1: System.out.println("This is a MasterCard."); break;
- case 2: System.out.println("This is a Visa."); break;
- case 3: System.out.println("This is a Discover."); break;
- case 4: System.out.println("This is an American Express."); break;
- }
- if(valid == true){
- System.out.println("This Card is Valid");
- }else{
- System.out.println("This Card is NOT Valid");
- }
- }
- public static boolean deterRestart(){
- boolean restart;
- Scanner sc = new Scanner(System.in);
- System.out.println("Would you like to restart?");
- String askrestart = sc.next();
- if(askrestart.equalsIgnoreCase("Yes") || askrestart.equalsIgnoreCase("Y")){
- restart = true;
- }else{
- restart = false;
- }
- return restart;
- }
- }
Add Comment
Please, Sign In to add comment