How do I compare one object type parameter to another (java)

Asked

Viewed 43 times

2

I have the addCurso method that receives a parameter that is a course type object, it has information such as name and area, all within the College class, which has a vector where these courses will be adapted. I have to do the check before if, the vector position is empty, I get, but also if there is no other course within this vector with the same name. How could I do this?

public class Faculdade
{
    private String nameFac;
    private int yearCreation;
    private Cursos []catalogoCursos;
    private Cursos[]cursosNovos;
    public Faculdade(String nameFacParam, int yearCreationParam){
     this.nameFac=nameFacParam;
     this.yearCreation=yearCreationParam;
     this.catalogoCursos=new Cursos[20];
     this.cursosNovos=new Cursos[5];
    }
    public void setnameFac(String nameAc){
        this.nameFac=nameAc;
    }
    public String getnameFac(){
        return this.nameFac;
    }
    public void setyearCreation(int year){
        this.yearCreation=year;
    }
    public int getyearCreation(){
        return this.yearCreation;
    }
    public void addCurso(Cursos novoCurso){
       for(int i=0;i<catalogoCursos.length;i++){
          if(catalogoCursos[i]==null){
              catalogoCursos[i]=novoCurso;
          }
       }
    }
    public void deleteCurso(String nome){
        for(int i=0;i<catalogoCursos.length;i++){
            if(catalogoCursos[i]!=null){
                if(catalogoCursos[i].getname().equals(nome)){
                    catalogoCursos[i]=null;
                    break;
                }
                else
                System.out.print("Curso não encontrado, ou não existente.");
            }

        }
    }
    public Faculdade(int yearCreation){
        this("Facs",yearCreation);
        for(int i=0;i<catalogoCursos.length;i++){
            if(catalogoCursos[i]!=null){
                if(catalogoCursos[i].getyear()>(getyearCreation()+5)){
                    cursosNovos[i]=catalogoCursos[i];
                }
            }
        }
    }
}

The course class:

public class Cursos
{
    private String code,name;
    private int duration;
    private float cost;
    private int year;

    public Cursos(String code, String name, int duration,float cost, int year){
        this.code=code;
        this.name=name;
        this.duration=duration;
        this.cost=cost;
        this.year=year;
    }
    public Cursos(String code, String name,float cost,int year){
        this(code,name,8,cost,year);
    }
    public void setcode(String code){
        this.code=code;
    }
    public String getcode(){
        return this.code;
    }
    public void setname(String name){
        this.name=name;
    }
    public String getname(){
        return this.name;
    }
    public void setduration(int duration){
        this.duration=duration;
    }
    public int getduration(){
        return this.duration;
    }
    public void setcost(float cost){
        this.cost=cost;
    }
    public float getcost(){
        return this.cost;
    }
    public void setyear(int year){
        this.year=year;
    }
    public int getyear(){
        return this.year;
    }
    public void calculateCost(){
        float value=0,semestre=6;
        value=getcost()/(getduration()*semestre);
    }
}
  • Opa Marcela, insert in the question the basis of your code so far so that we can use to answer.

1 answer

2


You can simply add a new condition to your if to check if there are any courses with the same name, your for would look like this:

  for(int i=0;i<catalogoCursos.length;i++){
      if(catalogoCursos[i]!=null && catalogoCursos[i].getname().equals(novoCurso.getname())) {
          return; //aborta um método void sem adicionar o novo curso
      }
      else {//Se ele chegar numa posição que é nula
         catalogoCursos[i] = novoCurso;
      }
   }
  • I’m not sure if this is quite what the question asks, because you can only get it if you do not have another similar course.

  • I’ll change the code a little to see if you understand better the implementation

  • Okay, take a look there, if you have any questions you can ask!

  • Caramba obg brother gsauygs I was not understanding why you were putting != but now I understand, thank you very much!

  • You are welcome! Please click on the flower next to my answer, the green, this helps me to help you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.