exist是检查构造函数有效输入的标准方法

rsl1atfo  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(402)

我试着写java类,我也创建了构造函数,我试着为类创建单元测试,也检查构造函数的有效输入,但对于构造函数的有效输入,我确实检查了字段的多个情况以进行验证,例如,为string和regex检查not null和not blank,因为我的类对于检查每一个验证的情况,我们应该新建一个类的对象,我的问题是,如果我写一个静态方法包访问,然后在构造函数中调用它们,没有问题吗??
不管怎样谢谢你的帮助。。。

public class Address {
    private final String street;
    private final String city;
    private final String pin;//postal index number.

    public Address(String street, String city, String pin) {
        if (street.isBlank())
            throw new IllegalArgumentException("the street input cannot be blank");
        if (city.isBlank())
            throw new IllegalArgumentException("the city input cannot be blank");
        if (pin.isBlank())
            throw new IllegalArgumentException("the pin input cannot be blank");
        if (!street.matches("^\\w(?:\\w+\\-?)\\w+$"))
            throw new IllegalArgumentException("the street input must be character number and -");
        if (!city.matches("^[a-zA-Z]{2,20}$"))
            throw new IllegalArgumentException("the city name must be character and length between 2 and 20");
        if (!pin.matches("^\\d{5}\\-?\\d{5}$"))
            throw new IllegalArgumentException("the pin must be digit and can be a - between 5'th and 6'th of character");
        this.street = Objects.requireNonNull(street,"the street cannot be null");
        this.city = Objects.requireNonNull(city,"the city cannot be null");
        this.pin = Objects.requireNonNull(pin,"the pin cannot be null");
    }
}
public class Address {
    private final String street;
    private final String city;
    private final String pin;//postal index number.

    public Address(String street, String city, String pin) {
        checkValidInput(street,city,pin);
        this.street = Objects.requireNonNull(street,"the street cannot be null");
        this.city = Objects.requireNonNull(city,"the city cannot be null");
        this.pin = Objects.requireNonNull(pin,"the pin cannot be null");
    }

    static void checkValidInput(String street,String city,String pin){
        Objects.requireNonNull(street,"the street cannot be null");
        Objects.requireNonNull(city,"the city cannot be null");
        Objects.requireNonNull(pin,"the pin cannot be null");

        if (street.isBlank())
            throw new IllegalArgumentException("the street input cannot be blank");
        if (city.isBlank())
            throw new IllegalArgumentException("the city input cannot be blank");
        if (pin.isBlank())
            throw new IllegalArgumentException("the pin input cannot be blank");
        if (!street.matches("^\\w(?:\\w+\\-?)\\w+$"))
            throw new IllegalArgumentException("the street input must be character number and -");
        if (!city.matches("^[a-zA-Z]{2,20}$"))
            throw new IllegalArgumentException("the city name must be character and length between 2 and 20");
        if (!pin.matches("^\\d{5}\\-?\\d{5}$"))
            throw new IllegalArgumentException("the pin must be digit and can be a - between 5'th and 6'th of character");

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题