zoukankan      html  css  js  c++  java
  • 订单号生成规则,谁用谁知道

     1 class Program
     2     {
     3         //订单号数据源,实际项目中,这里是从数据库中读取过来的
     4         string[] strList = { "201512000001", "201512000002", "201512000003", "201512000004", 
     5                              "201512000008", "201512000007", "201512000006", "201512000005", 
     6                              "201512000009", "201512000010", "201512000011", "201512000012",
     7                              "201512000013","201512000014","201512000015" };        
     8         static void Main(string[] args)
     9         {
    10             Program p = new Program();
    11             Console.WriteLine(p.GetOrderNum());
    12             Console.ReadKey();
    13         }
    14 
    15         public string GetOrderNum()
    16         {
    17             string result = "";            
    18             string YearMonth = DateTime.Now.ToString("yyyyMM");
    19             var count = Count(YearMonth);//201512  5
    20             result = YearMonth + "0000001";
    21             var dd = count.ToString().Length;
    22             switch (dd)
    23             {
    24                 case 1:
    25                     result = YearMonth + "00000" + (count + 1);
    26                     break;
    27                 case 2:
    28                     result = YearMonth + "0000" + (count + 1);
    29                     break;
    30                 case 3:
    31                     result = YearMonth + "000" + (count + 1);
    32                     break;
    33                 case 4:
    34                     result = YearMonth + "00" + (count + 1);
    35                     break;
    36                 case 5:
    37                     result = YearMonth + "0" + (count + 1);
    38                     break;
    39                 default:
    40                     result = YearMonth + (count + 1);
    41                     break;
    42             }
    43             return result;
    44         }
    45         
    46         public int Count(string yearMonth) {
    47             int result = 0;
    48             result = strList.Count(item=>item.Contains(yearMonth));
    49             return result;
    50         }       
    51     }

    用string.PadLeft(n,'0')补零写出来的代码要比上面if的要好看很多,多谢园友提醒,最终代码请看下面

    public string GetOrderNum2()
    {
    string result = "";
    string YearMonth = DateTime.Now.ToString("yyyyMM");
    var count = Count(YearMonth);//201512 5 
    result= YearMonth+(count + 1).ToString().PadLeft(6,'0');
    return result;
    }
    

      

    谁有更好的生成规则,不妨一起讨论下

  • 相关阅读:
    美团面试准备
    SSM实战项目——Java高并发秒杀API
    接口和抽象类有什么区别
    Java中static、final、static final的区别
    多线程面试题
    idea新建maven项目没有src目录
    聊聊MyBatis缓存机制
    Java 8系列之重新认识HashMap
    数据库SQL实战练习
    牛客网刷题(一)
  • 原文地址:https://www.cnblogs.com/axinno1/p/5025268.html
Copyright © 2011-2022 走看看