Guest User

Untitled

a guest
Jul 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String args[]){
  4. Scanner sc = new Scanner(System.in);
  5. boolean run = false;
  6.  
  7. do{
  8. System.out.println
  9. ("Enter Credit Card Number:");
  10. String asknumber = sc.next();
  11. int cclen = asknumber.length();
  12. boolean oddeven = deterOdEv(cclen);
  13. int[] ccnum = splitCC(asknumber, cclen);
  14. char[] charcc = splitSecureCC(asknumber, cclen);
  15. byte ctype = findType(ccnum);
  16. boolean valid = calcLahn(ccnum, cclen, oddeven);
  17. printVerdict(ctype, valid, charcc, cclen);
  18. run = deterRestart();
  19. }while(run == true);
  20. }
  21.  
  22. public static boolean calcLahn(int[] ccnum, int digits, boolean oddeven){
  23. int[] dubccnum = new int[digits+1];
  24. boolean valid;
  25. int start;
  26. int ftest;
  27. int tadd;
  28. double totlnum = 0;
  29.  
  30. if(oddeven == true){
  31. start = 2;
  32. tadd = 0;
  33. ftest= 1;
  34. }else{
  35. start = 1;
  36. tadd = 1;
  37. ftest= 0;
  38. }
  39. for (int i = start; i <= ccnum.length-2; i +=2){
  40. if (ccnum[i] <=4){
  41. dubccnum[i] = ccnum[i] * 2;
  42. }else if (ccnum[i] >4){
  43. int tempnu1 = ccnum[i] * 2;
  44. int tempnu2 = tempnu1 % 10;
  45. tempnu1 /= 10;
  46. int tempnu3 = tempnu1 % 10;
  47. dubccnum[i] = tempnu3;
  48. dubccnum[i+1] = tempnu2;
  49. }
  50. }
  51. for (int i = start; i <= ccnum.length-1; i++){
  52. System.out.print(dubccnum[i]);
  53. }
  54. for (int i = 1; i <= ccnum.length-1; i+=2){
  55. int tempnu = dubccnum[i + ftest] + dubccnum[i + ftest + 1] + ccnum[i + tadd];
  56. totlnum+=tempnu;
  57. System.out.println
  58. (totlnum);
  59. }
  60. if (totlnum % 10 == 0){
  61. System.out.println
  62. ("This is valid");
  63. valid = true;
  64. }else{
  65. System.out.println
  66. ("This is invalid");
  67. valid = false;
  68. }
  69. return valid;
  70. }
  71. public static int[] splitCC(String asknumber, int cclen){
  72. long longccnum = Long.parseLong(asknumber);
  73. int[] ccnum = new int[cclen + 1];
  74. for (int i = ccnum.length-1; i >= 1; i--){
  75. long digit = longccnum % 10;
  76. ccnum[i] = ((int)digit);
  77. longccnum /= 10;
  78. System.out.println(ccnum[i]);
  79. }
  80. System.out.println
  81. ("The Number Entered is: ");
  82. for (int i = 1; i <= ccnum.length-1; i++){
  83. System.out.print(ccnum[i]);
  84. }
  85. System.out.println
  86. ("\n");
  87. return ccnum;
  88. }
  89. public static char[] splitSecureCC(String asknumber, int cclen){
  90. char symbol = 42;
  91. char[] charcc = new char[cclen];
  92. char[] ccsec = new char[cclen];
  93. for(int i = 0; i < cclen; i++){
  94. charcc[i] = asknumber.charAt(i);
  95. }
  96. for(int i = 0; i < cclen-4; i++){
  97. ccsec[i] = symbol;
  98. }
  99. for(int i = cclen-4; i < cclen; i++){
  100. ccsec[i] = charcc[i];
  101. }
  102. return ccsec;
  103. }
  104. public static byte findType(int[] ccnum){
  105. int prefix = (ccnum[1]*1000) + (ccnum[2]*100) + (ccnum[3]*10) + (ccnum[4]*1);
  106. byte ctype;
  107.  
  108. System.out.println
  109. ("Prefix is: " + prefix);
  110. if (prefix >= 5100 && prefix <=5500){
  111. System.out.println
  112. ("This is a MasterCard");
  113. ctype = 1;
  114. }else if (prefix >= 4000 && prefix <=4999){
  115. System.out.println
  116. ("This is a Visa");
  117. ctype = 2;
  118. }else if (prefix == 6011){
  119. System.out.println
  120. ("This is a Discover");
  121. ctype = 3;
  122. }else if (prefix >= 3400 && prefix <=3700){
  123. System.out.println
  124. ("This is an Amercan Express");
  125. ctype = 4;
  126. }else{
  127. System.out.println
  128. ("Please Enter Valid Number");
  129. ctype = 0;
  130. }
  131. return ctype;
  132. }
  133. public static boolean deterOdEv(int cclen){
  134. boolean oddeven;
  135. if(cclen % 2 == 0){
  136. oddeven = false;
  137. }else{
  138. oddeven = true;
  139. }
  140. return oddeven;
  141. }
  142. public static void printVerdict(byte ctype, boolean valid, char[] charcc, int cclen){
  143. System.out.println
  144. ("The Credit Card Number is:\n");
  145. try{
  146. for(int i = 0; i < cclen; i++){
  147. System.out.print(charcc[i]);
  148. }
  149. }finally{
  150. System.out.println("");
  151. }
  152. switch(ctype){
  153. case 0: System.out.println("The Card Type Does Not Match Any Stored Types"); break;
  154. case 1: System.out.println("This is a MasterCard."); break;
  155. case 2: System.out.println("This is a Visa."); break;
  156. case 3: System.out.println("This is a Discover."); break;
  157. case 4: System.out.println("This is an American Express."); break;
  158. }
  159. if(valid == true){
  160. System.out.println("This Card is Valid");
  161. }else{
  162. System.out.println("This Card is NOT Valid");
  163. }
  164. }
  165. public static boolean deterRestart(){
  166. boolean restart;
  167. Scanner sc = new Scanner(System.in);
  168. System.out.println("Would you like to restart?");
  169. String askrestart = sc.next();
  170. if(askrestart.equalsIgnoreCase("Yes") || askrestart.equalsIgnoreCase("Y")){
  171. restart = true;
  172. }else{
  173. restart = false;
  174. }
  175. return restart;
  176. }
  177. }
Add Comment
Please, Sign In to add comment