Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit 39bac58

Browse files
committed
Add more practical methods to support simpler method call
1 parent e3672eb commit 39bac58

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/utils/FoundationClassUtilities.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package utils;
22

3+
import java.util.Arrays;
34
import java.util.Collection;
5+
import java.util.HashSet;
46

57
/**
68
* Additional classes and methods to support more features like C/C++ like style coding
@@ -27,6 +29,40 @@ public static int scanValidInteger(String prompt, FunctionPointer<Boolean, Integ
2729
}
2830
}
2931

32+
public static double scanValidDouble(String prompt, FunctionPointer<Boolean, Double> callback) {
33+
double returnValue;
34+
for(;;) {
35+
if(callback.invoke(returnValue = ScannerInput.validNextDouble(prompt))) {
36+
return returnValue;
37+
}
38+
System.out.println("Sorry, that is an invalid value, please enter again!");
39+
}
40+
}
41+
42+
public static String scanValidString(String prompt, FunctionPointer<Boolean, String> callback) {
43+
String returnValue;
44+
for(;;) {
45+
if(callback.invoke(returnValue = ScannerInput.validNextLine(prompt))) {
46+
return returnValue;
47+
}
48+
System.out.println("Sorry, that is an invalid value, please enter again!");
49+
}
50+
}
51+
52+
public static String scanValidString(String prompt, Collection<String> collection) {
53+
String returnValue;
54+
for(;;) {
55+
if(collection.contains(returnValue = ScannerInput.validNextLine(prompt).toLowerCase())) {
56+
return returnValue;
57+
}
58+
System.out.println("Sorry, that is an invalid value, please enter again!");
59+
}
60+
}
61+
62+
public static String scanValidString(String prompt, String[] array) {
63+
return scanValidString(prompt, new HashSet<>(Arrays.asList(array)));
64+
}
65+
3066
/**
3167
* A helper class that helps to count, sum, calculate, etc.
3268
* @param <E> the type of the elements that the collection contains

0 commit comments

Comments
 (0)