Any help would be greatly appreciated! Extra Credit: Create an object-oriented v
ID: 3565493 • Letter: A
Question
Any help would be greatly appreciated!
Extra Credit: Create an object-oriented validation class
Console:
Welcome to the Validation Tester application
Int Test
Enter an integer between -100 and 100: x
Error! Invalid integer value. Try again.
Enter an integer between -100 and 100: -101
Error! Number must be greater than -101
Enter an integer between -100 and 100: 101
Error! Number must be less than 101
Enter an integer between -100 and 100: 100
Double Test
Enter any number between -100 and 100: x
Error! Invalid decimal value. Try again.
Enter any number between -100 and 100: -101
Error! Number must be greater than -101.0
Enter any number between -100 and 100: 101
Error! Number must be less than 101.0
Enter any number between -100 and 100: 100
Required String Test
Enter your email address:
Error! This entry is required. Try again.
Enter your email address: joelmurach@yahoo.com
String Choice Test
Select one (x/y):
Error! This entry is required. Try again.
Select one (x/y): q
Error! Entry must be 'x' or 'y'. Try again.
Select one (x/y): x
Operation
Explanation / Answer
validator class
package pckg;
import java.util.Scanner;
public class Validator
{
public static String getString(Scanner sc, String prompt)
{
String s = null;
do
{
System.out.print(prompt);
s = sc.nextLine(); // read user entry
if (s.equals(""))
{
System.out.println("String cannot be left blank!");
}
} while (s.equals(""));
return s;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static int getInt(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static double getDouble(Scanner sc, String prompt,
double min, double max)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}
}
import myUtils.util.Console;
public class ConsoleTestApp
{
public static void main(String args[])
{
// create the Console object
OOValidator c = new OOValidator();
// display a welcome message
c.println("Welcome to the Validation Test application");
c.println();
// int
c.println("Int Test");
int i = c.getIntWithinRange("Enter an integer between -100 and 100: ", -101, 101);
c.println();
// double
c.println("Double Test");
double d = c.getDoubleWithinRange("Enter any number between -100 and 100: ", -101, 101);
c.println();
// required string
c.println("Required String Test");
String s1 = c.getRequiredString("Enter your email address: ");
c.println();
// string choice
c.println("String Choice Test");
String s2 = c.getChoiceString("Select one (x/y): ", "x", "y");
c.println();
}
}
package myUtils.util;
import java.util.Scanner;
public class OOValidator
{
Scanner sc = new Scanner(System.in);
/******************************************************/
public void println()
{
}
/******************************************************/
public void print(String s)
{
System.out.println(s);
}
/******************************************************/
public void println(String s)
{
System.out.println(s);
}
private Scanner sc;
private static final String ERROR = "Error! Invalid integer value."
+ "Try again.";
public OOValidator(Scanner sc) {
this.sc = sc;
}
public int getInt(String prompt) {
while (true) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
sc.nextLine(); // discard any other data entered on the line
break;
} else {
System.out.println(ERROR);
sc.nextLine(); // discard any other data entered on the line
}
}
return i;
}
/*****************************************************/
public int getIntWithinRange (String prompt, int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.println(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
if (i < min)
{ System.out.println
("Error! Please enter an integer greater than -100");
}
else if (i > max)
{
System.out.println ("Error! Please enter an integer less than 100");
}
else
isValid = true;
}
else
{
System.out.println ("Error! Invalid number value");
sc.nextLine();
}
}
return i;
}
/*****************************************************/
public double getDoubleWithinRange (String prompt, double min, double max)
{
int d = 0 ;
boolean isValid = false;
while (isValid == false)
{
System.out.println(prompt);
if (sc.hasNextInt())
{
d = sc.nextInt();
if (d < min)
{
System.out.println ("Error! Please enter a number greater than -100");
}
else if (d > max)
{
System.out.println ("Error! Please enter a number less than 100");
}
else
isValid = true;
}
else
{
System.out.println("Error! Invalid number value");
sc.nextLine();
}
}
return d;
}
/*****************************************************/
public String getRequiredString(String prompt)
{
String R = "";
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
R = sc.nextLine();
if (R.equals(""))
{
System.out.println("Error! This entry is required. Try again.");
}
else
{
isValid = true;
}
}
return R;
}
public String getChoiceString (String prompt, String s1, String s2)
{
String s = "";
boolean isvalid = false;
while (isvalid == false);
{
s = this.getChoiceString(prompt, s1, s2);
System.out.println(s);
s = sc.nextLine();
if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2))
{
System.out.println("Error! Entry must be '"+
s1 +"', '"+ s2 +"', or '."+ "Try again.");
}
else
{
isvalid = true;
}
}
return s;
}
public int getInt(String prompt)
{
return 0;
}
}
R = sc.nextLine();
R = sc.next();
while (isvalid == false); <---
{
....
}
public String getChoiceString(String prompt, String s1, String s2)
{
String s = "";
boolean isvalid = false;
while (true)
{
if(isvalid == true)
{
break;
}
//s = this.getChoiceString(prompt, s1, s2);
//System.out.println(s);
System.out.println("Enter your String: ");
s = sc.nextLine();
if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2))
{
System.out.println("Error! Entry must be '"+
s1 +"', '"+ s2 +"', or '."+ "Try again.");
}
else
{
isvalid = true;
}
}
return s;
}
System.out.print(prompt);
R = sc.next();
System.out.println("Email address: " + R.toString());
if (R.equals(""))
{
System.out.println("Error! This entry is required. Try again.");
}
else
{
isValid = true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.