mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -67,8 +67,8 @@ public class Luhn {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Luhn algorithm usage examples:");
|
||||
int[] validInput = {4, 5, 6, 1, 2, 6, 1, 2, 1, 2, 3, 4, 5, 4, 6, 7};
|
||||
int[] invalidInput = {4, 5, 6, 1, 2, 6, 1, 2, 1, 2, 3, 4, 5, 4, 6, 4}; //typo in last symbol
|
||||
int[] validInput = { 4, 5, 6, 1, 2, 6, 1, 2, 1, 2, 3, 4, 5, 4, 6, 7 };
|
||||
int[] invalidInput = { 4, 5, 6, 1, 2, 6, 1, 2, 1, 2, 3, 4, 5, 4, 6, 4 }; //typo in last symbol
|
||||
checkAndPrint(validInput);
|
||||
checkAndPrint(invalidInput);
|
||||
|
||||
@ -82,13 +82,12 @@ public class Luhn {
|
||||
}
|
||||
|
||||
private static void checkAndPrint(int[] input) {
|
||||
String validationResult = Luhn.luhnCheck(input)
|
||||
? "valid"
|
||||
: "not valid";
|
||||
System.out.println("Input " + Arrays.toString(input) + " is " + validationResult);
|
||||
String validationResult = Luhn.luhnCheck(input) ? "valid" : "not valid";
|
||||
System.out.println(
|
||||
"Input " + Arrays.toString(input) + " is " + validationResult
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
========================
|
||||
Business usage example
|
||||
@ -98,7 +97,6 @@ public class Luhn {
|
||||
* Object representation of credit card.
|
||||
*/
|
||||
private record CreditCard(int[] digits) {
|
||||
|
||||
private static final int DIGITS_COUNT = 16;
|
||||
|
||||
/**
|
||||
@ -111,14 +109,21 @@ public class Luhn {
|
||||
public static CreditCard fromString(String cardNumber) {
|
||||
Objects.requireNonNull(cardNumber);
|
||||
String trimmedCardNumber = cardNumber.replaceAll(" ", "");
|
||||
if (trimmedCardNumber.length() != DIGITS_COUNT || !trimmedCardNumber.matches("\\d+")) {
|
||||
throw new IllegalArgumentException("{" + cardNumber + "} - is not a card number");
|
||||
if (
|
||||
trimmedCardNumber.length() != DIGITS_COUNT ||
|
||||
!trimmedCardNumber.matches("\\d+")
|
||||
) {
|
||||
throw new IllegalArgumentException(
|
||||
"{" + cardNumber + "} - is not a card number"
|
||||
);
|
||||
}
|
||||
|
||||
int[] cardNumbers = toIntArray(trimmedCardNumber);
|
||||
boolean isValid = luhnCheck(cardNumbers);
|
||||
if (!isValid) {
|
||||
throw new IllegalArgumentException("Credit card number {" + cardNumber + "} - have a typo");
|
||||
throw new IllegalArgumentException(
|
||||
"Credit card number {" + cardNumber + "} - have a typo"
|
||||
);
|
||||
}
|
||||
|
||||
return new CreditCard(cardNumbers);
|
||||
@ -141,23 +146,34 @@ public class Luhn {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s {%s}", CreditCard.class.getSimpleName(), number());
|
||||
return String.format(
|
||||
"%s {%s}",
|
||||
CreditCard.class.getSimpleName(),
|
||||
number()
|
||||
);
|
||||
}
|
||||
|
||||
private static int[] toIntArray(String string) {
|
||||
return string.chars()
|
||||
.map(i -> Character.digit(i, 10))
|
||||
.toArray();
|
||||
return string.chars().map(i -> Character.digit(i, 10)).toArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static void businessExample(String cardNumber) {
|
||||
try {
|
||||
System.out.println("Trying to create CreditCard object from valid card number: " + cardNumber);
|
||||
System.out.println(
|
||||
"Trying to create CreditCard object from valid card number: " +
|
||||
cardNumber
|
||||
);
|
||||
CreditCard creditCard = CreditCard.fromString(cardNumber);
|
||||
System.out.println("And business object is successfully created: " + creditCard + "\n");
|
||||
System.out.println(
|
||||
"And business object is successfully created: " +
|
||||
creditCard +
|
||||
"\n"
|
||||
);
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.out.println("And fail with exception message: " + e.getMessage() + "\n");
|
||||
System.out.println(
|
||||
"And fail with exception message: " + e.getMessage() + "\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user