vendredi 22 février 2019

Equating a Double with a calculated Double value from a method results in 0.0? What could be the problem?

For these expressions:

     if (lim != null) {


         for (int i = 0; i < itemlist.size(); i++) {              

            Double itemquantity = calculateQuantity(itemlist.get(i));

            Integer limitvalue = lim.getValue();

            System.out.println("CALCULATED VALUE IS: " + calculateQuantity(itemlist.get(i)));

            System.out.println("ITEMQUANTITY IS " + itemquantity);

            System.out.println("LIMIT VALUE IS " + limitvalue);

           if (itemquantity < limitvalue) {
               System.out.println("QUANTITY " + itemquantity + " vs LIMIT " + limitvalue);
           below.add(itemlist.get(i).getName());
        }}

I get the following results:

CALCULATED VALUE IS: **210.0** 
ITEMQUANTITY IS **0.0** 
LIMIT VALUE IS **500** 
QUANTITY 0.0 vs LIMIT 500

Here is the is the calculateQuantity() method which returns a Double, but this method works as seen by the output:

     public Double calculateQuantity(Items item)
        {
        Double quantity = 0.0;     
        int ID = item.getItemId();
            try {
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();

                Query query = session.createQuery("select sum(t.flow * a.inOrOut) from Advicenote a join a.transactions t join t.item i where i.itemId = :keyword group by i.itemId");

                query.setParameter("keyword", ID);
                transaction.commit();
                if (query.uniqueResult() == null || query.list().isEmpty()) {
                    quantity = 0.0;                
                } 
                else{     
                quantity = Double.valueOf(query.uniqueResult().toString()); 

                }}catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }

}finally{
                        session.close();
                        return quantity;
                        }}

So my question is, why I get 0 when I try to equate the result of calculateQuantity() with a Double value? And how could make the itemquantity < limitvalue if statement work (the former should be a Double/double and the latter an Integer/int)?

Aucun commentaire:

Enregistrer un commentaire