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