0

I made this method which generates an ID number:

public String setID() {
    UUID id = UUID.randomUUID();
    String numericUUID = String.valueOf(Math.abs(Integer.parseInt(Byte.toString((byte) id.getMostSignificantBits()))));
    return numericUUID;
}

Now I want to assign each number generated to a specific object such:

employee emp1 = new employee("sarah", 2500,300);

How can I do that in the simplest way possible?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    What is the purpose of the converting between number and string back and forth, i.e. `String.valueOf(Math.abs(Integer.parseInt(Byte.toString((byte) id.getMostSignificantBits()))))`? There’s no difference to `String.valueOf(Math.abs((byte) id.getMostSignificantBits()))`. But even this is a waste of resources, creating a random UUID, just to effectively creating a random byte. – Holger Sep 06 '22 at 09:40

1 Answers1

0

Let's have a look at your code. Your method setId doesn't set an ID, so the name is misleading or you want to have a different functionality.

The code generates a type 4 UUID and gets its most significant 64 bits. Then, there's a cast to type byte, that is you effectively use only the lower 8 significant bits of the most significant 64 bits of the UUID. 8 bits are 256 different values in the range from -128 to 127. Afterwards you convert them to String, parse this String as int, calculate its absolute value and convert it back to String.

In the end, you'll get a random value between 0 and 255 (incl.), represented as String which you can get also by e.g. Integer.toString((int)(Math.random() * 256)), or rand.nextInt(256) with rand being of type java.util.Random.

Back to the naming of the method. It's unclear if this is a utility method or if it is part of your Employee class (btw: type names are written in UpperCamelCase in Java by convention, so it's class Employee instead of employee).

I'll assume you want this method to be part of the Employee class, so the question is, how can you set the ID of some employee?

public class Employee {
    private String id; // the id of the employee

    public final void setId() {
        id = Integer.toString((int)(Math.random() * 256));
    }

    // ...
}

If you now call

Employee emp1 = new Employee("sarah", 2500,300);
emp1.setId();

the id of emp1 will be set. Usually you want this to happen at the time the object is constructed, so you call this method inside the constructor. Then, you can optionally make this method private to prevent it from being called by objects of some other type (you encapsulate the functionality).

Apart from that you should think about using the UUID directly (e. g. member variable declaration private UUID id;) because a random number out of 256 is very likely to produce duplicates which is not the purpose of an ID.

Mihe
  • 2,270
  • 2
  • 4
  • 14
  • hello Mihe, thanks for your help... this method's purpose is to create a random unique ID made from a sequence of 1000, I'm going to try applying your answer now – Pristige Beetle Sep 06 '22 at 11:28
  • So, you want to get one value out of 1000 without duplicates? – Mihe Sep 06 '22 at 12:02
  • @PristigeBeetle IDs should not be random. If you are supposed to generate an ID from a sequence of 1000 values then you should be doing that - not generating pseudo-random values. Consider using persistent storage to store the next ID and have a class whose sole purpose is to get the next value. It would handle updating the next value in persistent storage as part of the process. If the IDs only have a lifespan of the program itself then you can do this with just a class that maintains that value. – vsfDawg Sep 06 '22 at 12:21
  • very well, thank you so much – Pristige Beetle Sep 06 '22 at 12:48