Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This Exercise contains two classes: ValidateTest and Validate . The Validate cla

ID: 3809716 • Letter: T

Question

This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions in methods to test data passed to it from the ValidateTest program.

Please watch the related videos listed in Canvas and finish creating the 3 remaining methods in the class.

The regular expression you will need for a Phone number is:
"^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$"
and for an SSN:
"^\d{3}[- ]?\d{2}[- ]?\d{4}$

Partial Code Given:

import java.util.*;
import java.util.regex.*;

/**
* Created:
* Author:
* FileName:
*/

public class ValidateTest {

public static void main(String[] args) {
Validate validate = new Validate();

int id = validate.collectInt("Please enter the ID of the item:");
String firstName = validate.collectString(2, "Please enter the first "
+ "name of the individual");
String lastName = validate.collectString(3, "Please enter the last "
+ "name of the individual");
String email = validate.collectEmail("Please enter the email address");
String phone = validate.collectPhone("Please enter the phone Number "
+ "Like (123) 456-7890 or 1234567890 or 123-456-7890");
String zipcode = validate.collectZip("Please enter the zipcode");
String ssn = validate.collectSsn("Please enter the SSN");
double wage = validate.collectDouble("Please enter the hourly wage");

System.out.print(String.format("id: %s Name: %s %s Email: %s "
+ "Phone: %s ZipCode: %s SSN: %s Wage:%s ",
id, firstName, lastName, email, phone, zipcode, ssn, wage));

} // end main method

} // end class ValidateTest

//************************************************************
//** Validate Class is below this box **
//************************************************************
class Validate {

private Scanner input;

public Validate() {
input = new Scanner(System.in);
}

public int collectInt(String messageIn) {
int intOut = 0;
boolean valid = false;

System.out.println(messageIn);
do {
try {
intOut = input.nextInt();
input.nextLine();
valid = true;
} catch (InputMismatchException e) {
input.nextLine();
System.out.println("This requires a whole number!");
} // end catch
} while (!valid);
return intOut;
} // end collectInt

public String collectString(int strLen, String messageIn) {
String outStr = "";
boolean valid = false;

System.out.println(messageIn);
do {
try {
outStr = input.nextLine();
if (outStr.length() < strLen) {
throw new Exception();
}
valid = true;
} catch (Exception e) {
System.out.printf("This requires more than %d charachters ", strLen);
} // end Catch
} while (!valid);
return outStr;
}

public String collectEmail(String messageIn) {
String expression = "^[\w\-]([\.\w])+[\w]+@([\w\-]+\.)+[A-Z]{2,4}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

String outStr = "";
boolean valid = false;
System.out.println(messageIn);
do {
try {
outStr = input.nextLine();

Matcher m = pattern.matcher(outStr);
if (!m.matches()) {
throw new Exception();
}
valid = true;
} catch (Exception e) {
System.out.printf("Please enter a valid email address ");
} // end catch
} while (!valid);// end while
return outStr;
}

/* create collectPhone method here using the supplied phone regex expression
* that, like the collectEmail method, tests the pattern and returns the a
* phone number string
*/

/* create a collectZip method here. It can be done by checking for an integer
* that's at least 5 characters long or reading up on regex expressions and
* using your own regex expression to create the test pattern.
*/

/* create collectSsn method here that like the collectEmail method, testa a
* string to make sure it matches a social security number based on the
* regex expression.
*/
  
/* create collectdouble method here that, like the collectInt method, tests
* the a string to make sure it is a double value or requests the value again
* A double value would be like 15.55
*/


} // end class Validate

Explanation / Answer

Here is the executable code :

import java.util.*;

import java.util.regex.*;

/**

* Created:

* Author:

* FileName:

*/

public class ValidateTest {

public static void main(String[] args) {

Validate validate = new Validate();

int id = validate.collectInt("Please enter the ID of the item:");

String firstName = validate.collectString(2, "Please enter the first "

+ "name of the individual");

String lastName = validate.collectString(3, "Please enter the last "

+ "name of the individual");

String email = validate.collectEmail("Please enter the email address");

String phone = validate.collectPhone("Please enter the phone Number "

+ "Like (123) 456-7890 or 1234567890 or 123-456-7890");

String zipcode = validate.collectZip("Please enter the zipcode");

String ssn = validate.collectSsn("Please enter the SSN");

double wage = validate.collectDouble("Please enter the hourly wage");

System.out.print(String.format("id: %s Name: %s %s Email: %s "

+ "Phone: %s ZipCode: %s SSN: %s Wage:%s ",

id, firstName, lastName, email, phone, zipcode, ssn, wage));

} // end main method

} // end class ValidateTest

//************************************************************

//** Validate Class is below this box **

//************************************************************

class Validate {

private Scanner input;

public Validate() {

input = new Scanner(System.in);

}

public int collectInt(String messageIn) {

int intOut = 0;

boolean valid = false;

System.out.println(messageIn);

do {

try {

intOut = input.nextInt();

input.nextLine();

valid = true;

} catch (InputMismatchException e) {

input.nextLine();

System.out.println("This requires a whole number!");

} // end catch

} while (!valid);

return intOut;

} // end collectInt

public String collectString(int strLen, String messageIn) {

String outStr = "";

boolean valid = false;

System.out.println(messageIn);

do {

try {

outStr = input.nextLine();

if (outStr.length() < strLen) {

throw new Exception();

}

valid = true;

} catch (Exception e) {

System.out.printf("This requires more than %d charachters ", strLen);

} // end Catch

} while (!valid);

return outStr;

}

public String collectEmail(String messageIn) {

String expression = "^[\w\-]([\.\w])+[\w]+@([\w\-]+\.)+[A-Z]{2,4}$";

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

String outStr = "";

boolean valid = false;

System.out.println(messageIn);

do {

try {

outStr = input.nextLine();

Matcher m = pattern.matcher(outStr);

if (!m.matches()) {

throw new Exception();

}

valid = true;

} catch (Exception e) {

System.out.printf("Please enter a valid email address ");

} // end catch

} while (!valid);// end while

return outStr;

}

public String collectPhone(String number) {

String expression = "^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$";

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

String outStr = "";

boolean valid = false;

System.out.println(number);

do {

try {

outStr = input.nextLine();

Matcher m = pattern.matcher(outStr);

if (!m.matches()) {

throw new Exception();

}

valid = true;

} catch (Exception e) {

System.out.printf("Please enter a valid phone number ");

} // end catch

} while (!valid);// end while

return outStr;

}

/* create a collectZip method here. It can be done by checking for an integer

* that's at least 5 characters long or reading up on regex expressions and

* using your own regex expression to create the test pattern.

*/

public String collectZip(String zip) {

String expression = "[0-9]{5}(?:-[0-9]{4})?$";

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

String outStr = "";

boolean valid = false;

System.out.println(zip);

do {

try {

outStr = input.nextLine();

Matcher m = pattern.matcher(outStr);

if (!m.matches()) {

throw new Exception();

}

valid = true;

} catch (Exception e) {

System.out.printf("Please enter a valid zip code ");

} // end catch

} while (!valid);// end while

return outStr;

}

  

  

/* create collectSsn method here that like the collectEmail method, testa a

* string to make sure it matches a social security number based on the

* regex expression.

*/

  

public String collectSsn(String ssn) {

String expression = "^\d{3}[- ]?\d{2}[- ]?\d{4}$";

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

String outStr = "";

boolean valid = false;

System.out.println(ssn);

do {

try {

outStr = input.nextLine();

Matcher m = pattern.matcher(outStr);

if (!m.matches()) {

throw new Exception();

}

valid = true;

} catch (Exception e) {

System.out.printf("Please enter a valid Ssn ");

} // end catch

} while (!valid);// end while

return outStr;

}

  

/* create collectdouble method here that, like the collectInt method, tests

* the a string to make sure it is a double value or requests the value again

* A double value would be like 15.55

*/

  

public double collectDouble(String messageIn) {

String expression = "^\d{3}[- ]?\d{2}[- ]?\d{4}$";

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

double outStr = 0;

boolean valid = false;

System.out.println(messageIn);

do {

try {

outStr = input.nextDouble();

input.nextLine();

valid = true;

} catch (Exception e) {

System.out.printf("Please enter a valid double number ");

} // end catch

} while (!valid);// end while

return outStr;

}

} // end class Validate

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote