简体   繁体   中英

ID auto increment for extended class object java

Have a problem, my ID auto increment code is not working. ID for object is always 0. Maybe someone can help me with solving this. Tried different ways, but still not working for me. For my case I should use Employee class for ID counter. but Im not sure that Im doing correct.

Superclass

public class Person {
    public String PersonName;
    public String PersonSurname;

    public Person()
    {
        this.PersonName = "";
        this.PersonSurname = "";
    }
    public Person(String PersonName, String PersonSurname)
    {
        this.PersonName = PersonName;
        this.PersonSurname = PersonSurname;
    }
    @Override
    public String toString()
    {
        return "Person name, surname: "+ this.PersonName + " " + this.PersonSurname;
    }

    public void setPersonName(String PersonName)
    {
        this.PersonName = PersonName;
    }
    public void setPersonSurname(String PersonSurname)
    {
        this.PersonSurname = PersonSurname;
    }

    public String getPersonName()
    {
        return PersonName;
    }
    public String getPersonSurname()
    {
        return PersonSurname;
    }

class with id counter

public class Employee extends Person {
    private int EmployeeID;
    private static int IdCounter = 0;

    public Employee(){
        super("","");
        EmployeeID = 0;
        this.EmployeeID = IdCounter++;
    }

    public Employee(int EmployeeID, String PersonName, String PersonSurname){
        super(PersonName,PersonSurname);
        this.EmployeeID = IdCounter++;
    }
    @Override
    public String toString()
    {
        return "Person name, surname: " +this.EmployeeID + ". " + this.PersonName + " " + this.PersonSurname;
    }

    public void setEmployeeID(int EmployeeID)
    {
        this.EmployeeID = EmployeeID;
    }

    public int getEmployeeID()
    {
        return EmployeeID;
    }

}

my main class

public class Main {

    public static void main(String[] rez) {
        Scanner scanner = new Scanner(System.in);
        String firstDigit = "";
        String secDigit = "";
        Employee CompanyEmployee = new Employee();

        do {

            System.out.println("Please input Name: ");
            firstDigit = scanner.nextLine();

            System.out.println("Please input Surname: ");
            secDigit = scanner.nextLine();

            CompanyEmployee.setPersonName(firstDigit);
            CompanyEmployee.setPersonSurname(secDigit);

            System.out.println(CompanyEmployee);
        }
        while (!firstDigit.equals("dasd"));

Please update your code like below

public class Main {

    public static void main(String[] rez) {
        Scanner scanner = new Scanner(System.in);
        String firstDigit = "";
        String secDigit = "";
        List<Employee> listofemployee = new ArrayList<>();
       
        do {

            Employee companyEmployee = new Employee();

            System.out.println("Please input Name: ");
            firstDigit = scanner.nextLine();

            System.out.println("Please input Surname: ");
            secDigit = scanner.nextLine();

            companyEmployee.setPersonName(firstDigit);
            companyEmployee.setPersonSurname(secDigit);

            System.out.println(companyEmployee);
            listofemployee.add(companyEmployee);
        }
        while (!firstDigit.equals("dasd"));

I guess you don't need that EmployeeID variable.

public class Employee extends Person {
    private static int id = 0; //renamed your IdCounter variable to just id

    public Employee(){
        super("","");
        id++;            //will increment every time you construct an object 
    }
    
    public Employee(int EmployeeID, String PersonName, String PersonSurname){
        super(PersonName,PersonSurname);
        id++;            //will increment every time you construct an object
    }
   
  
   public getId(){
       return id;
   }


I would advice you to use randomizer for giving an id to you objects, because if you run programm again id filed becomes 0 and new objects will get same id as previous ones


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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