Guest User

Untitled

a guest
May 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. package jav1_1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Credit {
  6. public static void main(String[] args) {
  7.  
  8. Scanner sc = new Scanner(System.in);
  9. long ccnum;
  10. do{
  11. System.out.println("Enter a number");
  12. ccnum = sc.nextLong();
  13. }
  14. while(ccnum<0);
  15. sc.close();
  16.  
  17. //"count" variable to count no. of digits in ccnumber
  18. int count=0;
  19. long num=ccnum;
  20.  
  21. //this while loop counts no.of digits in ccnumber
  22. while(num>0)
  23. {
  24. num=num/10;
  25. count++;
  26. }
  27.  
  28. //below if condn checks ccnumber contains how many digits and sees if valid or not
  29. if(count!=15 && count!=13 && count!=16)
  30. {
  31. System.out.println("Invalid credit card");
  32. }
  33. else
  34. {
  35. //below array stores the ccnum in reverse order =>1 digit each
  36. int[] number = new int[count];
  37. for(int i=0;i<count;i++)
  38. {
  39. number[i]=(int) (ccnum%10);
  40. ccnum=ccnum/10;
  41. }
  42.  
  43. //below array stores the same reverse array by eliminating 1st num and makes it '0' kind of empty
  44. int[] orignumber = new int[count];
  45. for(int i=1;i<count;i++)
  46. {
  47. orignumber[i]=number[i];
  48. }
  49.  
  50. //below process multiplies '2' to the 'number' array elements in index 1,3,5...so on and
  51. //if any of it is double digit it makes it single als0(while loop)
  52. int digit =0;
  53. for(int i=1;i<count;i+=2)
  54. {
  55. number[i]*=2;
  56. while(number[i]>9)
  57. {
  58. digit=number[i]%10;
  59. number[i]=number[i]/10;
  60. number[i]=number[i]+digit;
  61. }
  62.  
  63. }
  64. //this is optional to see the 2 arrays how they are for understanding
  65. /*for(int i=0;i<count;i++)
  66. {
  67. System.out.print(number[i]);
  68. }
  69. System.out.println();
  70. for(int i=0;i<count;i++)
  71. {
  72. System.out.print(orignumber[i]);
  73. }
  74. */
  75. //this process adds all the digits which are multiplied by 2 and total is called 'sum2nd'
  76. int sum2nd=0;
  77. for(int i=1;i<count;i+=2)
  78. {
  79. sum2nd=sum2nd+number[i];
  80. }
  81.  
  82. //this process adds 'sum2nd' and the remaining elements which 'not' multiplied by 2
  83. int checksum=sum2nd;
  84. for(int i=0;i<count;i+=2)
  85. {
  86. checksum=checksum+number[i];
  87. }
  88. //this checks if the totalsum(checksum) ends in zero or not
  89. if(checksum%10==0)
  90. {
  91. System.out.println("\nValid card");
  92.  
  93. //visa starts with 4
  94. if((orignumber[count-1]==4))
  95. {
  96. System.out.println("Visa card");
  97. }
  98. //visa starts with 4
  99. else if((orignumber[count-2]==7) && (orignumber[count-1]==4||orignumber[count-1]==3))
  100. {
  101. System.out.println("Amex card");
  102. }
  103. //Amex starts with 34,37
  104. else if((orignumber[count-1]==5) && (orignumber[count-2]==1||orignumber[count-2]==2||orignumber[count-2]==3||orignumber[count-2]==4||orignumber[count-2]==5))
  105. {
  106. System.out.println("Master card");
  107. }
  108. //master card starts with 51,52,53,54,55
  109. else
  110. {
  111. System.out.println("Invalid card");
  112. }
  113. }
  114. else
  115. {
  116. System.out.println("Invalid");
  117. }
  118. }
  119. }
  120.  
  121. }
Add Comment
Please, Sign In to add comment