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?
– user28595