/// <summary> /// Checks if any of the suppliers has Article in specified price range in stock /// </summary> /// <param name="id">Id of the article</param> /// <param name="maxExpectedPrice">Max price of the Article</param> /// <param name="suppliers">List of suppliers available</param> /// <param name="index">recursion index</param> /// <returns>Article if found, null if not existing</returns> private Article FindArticle(int id, int maxExpectedPrice, ISupplier[] suppliers, int index = 0) { if (suppliers.Length == index) { logger.Debug("No more suppliers to check"); return(null); } ISupplier supplier = suppliers[index]; logger.Debug($"Checking supplier [Name = {supplier.ToString()}]"); var articleExists = supplier.ArticleInInventory(id); if (articleExists) { Article tempArticle = supplier.GetArticle(id); if (maxExpectedPrice >= tempArticle.ArticlePrice) { return(tempArticle); } } logger.Debug($"Supplier [Name = {supplier.ToString()}]: Article not found, searching at another supplier"); return(FindArticle(id, maxExpectedPrice, suppliers, index + 1)); }
public void SellArticle(ISupplier supplier, int articleId, int buyerId) { Article article = supplier.GetArticle(articleId); if (article.ID == -1) { throw new NullReferenceException("It's not allowed to sell empty articles!"); } article.IsSold = true; article.SoldDate = DateTime.Now; article.BuyerUserID = buyerId; _logger.WriteMessage(LogLevelConsts.DEBUG, String.Format("Trying to sell article with ID: {0}, from supplier: '{1}'", article.ID, supplier.ToString())); try { _databaseDriver.SaveArticle(article); _logger.WriteMessage(LogLevelConsts.INFO, String.Format("Article with ID: {0} is sold. Supplier is: '{1}'", article.ID, supplier.ToString())); } catch (NullReferenceException ex) { _logger.WriteMessage(LogLevelConsts.ERROR, String.Format("Could not save article with ID: {0}.", article.ID)); throw new Exception(String.Format("Could not save article with ID: {0}. \n Message: {1}", article.ID, ex.Message)); } }
public Article Order(int id, int maxExpectedPrice) { if (_supplier.ArticleInInventory(id)) { Article article = _supplier.GetArticle(id); if (article.ArticlePrice < maxExpectedPrice) { article.SupplierId = _supplier.Id; return(article); } } return(Successor.Order(id, maxExpectedPrice)); }