Exemplo n.º 1
0
        public void Initialize()
        {
            (coinSizes, coins, notes, users, atm, currencyDictionary) = Helpers
                                                                        .DatabaseHelper
                                                                        .CreateEntitiesArrays();

            var allDispensers = new Dictionary <CoinSize, bool>();

            foreach (var size in coinSizes)
            {
                allDispensers.Add(size, true);
            }

            emptyCurrenctyDictionary = new CurrencyDictionary()
            {
                CoinDictionary = new Dictionary <Coin, int>(),
                NoteDictionary = new Dictionary <Note, int>()
            };

            emptyAtm = new AutomatedTellerMachine()
            {
                CurrencyDictionary = emptyCurrenctyDictionary,
                Alias = "Empty ATM",
                CoinDispensersDictionary = allDispensers,
                HasNoteDispenser         = true
            };
        }
Exemplo n.º 2
0
        private static Dictionary <IMoney, int> GenerateValidMoneyDictionary(CurrencyDictionary dict)
        {
            var validMoney = dict
                             .MoneyDictionary
                             .Where(kvp => kvp.Value > 0)
                             .OrderByDescending(kvp => kvp.Key is Note)
                             .ToDictionary(kvp => kvp.Key as IMoney, kvp => kvp.Value);

            return(validMoney);
        }
Exemplo n.º 3
0
 public static Currency Get(CurrencyCodes currencyCode)
 {
     if (CurrencyDictionary.ContainsKey(currencyCode))
     {
         return(CurrencyDictionary[currencyCode]);
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 4
0
        public static string[] GenerateWithdrawDetails(CurrencyDictionary dict)
        {
            var noteInfo = dict
                           .NoteDictionary
                           .Select(kvp => $"{kvp.Value} notes of ${kvp.Key.Value}")
                           .ToArray();

            var coinInfo = dict
                           .CoinDictionary
                           .Select(kvp => $"{kvp.Value} coins of ${kvp.Key.Value} (size: {kvp.Key.Size.ToString()})")
                           .ToArray();

            var detailedInfo = new string[]
            {
                $"The money was given in the following fashion: ",
                $"Notes: {string.Join(", ", (noteInfo.Length > 0 ? noteInfo : new[] { "No notes were given" }))}",
                $"Coins: {string.Join(", ", (coinInfo.Length > 0 ? coinInfo : new[] { "No coins were given" }))}"
            };

            return(detailedInfo);
        }
Exemplo n.º 5
0
        public static CurrencyDictionary WithdrawFromAtm(User user, AutomatedTellerMachine atm, int amount, CoinifyWebContext context = null)
        {
            var curDict = atm.CurrencyDictionary;

            var ret = new CurrencyDictionary()
            {
                CoinDictionary = new Dictionary <Coin, int>(),
                NoteDictionary = new Dictionary <Note, int>()
            };

            if (user.Balance - amount < 0)
            {
                throw new InsufficentFundsException($"User {user.UserId} has insuficient funds to withdraw ${amount}");
            }

            user.Balance -= amount;

            var validMoney = GenerateValidMoneyDictionary(curDict);

            foreach (var kvp in validMoney.ToList())
            {
                while (validMoney[kvp.Key] != 0)
                {
                    if ((amount - kvp.Key.Value) < 0)
                    {
                        break;
                    }

                    amount -= kvp.Key.Value;
                    validMoney[kvp.Key]--;

                    if (kvp.Key is Note)
                    {
                        var note = kvp.Key as Note;

                        if (ret.NoteDictionary.ContainsKey(note))
                        {
                            ret.NoteDictionary[note]++;
                        }
                        else
                        {
                            ret.NoteDictionary[note] = 1;
                        }

                        curDict.NoteDictionary[note]--;
                    }
                    else if (kvp.Key is Coin)
                    {
                        var coin = kvp.Key as Coin;

                        if (ret.CoinDictionary.ContainsKey(coin))
                        {
                            ret.CoinDictionary[coin]++;
                        }
                        else
                        {
                            ret.CoinDictionary[coin] = 1;
                        }

                        curDict.CoinDictionary[coin]--;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (amount > 0)
            {
                throw new
                      InsufficientChangeException($"There is no change in ATM {atm.AutomatedTellerMachineId} to withdraw ${amount}");
            }

            if (context != null)
            {
                context.Update(user);
                context.Update(atm);

                context.SaveChanges();
            }

            return(ret);
        }
Exemplo n.º 6
0
 public static bool Exists(CurrencyCodes currencyCode)
 {
     return(CurrencyDictionary.ContainsKey(currencyCode));
 }
Exemplo n.º 7
0
        private async Task GetAllSymbols()
        {
            using (var client = new BinanceClient())
            {
                var result = await client.GetAllPricesAsync();

                if (result.Success)
                {
                    AllPrices = new ObservableCollection <BinanceSymbolViewModel>(result.Data.Select(r => new BinanceSymbolViewModel(r.Symbol, r.Price)).OrderBy(s => s.SymbolCurrency).ThenBy(s => s.SymbolAsset).ToList());

                    foreach (var price in AllPrices)
                    {
                        if (CurrencyDictionary.ContainsKey(price.SymbolCurrency))
                        {
                            if (!CurrencyDictionary[price.SymbolCurrency].Contains(price.SymbolAsset))
                            {
                                CurrencyDictionary[price.SymbolCurrency].Add(price.SymbolAsset);
                            }
                        }
                        else
                        {
                            CurrencyDictionary.Add(price.SymbolCurrency, new ObservableCollection <string> {
                                price.SymbolAsset
                            });
                        }

                        if (AssetDictionary.ContainsKey(price.SymbolAsset))
                        {
                            if (!AssetDictionary[price.SymbolAsset].Contains(price.SymbolCurrency))
                            {
                                AssetDictionary[price.SymbolAsset].Add(price.SymbolCurrency);
                            }
                        }
                        else
                        {
                            AssetDictionary.Add(price.SymbolAsset, new ObservableCollection <string> {
                                price.SymbolCurrency
                            });
                        }
                    }

                    var cdKeys = CurrencyDictionary.Keys.ToList();
                    var adKeys = AssetDictionary.Keys.ToList();
                    for (int k = 0; k < cdKeys.Count; k++)
                    {
                        CurrencyDictionary[cdKeys[k]] = new ObservableCollection <string>(CurrencyDictionary[cdKeys[k]].OrderBy(v => v));
                    }
                    for (int k = 0; k < adKeys.Count; k++)
                    {
                        AssetDictionary[adKeys[k]] = new ObservableCollection <string>(AssetDictionary[adKeys[k]].OrderBy(v => v));
                    }
                }
                else
                {
                    messageBoxService.ShowMessage($"Error getting all symbols data.\n{result.Error.Message}", $"Error Code: {result.Error.Code}", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            List <Task> tasks = new List <Task>();

            foreach (var symbol in AllPrices)
            {
                var task = new Task(() => binanceSocketClient.SubscribeToKlineStream(symbol.Symbol.ToLower(), KlineInterval.OneMinute, (data) =>
                {
                    symbol.Price = data.Data.Close;
                }));
                tasks.Add(task);
                task.Start();
            }
            Task.WaitAll(tasks.ToArray());
        }
Exemplo n.º 8
0
            ) CreateEntitiesArrays()
        {
            var coinSizes = new[]
            {
                new CoinSize()
                {
                    Size = 10
                },
                new CoinSize()
                {
                    Size = 20
                },
                new CoinSize()
                {
                    Size = 30
                },
                new CoinSize()
                {
                    Size = 40
                },
                new CoinSize()
                {
                    Size = 50
                },
            };

            var coins = new[]
            {
                new Coin()
                {
                    Value = 20,
                    Size  = coinSizes[3]
                },
                new Coin()
                {
                    Value = 10,
                    Size  = coinSizes[1]
                },
                new Coin()
                {
                    Value = 5,
                    Size  = coinSizes[4]
                },
                new Coin()
                {
                    Value = 2,
                    Size  = coinSizes[2]
                },
                new Coin()
                {
                    Value = 1,
                    Size  = coinSizes[0]
                },
            };

            var notes = new[]
            {
                new Note()
                {
                    Value = 1000
                },
                new Note()
                {
                    Value = 500
                },
                new Note()
                {
                    Value = 200
                },
                new Note()
                {
                    Value = 100
                },
                new Note()
                {
                    Value = 50
                },
            };

            var currencyDictionary = new CurrencyDictionary()
            {
                CoinDictionary = new System.Collections.Generic.Dictionary <Coin, int>()
                {
                    { coins[0], 100 },
                    { coins[1], 200 },
                    { coins[2], 300 },
                    { coins[3], 400 },
                    { coins[4], 500 },
                },
                NoteDictionary = new System.Collections.Generic.Dictionary <Note, int>()
                {
                    { notes[0], 100 },
                    { notes[1], 200 },
                    { notes[2], 300 },
                    { notes[3], 400 },
                    { notes[4], 500 },
                }
            };

            var atm = new AutomatedTellerMachine()
            {
                Alias = "AmsTerdaM",
                CurrencyDictionary       = currencyDictionary,
                HasNoteDispenser         = true,
                CoinDispensersDictionary = new System.Collections.Generic.Dictionary <CoinSize, bool>()
                {
                    { coinSizes[4], true },
                    { coinSizes[2], true },
                }
            };

            var users = new[]
            {
                new User()
                {
                    Name    = "Bill Gates",
                    Balance = 1000000000
                },
                new User()
                {
                    Name    = "Bruno",
                    Balance = 1000
                }
            };

            return(coinSizes, coins, notes, users, atm, currencyDictionary);
        }