首页 新闻 会员 周边 捐助

有谁能把这段代码重构一下吗?

0
[待解决问题]

下面的条件判断语句,相比大家编写代码的时候都碰见过。

/// <summary>
        /// 根据规则设置物流号
        /// </summary>
        /// <param name="expressCode"></param>
        private void SetExpressCode(string expressCode)
        {
            string traceExpressNo = this.txtExpressNo.Text.Trim();
            if (expressCode == "CNSFEDEX" || expressCode == "HKFEDEX" || expressCode == "SHSFEDEX"
                || expressCode == "CNFEDEX" || expressCode == "CNSFEDEXZY")
            {
                if (traceExpressNo.Length > 12)
                {
                    traceExpressNo = traceExpressNo.Substring(0, 12);
                }
            }
            else if (expressCode == "UPSNR")
            {
                if (traceExpressNo.Length > 26)
                {
                    traceExpressNo = traceExpressNo.Substring(8, 26);
                }
            }
            else if (expressCode == "DHLRM")
            {
                if (traceExpressNo.Length > 22)
                {
                    traceExpressNo = traceExpressNo.Substring(8, 22);
                }
            }
            else if (expressCode == "GBEXP")
            {
                if (traceExpressNo.Length == 19)
                {
                    traceExpressNo = traceExpressNo.Substring(1, 18);
                }
            }
            else if (expressCode == "GBEXP")
            {
                if (traceExpressNo.Length == 19)
                {
                    traceExpressNo = traceExpressNo.Substring(1, 18);
                }
            }
            else if (expressCode == "DEEXP")
            {
                if (traceExpressNo.Length == 28)
                {
                    traceExpressNo = traceExpressNo.Substring(8, 14);
                }
            }
            txtExpressNo.Text = traceExpressNo;
        }
跟着阿笨一起玩.NET的主页 跟着阿笨一起玩.NET | 初学一级 | 园豆:9
提问于:2015-04-02 13:55
< >
分享
所有回答(6)
0

用switch

吴瑞祥 | 园豆:29449 (高人七级) | 2015-04-02 14:14
0
        /// <summary>
        /// 根据规则设置物流号
        /// </summary>
        /// <param name="expressCode"></param>
        private void SetExpressCode(string expressCode)
        {
            string traceExpressNo = this.txtExpressNo.Text.Trim();
            int maxLength = 0;
            int fixedLength = 0;
            int beginIndex = 0;
            int endIndex = 0;
            switch(expressCode){
                case "CNSFEDEX":
                case "HKFEDEX":
                case "SHSFEDEX":
                case "CNFEDEX":
                case "CNSFEDEXZY":
                    maxLength =  12;
                    beginIndex = 0;
                    endIndex = 12;
                    break;
                case "UPSNR":
                    maxLength = 26;
                    beginIndex = 8;
                    endIndex = 26;
                    break;
                case "DHLRM":
                    maxLength = 22;
                    beginIndex = 8;
                    endIndex = 22;
                    break;
                case "GBEXP":
                    fixedLength = 19;
                    beginIndex = 1;
                    endIndex = 18;
                    break;
                case "DEEXP":
                    fixedLength = 28;
                    beginIndex = 8;
                    endIndex = 14;
                    break;
                default:
                    //TODO
            }
            int length = traceExpressNo.Length;
            if((maxLength > 0 && length > maxLength) || (fixedLength > 0 && length == fixedLength)){
                 traceExpressNo = traceExpressNo.Substring(beginIndex, endIndex);
            }
            txtExpressNo.Text = traceExpressNo;
rubekid | 园豆:206 (菜鸟二级) | 2015-04-02 14:14

这方法不错,至少比原始的写法看着简洁多了。

0

switch 是可以的 。在此基础上 ,如果数字都是固定的话 可以用 model + 枚举  容易修改和拓展

akforsure | 园豆:248 (菜鸟二级) | 2015-04-02 14:27
0
            string traceExpressNo = this.txtExpressNo.Text.Trim();
            int beginIndex = 0;
            int length = traceExpressNo.Length;
            switch(expressCode){
                case "CNSFEDEX":
                case "HKFEDEX":
                case "SHSFEDEX":
                case "CNFEDEX":
                case "CNSFEDEXZY":
                    if (traceExpressNo.Length > 12)
                    {
                        beginIndex = 0;
                        length = 12;
                    }
                    break;
                case "UPSNR":
                    //类推
                    break;
                case "DHLRM":
                    //类推
                   break;
                case "GBEXP":
                    //类推
                    break;
                case "DEEXP":
                    //类推
                    break;
            }
            txtExpressNo.Text = traceExpressNo.Substring(beginIndex, length);
519740105 | 园豆:5810 (大侠五级) | 2015-04-02 15:08
0

这种是比较少的情况,如果你code,长度这些都几百个用switch,model+枚举都不合适

建议用状态列表,二纬表配置每种code对应其截取规则,然后写一个跟据code去取规则的方法就好了。

二纬表可以使用配置文件维护,也可以使用数据库维护

茂茂 | 园豆:2902 (老鸟四级) | 2015-04-02 15:10
0

使用委托和字典

public delegate string StringHandler();
    public class Class1
    {
        public static void SetExpressCode(string expressCode)
        {
            string traceExpressNo = "abcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopq";
            var dic = new Dictionary<string, StringHandler>();
            dic.Add("UPSNR", () => { return GetString(traceExpressNo, 26, 8, 26); });
            dic.Add("DHLRM", () => { return GetString(traceExpressNo, 22, 8, 22); });
            var str = dic[expressCode]();
        }
        static string GetString(string str, int length, int index, int length2)
        {
            if (str.Length > length)
            {
                str = str.Substring(index, length2);
            }
            return str;
        }
    }
hubro | 园豆:98 (初学一级) | 2015-04-02 16:26

string traceExpressNo = "abcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopq";

兄才,这个是什么意思啊?为什么是A-Q

@422159763: 

随便写的个字符串,为了让编译通过

支持(0) 反对(0) hubro | 园豆:98 (初学一级) | 2015-04-13 09:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册