如何避免很多其他情况

时间:2018-10-23 09:50:27

标签: java xml if-statement conditional-statements netsuite

我已经阅读了很多有关代码重构和避免if else语句的主题。实际上,我有一堂课,我在使用很多if-else条件。

private static String getXSIType(String fieldType) {
    if ("_freeFormText".equals(fieldType) || "_eMailAddress".equals(fieldType) || "_help".equals(fieldType)
            || "_hyperlink".equals(fieldType) || "_inlineText".equals(fieldType) || "_longText".equals(fieldType)
            || "_password".equals(fieldType) || "_phoneNumber".equals(fieldType) || "_richText".equals(fieldType)
            || "_textArea".equals(fieldType)) {

        return "platformCore:StringCustomFieldRef";

    } else if ("_integerNumber".equals(fieldType)) {
        return "platformCore:LongCustomFieldRef";

    } else if ("_multipleSelect".equals(fieldType)) {
        return "platformCore:MultiSelectCustomFieldRef";

    } else if ("_document".equals(fieldType) || "_listRecord".equals(fieldType) || "_image".equals(fieldType)) {
        return "platformCore:SelectCustomFieldRef";

    } else if ("_currency".equals(fieldType) || "_decimalNumber".equals(fieldType)
            || "_percent".equals(fieldType)) {
        return "platformCore:DoubleCustomFieldRef";

    } else if ("_checkBox".equals(fieldType)) {
        return "platformCore:BooleanCustomFieldRef";

    } else if ("_date".equals(fieldType) || "_datetime".equals(fieldType) || "_timeOfDay".equals(fieldType)) {
        return "platformCore:DateCustomFieldRef";

    }
    return "platformCore:StringCustomFieldRef";
}

现在,我想使用其他东西,而不是其他条件,但是我不知道什么。

您能给我一个例子还是一个很好的教程页面?

谢谢

4 个答案:

答案 0 :(得分:7)

这是您需要的:

handleSubmit = event => {
        var user = this.state.user
        var password = this.state.password
        this.state.users.forEach(users => {
            if ((users.name === user) && (users.password === password)) {
                alert("Login Successful!")

             this.props.history.push({
                pathname:"/user",
                state:{
                    userId:users.id
                 }
             });
            } else {
                alert("Verify the information you have entered!")
            }
        });
    }

render() {
      return (
        <div className="Login">
            <h3>USER AREA</h3>
            <form onSubmit={this.handleSubmit}>

                <FormGroup controlId="user" bsSize="large">
                    <ControlLabel>Username</ControlLabel>
                    <FormControl
                        autoFocus
                        type="user"
                        value={this.state.user}
                        onChange={this.handleChange}
                    />
                </FormGroup>
                <FormGroup controlId="password" bsSize="large">
                    <ControlLabel>Password</ControlLabel>
                    <FormControl
                        value={this.state.password}
                        onChange={this.handleChange}
                        type="password" />
                </FormGroup>
                <Button
                    block
                    bsStyle="primary"
                    bsSize="large"
                    disabled={!this.validateForm()}
                    type="submit"
                >
                    LOGIN
                </Button>
            </form>
        </div>
    );
}

答案 1 :(得分:2)

enumswitch语句的组合用于您的示例很方便:

private static String getXSIType(String fieldType) {

    FieldType foundFieldType = FieldType.findByValue(fieldType);
    if(foundFieldType == null) {
        return CustomFieldRef.STRING.getValue();
    }
    switch(foundFieldType) {
    case FREE_FORM_TEXT:
    case EMAIL_ADRESS:
    case HELP:
    case HYPERLINK:
    case INLINE_TEXT:
    case LONG_TEXT:
    case PASSWORD:
    case PHONE_NUMBER:
    case RICH_TEXT:
    case TEXT_AREA:
        return CustomFieldRef.STRING.getValue();
    case INTEGER_NUMBER:
        return CustomFieldRef.LONG.getValue();
    case MULTIPLE_SELECT:
        return CustomFieldRef.MULTI_SELECT.getValue();
    case DOCUMENT:
    case LIST_RECORD:
    case IMAGE:
        return CustomFieldRef.SELECT.getValue();
    case CURRENCY:
    case DECIMAL_NUMBER:
    case PERCENT:
        return CustomFieldRef.DOUBLE.getValue();
    case CHECK_BOX:
        return CustomFieldRef.BOOLEAN.getValue();
    case DATE:
    case DATETIME:
    case TIME_OF_DAY:
        return CustomFieldRef.DATE.getValue();
    default:
        return CustomFieldRef.STRING.getValue();
    }
}

enum FieldType {

    FREE_FORM_TEXT("_freeFormText"),
    EMAIL_ADRESS("_eMailAddress"),
    HELP("_help"),
    HYPERLINK("_hyperlink"),
    INLINE_TEXT("_inlineText"),
    LONG_TEXT("_longText"),
    PASSWORD("_password"),
    PHONE_NUMBER("_phoneNumber"),
    RICH_TEXT("_richText"),
    TEXT_AREA("_textArea"),
    INTEGER_NUMBER("_integerNumber"),
    MULTIPLE_SELECT("_multipleSelect"),
    DOCUMENT("_document"),
    LIST_RECORD("_listRecord"),
    IMAGE("_image"),
    CURRENCY("_currency"),
    DECIMAL_NUMBER("_decimalNumber"),
    PERCENT("_percent"),
    CHECK_BOX("_checkBox"),
    DATE("_date"),
    DATETIME("_datetime"),
    TIME_OF_DAY("_timeOfDay");

    private final String value;

    FieldType(String fieldType) {
        this.value = fieldType;
    }

    public static FieldType findByValue(final String value) {
        return Arrays.stream(FieldType.values())
                .filter(o -> StringUtils.equals(o.value, value))
                .findFirst()
                .orElse(null);
    }
}

enum CustomFieldRef {

    STRING("String"),
    LONG("Long"),
    DOUBLE("Double"),
    BOOLEAN("Boolean"),
    DATE("Date"),
    SELECT("Select"),
    MULTI_SELECT("MultiSelect");

    private final String value;

    CustomFieldRef(String customFieldRef) {
        this.value = customFieldRef;
    }

    public String getValue() {
        String prefix = "platformCore";
        String suffix = "CustomFieldRef";
        return String.format("%s:%s%s", prefix, value, suffix);
    }
}

在这种情况下,不需要break,因为return停止了getXSIType()函数的执行。

switch语句中,不能使用||运算符,而可以定义 fall through 语句:

...
case PHONE_NUMBER:
case RICH_TEXT:
case TEXT_AREA:
    return CustomFieldRef.STRING.getValue();

这等效于:

if(FieldType.findByValue(fieldType).equals(FieldType.PHONE_NUMBER)
|| FieldType.findByValue(fieldType).equals(FieldType.RICH_TEXT)
|| FieldType.findByValue(fieldType).equals(FieldType.TEXT_AREA)) {
    return CustomFieldRef.STRING.getValue();
}

switch语句可以有一个可选的default大小写,它必须出现在switch语句的末尾。当上述情况都不成立时,可用于执行操作。

答案 2 :(得分:1)

您可以尝试stream()及其功能:映射和过滤。 另外,您可以尝试执行字符串的ArrayList,然后检查循环(或最好在其他函数中循环)是否等于字段类型。如果其中一个返回true,否则返回false。

现在可能会有一点帮助,那就是添加函数检查它是否等于fieldtype(它会短一点)。

答案 3 :(得分:0)

以下是使用Enum进行操作并匹配表达式的示例,这使代码更易读。

import java.io.IOException;

public class Test {
    public enum Types{
        INTEGER_NUM("_integerNumber"), MULTI_SELECT("_multipleSelect"), DOC("_document"), CURRENCY("_currency"), CHK_BOX("_checkBox"), DATE("_date"),
        DEC_NUM("_decimalNumber"), LIST_RCRD("_listRecord"), IMG("_image"), DTTM("_datetime"), TIMESTAMP("_timeOfDay");

        private String type ;
        private Types(String type) {
            this.type = type;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }   
        public static Types getEnum(String str){
            for (Types type : Types.values()) {
                if(type.getType().equalsIgnoreCase(str)){
                    return type;
                }
            }
            return null;
        }   
    }   
    private static String getXSIType(String fieldType) {

        if(fieldType.matches("_freeFormText|_eMailAddress|_help|_hyperlink|_inlineText|_longText|_password|_phoneNumber|_richText|_textArea")){
            return "platformCore:StringCustomFieldRef"; 
        }   
        switch (Types.getEnum(fieldType)) {
        case INTEGER_NUM:   return "platformCore:LongCustomFieldRef";
        case MULTI_SELECT:  return "platformCore:MultiSelectCustomFieldRef";
        case DOC:           
        case LIST_RCRD:     
        case IMG:           return "platformCore:SelectCustomFieldRef";
        case CURRENCY:     
        case DEC_NUM:       return "platformCore:DoubleCustomFieldRef";
        case CHK_BOX:       return "platformCore:BooleanCustomFieldRef";
        case DATE:          
        case DTTM:          
        case TIMESTAMP:     return "platformCore:DateCustomFieldRef";       
        default:            return "platformCore:StringCustomFieldRef";
        }
    }
    public static void main(String[] args) throws IOException { 
        System.out.println(getXSIType("_integerNumber"));
    }
}