操纵小组中的学生

时间:2016-11-12 13:13:12

标签: java

这是一个班级学生

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;
    }

我写了一个课Groupe来操纵学生...... 但是当我编写main方法

时出现工作区错误

这是我的代码:

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());
        }
    }
}

请告诉我方法中的错误在哪里。

1 个答案:

答案 0 :(得分:0)

在ajouterEtudiant()和supprimerEtudiant()中创建一个新的Groupe实例,将其分配给g并在那里添加/删除学生。离开方法后,这个'g'将被丢弃。而是直接使用调用该方法的实例的成员etud:

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

BTW:该行

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

写得更好

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