Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在对象的输入上放置验证检查_Java_Validation_Input - Fatal编程技术网

Java 在对象的输入上放置验证检查

Java 在对象的输入上放置验证检查,java,validation,input,Java,Validation,Input,我可能想得太多了,但我已经编写了一个程序,可以对Employee对象的实例执行以下操作: Check if the input for an employeeID follows the format DDD-L where D is a digit 0-9, - is a hyphen between digits and the letter. L is an alphanumeric A-L. 目前,我是如何编写的:我为Employee对象的实例调用一个无参数构造函数,然后要求用户

我可能想得太多了,但我已经编写了一个程序,可以对Employee对象的实例执行以下操作:

Check if the input for an employeeID follows the format DDD-L where 

D is a digit 0-9, 
- is a hyphen between digits and the letter.
L is an alphanumeric A-L.
目前,我是如何编写的:我为Employee对象的实例调用一个无参数构造函数,然后要求用户输入字符串输入

在获得他们的输入后,我调用make while循环,该循环针对Employee实例中的一个方法运行:

while(employee.isValidInput(input,keyboard) == false){
        System.out.println(employee.printError());

        input = keyboard.nextLine();
        employee.setEmployeeNumber(input);
    }
    System.out.println("Input valid.");
用于验证的公共和私有方法存在于Employee对象上,而不是我所指的单独对象中的验证


TLDR是,我的对象本身应该执行这样的验证,还是应该将其传递给某种inputValidation对象,该对象包含我写入employee对象的所有验证代码?或者这是任何一种方法都是有效的吗?如果我需要澄清这个问题,请告诉我。提前感谢。

这是一个风格问题。这可能取决于你,或者你的公司,甚至是你未来工作的公司——我以前有一些公司坚持使用一些奇怪的代码,我也用它来赚钱,哈哈


我会避免在Employee对象中出现这种情况,因为这不是对象行为的一部分

你知道,我也在考虑这个问题。这是有道理的。我会把那东西拿出来放在别的地方。