public void SetMoney(MoneyCollection money)
 {
     lock (_lock)
     {
         _state.SetMoney(money);
     }
 }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
0
 private void EnsureMoneyLoaded(Currency currency)
 {
     if (!_totalMoney.ContainsKey(currency))
     {
         var money = MoneyCollection.Create(currency);
         LoadNotes(money);
         LoadCoins(money);
         _totalMoney.Add(currency, money);
     }
 }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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
            });
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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);
 }
Ejemplo n.º 14
0
 public void CollectMoney(MoneyProjectile money, MoneyCollection OutDirection)
 {
     money.StartCoroutine(money.CollectMoney(OutDirection));
 }
Ejemplo n.º 15
0
 public static MoneyInfo CreateFrom(MoneyCollection money)
 {
     return(new MoneyInfo(money.Currency.Name,
                          money.Notes.ToMonetaryAggregateInfoCollection(),
                          money.Coins.ToMonetaryAggregateInfoCollection()));
 }
Ejemplo n.º 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");
 }
Ejemplo n.º 18
0
 public void SetMoney(MoneyCollection money)
 {
     RemoveMoney(LoadMoney(money.Currency));
     AddMoney(money);
 }
Ejemplo n.º 19
0
 public MoneyBuilder(Currency currency)
 {
     _money = MoneyCollection.Create(currency);
 }