package xhs.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 生成单据号(需考虑WEB全局引用的问题)
* @since 2011-6-14
* @author xiaohuasi
*/
public class Bills {
// 格式化年月日的字符窜
private static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
private static Map<String, String> billsMap = new HashMap<String, String>();
private static Bills bills = null;
private Date date = null;
public Bills() {
}
/**
* 返回一个对象
* 供外调用非静态方法
*/
public static Bills instance() {
if (bills == null) {
bills = new Bills();
}
return bills;
}
public static Map<String, String> getBillsMap() {
return billsMap;
}
/** 获得单据号 在正常项目中此处需改成访问数据库的方法,建议在service层更改此方法*/
public String getBillsCode() {
int size = 0;
String dateStr = this.getYyyyMMdd();
Set<String> keySet = billsMap.keySet();
String keyVal = null;
for (Iterator<String> iter = keySet.iterator(); iter.hasNext();) {
keyVal = iter.next();
if (keyVal.indexOf(dateStr) == -1) {
iter.remove();
}
}
size = billsMap.size();
String billsCode = this.createBillsCode(size);
billsMap.put(billsCode, billsCode);
return billsCode;
}
/**
* 创建一个单据号
* @count:今日已经创建的单据号的数量
*/
private String createBillsCode(int count) {
int size = count + 1;
StringBuffer numbers = new StringBuffer();
//单据号前缀
numbers.append(getYyyyMMdd());
//单据号后缀
if (size >= 0 && size < 10) {
numbers.append("000");
} else if (size >= 10 && size < 100) {
numbers.append("00");
} else if (size >= 100 && size < 1000) {
numbers.append("0");
}
numbers.append(size);
return numbers.toString();
}
/**返回或创建一个日期*/
private Date getDate() {
if (date == null) {
date = new Date();
}
return date;
}
/**返回日期字符串 */
private String getYyyyMMdd() {
return yyyyMMdd.format(getDate());
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 生成单据号(需考虑WEB全局引用的问题)
* @since 2011-6-14
* @author xiaohuasi
*/
public class Bills {
// 格式化年月日的字符窜
private static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
private static Map<String, String> billsMap = new HashMap<String, String>();
private static Bills bills = null;
private Date date = null;
public Bills() {
}
/**
* 返回一个对象
* 供外调用非静态方法
*/
public static Bills instance() {
if (bills == null) {
bills = new Bills();
}
return bills;
}
public static Map<String, String> getBillsMap() {
return billsMap;
}
/** 获得单据号 在正常项目中此处需改成访问数据库的方法,建议在service层更改此方法*/
public String getBillsCode() {
int size = 0;
String dateStr = this.getYyyyMMdd();
Set<String> keySet = billsMap.keySet();
String keyVal = null;
for (Iterator<String> iter = keySet.iterator(); iter.hasNext();) {
keyVal = iter.next();
if (keyVal.indexOf(dateStr) == -1) {
iter.remove();
}
}
size = billsMap.size();
String billsCode = this.createBillsCode(size);
billsMap.put(billsCode, billsCode);
return billsCode;
}
/**
* 创建一个单据号
* @count:今日已经创建的单据号的数量
*/
private String createBillsCode(int count) {
int size = count + 1;
StringBuffer numbers = new StringBuffer();
//单据号前缀
numbers.append(getYyyyMMdd());
//单据号后缀
if (size >= 0 && size < 10) {
numbers.append("000");
} else if (size >= 10 && size < 100) {
numbers.append("00");
} else if (size >= 100 && size < 1000) {
numbers.append("0");
}
numbers.append(size);
return numbers.toString();
}
/**返回或创建一个日期*/
private Date getDate() {
if (date == null) {
date = new Date();
}
return date;
}
/**返回日期字符串 */
private String getYyyyMMdd() {
return yyyyMMdd.format(getDate());
}
}