-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strategy.java
42 lines (38 loc) · 1.64 KB
/
Strategy.java
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
import java.util.List;
public abstract class Strategy {
abstract List<CreditCard> readCardsFromFile(String filename) throws Exception;
abstract void writeCardsToFile(List<CreditCard> cards, String filename) throws Exception;
public String determineErrorMessage(String cardNumber) {
if (cardNumber == null || cardNumber.isEmpty()) {
return "Invalid: empty/null card number";
}
if (cardNumber.length() > 19) {
return "Invalid: more than 19 digits";
}
if (!cardNumber.matches("\\d+")) {
return "Invalid: non numeric characters";
}
// Add a condition to check for 'not a possible card number'
if (!isPossibleCardNumber(cardNumber)) {
return "Invalid: not a possible card number";
}
return "";
}
public boolean isPossibleCardNumber(String cardNumber) {
// Example logic: Check if the card number starts with certain digits
// and has a valid length. Adjust this logic according to your specific rules.
if (cardNumber.startsWith("4") && (cardNumber.length() == 13 || cardNumber.length() == 16)) {
return true; // Visa
}
if (cardNumber.startsWith("5") && cardNumber.length() == 16) {
return true; // MasterCard
}
if ((cardNumber.startsWith("34") || cardNumber.startsWith("37")) && cardNumber.length() == 15) {
return true; // American Express
}
if (cardNumber.startsWith("6011") && cardNumber.length() == 16) {
return true; // Discover
}
return false; // Not a plausible card number
}
}