程序员
输入您的需求,我来帮您实现
根据您提供的代码,我已经帮您实现了自定义异常类、方法类和测试类。以下是完整的代码:
- 自定义异常类 - StudentIDException.java
public class StudentIDException extends Exception {
private static final long serialVersionUID = 1L;
public StudentIDException() {
super();
}
public StudentIDException(String message) {
super(message);
}
}
- 方法类 - StudentIDValidationUtils.java
public class StudentIDValidationUtils {
public static boolean containsLetter(String str) {
for (char c : str.toCharArray()) {
if (Character.isLetter(c)) {
return true;
}
}
return false;
}
public static void validateStudentID(String studentID) throws StudentIDException {
if (containsLetter(studentID)) {
throw new StudentIDException("学号包含字母");
}
if (studentID.length() < 10) {
throw new StudentIDException("学号长度不足十位");
}
}
}
- 测试类 - StudentIDValidationTest.java
import java.util.Scanner;
public class StudentIDValidationTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学号:");
String studentID = sc.nextLine();
try {
StudentIDValidationUtils.validateStudentID(studentID);
System.out.println("学号合法");
} catch (StudentIDException e) {
System.out.println("学号不合法:" + e.getMessage());
} finally {
sc.close();
}
}
}
您可以将以上代码复制到您的IDE中,然后运行StudentIDValidationTest
类来测试自定义异常类和方法类的功能。