Tugas 3 PBKK - Membuat Currency Converter Dynamic dan Kalkulator menggunakan .NET Framework

1. Membuat Aplikasi Kalkulator Menggunakan .NET Framework

Source Code : Github/VieriFath/CurrencyConverterApp


1.1.  Buka Microsoft Visual Studio

1.2.  Pilih Create a New Project



1.3.  Configure Your New Project : Atur project name dan file location. Setelah itu, klik "Create"



1.4. Pada "Form1.cs [Design]", tambahkan sebuah text box dan beberapa button untuk membentuk sebuah currency converter. Seperti dibawah ini :





1.5. Tambahkan baris kode seperti yang tertera di bawah ini pada file "Form1.cs"

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 CurrencyConverterApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void convert_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void convert_Click_1(object sender, EventArgs e)
        {
            Convert();
        }

        private void Convert()
        {
            var value1 = double.Parse(this.textBox1.Text);
            double value2 = value1;
            string from = this.comboBox2.SelectedItem.ToString().ToLower();
            string to = this.comboBox1.SelectedItem.ToString().ToLower();
            if (from == "eur")
            {
                value2 = value1 * GetCurrencyRateInEuro(to);
            }
            if (to == "eur")
            {
                value2 = value1 / GetCurrencyRateInEuro(from);
            }

            float toRate = GetCurrencyRateInEuro(to);
            float fromRate = GetCurrencyRateInEuro(from);

            // Calculate exchange rate From A to B
            value2 = (value1 * toRate) / fromRate;

            if (from == to) value2 = value1;

            this.label7.Text = Math.Round(value2, 6) + "";

        }

        public static string[] GetCurrencyTags()
        {

            // Hardcoded currency tags neccesairy to parse the ecb xml's
            return new string[] {"eur", "usd", "jpy", "bgn", "czk", "dkk", "gbp", "huf", "ltl", "lvl"
            , "pln", "ron", "sek", "chf", "nok", "hrk", "rub", "try", "aud", "brl", "cad", "cny", "hkd", "idr", "ils"
            , "inr", "krw", "mxn", "myr", "nzd", "php", "sgd", "zar"};
        }


        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 with currency parameter, a valid RSS url to ECB 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;
            }
        }


        public static float GetExchangeRate(string from, string to, float amount = 1)
        {
            // If currency's are empty abort
            if (from == null || to == null)
                return 0;

            // Convert Euro to Euro
            if (from.ToLower() == "eur" && to.ToLower() == "eur")
                return amount;

            try
            {
                // First Get the exchange rate of both currencies in euro
                float toRate = GetCurrencyRateInEuro(to);
                float fromRate = GetCurrencyRateInEuro(from);

                // Convert Between Euro to Other Currency
                if (from.ToLower() == "eur")
                {
                    return (amount * toRate);
                }
                else if (to.ToLower() == "eur")
                {
                    return (amount / fromRate);
                }
                else
                {
                    // Calculate non EURO exchange rates From A to B
                    return (amount * toRate) / fromRate;
                }
            }
            catch { return 0; }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}


1.6. Jalankan program dengan klik tombol "Start without Debugging" pada menu "Debug" atau dengan menggunakan shortcut Ctrl+F5.

  • Berikut ini aplikasi Currency Converter yang telah saya buat :




2. Membuat Aplikasi Kalkulator Menggunakan .NET Framework

Source Code : Github/VieriFath/CalculatorApp


2.1.  Buka Microsoft Visual Studio

2.2.  Pilih Create a New Project



2.3.  Configure Your New Project : Atur project name dan file location. Setelah itu, klik "Create"



2.4. Pada "CalculatorApp.cs [Design]", tambahkan sebuah text box dan beberapa button untuk membentuk sebuah calculator. Seperti dibawah ini :



2.5. Tambahkan baris kode seperti yang tertera di bawah ini pada file "CalculatorApp.cs"

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;

namespace CalculatorApp
{
    public partial class CalculatorApp : Form
    {
        Double value = 0;
        String operation = "";
        bool operation_pressed = false;
        public CalculatorApp()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void CalculatorApp_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void C_Click(object sender, EventArgs e)
        {

        }

        private void button_Click(object sender, EventArgs e)
        {
            if ((result.Text == "0")||(operation_pressed))
                result.Clear();
            operation_pressed = false;
            Button b = (Button)sender;
            result.Text = result.Text + b.Text;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            result.Text = "0";
        }

        private void Operator_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operation = b.Text;
            value = Double.Parse(result.Text);
            operation_pressed = true;
            equation.Text = value + " " + operation;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            equation.Text = "";
            switch (operation)
            {
                case "+":
                    result.Text = (value + Double.Parse(result.Text)).ToString();
                    break;
                case "-":
                    result.Text = (value - Double.Parse(result.Text)).ToString();
                    break;
                case "*":
                    result.Text = (value * Double.Parse(result.Text)).ToString();
                    break;
                case "/":
                    result.Text = (value / Double.Parse(result.Text)).ToString();
                    break;
                default:
                    break;
             
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            result.Text = "0";
            value = 0;
        }
    }
}

2.6. Jalankan program dengan klik tombol "Start without Debugging" pada menu "Debug" atau dengan menggunakan shortcut Ctrl+F5.

  • Berikut ini aplikasi kalkulator yang telah saya buat :










Comments

Popular posts from this blog

TUGAS 7 PBKK - Membuat CRUD menggunakan CodeIgniter 3

EAS PBKK B - PERABOTANKU - Aplikasi Toko Online dengan Codeigniter

Tugas 2 PBKK - Aplikasi .NET Framework