life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
/// <summary>
        /// 根据时间生成流水号
        /// 流水号组成如XS200811050001
        /// XS:销售
        /// 20081105:日期
        /// 0001:20081105日的第一个订单
        /// </summary>
        /// <returns></returns>
        private string GetNumberString()
        {
            string orderNumber 
= null;

            
//取得当天的最大订单号
            DateTime now = DateTime.Now;
            string sql 
= "select max(convert(int,substring(OrderNumber,11,4))) from Orders where OrderNumber like 'XS" + now.Year.ToString("d4"+ now.Month.ToString("d2"+ now.Day.ToString("d2"+ "%'";

            DataSet ds 
= null;
            using (DatabaseOperater2 op 
= new DatabaseOperater2())
            {
                ds 
= op.ExcuteSelectByAdater(sql);
            }

            
if (ds.Tables[0].Rows[0][0] is DBNull)
            {
                
//如果今天还没有订单
                orderNumber = "XS" + now.Year.ToString("d4"+ now.Month.ToString("d2"+ now.Day.ToString("d2"+ "0001";
            }
            
else
            {
                
//如果有订单,则在最大订单号上+1
                int number = Convert.ToInt32(ds.Tables[0].Rows[0][0])+1;
                orderNumber 
= "XS" + now.Year.ToString("d4"+ now.Month.ToString("d2"+ now.Day.ToString("d2"+ number.ToString("d4");
            }
            
return orderNumber;
        }

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
   * JAVA版本的自动生成有规则的订单号(或编号)
   * 生成的格式是: 200908010001 前面几位为当前的日期,后面五位为系统自增长类型的编号
   * 原理:
   *      1.获取当前日期格式化值;
   *      2.读取文件,上次编号的值+1最为当前此次编号的值
   *      (新的一天会重新从1开始编号)
   */

public class Test01 {
   
    public static void main(String[] args) throws InterruptedException {
        SerialNumber serial = new FileEveryDaySerialNumber(5, "EveryDaySerialNumber.dat");
        while(true) {
            System.out.println(serial.getSerialNumber());
            TimeUnit.SECONDS.sleep(2);
        }
    }
}


abstract class SerialNumber {

    public synchronized String getSerialNumber() {
        return process();
    }
    protected abstract String process();
}


abstract class EveryDaySerialNumber extends SerialNumber {
   
    protected final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    protected DecimalFormat df = null;
   
    public EveryDaySerialNumber(int width) {
        if(width < 1) {
            throw new IllegalArgumentException("Parameter length must be great than 1!");
        }
        char[] chs = new char[width];
        for(int i = 0; i < width; i++) {
            chs[i] = '0';
        }
        df = new DecimalFormat(new String(chs));
    }
   
    protected String process() {
        Date date = new Date();
        int n = getOrUpdateNumber(date, 1);
        return format(date) + format(n);
    }
   
    protected String format(Date date) {
        return sdf.format(date);
    }
    protected String format(int num) {
        return df.format(num);
    }
   
    /**
     * 获得序列号,同时更新持久化存储中的序列
     * @param current 当前的日期
     * @param start   初始化的序号
     * @return 所获得新的序列号
     */
    protected abstract int getOrUpdateNumber(Date current, int start);
}


class FileEveryDaySerialNumber extends EveryDaySerialNumber {

    /**
     * 持久化存储的文件
     */   
    private File file = null;
   
    /**
     * 存储的分隔符
     */
    private final static String FIELD_SEPARATOR = ",";   

    public FileEveryDaySerialNumber(int width, String filename) {
        super(width);
        file = new File(filename);
    }

    @Override
    protected int getOrUpdateNumber(Date current, int start) {
        String date = format(current);
        int num = start;
        if(file.exists()) {
            List<String> list = FileUtil.readList(file);       
            String[] data = list.get(0).split(FIELD_SEPARATOR);
            if(date.equals(data[0])) {
                num = Integer.parseInt(data[1]);
            }
        }
        FileUtil.rewrite(file, date + FIELD_SEPARATOR + (num + 1));
        return num;
    }       
}


class FileUtil {

    public static void rewrite(File file, String data) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {       
            if(bw != null) {
               try {
                   bw.close();
               } catch(IOException e) {
                   e.printStackTrace();
               }
            }           
        }
    }
   
    public static List<String> readList(File file) {
        BufferedReader br = null;
        List<String> data = new ArrayList<String>();
        try {
            br = new BufferedReader(new FileReader(file));
            for(String str = null; (str = br.readLine()) != null; ) {
                data.add(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null) {
               try {
                   br.close();
               } catch(IOException e) {
                   e.printStackTrace();
               }
            }
        }
        return data;
    }
}

存储自动编号值的文件如下(文件名: EveryDaySerialNumber.dat)

 

原文地址:http://hi.baidu.com/itlangqun/blog/item/24a490838d4c7fb66c8119fd.html



posted on 2012-02-18 21:06 life02 阅读(1267) 评论(0)  编辑 收藏 引用 所属分类: Android开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理