Пример #1
0
        /// <summary>
        /// Queries https://api.exchangeratesapi.io for the latest rates of a base currency.
        /// </summary>
        /// <param name="baseCurrency">The base currency we wish to know exchange rates for.</param>
        /// <returns>Returns Task for the exchange rates, use '.Result' to get the actual Value.</returns>
        public static async Task <ExchangeRates> GetExchangeRates(string baseCurrency)
        {
            ExchangeRates rates = new ExchangeRates();

            try
            {
                HttpResponseMessage response = await client.GetAsync("https://api.exchangeratesapi.io/latest?base=" + baseCurrency);

                if (response.IsSuccessStatusCode)
                {
                    string data = await response.Content.ReadAsStringAsync();

                    rates.Base = data.Substring(data.IndexOf("base") + 7, data.IndexOf(',', data.IndexOf("base")) - data.IndexOf("base") - 8);
                    rates.Date = DateTime.Parse(data.Substring(data.IndexOf("date") + 7, data.IndexOf(',', data.IndexOf("date")) - data.IndexOf("date") - 8));
                    string[] ratePairs = data.Substring(data.IndexOf("rates") + 8).Split(',');
                    rates.Rates = new Dictionary <string, double>();
                    foreach (string ratePair in ratePairs)
                    {
                        string[] pair = ratePair.Split(':');
                        double   n    = new double();
                        if (double.TryParse(pair[1], NumberStyles.Any, CultureInfo.InvariantCulture, out n))
                        {
                            rates.Rates.Add(pair[0].Trim('"'), n);
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Klarte ikke å få kontakt med WebAPI.");
            }
            return(rates);
        }
Пример #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public Exchanger()
        {
            InitializeComponent();

            List <ComboboxItem> fromCurrencyComboboxItems = new List <ComboboxItem>();
            List <ComboboxItem> toCurrencyComboboxItems   = new List <ComboboxItem>();

            foreach (string acceptedCurrency in Program.AcceptedCurrencies)
            {
                fromCurrencyComboboxItems.Add(new ComboboxItem {
                    Text = acceptedCurrency + " - " + Program.CurrencyDictionary[acceptedCurrency], Value = acceptedCurrency
                });
                toCurrencyComboboxItems.Add(new ComboboxItem {
                    Text = acceptedCurrency + " - " + Program.CurrencyDictionary[acceptedCurrency], Value = acceptedCurrency
                });
            }
            fromCurrencyComboBox.DisplayMember = "Text";
            fromCurrencyComboBox.ValueMember   = "Value";
            fromCurrencyComboBox.DataSource    = fromCurrencyComboboxItems;
            toCurrencyComboBox.DisplayMember   = "Text";
            toCurrencyComboBox.ValueMember     = "Value";
            toCurrencyComboBox.DataSource      = toCurrencyComboboxItems;

            rates = new ExchangeRates();
        }
Пример #3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="amount">Amount of base currency converted.</param>
 /// <param name="from">Base currency converted from.</param>
 /// <param name="to">Currency converted to.</param>
 /// <param name="rates">Currency exchange rates of base currency.</param>
 public Receipt(double amount, string from, string to, ExchangeRates rates)
 {
     InitializeComponent();
     dateLabel2.Text        = rates.Date.ToShortDateString();
     amountLabel2.Text      = Math.Round(amount, 2, MidpointRounding.AwayFromZero).ToString() + " " + from;
     commisionLabel2.Text   = Math.Round(Program.CommisionRate * amount, 2, MidpointRounding.AwayFromZero).ToString() + " " + from;
     toPayLabel2.Text       = Math.Round((1 - Program.CommisionRate) * amount * rates.Rates[to], 2, MidpointRounding.AwayFromZero).ToString() + " " + to;
     exchangeRateLabel.Text = "1 " + from + " = " + rates.Rates[to].ToString() + " " + to;
 }
Пример #4
0
 /// <summary>
 /// Queries API for Exchange rates of a base currency.
 /// </summary>
 private void GetExchangeRateButton_OnClick(object sender, EventArgs e)
 {
     if (amountTextBox.Text == "")
     {
         MessageBox.Show("Vennligst angi et beløp.");
     }
     else if (double.TryParse(amountTextBox.Text, out amount))
     {
         from                    = fromCurrencyComboBox.SelectedValue.ToString();
         to                      = toCurrencyComboBox.SelectedValue.ToString();
         rates                   = Task.Run(() => Program.GetExchangeRates(from)).Result;
         toPayTextBox.Text       = Math.Round(amount * (1 - Program.CommisionRate) * rates.Rates[to], 2, MidpointRounding.AwayFromZero).ToString();
         toPayCurrencyLabel.Text = to;
     }
     else
     {
         MessageBox.Show("Vennligst sørg for at beløpet som er angitt er et tall.");
     }
 }