C.cena C.cena - 21 days ago 14
Java Question

Manipulate a student in a group

THIS is a class Student

public class Etudiant {

private int id;
private String name;
private String birthdate;

public Etudiant(int id,String name,String birthdate){
this.id=id;
this.nom=name;
this.birthdate=birthdate;
}
public String getInfo(){
return " "+ this.id+" "+this.name+" "+this.birthdate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getbirthdate() {
return birthdate;
}
public void setbirthdate(String birthdate) {
this.birthdate=birthdate;
}


I wrote a class
Groupe
to manipulate the students ...
but a workspace error showed up when I wrote my
main
method

This is my code:

import java.util.ArrayList;
import java.util.Scanner;

public class Groupe {
ArrayList <Student> etud = new ArrayList<Student>();
private int id;
private String formation;

public Groupe(int id, String formation) {
this.id = id;
this.formation = formation;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFormation() {
return formation;
}

public void setFormation(String formation) {
this.formation = formation;
}

public void ajouterEtudiant (Student e){ // add a student
Groupe g =new Groupe(this.id, this.formation);
g.etud.add(new Student(e.getId(), e.getName(), e.getbirthdate()));
}
public void supprimerEtudiant(Student e){
Groupe g =new Groupe(id, formation);
if(!g.etud.isEmpty()){
g.etud.remove(e);
}else{
System.out.println("list empty ");
}
}
public void supprimerEtudiant(int id){ // delete a student using the id
int i=0; boolean B =true;
Groupe g = new Groupe(id, formation);
while ( (i<g.etud.size())&&(B==true) &&(!g.etud.isEmpty())){
if ((g.etud.get(i)).getId()==id){
g.etud.remove(i);
B=false;
}
i++;
}
}

public void rechercheEtudiantNom(String name){ // search for a student by `enter code here`//his name

int i=0;
Groupe g =new Groupe(this.id, this.formation);
boolean B=true;
while ((i<g.etud.size())&& (B==true)&&(!g.etud.isEmpty())){
if(g.etud.get(i).getname()==name){
B=false;
}
i++;
}
if (B) {
System.out.println("student doesnt exist !");
}else{
System.out.println("student existe !");
}
}
public void afficheTousEtudiant(){ // to show the list of students
Groupe g = new Groupe(this.id, this.formation);
for(int i = 0;i<g.etud.size()-1;i++){
System.out.println(g.etud.get(i).getName());
}
}
}


Please tell me where is the mistake in my methods!?

Answer

in ajouterEtudiant() and supprimerEtudiant() you create a new instance of Groupe, assign it to g and add/remove the student there. This 'g' is discarded after you leave the method. Instead use directly the member etud of your instance on which the method is called:

public void ajouterEtudiant (Student e){   // add a student
    etud.add(new Student(e.getId(), e.getName(), e.getbirthdate()));
}

BTW: The line

ArrayList <Student> etud =  new ArrayList<Student>();

is better written

List <Student> etud =  new ArrayList<>();
Comments