HW2 Utility.java

import java.util.Scanner;

public class Utility {
// get user input double
public static double getUserInputDouble() {
double value;
Scanner scan = new Scanner(System.in);
while(true) {
try {
value = scan.nextDouble();
break;
}
catch (Exception e) {
System.out.print(“Error! Please re-enter [double value]: “);
scan.next();
}
}
return value;
}

// get between min_value and max_value
public static int getUserInputBetween(int min, int max)
{
int value = 0;
Scanner scan = new Scanner(System.in);
do {
System.out.printf(“Please enter the value %d-%d.\n”, min, max);
try {
value = scan.nextInt();
} catch (Exception e) {
System.out.printf(“ERROR! Valid range %d-%d. Please re-enter!\n”, min, max);
scan.next();
continue;
}
} while (value < min || value > max);
return value;
}

// get user input ‘q’-key
public static boolean getUserExitKey()
{
System.out.println(“Press q-key to exit the program or enter-key to start the program”);
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
if (s.contentEquals(“q”)) {
return true;
}
else {
return false;
}
}

public static FigureType getUserFigure()
{
FigureType type = null;
Scanner scan = new Scanner(System.in);
do {
System.out.println(“Please select Figure [TRIANGLE, SQUARE, RECTANGLE, PARALLELOGRAM, RHOMBUS, TRAPEZOID]”);
try {
String inputString = scan.nextLine();
type = FigureType.nameOf(inputString);
if (type != null) return type;
} catch (Exception e) {
System.out.println(“ERROR! Please re-enter!”);
scan.next();
}
} while (type == null);
return type;
}

public static GeometryType getUserGeometry()
{
GeometryType type = null;
Scanner scan = new Scanner(System.in);
do {
System.out.println(“Please select geometry [SPHERE, CONE, CYLINDER, RECTANGULAR_PRISM, SQUARE_PYRAMID, ISOSCELES_TRIANGULAR_PRISM]:”);
try {
String inputString = scan.nextLine();
type = GeometryType.nameOf(inputString);
if (type != null) return type;
} catch (Exception e) {
System.out.println(“ERROR! Please re-enter!”);
scan.next();
}
} while (type == null);
return type;
}
}