-1
I’m learning java in college and I’m having a hard time developing the logic to search an Arraylist. I want to know if there is a password registered by the type attribute, the passwords were generated with a char(type) value and an int(number) value and concatenated inside the Arraylist. Ex passwords: R-1,P-1,C-1.
I want to search through the Box method1 first on the list, only the passwords started with P respecting the queue, from the first registered to the last, and after all found and deleted, then search by C and R without need of priority, the latter two shall be random according to the position in the queue.
Let me give you another example: Let’s say I add passwords to this sequence in Arraylist: C-1, P-2, C-3, R-4, P-5, P-6, R-7.
Here suppose I want to extract all Arraylist passwords from box 1, but box 1 must prioritize passwords P(Priority) which must follow the following process, remove in the insertion order: P-2, P-5, P-6.
After continuing the removal through the box method1, it should continue and remove in the order that remained without priority: C-1, C-3, R-4, R-7.
Jframe source code to assign type and number to Arraylist.
private void clienteComumActionPerformed(java.awt.event.ActionEvent evt) {
numero++;
Senha senha = new Senha('C', numero);
listaSenhas.add(senha);
senhaGerada.setText(String.valueOf(senha.getTipo())+'-'+String.valueOf(senha.getNumero()));
}
private void clientePrioritarioActionPerformed(java.awt.event.ActionEvent evt) {
numero++;
Senha senha = new Senha('P', numero);
listaSenhas.add(senha);
senhaGerada.setText(String.valueOf(senha.getTipo())+'-'+String.valueOf(senha.getNumero()));
}
private void clienteRapidoActionPerformed(java.awt.event.ActionEvent evt) {
numero++;
Senha senha = new Senha('R', numero);
listaSenhas.add(senha);
System.out.println("lista depois"+listaSenhas);
senhaGerada.setText(String.valueOf(senha.getTipo())+'-'+String.valueOf(senha.getNumero()));
}
//Busca, apresentação e remoção
private void jbcaixa1ActionPerformed(java.awt.event.ActionEvent evt) {
saidaCaixa.setText("Caixa 1");
Iterator<Senha> itr = listaSenhas.iterator();
while(itr.hasNext()){
Senha s = itr.next();
if (s.getTipo() == 'P') {
saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));
itr.remove();
break;
}
}
}
private void jbcaixa2ActionPerformed(java.awt.event.ActionEvent evt) {
saidaCaixa.setText("Caixa 2");
Iterator<Senha> itr = listaSenhas.iterator();
while(itr.hasNext()) {
Senha s = itr.next();
itr.remove();
saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));
break;
}
}
public class Senha {
private char tipo;
private int numero;
char contains;
public Senha() { // Metodo construtor
this(' ', 0); //Esta recebendo os parametros do construtor abaixo
}
//Construtor 2
public Senha (char tipo, int numero) {
this.setTipo(tipo);
this.setNumero(numero);
}
public char getTipo() {
return tipo;
}
public void setTipo(char tipo) {
this.tipo = tipo;
}
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
}
In this part of the code you have a condition to first display/remove all passwords started in P in the insertion sequence, but I don’t know how to add logic to then be able to display/remove the other C/R passwords in the insertion order.
private void jbcaixa1ActionPerformed(java.awt.event.ActionEvent evt) { saidaCaixa.setText("Caixa 1"); Iterator<Senha> itr = listaSenhas.iterator(); while(itr.hasNext()){ Senha s = itr.next(); if (s.getTipo() == 'P') { saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero())); itr.remove(); break; } }
Thank you for your contribution. Actually I ended up getting hint to use a Return at the end of the first loop, replacing the break;, thus preventing it to execute the second loop in case it did not find the preferred value that was causing error in the code.
– Santon