user804406
user804406

Reputation: 73

how to set a value to an object without a parameter

I'm a beginner in Java programming and I want to rewrite the code below, but instead to set the id number myself, I want the id number to be set automatically in order to $1,2,3,...$ I know I have to use id as a static variable and I should increment with id++ in the constructor. The problem is I don't know how to make this automatically. I want that each student has his own id, so when I write for example robert.getId() it gives me his id, but I don't want the id to be a parameter, then my object construction would be:

Students robert = new Students("Robert Smith", 8, 3500);

So I only need to provide the name, grade and tuition with the id set automatically to robert object, how can I do that?

package schoolManagementSystem;

public class Students 
{
    //Instance Variables
    
    private int id,grade, tuition;
    private String name;
    
    
    //Constructors
    
    /**
     * constructs an object of the class Student
     * @param id id of the student
     * @param name name of the student
     * @param grade grade of the student
     * @param tuition tuition the student has to pay
     */
    public Students(int id,String name, int grade,int tuition)
    {
        this.id = id;
        this.name = name;
        this.grade = grade;
        this.tuition = tuition;
    }
public int getId()
{
    return id;
}
}
    

Upvotes: 0

Views: 707

Answers (2)

Gurpreet Singh Sran
Gurpreet Singh Sran

Reputation: 31

Use AtomicInteger and getAndIncrement() method your program will look like as follows

public class Students 
{
    //Instance Variables
    private  AtomicInteger count = new AtomicInteger();
    private int id,grade, tuition;
    private String name;
    
    
    //Constructors
    
    /**
     * constructs an object of the class Student
     * @param id id of the student
     * @param name name of the student
     * @param grade grade of the student
     * @param tuition tuition the student has to pay
     */
    public Students(int id,String name, int grade,int tuition)
    {
        this.id = id;
        this.name = name;
        this.grade = grade;
        this.tuition = tuition;
    }
public int getId()
{
    return  count.getAndIncrement();

}
}

Upvotes: 0

Stacking_Flow
Stacking_Flow

Reputation: 125

This should work. Let me know

import java.util.*;

public class Students
{
    //Instance Variables

    private int id,grade, tuition;
    private String name;

    private static Set<Integer> idSet = new HashSet<>();
    private static Map<Integer, Students> db = new HashMap<>();


    //Constructors
    /**
     * constructs an object of the class Student
     * @param name name of the student
     * @param grade grade of the student
     * @param tuition tuition the student has to pay
     */
    public Students(String name, int grade,int tuition)
    {
        this.name = name;
        this.grade = grade;
        this.tuition = tuition;
    }

    public int getId() {
        if (idSet.contains(0)) {
            int max = Collections.max(idSet);
            int newMax = max + 1;
            db.put(newMax, this);
            idSet.add(newMax);
            return newMax;
        } else {
            int max = 0;
            db.put(max, this);
            idSet.add(max);
            return max;
        }
    }
}

Upvotes: 1

Related Questions