Bonjour a tous !!
Je début de la programmation orienté objets actuellement sous Java, et rencontre un problème illogique d'affichage... J’explique mon programme:
=> Le but de ce petit programme est la gestion des étudiants : (tout ce qu'il y a de plus simple!).
- J'utilise 2 classes : Etudiant et Filiere.
- Une classe programme (main) appelé Programme.
- Au niveau de chaque classe on trouve les définitions des attributs, constructeurs, accesseurs & modificateurs, et la
méthode toString() pour l'affichage.
- La gestion des étudiants est éffectuée au niveau de la classe Filiere, on note donc l'ajout des différents méthodes de mise à
jour (dont la méthode afficherEtudiant() qui me pose problème!).
=> Voici le code des différentes classes:
- Etudiant:
* Filiere:
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
82
83
84
85
86
87
88
89 import java.time.LocalDate; public class Etudiant { // Attributs : private int idEtudiant; private String nom; private String prenom; private LocalDate dateNaissance; private double moyenne; // Constructeurs: public Etudiant() { } public Etudiant(int idEtudiant, String nom, String prenom, LocalDate dateNaissance, double moyenne) { this.idEtudiant = idEtudiant; this.nom = nom; this.prenom = prenom; this.dateNaissance = dateNaissance; this.moyenne = moyenne; } // Accesseurs & modificateurs: public int getIdEtudiant() { return idEtudiant; } public void setIdEtudiant(int idEtudiant) { this.idEtudiant = idEtudiant; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public LocalDate getDateNaissance() { return dateNaissance; } public void setDateNaissance(LocalDate dateNaissance) { this.dateNaissance = dateNaissance; } public double getMoyenne() { return moyenne; } public void setMoyenne(double moyenne) { this.moyenne = moyenne; } // Méthodes: @Override public String toString() { return "Etudiant [idEtudiant=" + idEtudiant + ", nom=" + nom + ", prenom=" + prenom + ", dateNaissance=" + dateNaissance + ", moyenne=" + moyenne + "]"; } public void afficherEd() { System.out.println(this.toString()); } }
* Programme:
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95 import java.util.Arrays; public class Filiere { // Variables: //int nbrEtudiant; // Attributs: private int codeFiliere; private String intitule; private int nbrModules; private int nbrEtudiant; public Etudiant[] etudiants; // Constructeurs: public Filiere() { this.nbrEtudiant = 0; etudiants = new Etudiant[30]; } public Filiere(int codeFiliere, String intitule, int nbrModules, int nbrEtudiant, Etudiant[] etudiants) { this.codeFiliere = codeFiliere; this.intitule = intitule; this.nbrModules = nbrModules; this.nbrEtudiant = nbrEtudiant; this.etudiants = etudiants; } // Accésseurs modificateurs: public int getCodeFiliere() { return codeFiliere; } public void setCodeFiliere(int codeFiliere) { this.codeFiliere = codeFiliere; } public String getIntitule() { return intitule; } public void setIntitule(String intitule) { this.intitule = intitule; } public int getNbrModules() { return nbrModules; } public void setNbrModules(int nbrModules) { this.nbrModules = nbrModules; } public int getNbrEtudiant() { return nbrEtudiant; } public void setNbrEtudiant(int nbrEtudiant) { this.nbrEtudiant = nbrEtudiant; } // Méthodes : @Override public String toString() { return "Filiere [codeFiliere=" + codeFiliere + ", intitule=" + intitule + ", nbrModules=" + nbrModules + ", etudiants=" + Arrays.toString(etudiants) + "]";/**/ } public void afficherFiliere() { System.out.print(this.toString()); } public void ajouterEtudiant(Etudiant ed) { this.etudiants[nbrEtudiant] = ed; this.nbrEtudiant++; } public void afficherEtudiant() { for(int i=0; i<this.nbrEtudiant; i++) { this.etudiants[i].afficherEd(); } } }
=> Au niveau de l’exécution l'ajout d'un nouveau étudiant passe visiblement bien! mais coté affichage, ça affiche le dernier
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 import java.util.Scanner; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Programme { public static void main(String[] args) { Filiere f1 = new Filiere(); Etudiant e1 = new Etudiant(); Scanner sc = new Scanner(System.in); int choix; char continu = 'o'; String strNaissance; while(continu=='o' || continu=='O') { System.out.println("\n1- Ajouter étudiant."); System.out.println("2- Afficher le étudiants."); System.out.print("\t => Votre choix: "); choix = sc.nextInt(); switch(choix) { case 1: { System.out.print("Donnez l'identifiant de l'étudiant: "); e1.setIdEtudiant(sc.nextInt()); System.out.print("Donnez le nom de l'étudiant: "); sc.nextLine(); e1.setNom(sc.nextLine()); System.out.print("Donnez le prénom de l'étudiant: "); e1.setPrenom(sc.nextLine()); System.out.print("Donnez la date de naissance de l'étudiant: "); strNaissance = sc.next(); e1.setDateNaissance(LocalDate.parse(strNaissance,DateTimeFormatter.ofPattern("dd/MM/yyyy"))); System.out.print("Donnez la moyenne de l'étudiant: "); e1.setMoyenne(sc.nextDouble()); f1.ajouterEtudiant(e1); System.out.print("\n\t=> Etudiant bien ajouté !"); break; } case 2: { f1.afficherEtudiant(); break; } default: { System.out.print("\n\t => Ce choix n'est pas prévut!"); } } System.out.print("\n - Retourner au menu ? oui(o)/non: "); sc.nextLine(); String strContinu = sc.nextLine(); continu = strContinu.charAt(0); } } }
étudiant se répétant plusieurs fois !!! y a t'il une erreur au niveau de la méthode afficherEtudiant() de Filiere. ou ai-je
négliger quelque chose en manipulant le tableau d'étudiants ?
- Voici mon problème d’exécution:
1- Ajouter étudiant.
2- Afficher le étudiants.
=> Votre choix: 1
Donnez l'identifiant de l'étudiant: 10
Donnez le nom de l'étudiant: aaa
Donnez le prénom de l'étudiant: qqq
Donnez la date de naissance de l'étudiant: 12/02/1993
Donnez la moyenne de l'étudiant: 13
=> Etudiant bien ajouté !
- Retourner au menu ? oui(o)/non: o
1- Ajouter étudiant.
2- Afficher le étudiants.
=> Votre choix: 1
Donnez l'identifiant de l'étudiant: 20
Donnez le nom de l'étudiant: xxx
Donnez le prénom de l'étudiant: fff
Donnez la date de naissance de l'étudiant: 20/04/1992
Donnez la moyenne de l'étudiant: 14
=> Etudiant bien ajouté !
- Retourner au menu ? oui(o)/non: o
1- Ajouter étudiant.
2- Afficher le étudiants.
=> Votre choix: 2
=>Etudiant :0:
- Indentifiant: 20
=>Etudiant :1:
- Indentifiant: 20
- Retourner au menu ? oui(o)/non:
Merci d'avance pour votre aide !!
Partager