Skip to content

Commit

Permalink
feat(InputView): 오류 발생 지점부터 다시 입력
Browse files Browse the repository at this point in the history
  • Loading branch information
homebdy committed Sep 8, 2023
1 parent c9a0cd1 commit 762d671
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/main/java/racingcar/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class InputView {
Expand All @@ -15,16 +16,29 @@ public class InputView {
private final InputValidator inputValidator = new InputValidator();

public List<Name> readNames() {
String names = Console.readLine();
inputValidator.validateDelimiter(names);
return Arrays.stream(names.split(NAMES_REGEX))
.map(Name::new)
.collect(Collectors.toList());
return attemptedInput(() -> {
String names = Console.readLine();
inputValidator.validateDelimiter(names);
return Arrays.stream(names.split(NAMES_REGEX))
.map(Name::new)
.collect(Collectors.toList());
});
}

public int readTryCount() {
String input = Console.readLine();
inputValidator.validateIsNumber(input);
return Integer.parseInt(input);
return attemptedInput(() -> {
String input = Console.readLine();
inputValidator.validateIsNumber(input);
return Integer.parseInt(input);
});
}

private <T> T attemptedInput(Supplier<T> supplier) {
try {
return supplier.get();
} catch (IllegalArgumentException exception) {
System.out.println(exception.getMessage());
return supplier.get();
}
}
}

0 comments on commit 762d671

Please sign in to comment.