Aplikasi Currency Converter dengan API
Aplikasi yang dikembangkan merupakan pengembangan aplikasi dari Tugas 2, dengan menambahkan fitur API sehingga menghasilkan konversi mata uang yang lebih dinamis dan
dalam penggunaanya.
Pada program tersebut, kami menggunakan sebanyak 10 jenis mata uang yang dapat digunakan dan ditambahkan kedepannya.
Algoritma pada program yang digunakan terdiri dari:
- Membuat currency parameter dari mata uang bersangkutan ke nilai tukar Euro berdasarkan European Central Bank
- Membuat dan mengatur XML yang didapat dari parameter tersebut
- Melakukan loop mulalui semua data XML
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Globalization; namespace WindowsFormsTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Converter() { float input = (float)this.numericUpDown1.Value; float output = GetExchangeRate((string)this.comboBox1.SelectedItem, (string)this.comboBox2.SelectedItem, input); this.textBox1.Text = output.ToString("0.##"); } private void Form1_Load(object sender, EventArgs e) { GetCurrencyTags(); this.comboBox1.SelectedItem = "IDR"; this.comboBox2.SelectedItem = "USD"; } private void label1_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void label4_Click(object sender, EventArgs e) { } private void label3_Click_1(object sender, EventArgs e) { } private void button1_Click_1(object sender, EventArgs e) { Converter(); } private string[] GetCurrencyTags() { // Hardcoded currency tags to parse the ecb xml's return new string[] {"eur", "usd", "jpy", "idr", "aud", "cad", "jpy", "sgd", "php", "myr", "cny"}; } private float GetCurrencyRate(string currency) { if (currency.ToLower() == "") throw new ArgumentException("Invalid Argument! currency parameter cannot be empty!"); try { // Create with currency parameter, a valid RSS url to euro exchange rate feed 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 { } } // currency not parsed!! // return default value return 0; } catch { // currency not parsed!! // return default value return 0; } } private float GetExchangeRate(string from, string to, float amount) { // If currency's are empty abort if (from == null || to == null) return 0; try { // First Get the exchange rate of both currencies in app standard float toRate = GetCurrencyRate(to); float fromRate = GetCurrencyRate(from); // Convert Same Exchange Rate if (from.ToLower() == to.ToLower()) { return amount; } else { // Calculate exchange rates From A to B return (amount * toRate) / fromRate; } } catch { return 0; } } } }
Berikut Hasil program:
Comments