This paste brought to you by Pastebin. View Raw

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Data;
  16. using System.Text.RegularExpressions;
  17. using System.Net.Http;
  18. using Newtonsoft.Json;
  19.  
  20. namespace DynamicCurrencyConverter
  21. {
  22.     public partial class MainWindow : Window
  23.     {
  24.         root val = new root();
  25.  
  26.         public class root
  27.         {
  28.             public Rate rates { get; set; }
  29.         }
  30.  
  31.         public class Rate
  32.         {
  33.             public double IDR { get; set; }
  34.             public double JPY { get; set; }
  35.             public double USD { get; set; }
  36.             public double KRW { get; set; }
  37.             public double HKD { get; set; }
  38.             public double EUR { get; set; }
  39.             public double KWD { get; set; }
  40.         }
  41.  
  42.         public MainWindow()
  43.         {
  44.             InitializeComponent();
  45.             GetValue();
  46.         }
  47.  
  48.         //Mendapatkan data dari API
  49.         private async void GetValue()
  50.         {
  51.             val = await GetDataGetMethod<root>("https://api.ratesapi.io/api/latest");
  52.             Currency();
  53.         }
  54.  
  55.         public static async Task<root> GetDataGetMethod<T>(string url)
  56.         {
  57.             var ss = new root();
  58.             try
  59.             {
  60.                 using (var client = new HttpClient())
  61.                 {
  62.                     client.Timeout = TimeSpan.FromMinutes(1);
  63.                     HttpResponseMessage response = await client.GetAsync(url);
  64.  
  65.                     if (response.StatusCode == System.Net.HttpStatusCode.OK)
  66.                     {
  67.                         var ResponceString = await response.Content.ReadAsStringAsync();
  68.                         var ResponceObject = JsonConvert.DeserializeObject<root>(ResponceString);
  69.  
  70.                         return ResponceObject;
  71.                     }
  72.                     return ss;
  73.                 }
  74.             }
  75.  
  76.             catch
  77.             {
  78.                 return ss;
  79.             }
  80.         }
  81.  
  82.         private void Currency()
  83.         {
  84.             //Membuat tabel untuk memasukkan mata uang dan nilai currency
  85.             DataTable dataCurrency = new DataTable();
  86.  
  87.             //Membuat kolom pada tabel
  88.             dataCurrency.Columns.Add("mataUang");
  89.             dataCurrency.Columns.Add("Nilai");
  90.  
  91.             //Menambahkan data pada baris pada tabel
  92.             dataCurrency.Rows.Add("IDR", val.rates.IDR);
  93.             dataCurrency.Rows.Add("JPY", val.rates.JPY);
  94.             dataCurrency.Rows.Add("USD", val.rates.USD);
  95.             dataCurrency.Rows.Add("KRW", val.rates.KRW);
  96.             dataCurrency.Rows.Add("HKD", val.rates.HKD);
  97.             dataCurrency.Rows.Add("EUR", val.rates.EUR);
  98.             dataCurrency.Rows.Add("KWD", val.rates.KWD);
  99.  
  100.             //Memasukkan data ke combo box From
  101.             comboFrom.ItemsSource = dataCurrency.DefaultView;
  102.             comboFrom.DisplayMemberPath = "mataUang";
  103.             comboFrom.SelectedValuePath = "Nilai";
  104.  
  105.             //Memasukkan data ke combox box To
  106.             comboTo.ItemsSource = dataCurrency.DefaultView;
  107.             comboTo.DisplayMemberPath = "mataUang";
  108.             comboTo.SelectedValuePath = "Nilai";
  109.         }
  110.  
  111.         //Tombol Convert
  112.         private void Button_Click(object sender, RoutedEventArgs e)
  113.         {
  114.             //Kasus jika box Amount masih kosong
  115.             if(textAmount.Text == null || textAmount.Text.Trim() == "")
  116.             {
  117.                 MessageBox.Show("Please enter the amount You want to convert", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
  118.                 textAmount.Focus();
  119.                 return;
  120.             }
  121.  
  122.             //Kasus jika combo box From masih kosong
  123.             else if (comboFrom.Text == null || comboFrom.Text.Trim() == "")
  124.             {
  125.                 MessageBox.Show("Please Choose The Currency You Want From", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
  126.                 comboFrom.Focus();
  127.                 return;
  128.             }
  129.  
  130.             //Kasus jika combo box To masih kosong
  131.             else if(comboTo.Text == null || comboTo.Text.Trim() == "")
  132.             {
  133.                 MessageBox.Show("Please Choose The Currency You Want To Convert To", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
  134.                 comboTo.Focus();
  135.                 return;
  136.             }
  137.  
  138.  
  139.             double ConvertedValue;
  140.  
  141.             //Kasus jika isi combo box From sama dengan isi combo box To
  142.             if(comboFrom.Text == comboTo.Text)
  143.             {
  144.                 ConvertedValue = double.Parse(textAmount.Text);
  145.                 textResult.Content = textAmount.Text + " " + comboFrom.Text + " " + " = " + ConvertedValue.ToString(".##") + " " + comboTo.Text;
  146.             }
  147.  
  148.             //Jika semua sudah terisi maka dihitung seperti cara bawah
  149.             else
  150.             {
  151.                 ConvertedValue = double.Parse(comboTo.SelectedValue.ToString()) / double.Parse(comboFrom.SelectedValue.ToString()) * double.Parse(textAmount.Text);
  152.                 textResult.Content = textAmount.Text + " " + comboFrom.Text + " " + " = " + ConvertedValue.ToString(".##") + " " + comboTo.Text;
  153.             }
  154.         }
  155.     }
  156. }