Пример #1
0
  /*
   * Termina a realização da Tarefa.
   * Retorna true se foi terminada com sucesso false caso contrário
   */
  public boolean endTask(String id) throws InterruptedException {
    Map<String, Integer> objectsToSupply;
    String type;
    lock.lock();
    try {
      if (!this.tasksRunning.containsKey(Integer.valueOf(id))) {
        return false;
      } else {
        type = this.tasksRunning.get(Integer.valueOf(id));
        objectsToSupply = this.tasks.get(type).getObjects();
      }
    } finally {
      lock.unlock();
    }

    // Supply de todos os objetos
    for (Map.Entry<String, Integer> entry : objectsToSupply.entrySet()) {
      warehouse.supply(entry.getKey(), entry.getValue());
    }

    lock.lock();
    try {
      this.tasksRunning.remove(Integer.valueOf(id));
      this.tasks.get(type).signalP();
    } finally {
      lock.unlock();
    }

    return true;
  }
Пример #2
0
  /*
   * Inicia a realização de uma Tarefa.
   * Retorna o ID se foi iniciada com sucesso -1 caso contrário
   */
  public int beginTask(String type) {
    Map<String, Integer> objectsToConsume;
    lock.lock();
    try {
      if (!this.tasks.containsKey(type)) return -1;
      else {
        objectsToConsume = this.tasks.get(type).getObjects();
      }
    } finally {
      lock.unlock();
    }

    try {
      warehouse.consume(objectsToConsume);
    } catch (Exception e) {
      return -1;
    }

    lock.lock();
    try {
      countID++;
      this.tasksRunning.put(countID, type);
      return countID; // Retornar o ID da tarefa
    } finally {
      lock.unlock();
    }
  }
Пример #3
0
  /**
   * Get a warehouse to put in the database
   *
   * @param id for the warehouse
   * @return warehouse
   */
  public Warehouse getWarehouse(int id) {
    Warehouse warehouse = new Warehouse();

    warehouse.setWarehouseID(id);
    warehouse.setName("Warehouse #" + id);
    warehouse.setAddress(randAddress());
    warehouse.setCity(cities.get(randInt(0, cities.size())));
    warehouse.setState(states.get(randInt(0, states.size())));
    warehouse.setZip(zips.get(randInt(0, zips.size())));
    warehouse.setSalesTax(new BigDecimal(randDouble(minSalesTax, maxSalesTax)));
    warehouse.setTotalSales(new BigDecimal(0));

    return warehouse;
  }
Пример #4
0
 /* Abastece o armazém com o produto - name, e a quantidade - qtd */
 public boolean supply(String name, String qtd) throws InterruptedException {
   if (name.equals("") || qtd.equals("")) return false;
   try {
     warehouse.supply(name, Integer.parseInt(qtd));
     return true;
   } catch (InterruptedException ie) {
     return false;
   }
 }
Пример #5
0
  /**
   * Get a line item to put in the database
   *
   * @param id for the line item
   * @param warehouse for the station
   * @param Station station
   * @param Customer customer
   * @param Order order
   * @param ArrayList<Item> itemList
   * @return lineItem
   */
  public LineItem getLineItem(
      int id,
      Warehouse warehouse,
      Station station,
      Customer customer,
      Order order,
      ArrayList<Item> itemList) {
    LineItem lineItem = new LineItem();

    Item item = itemList.get(randInt(0, itemList.size()));

    int numOrdered = randInt(1, 5);

    if (item.getCurrentStock() < numOrdered) return null;

    lineItem.setLineItemID(id);
    lineItem.setOrderID(order.getOrderID());
    lineItem.setItemID(item.getItemID());
    lineItem.setCustomerID(customer.getCustomerID());
    lineItem.setWarehouseID(warehouse.getWarehouseID());
    lineItem.setStationID(station.getStationID());
    lineItem.setNumOrdered(numOrdered);
    lineItem.setAmountDue(item.getPrice().multiply(new BigDecimal(lineItem.getNumOrdered())));
    lineItem.setAmountDue(
        lineItem.getAmountDue().add(station.getSalesTax().multiply(lineItem.getAmountDue())));
    lineItem.setAmountDue(
        lineItem.getAmountDue().subtract(customer.getDiscount().multiply(lineItem.getAmountDue())));
    lineItem.setDateDelivered(randDate());

    item.setNumLineItems(item.getNumLineItems() + 1);
    item.setNumSold(item.getNumSold() + lineItem.getNumOrdered());
    // item.setCurrentStock(item.getCurrentStock() - lineItem.getNumOrdered()); don't modify stock
    // counts for random generation

    customer.setTotalPaid(customer.getTotalPaid().add(lineItem.getAmountDue()));
    station.setTotalSales(station.getTotalSales().add(lineItem.getAmountDue()));
    warehouse.setTotalSales(warehouse.getTotalSales().add(lineItem.getAmountDue()));

    return lineItem;
  }
Пример #6
0
  /**
   * Get an item to put in the database
   *
   * @param id for the item
   * @param warehouse for the item
   * @return item
   */
  public Item getItem(int id, Warehouse warehouse) {
    Item item = new Item();

    int itemIndex = randInt(0, inventory.size()); // choose an inventory item from list

    item.setItemID(id);
    item.setWarehouseID(warehouse.getWarehouseID());
    item.setName(inventory.get(itemIndex) + id);
    item.setPrice(new BigDecimal((double) randInt(0, 20) + randDouble()));
    item.setCurrentStock(randInt(50, 150)); // random number of stock listings per item, avg of 100

    return item;
  }
Пример #7
0
  /**
   * Get a station to put in the database
   *
   * @param id for the station
   * @param warehouse for the station
   * @return station
   */
  public Station getStation(int id, Warehouse warehouse) {
    Station station = new Station();

    station.setStationID(id);
    station.setWarehouseID(warehouse.getWarehouseID());
    station.setName("Station #" + id);
    station.setAddress(randAddress());
    station.setCity(cities.get(randInt(0, cities.size())));
    station.setState(states.get(randInt(0, states.size())));
    station.setZip(zips.get(randInt(0, zips.size())));
    station.setSalesTax(new BigDecimal(randDouble(minSalesTax, maxSalesTax)));
    station.setTotalSales(new BigDecimal(0));

    return station;
  }
Пример #8
0
  /**
   * Get an order to put in the database
   *
   * @param id for the order
   * @param warehouse for the order
   * @param station for the order
   * @param customer for the the order
   * @return order
   */
  public Order getOrder(int id, Warehouse warehouse, Station station, Customer customer) {
    Order order = new Order();

    order.setOrderID(id);
    order.setCustomerID(customer.getCustomerID());
    order.setWarehouseID(warehouse.getWarehouseID());
    order.setStationID(station.getStationID());
    order.setDateOrdered(randDate());
    order.setCompleted(1); // set as completed by default
    order.setNumLineItems(randInt(3, 10)); // number of line items per order

    customer.setDeliveriesReceived(customer.getDeliveriesReceived() + 1);
    customer.setNumPayments(customer.getNumPayments() + 1);

    return order;
  }
Пример #9
0
  /**
   * Get a customer to put in the database
   *
   * @param id for the customer
   * @param warehouse for the customer
   * @param station for the customer
   * @return customer
   */
  public Customer getCustomer(int id, Warehouse warehouse, Station station) {
    Customer customer = new Customer();

    customer.setCustomerID(id);
    customer.setWarehouseID(warehouse.getWarehouseID());
    customer.setStationID(station.getStationID());
    customer.setFirstName(fnames.get(randInt(0, fnames.size())));
    customer.setMiddleInitial(letters[randInt(0, letters.length)]);
    customer.setLastName(lnames.get(randInt(0, lnames.size())));
    customer.setAddress(randAddress());
    customer.setCity(cities.get(randInt(0, cities.size())));
    customer.setState(states.get(randInt(0, states.size())));
    customer.setZip(zips.get(randInt(0, zips.size())));
    customer.setPhone(randInt(100, 1000) + "-" + randInt(100, 1000) + "-" + randInt(1000, 10000));
    customer.setDateAdded(randDate());
    customer.setDiscount(new BigDecimal(randDouble(minDiscount, maxDiscount)));
    customer.setBalance(new BigDecimal(0));
    customer.setTotalPaid(new BigDecimal(0));
    customer.setNumPayments(0);
    customer.setDeliveriesReceived(0);

    return customer;
  }
Пример #10
0
  static int loadWhse(int whseKount) {

    try {

      now = new java.util.Date();
      System.out.println("\nStart Whse Load for " + whseKount + " Whses @ " + now + " ...");

      if (outputFiles == true) {
        out = new PrintWriter(new FileOutputStream(fileLocation + "warehouse.csv"));
        System.out.println("\nWriting Warehouse file to: " + fileLocation + "warehouse.csv");
      }

      Warehouse warehouse = new Warehouse();
      for (int i = 1; i <= whseKount; i++) {

        warehouse.w_id = i;
        warehouse.w_ytd = 300000;

        // random within [0.0000 .. 0.2000]
        warehouse.w_tax = (float) ((jTPCCUtil.randomNumber(0, 2000, gen)) / 10000.0);

        warehouse.w_name = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(6, 10, gen));
        warehouse.w_street_1 = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(10, 20, gen));
        warehouse.w_street_2 = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(10, 20, gen));
        warehouse.w_city = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(10, 20, gen));
        warehouse.w_state = jTPCCUtil.randomStr(3).toUpperCase();
        warehouse.w_zip = "123456789";

        if (outputFiles == false) {
          whsePrepStmt.setLong(1, warehouse.w_id);
          whsePrepStmt.setDouble(2, warehouse.w_ytd);
          whsePrepStmt.setDouble(3, warehouse.w_tax);
          whsePrepStmt.setString(4, warehouse.w_name);
          whsePrepStmt.setString(5, warehouse.w_street_1);
          whsePrepStmt.setString(6, warehouse.w_street_2);
          whsePrepStmt.setString(7, warehouse.w_city);
          whsePrepStmt.setString(8, warehouse.w_state);
          whsePrepStmt.setString(9, warehouse.w_zip);
          whsePrepStmt.executeUpdate();
        } else {
          String str = "";
          str = str + warehouse.w_id + ",";
          str = str + warehouse.w_ytd + ",";
          str = str + warehouse.w_tax + ",";
          str = str + warehouse.w_name + ",";
          str = str + warehouse.w_street_1 + ",";
          str = str + warehouse.w_street_2 + ",";
          str = str + warehouse.w_city + ",";
          str = str + warehouse.w_state + ",";
          str = str + warehouse.w_zip;
          out.println(str);
        }
      } // end for

      transCommit();
      now = new java.util.Date();

      long tmpTime = new java.util.Date().getTime();
      if (!silent) {
        System.out.println("Elasped Time(ms): " + ((tmpTime - lastTimeMS) / 1000.000));
      }
      lastTimeMS = tmpTime;
      if (!silent) {
        System.out.println("End Whse Load @  " + now);
      }
    } catch (SQLException se) {
      System.out.println(se.getMessage());
      transRollback();
    } catch (Exception e) {
      e.printStackTrace();
      transRollback();
    }

    return (whseKount);
  } // end loadWhse()