alny Posted April 21, 2013 at 08:59 AM Report #504212 Posted April 21, 2013 at 08:59 AM (edited) ola pretendo adicinar um autor na minha track restrições : .não pode adicionar o author com ID igual, só pode ter maximo de cinco author na minha track. tenho o codigo feito mas não funciona tão bem como esperava e gostaria q alguém me explicasse ou algo assim public boolean addAuthor(int id, Author aut) { for (int index = 0; index < this.author.length; index++) { if (this.author[index] != null) { if (id != this.author[index].getIdAuthor() && this.author.length < 5) { this.author[index] = aut; return true; } } } return false; } Edited April 22, 2013 at 10:12 AM by brunoais geshi
AJBM Posted April 21, 2013 at 09:27 AM Report #504214 Posted April 21, 2013 at 09:27 AM Boas! O que esta acontecer é se o id do primeiro autor do array for diferente do que queres adicionar, ele adiciona nessa posição, não é isso que queres. Aconselho te a separar os métodos faz um que verifique se um dado id existe, e outro para adicionar. Não precisas de passar o id como parâmetro pois o autor já tem esse atributo.
alny Posted April 21, 2013 at 09:44 AM Author Report #504215 Posted April 21, 2013 at 09:44 AM pois é mas tb já fiz isso
alny Posted April 21, 2013 at 10:15 AM Author Report #504219 Posted April 21, 2013 at 10:15 AM (edited) bem tenho este novo codigo. este codigo já ta adicionar mas o problema é que tb adiciona author com ids iguai e eu não esperava isso. espero que alguém possa me ajudar public boolean addAuthor(Author[] aut) { int ids[] = new int[aut.length]; for (int index = 0; index < this.author.length; index++) { for (int y = 0; y < ids.length; y++) { if (ids[y] == 0) { ids[y] = aut[index].getIdAuthor(); break; } if (ids[y] != author[index].getIdAuthor()) { this.author = aut; return true; } } } return false; } } Edited April 22, 2013 at 10:12 AM by brunoais geshi
AJBM Posted April 21, 2013 at 10:16 AM Report #504220 Posted April 21, 2013 at 10:16 AM (edited) Porque que estas a passar um array de autores, basta passares um autor. Divide os métodos e mais fácil, no verificarID retorna true se o id existir, e retorna false caso contrario, depois trabalhas com este método no add Edited April 21, 2013 at 10:21 AM by AJBM
alny Posted April 21, 2013 at 10:20 AM Author Report #504221 Posted April 21, 2013 at 10:20 AM coloca esse código aqui public boolean addAuthor(Author[] aut) { int ids[] = new int[aut.length]; for (int index = 0; index < this.author.length; index++) { for (int y = 0; y < ids.length; y++) { if (ids[y] == 0) { ids[y] = aut[index].getIdAuthor(); break; } if (ids[y] != author[index].getIdAuthor()) { this.author = aut; return true; } } } return false; } }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now