Bonjour,

J'ai une classe User avec un identifiant et je souhaiterait à chaque fois que j'ajouter un nouvelle User, l'id augmente de 1.

Comment faire?

Classes User:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
import java.io.*;
import java.io.Serializable;
 
public class User implements Serializable 
{
	private static final long serialVersionUID = 1L;
 
	private int id;
	private String nom;
	private String prenom;
	private String mail;
	private String telephone;
 
	public User () {
	    this.id = 0;
	    this.nom = "";
	    this.prenom = "";
	    this.mail = "";
	    this.telephone = "";
	}
 
	public User (String nom, String prenom, String mail, String telephone) {
		this.id = addId();
		this.nom = nom;
		this.prenom = prenom;
		this.mail = mail;
		this.telephone = telephone;
	}
 
	public User (User user) {
		this.nom = user.getNom();
		this.prenom = user.getPrenom();
		this.mail = user.getMail();
		this.telephone = user.getTelephone();
	}
 
	public void setId(int id) {
		this.id  = id;
	}
 
	public int getId() {
		return this.id;
	}
 
	public void setNom(String nom){
		this.nom = nom;
	}
 
	public String getNom(){
		return this.nom;
	}
 
	public void setPrenom(String prenom){
		this.prenom = prenom;
	}
 
	public String getPrenom(){
		return this.prenom;
	}
 
	public void setMail(String mail){
		this.mail = mail;
	}
 
	public String getMail(){
		return this.mail;
	}
 
	public void setTelephone(String telephone){
		this.telephone = telephone;
	}
 
	public String getTelephone(){
		return telephone;
	}
 
	private int addId(){
		return id = id + 1;
	}
}
Classe Users:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Users implements Serializable 
{
	private static final long serialVersionUID = 1L;
 
	private ArrayList<User> users = new ArrayList<User>();
	private String nomListe;
	private int taille;
 
	public Users(){
		this.setNomListe("Liste des demandes");
		this.setTaille(users.size());
	}
 
	public void add(User user){
		users.add(user);
	}
 
	public User getUserByid (int id){
		for (int i = 0; i < users.size(); i++) {
 
		    if(users.get(i).getId() == id) {
		       return users.get(i);
		    }
		}
		return null;
	}
 
	public String getNomListe() {
		return nomListe;
	}
 
	public void setNomListe(String nomListe) {
		this.nomListe = nomListe;
	}
 
	public int getTaille() {
		return taille;
	}
 
	public void setTaille(int taille) {
		this.taille = taille;
	}
 
	public ArrayList<User> getUsers (){
		return users;
	}
}

Merci de votre aide