@Override
 public Item getItem(int id) {
   for (Item i : items) {
     if (i.getItemID() == id) return i;
   }
   return null;
 }
Пример #2
0
  @Override
  public String ship_items(Item[] items, Addresses address)
      throws UnknownAddressFault, UnknownProductFault, IllegalArgumentException {

    // check if all parameters are valid
    if (items == null || items.length <= 0)
      throw new IllegalArgumentException("No items to ship specified");
    if (address == null) throw new IllegalArgumentException("Provided address may not be null");

    // check if the address is known to the system
    Addresses storedAddress = DataBackend.getInstance().getAddress(address.getId());
    if (storedAddress == null || !storedAddress.equals(address))
      throw new UnknownAddressFault("[ShippingService] ship_items: unknown address");

    UUID ret = UUID.randomUUID();
    String retItems = "[ShippingService] Sending items ";
    for (Item item : items) {
      if (item != null) {
        Product p = item.getProduct();

        // is the stored product in the item valid?
        if (p == null) throw new UnknownProductFault("One of the products to ship is null");

        // check if the product is known to the system
        Product storedProduct = DataBackend.getInstance().getProduct(p.getId());
        if (storedProduct == null || !storedProduct.equals(p))
          throw new UnknownProductFault("Unknown product to ship");
        else
          // everything worked fine so ship the product
          // and add an info to the console
          retItems += "'" + storedProduct.getName() + "', ";
      }
    }

    // remove the last ", "
    retItems = retItems.substring(0, retItems.length() - 2);
    retItems += " to";

    // create log-output on server-side
    System.out.println("[ShippingService] " + new Date(System.currentTimeMillis()));
    System.out.println(retItems);
    System.out.println(
        "[ShippingService] "
            + address.getStreet()
            + " "
            + address.getHouse()
            + ", "
            + address.getZipCode()
            + " "
            + address.getCity());

    new ShippingCallbackHelper(ret.toString());

    return ret.toString();
  }