2017-04-04 64 views
-1

所以我创建员工工资系统和ID的要求是:如何生成员工ID?

  1. 必须始终为10个字符长。前七个是名字的前三个字母,中间的首字母(如果没有中间名,则默认为零)以及姓氏的前三个字母
  2. 最后3个字符是递增值,它表示发生的次数前7个字符(例如,AAABCCC001,AAABCCC002,XXXYZZZ001,XXX0ZZZ001等)。

我不知道如何解决这个问题。请帮助!

这是我到目前为止的代码:

count=1; 
    fnameSubstr= fname.substring(0,3).toUpperCase(); 
    mInitial= mnames.substring(0,0).toUpperCase(); 
    lnameSubstr= lname.substring(0,3).toUpperCase(); 

    nameStr=fnameSubstr + mInitial + lnameSubstr + String.valueOf(count).format("%03d", count); 

    for (Employee e: emp_list){ 
     if nameStr.equals(id){ 
      intStr=nameStr.substring(7); //string representing the first 7 chars 
      strInt=Integer.parseInt(intStr);//string of the last 3 chars 
      if count==strInt{ //compares the count to the int value of the last 3 chars 
       count++; 
       nameStr=fnameSub + mInitial + lnameSub+String.valueOf(count).format("%03d",count); 
      } 
     } 
     else{ 
      count=1; 
      nameStr=fnameSub + mInitial + lnameSub + String.valueOf(count).format("%03d", count); 
     } 


    } 

我不知道如果我在正确的轨道上。

+1

你尝试过这么远吗? – Mistalis

+1

我相信:你不会这样问这个问题。转到[帮助]了解如何/在这里问什么。提示:我们不是免费的编码服务。 – GhostCat

+0

请删除这些问题的暂停,因为问题是正确的 – Dilip

回答

1

请使用下面的代码

`

static Integer count = 0; 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    System.out.println(getEmployeeIdBy("DILIP","","DURAISWAMY")); 
    System.out.println(getEmployeeIdBy("KUTTY","","DILIP")); 
    System.out.println(getEmployeeIdBy("PANDA","R","SADASIBA")); 

} 

public static String getEmployeeIdBy(String firstName, String middleName, String lastName) { 

    String res1 = firstName.substring(0, 3); 
    String res2 = middleName.isEmpty() ? "0" : middleName.substring(0, 1); 
    String res3 = lastName.substring(0, 3); 
    String res4 = res1 + res2 + res3; 
    String res5 = count.toString().length() == 1 ? ("00" + count) 
      : count.toString().length() == 2 ? ("0" + count) : count.toString(); 
    count = count + 1; 
    String finalResult = res4 + res5; 
    return finalResult; 

}` 

最后的结果将是

DIL0DUR000 
KUT0DIL001 
PANRSAD002 
+1

谢谢。这与我的代码相似。但是,如果前7个字符相同,并且不是每个新ID,则该id只会递增。因此,您在此处将其作为“res4”,因此如果通过员工列表进行搜索并发现res4与现有ID的前7个字符匹配,则只计数递增。例如,如果某人的ID是DDDASSS001,并且您有另一个以DDDASSS开头的ID,则计数将递增到002,但是如果创建了另一个唯一ID,它将回到001,例如GGGNAAA001。这是我遇到的真正问题。 –

+0

Ru使用数据库? – Dilip

+1

不,我不是。只需编写一个Java代码。 –

1
  1. 使用substring方法获取名称中的字母。集变量

    String fName = //first three letters of the first name; 
    String mName = "0"; 
    String lName = //first three letters of the last name; 
    if (/*mName is not null*/){ 
        mName = //get the middle initial 
    } 
    
  2. 创建counter = 1算你有多少IDS做。请注意,您可以使用String.format("%03d",counter)以三位数格式化计数器;并最后分析所有变量。