Search in sources :

Example 1 with ProductNotInList

use of CommonDefs.GroceryListExceptions.ProductNotInList in project SmartCity-Market by TechnionYP5777.

the class GroceryList method removeProduct.

public void removeProduct(ProductPackage p) throws ProductNotInList, AmountIsBiggerThanAvailable {
    ProductPackage pp = groceryList.get(p.getSmartCode());
    if (pp == null)
        throw new ProductNotInList();
    int newAmount = pp.getAmount() - p.getAmount();
    if (newAmount < 0)
        throw new AmountIsBiggerThanAvailable();
    if (newAmount == 0)
        groceryList.remove(pp.getSmartCode());
    else {
        pp.setAmount(newAmount);
        groceryList.put(pp.getSmartCode(), pp);
    }
}
Also used : AmountIsBiggerThanAvailable(CommonDefs.GroceryListExceptions.AmountIsBiggerThanAvailable) ProductNotInList(CommonDefs.GroceryListExceptions.ProductNotInList)

Example 2 with ProductNotInList

use of CommonDefs.GroceryListExceptions.ProductNotInList in project SmartCity-Market by TechnionYP5777.

the class Customer method returnProductToShelf.

@Override
public void returnProductToShelf(SmartCode c, int amount) throws AmountBiggerThanAvailable, ProductPackageDoesNotExist, CriticalError, CustomerNotConnected {
    String serverResponse;
    log.info("Creating REMOVE_PRODUCT_FROM_GROCERY_LIST command wrapper to customer with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.REMOVE_PRODUCT_FROM_GROCERY_LIST, Serialization.serialize(new ProductPackage(c, amount, null))).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    try {
        resultDescriptorHandler(getCommandWrapper(serverResponse).getResultDescriptor());
    } catch (InvalidCommandDescriptor | InvalidParameter | ProductCatalogDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    /* update customer data: groceryList, cartProductCache, totalSum */
    if (groceryList == null)
        throw new CriticalError();
    try {
        groceryList.removeProduct(new ProductPackage(c, amount, null));
    } catch (ProductNotInList | AmountIsBiggerThanAvailable ¢) {
        throw new CriticalError();
    }
    long barcode = c.getBarcode();
    CartProduct cartProduct = cartProductCache.get(barcode);
    ProductPackage productPackage = new ProductPackage(c, amount, null);
    cartProduct.removeProductPackage(productPackage);
    if (cartProduct.getTotalAmount() <= 0)
        cartProductCache.remove(barcode);
    else
        cartProductCache.put(barcode, cartProduct);
    totalProductsAmount -= amount;
    totalSum -= amount * cartProduct.getCatalogProduct().getPrice();
    log.info("REMOVE_PRODUCT_FROM_GROCERY_LIST command succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) AmountIsBiggerThanAvailable(CommonDefs.GroceryListExceptions.AmountIsBiggerThanAvailable) CriticalError(SMExceptions.CommonExceptions.CriticalError) ProductPackage(BasicCommonClasses.ProductPackage) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductNotInList(CommonDefs.GroceryListExceptions.ProductNotInList) CartProduct(BasicCommonClasses.CartProduct) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Aggregations

AmountIsBiggerThanAvailable (CommonDefs.GroceryListExceptions.AmountIsBiggerThanAvailable)2 ProductNotInList (CommonDefs.GroceryListExceptions.ProductNotInList)2 CartProduct (BasicCommonClasses.CartProduct)1 ProductPackage (BasicCommonClasses.ProductPackage)1 CommandWrapper (ClientServerApi.CommandWrapper)1 AuthenticationError (CustomerContracts.ACustomerExceptions.AuthenticationError)1 ForgotPasswordWrongAnswer (CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer)1 GroceryListIsEmpty (CustomerContracts.ACustomerExceptions.GroceryListIsEmpty)1 InvalidCommandDescriptor (CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor)1 InvalidParameter (CustomerContracts.ACustomerExceptions.InvalidParameter)1 ProductCatalogDoesNotExist (CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)1 UsernameAlreadyExists (CustomerContracts.ACustomerExceptions.UsernameAlreadyExists)1 CriticalError (SMExceptions.CommonExceptions.CriticalError)1 SocketTimeoutException (java.net.SocketTimeoutException)1