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

I need help with creating the ColleceWage; CollectPhone; and CollectSssn methods

ID: 3545227 • Letter: I

Question

I need help with creating the ColleceWage; CollectPhone; and CollectSssn methods below:   would appreciate it.   Pls keep it basic and simple.


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



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

public class CollectInfo {

    public static void main(String[] args) {
        int id = Validate.collectInt("Please enter the ID of the Item");
        String fname = Validate.collectString(3, "Please enter the first name");
        String lname = Validate.collectString(5, "Please enter the last name");
        String email = Validate.collectEmail("Please enter your email address");
        String phone = Validate.collectPhone("Please enter your 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 your SSN");
        double wage = Validate.collectWage("Please enter your hourly wage");


        System.out.print(String.format("id: %s Name: %s %s Email: %s "
                + "Phone: %s ZipCode: %s SSN: %s Wage:%s ",
                id,fname,lname,email,phone,zipcode,ssn,wage));
    } //  end main method
}  //end class CollectInfo

class Validate {

    public static int collectInt(String messageIn) {
        Scanner input = new Scanner(System.in);
        int intOut = 0;
        boolean valid = true;

        System.out.println(messageIn);
        while (valid) {
            try {
                intOut = input.nextInt();
                valid = false;
            } catch (InputMismatchException e) {
                input.nextLine();
                System.out.println("You must enter a whole number!");
            } // end catch

        }   // end while
        return intOut;


    } // end collectInt

    public static String collectString(int strLen, String messageIn) {
        Scanner input = new Scanner(System.in);
        String outStr = "";
        boolean valid = true;
        System.out.println(messageIn);
        while (valid) {
            try {
                outStr = input.nextLine();
                if (outStr.length() < strLen) {
                    throw new Exception();
                }
                valid = false;
            } catch (Exception e) {
                System.out.printf("You must have more than %s characters ", strLen);
            } // end catch
        }// end while
        return outStr;
    }

    public static String collectZip(String messageIn) {
        Scanner input = new Scanner(System.in);
        String outStr = "";
        boolean valid = true;
        System.out.println(messageIn);
        while (valid) {
            try {
                outStr = input.nextLine();
                Integer.parseInt(outStr);
                if (outStr.length() != 5) {
                    throw new Exception();
                }
                valid = false;
            } catch (NumberFormatException ne) {
                System.out.printf("Please enter a valid zip code ");
            } catch (Exception e) {
                System.out.println("A zip code must be 5 characters long");
            }
        }// end while
        return outStr;
    }

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

        Scanner input = new Scanner(System.in);
        String outStr = "";
        boolean valid = true;
        System.out.println(messageIn);
        while (valid) {
            try {
                outStr = input.nextLine();
                CharSequence emailChk = outStr;
                Matcher matcher = pattern.matcher(emailChk);
                if (!matcher.matches()) {
                    throw new Exception();
                }
                valid = false;
            } catch (Exception e) {
                System.out.printf("Please enter a valid email address ");
            } // end catch
        }// end while
        return outStr;
    }
    // create collectWage method here
    // create collectPhone method here
    // create collectSsn method here
}  // end of class Validate

Explanation / Answer

public static String collectPhone(String messageIn){

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

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


Scanner input = new Scanner(System.in);

String outStr = "";

boolean valid = true;

System.out.println(messageIn);

while (valid) {

try {

outStr = input.nextLine();

CharSequence phoneChk = outStr;

Matcher matcher = pattern.matcher(phoneChk);

if (!matcher.matches()) {

throw new Exception();

}

valid = false;

} catch (Exception e) {

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

} // end catch

}// end while

return outStr;

}

public static String collectSsn(String messageIn){

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

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


Scanner input = new Scanner(System.in);

String outStr = "";

boolean valid = true;

System.out.println(messageIn);

while (valid) {

try {

outStr = input.nextLine();

CharSequence ssnChk = outStr;

Matcher matcher = pattern.matcher(ssnChk);

if (!matcher.matches()) {

throw new Exception();

}

valid = false;

} catch (Exception e) {

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

} // end catch

}// end while

return outStr;

}

public static double collectWage(String inMessage){

Scanner input = new Scanner(System.in);

double wage = 0 ;

boolean valid = true;

System.out.println(inMessage);

while (valid) {

try {

wage = input.nextDouble();

valid = false;

} catch (Exception e) {

valid=false;

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

} // end catch

}// end while

return wage;

}


********************************OUTPUT*********************************

Please enter the ID of the Item

1

Please enter the first name

Sheshagiri Rao

Please enter the last name

Mallipedhi

Please enter your email address

sample@example.com

Please enter your phone Number

Like (123) 456-7890 or 1234567890 or 123-456-7890

123-456-7890

Please enter the zipcode

56789

Please enter your SSN

123-456-7890

Please enter your hourly wage

1000000

id: 1

Name: Sheshagiri Rao Mallipedhi

Email: sample@example.com

Phone: 123-456-7890

ZipCode: 56789

SSN: 123-456-7890

Wage:1000000.0

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