简体   繁体   English

为我的对象创建唯一的ID

[英]Create unique IDs to my objects

I have basic knowledge of java and I want to create unique ids for my employees , but I also don't want to use java.util.UUID . 我有Java的基本知识,并且想为我的员工创建唯一的ID,但是我也不想使用java.util.UUID。 How can I do that ? 我怎样才能做到这一点 ? and where and what method should I add to my code ? 我应该在什么地方以及哪种方法添加代码? Thank you 谢谢

import java.util.ArrayList;

public class Main {

    private static ArrayList<Employees> list = new ArrayList<>();

    public static void main(String[] args) {
        Employees emp =new Employees(15, "xx", 23);
        Main.add(emp);        
    }

    public static void delete(int id) {
        list.remove(get(id));
    }

    public static void updateName(int id, String name) {
        get(id).name=name;
    }

    public static void updateAge(int id, int age) {
        get(id).age=age;
    }

    public static Employees get(int id) {
        for(Employees emp : list)
            if(emp.id==id)
                return emp;
        throw new RuntimeException("Employees with id : "+id+" not found");
    }

}
class Employees {

    String name;
    int age;
    int id ;

    public Employees(int id, String name, int age) {
        //super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

   @Override
    public String toString() {

        return id+" : "+" "+name+", "+age+" ans";
    }
}

You can have a static variable, which will be used to have an id, and after assignment it will be incremented, to assure that the next one will be different : 您可以有一个静态变量,该变量将用于有一个id,在分配后,该变量将递增,以确保下一个变量是不同的:

class Employees {

    String name;
    int age;
    int id ;
    static int counter = 0;

    public Employees(String name, int age) {
        this.id = counter++;
        this.name = name;
        this.age = age;
    }
  }

So you can remove the int id in the constructor 因此,您可以在构造函数中删除int id


Also : 另外:

  • Main.add(emp) is wrong and may be replaced by list.add(emp) Main.add(emp)错误,可以由list.add(emp)代替
  • updateName and updateAge may use setters defined in Employees to follow conventions (and better separate classes, and set attribute visibility to private) updateNameupdateAge可以使用Employees定义的setters来遵循约定(以及更好的单独类,并将属性可见性设置为private)

Hey Try using Random java class. 嘿,尝试使用Random java类。

import java.util.ArrayList; 导入java.util.ArrayList; import java.util.Random; 导入java.util.Random;

public class EmpUniqueId {

    public static void main(String[] args) {

         int empid1 =generateDummySSNNumber();
        Employees emp1 = new Employees(empid1 , "xx", 23);

        int empid2 =generateDummySSNNumber();
        Employees emp2 = new Employees(empid2, "xx", 23);
         ArrayList<Employees> list = new ArrayList();
         list.add(emp1);
         list.add(emp2);
         for (Employees object: list) {
                System.out.println("Employees id is :-"+object.getId());
            }
    }

    public static int generateDummySSNNumber() {
        Random random = new Random();
        int min, max;
        min = 1;
        max = 2000;
        int num = random.nextInt(min);
        int num1 = random.nextInt(max);
        System.out.println("Random Number between given range is " + num1);


        return num1;
    }
}



class Employees {

    String name;
    int age;
    int id ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Employees(int id, String name, int age) {
        //super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

   @Override
    public String toString() {

        return id+" : "+" "+name+", "+age+" ans";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM