public static void calculateDistances() {

    for (Order order : orders) {

      for (ProductType productType : order.products) {

        Map<Warehouse, Double> distances = new HashMap<Warehouse, Double>();
        TreeMap<Double, Warehouse> distanceDifferences = new TreeMap<Double, Warehouse>();

        for (Warehouse warehouse : warehouses) {
          if (warehouse.hasProduct(productType)) {
            double distance = order.position.distanceTo(warehouse.position);
            distances.put(warehouse, distance);
          }
        }

        for (Warehouse warehouse : warehouses) {
          double init = distances.get(warehouse);

          for (Warehouse key : distances.keySet()) {
            if (warehouse != key) {
              init = init - distances.get(key);
            }
          }

          distanceDifferences.put(init, warehouse);
        }

        Warehouse warehouse = distanceDifferences.get(distanceDifferences.lastKey());

        warehouse.remove(productType);
        order.addWarehouse(productType, warehouse);
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * insert
   *
   * @param order
   * @return
   */
  @Override
  public int insert(Order order) {

    int orderId = order.getOrderId();
    int userId = order.getUserId();
    int ready = order.getReady();
    LinkedList<Integer> dishIdList = order.getDishIDList();

    Connection connection = MyConnection.getInstance().getConnection();

    try {

      PreparedStatement preparedStatement =
          connection.prepareStatement(
              "INSERT INTO order_table (orderId, userId, ready, summ) " + "VALUES (?, ?, 0, 0)");
      preparedStatement.setInt(1, orderId);

      preparedStatement.setInt(2, userId);
      preparedStatement.execute();

      PreparedStatement preparedStatement2 = connection.prepareCall("select last_insert_id();");
      ResultSet resultSet2 = preparedStatement2.executeQuery();
      if (resultSet2.next()) {
        orderId = resultSet2.getInt(MYSQL_LAST_INSERT_ID.toString());
      }

      connection.setAutoCommit(false);
      for (int i = 0; i < dishIdList.size(); i++) {
        PreparedStatement preparedStatement1 =
            connection.prepareStatement(
                "INSERT INTO order_content (orderId, dishId) " + "VALUES (?, ?)");
        preparedStatement1.setInt(1, orderId);
        preparedStatement1.setInt(2, dishIdList.get(i));
        preparedStatement1.execute();
      }

      connection.commit();
      connection.setAutoCommit(true);

    } catch (SQLException | NullPointerException ex) {
      Logger.getLogger(MySQLOrderDAO.class.getName()).error(ex);
    } finally {
      try {
        if (connection != null) {
          connection.close();
        }
      } catch (SQLException ex) {
        Logger.getLogger(MySQLOrderDAO.class.getName()).error(ex);
      }
    }

    return orderId;
  }