因此,我正在尝试创建一个代表给定人员的虚构许可证号的类。许可证号由以下内容构成:
- 个人名字和姓氏的首字母
- 执照签发年份
- 一个随机的任意序列号
例如,Maggie Smith 的执照颁发于 1990 年,其执照编号可能为 MS-1990-11,其中 11 是序列号。然而,马克桑德斯可能在同一年获得了他的执照,这意味着他的执照的开头也可能是 MS-1990。
所以问题就出在这里。我需要确保此人的序列号与 Maggie 的不同。所以我必须检查所有具有相同首字母和发行年份的记录,然后生成一个新的唯一序列号。到目前为止,这是我的代码:
public class LicenceNumber {
private final Name driverName;
private final Date issueDate;
private static final Map<String, LicenceNumber> LICENCENOS = new HashMap<String, LicenceNumber>();
public LicenceNumber(Name driverName, Date issueDate){
this.driverName = driverName;
this.issueDate = issueDate;
}
public static LicenceNumber getInstance(Name driverName, Date issueDate){
Calendar tempCal = Calendar.getInstance();
tempCal.setTime(issueDate);
String issueYear = String.valueOf(tempCal.get(Calendar.YEAR));
int serialNo = 1;
String k = driverName.getForename().substring(0, 1) + driverName.getSurname().substring(0, 1) + "-" + issueYear + "-" + serialNo;
if(!LICENCENOS.containsKey(k)){
LICENCENOS.put(k, new LicenceNumber(driverName,issueDate));
}
return LICENCENOS.get(k);
}
public boolean isUnique(){
return true;
}
public Name getDriverName() {
return driverName;
}
public Date getIssueDate() {
return issueDate;
}
}
以及如何实例化它的片段:
public final class DrivingLicence {
private final Name driverName;
private final Date driverDOB;
private final Date issueDate;
private final LicenceNumber licenceNo;
private final boolean isFull;
public DrivingLicence(Name driverName, Date driverDOB, Date issueDate, boolean isFull){
//TO-DO validate inputs
this.driverName = driverName;
this.driverDOB = driverDOB;
this.issueDate = issueDate;
this.licenceNo = LicenceNumber.getInstance(driverName, issueDate);
//this.licenceNo = new LicenceNumber(driverName, issueDate);//instantiate a licence number using the driverName and dateOfIssue
this.isFull = isFull;
}
}
我基于一些讨论如何使用工厂实现唯一性的讲义。我也不确定是否应该使用 getInstance 或通过创建新对象来创建 LicenceNumber。有谁知道我可以检查给定字符串的序列号的方法,例如XX-XXXX 已经存在?
最佳答案
这是一种为程序持续时间创建增量数字的方法。由于没有后备数据库,每次运行程序都会重置。
它通过使用 AtomicInteger
来确保唯一性。我使用了 ConcurrentMap
来利用线程安全以及 .putIfAbsent
方法。但是,它可以很容易地转换为使用标准 Map
。我也只是使用了一个 String
,但更好的方法是使用一个真正的域对象。这足以处理 OP 的问题和说明目的。
// a Map for holding the sequencing
private ConcurrentMap<String, AtomicInteger> _sequence =
new ConcurrentHashMap<>();
/**
* Returns a unique, incrementing sequence, formatted to
* 0 prefixed, 3 places, based upon the User's initials
* and the registration year
*/
public String getSequence(String initials, String year)
{
String key = makePrefix(initials, year);
AtomicInteger chk = new AtomicInteger(0);
AtomicInteger ai = _sequence.putIfAbsent(key, chk);
if (ai == null) {
ai = chk;
}
int val = ai.incrementAndGet();
String fmt = String.format("%03d", val);
return fmt;
}
/**
* A helper method to make the prefix, which is the
* concatintion of the initials, a "-", and a year.
*/
private String makePrefix (String initials, String year)
{
return initials + "-" + year;
}
测试示例:
public static void main(String[] args)
{
LicensePlate_37169055 lp = new LicensePlate_37169055();
System.out.println("ko, 1999: " + lp.getSequence("ko", "1999"));
System.out.println("ac, 1999: " + lp.getSequence("ac", "1999"));
System.out.println("ko, 1999: " + lp.getSequence("ko", "1999"));
System.out.println("ac, 1999: " + lp.getSequence("ac", "1999"));
System.out.println("ms, 1999: " + lp.getSequence("ms", "1999"));
System.out.println("ko, 2001: " + lp.getSequence("ko", "2001"));
}
示例输出:
ko, 1999: 001
ac, 1999: 001
ko, 1999: 002
ac, 1999: 002
ms, 1999: 001
ko, 2001: 001
要集成到 OP 的代码中,建议进行以下修改:
public static LicenceNumber getInstance(Name driverName, Date issueDate){
Calendar tempCal = Calendar.getInstance();
tempCal.setTime(issueDate);
String issueYear = String.valueOf(tempCal.get(Calendar.YEAR));
// ** get the initials; I would actually move this functionality to be
// a method on the Name class
String initials = driverName.getForename().substring(0, 1) + driverName.getSurname().substring(0, 1);
// get the unique serial number
String serial = getSequence(initials, issueYear);
// make the full licenseplate String
String k = makePrefix(initials, issueYear) + "-" + serial;
if(!LICENCENOS.containsKey(k)){
LICENCENOS.put(k, new LicenceNumber(driverName,issueDate));
}
return LICENCENOS.get(k);
关于java - 如何为给定的字符串创建唯一的序列号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169055/