How to inform columns with contiguous positions?

Asked

Viewed 27 times

0

The method needs to return the lines that has given number of adjoining armchairs of a sector, I tried as follows(I could not do functional):

public String buscaContigua(int s, int c){
       String msg = "";
       String letras ="ABCDEFGHIJKL";
       int c2=c;
       for(int i=0; i<sala.length; i++){
           c=c2;
           for(int j=0; j<sala[i].length; j++){
               if(sala[i][j].getSetor()==s){
                   if(sala[i][j].estaOcupada()==false)c--;
                   if(sala[i][j].estaOcupada()) c=c2;
                }
               if(c==0)msg = ""+letras.charAt(i) + ", ";
           }
       }
       msg = "Existem poltronas contiguas nas filas: " + msg;
       if(msg.length()<39) msg = "Não existem contiguas com esse número nesse setor.";
       return msg;

This method initiates the theatre[12][20]

 public void inicializa(){
       String letras ="ABCDEFGHIJKL";
       int setor;
       for(int i=0; i<sala.length; i++){
           if(i<=2) setor = 0;
           else setor = 3;
           for(int j=0; j<sala[i].length; j++){
               if(i>2 && i<8){
                   if(j<=9) setor = 1;
                   else setor = 2;
               }
               sala[i][j] = new Poltrona(""+letras.charAt(i)+j,setor);
           }
       }
   }
  • sala is a variable of the type Poltrona[][]?

  • Yes, the dimensions are [12][20]

  • You can give an example of input and output values to make it clearer what you are trying to achieve ?

1 answer

1

This method will return one boolean[] indicating which rows have free contiguous positions:

private boolean[] buscaContigua(int setor, int contiguas) {
    boolean[] filasPossiveis = new boolean[sala.length];
    int f = -1;
    for (Poltrona[] fila : sala) {
        f++;
        int encontradas = 0;
        for (Poltrona p : fila) {
            if (p.getSetor() != s || p.estaOcupada()) {
                encontradas = 0;
                continue;
            }
            encontradas++;
            if (encontradas == contiguas) {
                filasPossiveis[f] = true;
                break;
            }
        }
    }
    return filasPossiveis;
}

With this, we can easily devise a function that gives the message of the available queues from this array:

private String mensagemFilasPossiveis(boolean[] filasPossiveis) {
    StringJoiner sj = new StringJoiner(", ", "Existem poltronas contíguas nas filas: ", ".")
            .setEmptyValue("Não existem contíguas com esse número nesse setor.");
    for (int i = 0; i < filasPossiveis.length; i++) {
        if (filasPossiveis[i]) sj.add((char) ('A' + i));
    }
    return sj.toString();        
}

And then, just put the two together:

public String mensagemBuscaContigua(int setor, int contiguas) {
    return mensagemFilasPossiveis(buscaContigua(setor, contiguas));
}

The ((char) ('A' + i)) is a trick to get A to the first row, B to the second, C to the third, etc.

Don’t forget the import java.util.StringJoiner; at the beginning of the file.

You can improve your method inicializa:

public void inicializa() {
    for (int i = 0; i < sala.length; i++) {
        for (int j = 0; j < sala[i].length; j++) {
            int setor = i <= 2 ? 0 : i >= 8 ? 3 : j <= 9 ? 1 : 2;
            sala[i][j] = new Poltrona(((char) ('A' + i)) + "" + j, setor);
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.