public void SetMoney(MoneyCollection money)
 {
     lock (_lock)
     {
         _state.SetMoney(money);
     }
 }
예제 #2
0
        private void LoadNotes(MoneyCollection moneyCollection)
        {
            var noteLoader = new NoteRepository(_sqlConnectionProvider);

            foreach (var noteEntity in noteLoader.Load(moneyCollection.Currency))
            {
                moneyCollection.Notes.Add(noteEntity.Nominal, noteEntity.Count);
            }
        }
예제 #3
0
        public static MoneyCollection ToMoneyCollection(this MoneyInfo moneyInfo)
        {
            var moneyCollection = MoneyCollection.Create(CurrencyRegistry.GetCurrency(moneyInfo.Currency));

            moneyInfo.Coins.ForEach(coinInfo => moneyCollection.Coins.Add(coinInfo.Nominal, coinInfo.Count));
            moneyInfo.Notes.ForEach(noteInfo => moneyCollection.Notes.Add(noteInfo.Nominal, noteInfo.Count));

            return(moneyCollection);
        }
예제 #4
0
 private void EnsureMoneyLoaded(Currency currency)
 {
     if (!_totalMoney.ContainsKey(currency))
     {
         var money = MoneyCollection.Create(currency);
         LoadNotes(money);
         LoadCoins(money);
         _totalMoney.Add(currency, money);
     }
 }
예제 #5
0
        private void LoadCoins(MoneyCollection moneyCollection)
        {
            var coinLoader = new CoinRepository(_sqlConnectionProvider);

            foreach (var coinEntity in coinLoader.Load(moneyCollection.Currency))
            {
                Verifiers.Verify(coinEntity.Count >= 0, "Coins count is less than zero: {0}", coinEntity.Count);
                moneyCollection.Coins.Add(coinEntity.Nominal, coinEntity.Count);
            }
        }
예제 #6
0
        private bool TryExchange(MoneyCollection resultMoney)
        {
            ICountableCollection <int> exchangeResult;
            var greedyExchanger = new GreedyExchanger();

            if (greedyExchanger.TryExchange(new DecreasingIntegerCollectionMultiplier(Money.Notes, _currency.UnitFractions),
                                            _insertedCoins.Total, out exchangeResult))
            {
                resultMoney.Notes.Add(exchangeResult.Select(nominal => nominal / Money.Currency.UnitFractions));
                return(true);
            }

            resultMoney.Coins.Add(_insertedCoins);
            return(false);
        }
        private bool TryExchange(MoneyCollection resultMoney)
        {
            int valueForExchange = _insertedNotes.Total * _currency.UnitFractions;
            var money            = _cashRepository.LoadMoney(_currency);

            ICountableCollection <int> exchangeResult;
            var greedyExchanger = new GreedyExchanger();

            if (greedyExchanger.TryExchange(money.Coins, valueForExchange, out exchangeResult))
            {
                resultMoney.Coins.Add(exchangeResult);
                return(true);
            }

            resultMoney.Notes.Add(_insertedNotes);
            return(false);
        }
        public IExchangeResult Exchange()
        {
            var  returnedMoney = MoneyCollection.Create(_currency);
            bool success       = TryExchange(returnedMoney);

            if (success)
            {
                _cashRepository.RemoveMoney(returnedMoney);
            }

            _owner.ChangeState <FreshMachineState>(_cashRepository, _currency);

            return(new ExchangeResult
            {
                Success = success,
                Money = returnedMoney
            });
        }
예제 #9
0
        void OnTriggerEnter(Collider col)
        {
            //Don't do a getcomponent if it isn't on our layer.
            if (col.gameObject.layer == 8)
            {
                MoneyProjectile money = col.GetComponent <MoneyProjectile>();
                if (money != null && CanWeCollectThisMoney(money))
                {
                    MoneyCollection collection = new MoneyCollection();
                    collection.collisionLocation = MyCollider.ClosestPoint(col.transform.position);
                    collection.moneyCollide      = col;
                    collection.collectionBin     = transform;
                    collection.collectionFloor   = Floor.transform;
                    collection.dirToOutOfBin     = OutDirection;

                    CollectMoney(money, collection);
                }
            }
        }
 private HttpResponseMessage SetMoney(MoneyCollection money)
 {
     return(HttpClient.SendAsync(CreateRequest("api/cashmachine/money", HttpMethod.Post, MoneyInfo.CreateFrom(money))).Result);
 }
        private void Given_InitialMoney_And_MoneyInserted_Then_MakeExchangeRequest_Should_ReturnExpectedMoney(MoneyCollection initialMoney,
                                                                                                              MoneyCollection insertedMoney,
                                                                                                              HttpStatusCode expectedStatusCode,
                                                                                                              MoneyCollection expectedExchangeResult,
                                                                                                              MoneyCollection expectedMoneyAfterExchange)
        {
            SetMoney(initialMoney).AssertSuccess();

            insertedMoney.Notes.SelectMany(noteNominalCounPair =>
                                           Enumerable.Repeat(noteNominalCounPair.Key, noteNominalCounPair.Value))
            .ForEach(noteNominal => InsertNote(noteNominal).AssertSuccess());
            insertedMoney.Coins.SelectMany(coinNominalCounPair =>
                                           Enumerable.Repeat(coinNominalCounPair.Key, coinNominalCounPair.Value))
            .ForEach(coinNominal => InsertCoin(coinNominal).AssertSuccess());

            var actualExchangeResult = Exchange().AssertStatusCode(expectedStatusCode)
                                       .ExtractJson <MoneyInfo>()
                                       .ToMoneyCollection();

            Assert.IsTrue(MoneyCollectionEqualityComparer.IsEquals(expectedExchangeResult, actualExchangeResult));

            var actualMoneyAfterExchange = GetAvailableMoney().AssertSuccess()
                                           .ExtractJson <MoneyInfo>()
                                           .ToMoneyCollection();

            Assert.IsTrue(MoneyCollectionEqualityComparer.IsEquals(expectedMoneyAfterExchange, actualMoneyAfterExchange));
        }
예제 #12
0
 public void RemoveMoney(MoneyCollection money)
 {
     EnsureMoneyLoaded(money.Currency);
     RemoveNotes(money.Notes, money.Currency);
     RemoveCoins(money.Coins, money.Currency);
 }
 public void SetMoney(MoneyCollection money)
 {
     _cashRepository.SetMoney(money);
 }
예제 #14
0
 public void CollectMoney(MoneyProjectile money, MoneyCollection OutDirection)
 {
     money.StartCoroutine(money.CollectMoney(OutDirection));
 }
예제 #15
0
 public static MoneyInfo CreateFrom(MoneyCollection money)
 {
     return(new MoneyInfo(money.Currency.Name,
                          money.Notes.ToMonetaryAggregateInfoCollection(),
                          money.Coins.ToMonetaryAggregateInfoCollection()));
 }
예제 #16
0
 public void AddMoney(MoneyCollection money)
 {
     EnsureMoneyLoaded(money.Currency);
     AddNotes(money.Notes, money.Currency);
     AddCoins(money.Coins, money.Currency);
 }
 public void SetMoney(MoneyCollection money)
 {
     throw new InvalidOperationException("Failed to set available money: complete notes exchange");
 }
예제 #18
0
 public void SetMoney(MoneyCollection money)
 {
     RemoveMoney(LoadMoney(money.Currency));
     AddMoney(money);
 }
예제 #19
0
 public MoneyBuilder(Currency currency)
 {
     _money = MoneyCollection.Create(currency);
 }