Doubts with business rule between class student and class

0

I'm having a question in a code, I'm new to programming, if anyone can help me, I'll be grateful.

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

The whole process is being done in the code, the only failure is to add 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, it warns that the student already is enrolled and does not change his class, but when the second class is ready, the student is there, but pointed to the first class he was allocated.

Someone can help me in a rule that when the student is already linked to a class, it does not add to the new collection.

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

Below the code:

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

    }

}
    
asked by anonymous 11.05.2016 / 23:22

1 answer

1

user45813 your error lies exactly in the Class class constructor.

On line 17 and 22 of the TestaTurma class you create two Objects, class1 and class2, respectively. However, on row 15 of the Class class, you test whether the Collection class attribute already contains some students from the list that you submitted to the constructor method, but since you just instantiated this object, that Collection is empty and the condition will always be true. If on line 15 of class class you do the following:

if (a.getTurma() == null)

The program will work exactly as you want. Good studies.

    
12.05.2016 / 00:34