Remember that as everything is private, the class that will inherit this will not have access to these members. Maybe you want to change to protected
.
You’ll add that to the class Aluno
:
private int totalCursos = 0;
public void addCurso(String curso) {
if (totalCursos == 5) {
throw Exception("Quantidade de cursos chegou ao limite");
}
cursos[totalCursos] = curso;
totalCursos++;
}
If you want to add several:
public void addCursos(String ... listaCursos) {
if (totalCursos > 5 - cursos.Length) {
throw Exception("Quantidade de cursos chegou ao limite");
}
for(String curso : listaCursos){
cursos[totalCursos ] = curso;
totalCursos++;
}
}
Of course you can do it a different way. But that’s the idea. If it were more than an exercise, other cares would probably need to be taken. If you could do otherwise outside the specified requirement, you would do better. Launch Exception
is not the correct in normal code, but to create a new exception just for this in a simple exercise is exaggeration. I would do without exception, but this is not the culture of Java. So:
public Boolean addCurso(String curso) {
if (totalCursos == 5) {
return false;
}
cursos[totalCursos] = curso;
totalCursos++;
return true;
}
Note that I preferred to use a name starting with add
to reflect what you are adding. If you prefer, switch to adiciona
.
To list:
public void listCursos() {
for(String curso : cursos) {
System.out.println(curso);
}
}
Modify:
public boolean changeCurso(String cursoModificar, String cursoNovo) {
for(String curso : cursos) {
if (curso.equals(cursoModificar)) {
curso = cursoNovo;
return true;
}
}
return false;
}
Another direct version by the position of the course in the register:
public boolean changeCurso(int cursoModificar, String cursoNovo) {
if (cursoModificar < 0 || cursoModificar > totalCursos - 1) {
return false;
}
cursos[cursoModificar] = cursoNovo;
return true;
}
Remove:
public boolean removeUltimoCurso() {
if (totalCursos == 0) {
return false;
}
cursos[totalCursos - 1] = null;
totalCursos--;
return true;
}
public boolean removeTodosCursos() {
cursos = new String[5];
return true;
}
I put in the Github for future reference.
You can do the removal by position and by search, same as what was done in the modification, but then you need to establish a cleaning criterion array, which although relatively simple, is not so trivial for those who are starting.
I don’t quite understand what you want. The
array
is already created. Do you want to create the methods that add and remove the courses that the student can take, one by one? What do you call editing?– Maniero
Help me? -- vector is to include remove list and edit course for the student. Do you understand? Editing would be like editing a course that’s in a position....
– Aline
See if I am on the right path to do the other parts. Whenever possible put all the code you have that is necessary. This helps to understand the problem better and in this way I could test and give a working version.
– Maniero