Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.2 kB
0
Indexable
Never
public void calculateTotal(){
        String selectQuery = "SELECT * FROM jwrms.stockandinventory WHERE productCode = ?";
        String selectQuery1 = "SELECT * FROM jwrms.customers WHERE customerID = ?";
        String selectQuery2 = "SELECT * FROM jwrms.staff WHERE employeeId = ?";
        try{
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            ArrayList<StockAndInventory> products = new ArrayList<>();
            var productIds = (ArrayList<String>) objIs.readObject();
            var customer = (Customer) objIs.readObject();
            var staff = (Staff) objIs.readObject();
            PreparedStatement ps = dBConn.prepareStatement(selectQuery);
            for (var id : productIds) {
                ps.setString(1,id);
                var result = ps.executeQuery();
                if(result.next()){
                    var product = new StockAndInventory();
                    product.setProductCode(result.getString(1));
                    product.setProductName(result.getString(2));
                    product.setShortDescription(result.getString(3));
                    product.setLongDescription(result.getString(4));
                    product.setItemsInStock(result.getInt(5));
                    product.setUnitPrice(result.getFloat(6));
                    products.add(product);
                }
            }
            ps =  dBConn.prepareStatement(selectQuery1);
            ps.setString(1,customer.getCustomerID());
            ResultSet resultSet = ps.executeQuery();
            if(resultSet.next()){
                customer.setCustomerID(resultSet.getString(1));
                customer.setName(resultSet.getString(2));
                customer.setDob(dateFormat.parse(String.valueOf(resultSet.getDate(3))));
                customer.setAddress(resultSet.getString(4));
                customer.setTelephone(resultSet.getString(5));
                customer.setEmail(resultSet.getString(6));
                customer.setDateOfMembership(dateFormat.parse(String.valueOf(resultSet.getDate(7))));
                customer.setDateOfMembershipExpiry(dateFormat.parse(String.valueOf(resultSet.getDate(8))));
            }
            ps = dBConn.prepareStatement(selectQuery2);
            ps.setInt(1,staff.getEmployeeId());
            ResultSet resultSet1 = ps.executeQuery();
            if(resultSet1.next()){
                ps.setInt(1,staff.getEmployeeId());
                ps.setString(2,staff.getName());
                ps.setString(3, staff.getEmployeeType());
                ps.setString(3, staff.getDepartmentCode());
            }
            objOs.writeObject(true);
            objOs.flush();
            objOs.writeObject(staff);
            objOs.flush();
            objOs.writeObject(products);
            objOs.flush();
            objOs.writeObject(customer);
            objOs.flush();
        }catch(IOException | ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }