/// <summary> /// Покупка напитка /// </summary> /// <param name="drink"></param> /// <param name="currentState"></param> /// <param name="coins"></param> /// <returns></returns> public bool BuyDrink(DrinkEntity drink, CurrentStateEntity currentState, IList <CoinEntity> coins) { //Если не можем купить выходим if (!IsCanBuy(drink, currentState)) { return(false); } /*List<CoinEntity> coins = _coinsRepo.Queryable().ToList();*/ var deposit = currentState.Deposit; var change = deposit - drink.CostPrice; currentState.Deposit = 0; //Если сдача равна 0 или в атомате достаточное кол-во монет для выдачи, то позволяем купить, иначе просто отдаем внесенные деньги if (change == 0 || IsHaveCoinsForChange(coins, change)) { drink.Count--; currentState.Change += change; return(true); } currentState.Change += deposit; return(false); }
public DrinkOperationInfo AddDrink(string name, int cost, int count, string imgPath) { DrinkEntity drinkEntity = new DrinkEntity { Id = Guid.NewGuid(), Name = name, Count = count, ImagePath = imgPath, CostPrice = cost }; _drinkRepository.Add(drinkEntity); _logger.Info(" before _db.SaveChanges()..."); _drinkRepository.SaveChanges(); /*new { success = true, message = "ok", id = drinkEntity.Id, name, cost, count, path = imgPath }*/ return(new DrinkOperationInfo { Drink = drinkEntity, Msg = "ok", Success = true }); }
public DrinkOperationInfo BuyDrink(Guid id) { DrinkEntity drink = _drinkRepository.Get(id); CurrentStateEntity currentState = _stateRepository.GetFirst(); _logger.Info(" before buy drink"); _printer.DrinkAndStateInfo(drink, currentState); bool success = true; var msg = ""; if (!_vengineMachine.BuyDrink(drink, currentState, _coinRepository.Queryable().ToList())) { success = false; msg = " Невозможно совершить покупку! В автомате нет сдачи! Деньги возвращены полностью в качестве сдачи"; _logger.Warning(msg); } _logger.Info(" after buy drink"); _printer.DrinkAndStateInfo(drink, currentState); _logger.Info(" before _db.SaveChanges()..."); _drinkRepository.SaveChanges(); return(new DrinkOperationInfo { Drink = drink, Change = currentState.Change, Success = success, Msg = msg }); }
public static DrinkDTO ToDrinkDTO(DrinkEntity entity) { return(new DrinkDTO() { Id = entity.Id, Name = entity.Name }); }
public void DrinkChangeCost() { Init(); DrinkEntity drink1 = DrinkEntitiesList[0]; VengineMachine.ChangeCost(drink1, 50); Assert.AreEqual(50, drink1.CostPrice); }
/*protected FakeRepository<DrinkEntity> RepositoryDrink;*/ public virtual void Init() { Drink1 = new DrinkEntity() { CostPrice = 10, Count = 2, Id = Guid.Parse("FFDF9A9C-1FC1-4DEB-B552-45EB5E0EC48C"), ImagePath = "image1", Name = "drink1" }; Drink2 = new DrinkEntity() { CostPrice = 20, Count = 4, Id = Guid.NewGuid(), ImagePath = "image2", Name = "drink2" }; Coin1 = new CoinEntity() { Id = Guid.NewGuid(), Count = 10, IsBlocking = false, Value = ValueCoins.One }; Coin2 = new CoinEntity() { Id = Guid.NewGuid(), Count = 10, IsBlocking = true, Value = ValueCoins.Two }; CurrentState = new CurrentStateEntity() { Change = 0, Deposit = 0 }; DrinkEntitiesList = new List <DrinkEntity> { Drink1, Drink2 }; CoinEntitiesList = new List <CoinEntity> { Coin1, Coin2 }; /*RepositoryDrink = new FakeRepository<DrinkEntity>(DrinkEntitiesList); * var repositoryCoin = new FakeRepository<CoinEntity>(CoinEntitiesList);*/ VengineMachine = new VengineMachine(/*RepositoryDrink, repositoryCoin*/); }
public void ChangeDrinkName(Guid id, string name) { DrinkEntity drinkEntity = _drinkRepository.Get(id); _printer.DrinkInfo(drinkEntity); _vengineMachine.ChangeName(drinkEntity, name); _logger.Info(" before _db.SaveChanges()..."); _drinkRepository.SaveChanges(); }
public void ChangeDrinkCount(Guid id, int count) { DrinkEntity drinkEntity = _drinkRepository.Get(id); _printer.DrinkInfo(drinkEntity); _vengineMachine.ChangeCount(drinkEntity, count); _logger.Info(" before _db.SaveChanges()..."); _drinkRepository.SaveChanges(); }
public void DrinkInfo(DrinkEntity drink) { _logger.Info(" "); _logger.Info(" DrinkInfo:"); _logger.Info(" Id = " + drink.Id); _logger.Info(" Name = " + drink.Name); _logger.Info(" Count = " + drink.Count); _logger.Info(" CostPrice = " + drink.CostPrice); _logger.Info(" ImagePath = " + drink.ImagePath); _logger.Info(" "); }
public JsonBase IsCanBuy(Guid id) { DrinkEntity drink = _drinkRepository.Get(id); CurrentStateEntity currentState = _stateRepository.GetFirst(); _printer.DrinkAndStateInfo(drink, currentState); if (!_vengineMachine.IsCanBuy(drink, currentState)) { var msg = " Сумма недостаточна для покупки!"; _logger.Warning(msg); return(new JsonError(msg)); } return(new JsonSuccess()); }
public void DrinkAndStateInfo(DrinkEntity drink, CurrentStateEntity state) { DrinkInfo(drink); StateInfo(state); }
public string Import(HttpPostedFileBase upload, string imagePath) { _logger.Info(" upload.FileName = " + upload.FileName); var ext = System.IO.Path.GetExtension(upload.FileName); if (_fileContext.IsAllowedExtension(ext)) { _logger.Info(" import started..."); List <Drink> drinks = null; try { drinks = _fileContext.Import(upload.InputStream); } catch (Exception e) { _logger.Error(" Неправильная структура файла!"); ExceptionWriter.WriteErrorDetailed(_logger, e); } _logger.Info(" import ended..."); if (drinks != null) { foreach (var drink in drinks) { //Если по каким-то причинам при импорте не нашлась картинка по указанному пути берем дефолтную string path = GetPathDefaultImg(imagePath); try { //Сохраняем файл в хранилище path = SaveFile(drink.ImgPath, imagePath); } catch (Exception e) { _logger.Error(" Указан неверный путь к файлу!"); ExceptionWriter.WriteErrorDetailed(_logger, e); } DrinkEntity drinkEntity = new DrinkEntity { Id = Guid.NewGuid(), ImagePath = path, Name = drink.Name, Count = drink.Count, CostPrice = drink.Cost }; _printer.DrinkInfo(drinkEntity); _drinkRepository.Add(drinkEntity); } _logger.Info(" before _db.SaveChanges()..."); _drinkRepository.SaveChanges(); } else { return(" Пустые данные!"); } } else { return(" Файл имеет неверный формат!"); } return(""); }
/// <summary> /// Изменение имени /// </summary> /// <param name="drinkEntity"></param> /// <param name="name"></param> public void ChangeName(DrinkEntity drinkEntity, string name) { drinkEntity.Name = name; }
/// <summary> /// Изменение картинки /// </summary> /// <param name="drinkEntity"></param> /// <param name="path"></param> public void ChangeImage(DrinkEntity drinkEntity, string path) { drinkEntity.ImagePath = path; }
/// <summary> /// Изменение стоимости /// </summary> /// <param name="drinkEntity"></param> /// <param name="cost"></param> public void ChangeCost(DrinkEntity drinkEntity, int cost) { drinkEntity.CostPrice = cost; }
/// <summary> /// Изменение кол-ва /// </summary> /// <param name="drinkEntity"></param> /// <param name="count"></param> public void ChangeCount(DrinkEntity drinkEntity, int count) { drinkEntity.Count = count; }
/// <summary> /// Проверка на возможность покупки /// Нельзя купит если напитка нет или кол-во внесенных средст меньше стоимости напитка /// </summary> /// <param name="drink"></param> /// <param name="currentState"></param> /// <returns></returns> public bool IsCanBuy(DrinkEntity drink, CurrentStateEntity currentState) { return(drink.Count > 0 && currentState.Deposit >= drink.CostPrice); }