Doubt with business rule between student class and class

Asked

Viewed 896 times

0

I have a question in a code, I am new in programming, if anyone can help me, I will be grateful.

  • The student class will receive the student’s name;
  • Class class will receive a collection of students and class name

The whole process is being done in the code, the only flaw is in adding a new class, when I create a new class and insert a student who is already enrolled in another class, the program does not accept, he warns that the student is already enrolled and does not change his class, but when I list the second class created, the student is there, but pointed to the first class that he was allocated.

Can anyone help me in a rule that when the student is already tied to a class, he does not add in the new collection.

It is quite clear that the problem is in the class class, there in the builder, but I do not know how to put already there to block the student if it is already tied to a class.

Below the code:

Pupil Class

import java.util.Collection;

public class Aluno {

    private String nome;
    private Turma turma;

    public Aluno(String nome) {
        this.nome = nome;
    }

    public void addTurma(Turma turma) {
        if (this.turma == null) {
            this.setTurma(turma);
        } else {

            System.out.println("Aluno " + getNome() + ", Já está matricula em uma turma");
        }
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Turma getTurma() {
        return turma;
    }

    public void setTurma(Turma turma) {
        this.turma = turma;
    }

    public String toString() {
        return "Aluno: " + nome + ", " + getTurma();
    }
}

Class Class:

import java.util.ArrayList;
import java.util.Collection;

public class Turma {

    private String nome;
    private Collection<Aluno> alunos;

    public Turma(String nome, Collection<Aluno> alunos) {
        this.nome = nome;
        this.alunos = new ArrayList<>();
        /* Verifica se o Aluno já está no array Alunos, caso não ele adiciona 
         e tambem adiciona o nome do aluno na classe turma*/
        for (Aluno a : alunos) {
            if (!this.alunos.contains(a)) {
                this.alunos.add(a);
                a.addTurma(this);
            } else {
                System.out.println("o " + a + ", já está matriculado em uma turma");
            }
        }
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Collection<Aluno> getAlunos() {
        return alunos;
    }

    public void setAlunos(Collection<Aluno> alunos) {
        this.alunos = alunos;
    }

    @Override
    public String toString() {
        return "Turma: " + nome;
    }

}

Class that tests the class:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TestaTurma {

    public static void main(String[] args) {

        Aluno aluno1 = new Aluno("Jose Almeida");
        Aluno aluno2 = new Aluno("Lucas Henrique");
        Aluno aluno3 = new Aluno("Carlos Soares");

        List lista1 = new ArrayList();
        lista1.add(aluno1);
        lista1.add(aluno2);

        Turma turma1 = new Turma("4ºA", lista1);

        List lista2 = new ArrayList();
        lista2.add(aluno1);
        lista2.add(aluno3);
        Turma turma2 = new Turma("6ºA", lista2);

        System.out.println("Alunos 4ºA");
        System.out.println(turma1.getAlunos());
        System.out.println("");
        System.out.println("Alunos 6ºA");
        System.out.println(turma2.getAlunos());

    }

}
  • What is the relationship between student and class?

1 answer

1

user45813 your error lies exactly in the class constructor Class.

In line 17 and 22 of the class Testaturma you create two Objects, class 1 and class 2, respectively. However, on line 15 of the Class, you test whether the student attribute, type Collection, already contains any student from the list you sent to the constructor method, but as you just instantiated this object, this Collection will be empty and the condition will always be true. If on line 15 of class you do the following:

if (a.getTurma() == null)

The Program will work exactly as desired. Good studies.

Browser other questions tagged

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