blob: fefb60e03379f0c3264714812657bccda3a835f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/**
* Assignment 2, COMP268 Class: CreditCard.java
*
* @description Provides an implementation of Luhn's algorithm
* @author: mo khan Student ID: 3431709
* @date Jun 9, 2019
* @version 1.0
*/
package Q3;
import java.util.*;
public class CreditCard {
private int evenSum = 0;
private int oddSum = 0;
private int sum = 0;
private String ccNumber;
private String company;
public CreditCard(String num) {
this.ccNumber = num;
this.company = this.identifyCompany(num);
this.calculateSums(num);
}
/**
* Returns the sum of the even digits
*
* @return the sum of the even digits
*/
public int getEvenSum() {
return evenSum;
}
/**
* Returns the sum of the odd digits
*
* @return the sum of the odd digits
*/
public int getOddSum() {
return oddSum;
}
/**
* Returns the sum of all the digits
*
* @return the sum of the even and odd digits.
*/
public int getSum() {
return sum;
}
/**
* Return the credit card number
*
* @return the credit card number.
*/
public String getCcNumber() {
return this.ccNumber;
}
/**
* Return the company that issues this credit card number.
*
* @return the name of the company that owns this credit card number.
*/
public String getCompany() {
return this.company;
}
/** @return true if the credit card # is valid, otherwise returns false. */
public boolean isValid() {
return this.validateCompany()
&& this.validateLength()
&& this.validateNumber()
&& this.validateSums()
&& this.isDivisibleBy10();
}
/** @return true if the total sum is divisible by 10, otherwise returns false. */
public boolean isDivisibleBy10() {
return ((this.getSum()) % 10) == 0;
}
/**
* Validates the company that the credit card number belongs to.
*
* @return true if the card belongs to one of the described credit card company, otherwise false.
*/
public boolean validateCompany() {
return this.ccNumber.startsWith("4")
|| this.ccNumber.startsWith("5")
|| this.ccNumber.startsWith("37")
|| this.ccNumber.startsWith("6");
}
/** @return true if the length of the card number is valid. */
public boolean validateLength() {
return this.ccNumber.length() >= 13 && this.ccNumber.length() <= 16;
}
/** @return true if each character in the credit card number is a digit. */
public boolean validateNumber() {
for (int i = 0; i < this.ccNumber.length(); i++)
if (!Character.isDigit(this.ccNumber.charAt(i))) return false;
return true;
}
/** @return true if the sum is greater than 0; */
public boolean validateSums() {
return this.evenSum > 0 && this.oddSum > 0;
}
private String identifyCompany(String number) {
if (validateCompany())
switch (number.charAt(0)) {
case '3':
return "American Express";
case '4':
return "Visa";
case '5':
return "MasterCard";
case '6':
return "Discover";
}
return "Unknown";
}
private void calculateSums(String number) {
if (!validateNumber()) return;
String reversed = this.reverseString(number);
for (int i = 0; i < reversed.length(); i++) {
int digit = this.digitFrom(reversed, i);
if (this.isOdd(i + 1)) this.oddSum += digit;
else {
String value = String.valueOf(digit * 2);
for (int j = 0; j < value.length(); j++) this.evenSum += this.digitFrom(value, j);
}
}
this.sum = this.oddSum + this.evenSum;
}
private String reverseString(String value) {
return new StringBuilder(value).reverse().toString();
}
private boolean isEven(int number) {
return number % 2 == 0;
}
private boolean isOdd(int number) {
return !this.isEven(number);
}
private int digitFrom(String input, int index) {
return Character.getNumericValue(input.charAt(index));
}
/**
* The entry point to the console application.
*
* @param args the argument vector provided to the console application.
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter credit card #:");
CreditCard creditCard = new CreditCard(in.next());
if (creditCard.isValid())
System.out.println(
String.format("%s is offerred by %s", creditCard.getCcNumber(), creditCard.getCompany()));
else System.out.println(String.format("%s in invalid", creditCard.getCcNumber()));
}
}
|