fork download
  1. import java.util.regex.Pattern;
  2.  
  3. // ------------------ Custom Exception ------------------
  4. class InvalidInputException extends Exception {
  5. public InvalidInputException(String message) {
  6. super(message);
  7. }
  8. }
  9.  
  10. // ------------------ Utility Class ------------------
  11. class StringValidator {
  12.  
  13. public static boolean isEmail(String input) throws InvalidInputException {
  14. if (input == null) throw new InvalidInputException("Email cannot be null");
  15. assert input.length() < 500 : "Email too long";
  16. String emailRegex = "^[\\w._%+-]+@[\\w.-]+\\.[A-Za-z]{2,6}$";
  17. return Pattern.matches(emailRegex, input);
  18. }
  19.  
  20. public static boolean isPhoneNumber(String input) throws InvalidInputException {
  21. if (input == null) throw new InvalidInputException("Phone number cannot be null");
  22. String phoneRegex = "^\\+?[0-9]{10,15}$";
  23. return Pattern.matches(phoneRegex, input);
  24. }
  25.  
  26. public static boolean isAlphanumeric(String input) throws InvalidInputException {
  27. if (input == null) throw new InvalidInputException("Input cannot be null");
  28. String alphaNumRegex = "^[A-Za-z0-9]+$";
  29. return Pattern.matches(alphaNumRegex, input);
  30. }
  31. }
  32.  
  33. // ------------------ Manual Test Runner ------------------
  34. public class Main {
  35. private static int passed = 0;
  36. private static int failed = 0;
  37.  
  38. private static void runTest(String testName, boolean result) {
  39. if (result) {
  40. System.out.println("[PASS] " + testName);
  41. passed++;
  42. } else {
  43. System.out.println("[FAIL] " + testName);
  44. failed++;
  45. }
  46. }
  47.  
  48. public static void main(String[] args) {
  49. try {
  50. // Email tests
  51. runTest("Valid Email", StringValidator.isEmail("test@example.com"));
  52. runTest("Invalid Email", !StringValidator.isEmail("invalid-email"));
  53.  
  54. // Phone tests
  55. runTest("Valid Phone", StringValidator.isPhoneNumber("+12345678901"));
  56. runTest("Invalid Phone", !StringValidator.isPhoneNumber("123-abc-7890"));
  57.  
  58. // Alphanumeric tests
  59. runTest("Valid Alphanumeric", StringValidator.isAlphanumeric("Test123"));
  60. runTest("Invalid Alphanumeric", !StringValidator.isAlphanumeric("Test 123"));
  61.  
  62. // Exception tests
  63. boolean exceptionThrown;
  64. exceptionThrown = false;
  65. try { StringValidator.isEmail(null); } catch (InvalidInputException e) { exceptionThrown = true; }
  66. runTest("Email Null Exception", exceptionThrown);
  67.  
  68. exceptionThrown = false;
  69. try { StringValidator.isPhoneNumber(null); } catch (InvalidInputException e) { exceptionThrown = true; }
  70. runTest("Phone Null Exception", exceptionThrown);
  71.  
  72. exceptionThrown = false;
  73. try { StringValidator.isAlphanumeric(null); } catch (InvalidInputException e) { exceptionThrown = true; }
  74. runTest("Alphanumeric Null Exception", exceptionThrown);
  75.  
  76. } catch (Exception e) {
  77. System.out.println("Unexpected Exception: " + e.getMessage());
  78. failed++;
  79. }
  80.  
  81. // Summary
  82. System.out.println("\n--- Test Summary ---");
  83. System.out.println("Passed: " + passed);
  84. System.out.println("Failed: " + failed);
  85. }
  86. }
  87.  
Success #stdin #stdout 0.17s 56148KB
stdin
Standard input is empty
stdout
[PASS] Valid Email
[PASS] Invalid Email
[PASS] Valid Phone
[PASS] Invalid Phone
[PASS] Valid Alphanumeric
[PASS] Invalid Alphanumeric
[PASS] Email Null Exception
[PASS] Phone Null Exception
[PASS] Alphanumeric Null Exception

--- Test Summary ---
Passed: 9
Failed: 0