use of CommonDefs.GroceryListExceptions.AmountIsBiggerThanAvailable 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);
}
}
use of CommonDefs.GroceryListExceptions.AmountIsBiggerThanAvailable 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.");
}
Aggregations