所以我创建了一个员工薪资系统,ID要求是:
我不确定如何处理这个问题。请帮忙!
这是我到目前为止的代码:
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);
}
}
我不确定自己是否走在正确的轨道上。
答案 0 :(得分: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
}
创建counter = 1
来计算您创建的ID数量。请注意,您可以使用String.format("%03d",counter)
以三位数格式化计数器;并最后整合所有变量。
答案 1 :(得分: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