PBKK Tugas 3 - NET Applications Currency Converter

 

Farrel Muhammad Taqi

05111840000071

PBKK - B 


Application Currency Converter NET Framework

1. Atur User Interface

 


2. Dapatkan API Mata uang


3. Buat Kode

using System;
using System.Globalization;
using System.Windows.Forms;

namespace TugasPBKK
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fromList.Items.AddRange(GetCurrencyTags());
            toList.Items.AddRange(GetCurrencyTags());
        }

        public static string[] GetCurrencyTags()
        {
            //get from https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html
            return new string[] {
                "EUR", "USD", "JPY", "BGN", "CZK", "DKK", "GBP", "HUF", "PLN", "RON", "SEK", "CHF", "ISK", "NOK", "HRK", "RUB", "TRY", "AUD", "BRL", "CAD", "CNY", "HKD", "IDR", "ILS", "INR", "KRW", "MXN", "MYR", "NZD", "PHP", "SGD", "ZAR"
            };
        }

        // Get currency exchange rate in euro's
        public static float GetCurrencyRateInEuro(string currency)
        {
            if (currency.ToLower() == "")
                throw new ArgumentException("Invalid Argument! currency parameter cannot be empty!");
            if (currency.ToLower() == "eur")
                throw new ArgumentException("Invalid Argument! Cannot get exchange rate from EURO to EURO");

            try
            {
                // Create valid RSS url to european central bank
                string rssUrl = string.Concat("http://www.ecb.int/rss/fxref-", currency.ToLower() + ".html");

                // Create & Load New Xml Document
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(rssUrl);

                // Create XmlNamespaceManager for handling XML namespaces.
                System.Xml.XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("rdf", "http://purl.org/rss/1.0/");
                nsmgr.AddNamespace("cb", "http://www.cbwiki.net/wiki/index.php/Specification_1.1");

                // Get list of daily currency exchange rate between selected "currency" and the EURO
                System.Xml.XmlNodeList nodeList = doc.SelectNodes("//rdf:item", nsmgr);

                // Loop Through all XMLNODES with daily exchange rates
                foreach (System.Xml.XmlNode node in nodeList)
                {
                    // Create a CultureInfo, this is because EU and USA use different sepperators in float (, or .)
                    CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
                    ci.NumberFormat.CurrencyDecimalSeparator = ".";

                    try
                    {
                        // Get currency exchange rate with EURO from XMLNODE
                        float exchangeRate = float.Parse(
                            node.SelectSingleNode("//cb:statistics//cb:exchangeRate//cb:value", nsmgr).InnerText,
                            NumberStyles.Any,
                            ci);

                        return exchangeRate;
                    }
                    catch { }
                }
                return 0;
            }
            catch
            {
                // currency not parsed!!
                // return default value
                return 0;
            }
        }


        private void GetExchangeRate()
        {
            double inputNum = (double)(this.fromNumeric.Value);
            string in_currency = this.fromList.SelectedItem.ToString().ToLower();
            string out_currency = this.toList.SelectedItem.ToString().ToLower();
            double outnum = inputNum;

            // Convert Euro to Other Currency
            if (in_currency == "eur")
            {
                outnum = inputNum * GetCurrencyRateInEuro(out_currency);
            }

            // Convert Other Currency to Euro
            if (out_currency == "eur")
            {
                outnum = inputNum / GetCurrencyRateInEuro(in_currency);
            }

            // Get the exchange rate of both currencies in euro
            float toRate = GetCurrencyRateInEuro(out_currency);
            float fromRate = GetCurrencyRateInEuro(in_currency);

            // Calculate exchange rate From A to B
            outnum = (inputNum * toRate) / fromRate;

            if (in_currency == out_currency) outnum = inputNum;

            this.convertValue.Text = out_currency.ToUpper() + " " + Math.Round(outnum, 6);

        }

        private void fromText_Click(object sender, EventArgs e)
        {

        }

        private void fromBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void toBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void AppName_Click(object sender, EventArgs e)
        {

        }

        private void toText_Click(object sender, EventArgs e)
        {

        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            GetExchangeRate();
        }

        private void fromList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void LabelDate_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}


4. Eksekusi








Comments