2017-04-04 63 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)以三位數格式化計數器;並最後分析所有變量。