Reputation: 568
I am building a small project for my self and every time a Employee
is created they are given an ID.
This Id is generated by finding the .size() of a ArrayList.
//Creating the Employee Id
int employeeID = listEmployee.size() +1;
I know this is a bad idea because when removing an employee the Id's will star to duplicate.
This is the function I am using when creating a Employee
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 = kb.nextLine();
System.out.println("Your log-in Id is: " + employeeID);
Employee employeeHired = new Employee(nameEmployee, employeeID,
employeePassword);
listEmployee.add(employeeHired);
kb.close();
listEmployee.toString();
}
I know about using UUID but as the Employee
Id will be used to login I cant really expect that some one enters a full UUID.
Is there a simple way of creating a random Id or even can I use the last 4 characters of a UUID just for the login and how could I implement selecting the last 4 characters?
Upvotes: 2
Views: 652
Reputation: 94
The best way to create a random Id for each Employee is to use UUID.
class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. There exist different variants of these global identifiers. The methods of this class are for manipulating the Leach-Salz variant, although the constructors allow the creation of any variant of UUID (described below). There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs. These types have a version value of 1, 2, 3 and 4, respectively.
Used to create session-id in web application. It is also used to create a transaction id. It extends Object class. It implements Serializable and Comparable interfaces.
Now let's go to implementation:-
use java.util.UUID package
UUID uniqId = UUID.randomUUID();
every time you'll use it and you'll get a random ID for your Employee.
Upvotes: 0
Reputation: 11
Usually ID is a not a visible argument. Simple way, if are you use datetime based id. Example:
long id = System.currentTimeMillis();
Upvotes: -1
Reputation: 4731
It depends on what you need the ID for. Technical IDs should in general be unique throughout the lifetime of your system. For databases, these are usually auto-increment numeric values. Rather than taking the count they store the last used id and increment it.
For login etc. you would want to use something else, such as a username. This is usually called a functional id or business id. It's still unique, but requirements are less strict. It can be reused, for example.
UUIDs are a good idea if you need to create your IDs in a highly parallel environment. While they can be generated in parallel without synchronisation, they have other drawbacks, like difficult to index, length and thus storage requirements etc.
Upvotes: 5