Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -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"
);
}
}
}