help to structure an if with results from a list

Asked

Viewed 146 times

0

i have 11 checkbox’s each have an id equal to the database id and the user can select more than one checkbox how I am using java fx and scenebuilder inicalizei in my controller

private CheckBox[] checkBoxes;

public void initialize(URL url, ResourceBundle rb) {
    initTable();
    tb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue observable, Object oldValue, Object newValue) {
            selecionada = (Requisicao) newValue;
        }
    });

  checkBoxes = new CheckBox[] {
  checkMonitor,
  checkTeclado,
  checkMouse,
  checkVGA,
  checkPlacaMae,
  checkHD,
  checkFonte,
  checkRam,
  checkCabos,
  checkGravador,
  checkBateria
    };  

}

here I take the id’s of the selected checkbox and save in a list and step as parameter for my DAO

public void xd(){
    List<Integer> pecasList = new ArrayList<>();
    for (int i = 0; i < checkBoxes.length; i++) {
        if (checkBoxes[i].isSelected()) {
            pecasList.add(i+1);
        }
    }
    Alerts alerts = new Alerts();
    PecasDAO pecas = new PecasDAO();
    pecas.pegarPecasById(pecasList);
    for (Pecas checarQtd : pecas.pegarPecasById(pecasList)){
        if(checarQtd.getQtd_Pecas()>0){
            alerts.alertReqCadastrado();
        }else{
            alerts.alertSenhaDif();
        }
    }

}

my DAO

public List<Pecas> pegarPecasById(List<Integer> ids) {
    List<Pecas> pecaslist = new ArrayList<>();
if (ids.isEmpty()) {
    return new ArrayList<>(0);
} else {
    String sql = ids.stream().map(Object::toString).collect(Collectors.joining(",", "SELECT * FROM pecas WHERE id_pecas IN (", ")"));

    try {
        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {       
            Pecas pecas = new Pecas();
            pecas.setIdpecas(rs.getInt("id_pecas"));
            pecas.setNome(rs.getString("nome"));
            pecas.setQtd_Pecas(rs.getInt("qtdPecas"));
            pecaslist.add(pecas);
        }
        stmt.close();
        rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(RequisicaoDAO.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
    return pecaslist;
}   
}

in my DAO I make a query in the database and take the values of table parts which are: id/ part name/ available quantity

and then I go through this list in my controller:

pecas.pegarPecasById(pecasList);
for (Pecas checarQtd : pecas.pegarPecasById(pecasList)){
    if(checarQtd.getQtd_Pecas()>0){
        alerts.alertReqCadastrado();
    }else{
        alerts.alertSenhaDif();
    }
}

my difficulty is in this part because I have all the data with this list: ex if the user selects 3 checkbox’s I will make this query in the database according to the id and I will get the quantity of parts available

and then I want to do an if to do this check, if the user has marked a checkbox and this id does not have a quantity of parts > 0 I generate an Alert ( we have no stock ) and if you have i send this as parameter to a DAO to insert into another table all my difficulty is in how to structure that if the way it is if I select 5 check boxes and only one has more than q > 0 will appear 1 successful Alert, and 4 Alert’s failure.

I was thinking of creating 2 lists one to add the values that have no stock greater than 0 and another to the ones that have and then no if to check if you have values in those lists in if, but I don’t know if it’s possible.

1 answer

0


Friend, have you ever stopped to think, that if you don’t have the item in stock, you shouldn’t even show this option to the customer? Or, you should validate if you have stock before adding it to your list, it basically works with a cart of any E-commerce. As soon as you can show the message for each item, without complicating your code too much.

  • mt thanks bro I ended up going through this option I had found a solution, but I will do novamento.

Browser other questions tagged

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