How to implement an arraylist in one condition?

Asked

Viewed 194 times

0

I need this code to have a class Estudante, one Professor and another Disciplina. I’m trying to limit the class students Disciplina in 50 with a ArrayList and a condition, but maybe they’re in the wrong places or it’s something very simple that I can’t see.

How do I say: if the number of students is > 50 then show : "Crowded room"?

Because basically a subject discipline can have a maximum of 50 students.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {



   Estudante e1 = new Estudante();
    e1.nome = "Carlos Jr";
    e1.nmatricula = "420.666";
    e1.ano= "2017";
    e1.idade= "15";

  Professor p1 = new Professor();
   p1.nome = "Kira Yagami";
   p1.ano = "2017";
   p1.especialidade = "Tecnologia da Informação";

    Disciplina d1 = new Disciplina();
    d1.professor = p1;
    d1.estudante = e1;

  if(est>50){
            System.out.println("Sala lotada. Mais de 50 alunos!");
        }
      else{
          System.out.println("Sala com menos de 50 alunos!");
        }

}
}


class Estudante{
  String nome;
  String nmatricula;
  String ano;
  String idade;
}
class Professor{
  String nome;
  String ano;
  String especialidade;
}
class Disciplina{
  Professor professor;
  ArrayList<Estudantes> estudantes;

  public Disciplina(){
    this.estudantes = new ArrayList<Estudantes>();
  }

  public void quantidadeDeEstudantes(){
    for(int i =0;i<this.estudantes.size();i++){
      String est = this.estudantes.get(i).nome;
    }
  }
}

The code had some typos, but considering this:

import java.util.ArrayList;

    class Main {
      public static void main(String[] args) {



       Estudante e1 = new Estudante();
        e1.nome = "Justos Walita";
        e1.nmatricula = "420.666";
        e1.ano= "2017";
        e1.idade= "15";

      Professor p1 = new Professor();
       p1.nome = "Kira Yagami";
       p1.ano = "2017";
       p1.especialidade = "Tecnologia de TI";

        Disciplina d1 = new Disciplina();
        d1.professor = p1;

        d1.estudantes.add(e1);

      if(this.estudantes.size()>50){
                System.out.println("Sala lotada. Mais de 50 alunos!");
            }
          else{
              System.out.println("Sala com menos de 50 alunos!");
            }

    }
    }


    class Estudante{
      String nome;
      String nmatricula;
      String ano;
      String idade;
    }
    class Professor{
      String nome;
      String ano;
      String especialidade;
    }
    class Disciplina{
      Professor professor;
      ArrayList<Estudante> estudantes;

      public Disciplina(){
        this.estudantes = new ArrayList<Estudante>();
        }

      public void quantidadeDeEstudante(){
        for(int i =0;i<this.estudantes.size();i++){
        }
      }
    }

I know the mistake is in the part:

 if(this.estudantes.size()>50){

because this saying that the function I did n exists. How do I arrange it?

  • 2

    You want to learn the right thing or just solve the exercise anyway?

  • make a method to add students in Discipline Arraylist ... before adding just check if the size of Arraylist is 50

1 answer

0

if(this.estudantes.size()>50){

should look like this

//maximum of students is 50 if(D1.estudantes.size()<50){

this is used to call attributes and methods of the class itself and the student attribute was not created within the Main class but within the Discipline class.

Browser other questions tagged

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