- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Data;
- using System.Text.RegularExpressions;
- using System.Net.Http;
- using Newtonsoft.Json;
- namespace DynamicCurrencyConverter
- {
- public partial class MainWindow : Window
- {
- root val = new root();
- public class root
- {
- public Rate rates { get; set; }
- }
- public class Rate
- {
- public double IDR { get; set; }
- public double JPY { get; set; }
- public double USD { get; set; }
- public double KRW { get; set; }
- public double HKD { get; set; }
- public double EUR { get; set; }
- public double KWD { get; set; }
- }
- public MainWindow()
- {
- InitializeComponent();
- GetValue();
- }
- //Mendapatkan data dari API
- private async void GetValue()
- {
- val = await GetDataGetMethod<root>("https://api.ratesapi.io/api/latest");
- Currency();
- }
- public static async Task<root> GetDataGetMethod<T>(string url)
- {
- var ss = new root();
- try
- {
- using (var client = new HttpClient())
- {
- client.Timeout = TimeSpan.FromMinutes(1);
- HttpResponseMessage response = await client.GetAsync(url);
- if (response.StatusCode == System.Net.HttpStatusCode.OK)
- {
- var ResponceString = await response.Content.ReadAsStringAsync();
- var ResponceObject = JsonConvert.DeserializeObject<root>(ResponceString);
- return ResponceObject;
- }
- return ss;
- }
- }
- catch
- {
- return ss;
- }
- }
- private void Currency()
- {
- //Membuat tabel untuk memasukkan mata uang dan nilai currency
- DataTable dataCurrency = new DataTable();
- //Membuat kolom pada tabel
- dataCurrency.Columns.Add("mataUang");
- dataCurrency.Columns.Add("Nilai");
- //Menambahkan data pada baris pada tabel
- dataCurrency.Rows.Add("IDR", val.rates.IDR);
- dataCurrency.Rows.Add("JPY", val.rates.JPY);
- dataCurrency.Rows.Add("USD", val.rates.USD);
- dataCurrency.Rows.Add("KRW", val.rates.KRW);
- dataCurrency.Rows.Add("HKD", val.rates.HKD);
- dataCurrency.Rows.Add("EUR", val.rates.EUR);
- dataCurrency.Rows.Add("KWD", val.rates.KWD);
- //Memasukkan data ke combo box From
- comboFrom.ItemsSource = dataCurrency.DefaultView;
- comboFrom.DisplayMemberPath = "mataUang";
- comboFrom.SelectedValuePath = "Nilai";
- //Memasukkan data ke combox box To
- comboTo.ItemsSource = dataCurrency.DefaultView;
- comboTo.DisplayMemberPath = "mataUang";
- comboTo.SelectedValuePath = "Nilai";
- }
- //Tombol Convert
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- //Kasus jika box Amount masih kosong
- if(textAmount.Text == null || textAmount.Text.Trim() == "")
- {
- MessageBox.Show("Please enter the amount You want to convert", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
- textAmount.Focus();
- return;
- }
- //Kasus jika combo box From masih kosong
- else if (comboFrom.Text == null || comboFrom.Text.Trim() == "")
- {
- MessageBox.Show("Please Choose The Currency You Want From", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
- comboFrom.Focus();
- return;
- }
- //Kasus jika combo box To masih kosong
- else if(comboTo.Text == null || comboTo.Text.Trim() == "")
- {
- MessageBox.Show("Please Choose The Currency You Want To Convert To", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
- comboTo.Focus();
- return;
- }
- double ConvertedValue;
- //Kasus jika isi combo box From sama dengan isi combo box To
- if(comboFrom.Text == comboTo.Text)
- {
- ConvertedValue = double.Parse(textAmount.Text);
- textResult.Content = textAmount.Text + " " + comboFrom.Text + " " + " = " + ConvertedValue.ToString(".##") + " " + comboTo.Text;
- }
- //Jika semua sudah terisi maka dihitung seperti cara bawah
- else
- {
- ConvertedValue = double.Parse(comboTo.SelectedValue.ToString()) / double.Parse(comboFrom.SelectedValue.ToString()) * double.Parse(textAmount.Text);
- textResult.Content = textAmount.Text + " " + comboFrom.Text + " " + " = " + ConvertedValue.ToString(".##") + " " + comboTo.Text;
- }
- }
- }
- }