0

我尝试编写java类,我也创建了构造函数,我尝试为类创建单元测试,并检查构造函数的有效输入,但对于构​​造函数的有效输入,我确实检查了多个字段情况以进行验证,例如检查不为空并且对于字符串和正则表达式也不为空白,因为我的类对于检查每种验证情况都是不可变的,我们应该新建一个类的对象我的问题是,如果我编写一个包访问的静态方法然后在我调用的构造函数中没有问题他们 ??

无论如何谢谢你的帮助...

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");
        
    }
}
4

0 回答 0