我正在为自己构建一个小项目,每次Employee创建a 时,他们都会获得一个 ID。
此 Id 是通过查找ArrayList的.size()生成的。
//Creating the Employee Id
int employeeID = listEmployee.size() +1;
Run Code Online (Sandbox Code Playgroud)
我知道这是一个坏主意,因为在删除员工时,Id 将开始复制。
这是我在创建员工时使用的功能
public void addHiredEmployee() {
Scanner kb = new Scanner(System.in);
System.out.println("Enter Name: ");
String nameEmployee = kb.nextLine();
//Creating Id for the employee by getting the last employee in the list
//getting their Id and adding 1 to it
Employee lastEmployee = listEmployee.get(listEmployee.size() -1);
int idCreation = lastEmployee.getEmployeeId();
int employeeID = idCreation + 1;
System.out.println("Enter password");
String employeePassword …Run Code Online (Sandbox Code Playgroud)