示例#1
0
        public void fillDropDowns()
        {
            int             i            = 0;
            List <currency> currencyList = KP.getCurrencyList();
            currency        C            = null;
            String          option       = "";

            //no currencies - quit early
            if (currencyList.Count == 0)
            {
                return;
            }

            for (i = 0; i < currencyList.Count; i++)
            {
                C      = currencyList[i];
                option = C.getISO() + " - " + C.getName();

                LeftDropdown.Items.Add(option);
                RightDropdown.Items.Add(option);
            }

            //attempt to set default to USD - United States Dollar
            try
            {
                LeftDropdown.SelectedIndex  = 17;
                RightDropdown.SelectedIndex = 17;
            }
            catch (Exception E) { }

            return;
        }
示例#2
0
        public void insertAlphabetical(currency C)
        {
            int    i           = 0;
            int    j           = 0;
            int    length      = currencyList.Count;
            String insertName  = C.getISO().ToUpper();
            String compareName = "";

            //empty list, insert at front
            if (length == 0)
            {
                currencyList.Add(C);
                return;
            }

            //iterate through every element of our list
            for (i = 0; i < length; i++)
            {
                compareName = currencyList[i].getISO().ToUpper();

                //ignore duplicates
                if (insertName.Equals(compareName))
                {
                    return;
                }

                //insert comes before compare alphabetically, insert here
                if (insertName[0] < compareName[0])
                {
                    currencyList.Insert(i, C);
                    return;
                }
                //each one begins with the same letter
                else if (insertName[0] == compareName[0])
                {
                    //compare each character in Insertname and Comparename until we find a mismatching character
                    for (j = 0; j < insertName.Length && j < compareName.Length; j++)
                    {
                        //insertName comes before compareName alphabetically at index j
                        if (insertName[j] < compareName[j])
                        {
                            currencyList.Insert(i, C);
                            return;
                        }
                    }
                }
            }

            //exhausted list, insert at end
            currencyList.Insert(i, C);
            return;
        }
示例#3
0
        public currency lookupISO(String ISO)
        {
            int      i = 0;
            currency C = new currency("not found", "not found", 1);

            for (i = 0; i < currencyList.Count; i++)
            {
                if (currencyList[i].getISO().Equals(ISO))
                {
                    return(currencyList[i]);
                }
            }

            return(C);
        }
示例#4
0
        public double convert(String from, String to, double amount)
        {
            //grab new exchange rates every minute
            string currentTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm tt");

            //refresh rates if 60 seconds has passed
            if (!timestamp.Equals(currentTime))
            {
                updateRates();
            }

            //begin conversion
            currency fromCurrency = lookupISO(from);
            currency toCurrency   = lookupISO(to);
            double   result       = 0;

            result = amount / fromCurrency.getRate();
            result = result * toCurrency.getRate();

            return(result);
        }
示例#5
0
        public void getCryptoExchangeRates()
        {
            currency C            = null;
            double   exchangeRate = 0;
            String   ISO          = "";
            String   fullName     = "";
            String   rate         = "";
            String   url          = "https://min-api.cryptocompare.com/data/price?fsym=USD&tsyms=BTC,ETH,XRP,EOS,LTC,XLM,ADA,MIOTA,USDT,TRX,NEO";

            //call CryptoCompare
            WebClient wb       = new WebClient();
            var       response = wb.DownloadString(url);
            dynamic   obj      = JsonConvert.DeserializeObject(response);

            //parse json
            foreach (var temp in obj)
            {
                ISO  = temp.Name;
                rate = temp.Value;

                //attempt to parse exchange rate
                try
                {
                    exchangeRate = double.Parse(rate);
                }
                catch (Exception e) { exchangeRate = 1; }

                //check if the ISO is one that we care about
                fullName = lookupName(ISO);

                if (!fullName.Equals("ERROR"))
                {
                    //add a new currency to currencyList
                    C = new currency(ISO, fullName, exchangeRate);
                    insertAlphabetical(C);
                }
            }

            return;
        }
示例#6
0
        public void getRealExchangeRates()
        {
            currency C            = null;
            double   exchangeRate = 0;
            String   ISO          = "";
            String   fullName     = "";
            String   rate         = "";
            String   url          = "https://openexchangerates.org/api/latest.json?app_id=4d9a6a283a104a6fa454298522d751d7";


            //call OpenExchange
            WebClient wb       = new WebClient();
            var       response = wb.DownloadString(url);
            dynamic   obj      = JsonConvert.DeserializeObject(response);

            foreach (var temp in obj.rates)
            {
                ISO  = temp.Name;
                rate = temp.Value;

                //attempt to parse exchange rate
                try
                {
                    exchangeRate = double.Parse(rate);
                }
                catch (Exception e) { exchangeRate = 1; }

                //check if the ISO is one that we care about
                fullName = lookupName(ISO);

                if (!fullName.Equals("ERROR"))
                {
                    //add a new currency to currencyList
                    C = new currency(ISO, fullName, exchangeRate);
                    insertAlphabetical(C);
                }
            }
            return;
        }