为我的对象创建唯一ID

时间:2017-08-16 09:25:04

标签: java

我有java的基本知识,我想为我的员工创建唯一的ID,但我也不想使用java.util.UUID。我怎样才能做到这一点 ?我应该在哪里和什么方法添加到我的代码?谢谢

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";
    }
}

2 个答案:

答案 0 :(得分:4)

你可以有一个静态变量,它将用于拥有一个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;
    }
  }

因此,您可以删除构造函数中的int id

另外:

  • Main.add(emp)错误,可能会被list.add(emp)
  • 取代
  • updateNameupdateAge可以使用setters中定义的Employees来遵循惯例(以及更好的单独类,并将属性可见性设置为私有)

答案 1 :(得分:0)

嘿,尝试使用Random Java类。

import java.util.ArrayList;     导入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";
    }
}