Skip to content

Instantly share code, notes, and snippets.

@azidanit
Created March 29, 2021 10:03
Show Gist options
  • Save azidanit/1407b905b7566cce98fca23918c78b43 to your computer and use it in GitHub Desktop.
Save azidanit/1407b905b7566cce98fca23918c78b43 to your computer and use it in GitHub Desktop.
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.Diagnostics;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CurrencyExchange
{
public partial class Form1 : Form
{
private ApiConn api_connection;
private JToken list_cur;
public Form1()
{
InitializeComponent();
api_connection = new ApiConn();
list_cur = api_connection.getAllCurrency();
addCurrencyToComboBox();
resetUI();
}
private void resetUI()
{
this.curr1_label.Text = "";
this.curr2_label.Text = "";
this.curr2_name_label.Text = "";
}
private void addCurrencyToComboBox()
{
foreach (var cur_item in list_cur)
{
foreach (var curr_detail_item in cur_item)
{
string curr_name = curr_detail_item["id"] + " - " + curr_detail_item["currencyName"];
this.curr1_comboBox.Items.Add(curr_name);
this.curr2_comboBox.Items.Add(curr_name);
//Debug.WriteLine(curr_name);
}
}
}
private void curr2_label_Click(object sender, EventArgs e)
{
Console.WriteLine("Api conn created");
}
private void konversi_button_Click(object sender, EventArgs e)
{
if (this.curr1_comboBox.SelectedItem == null || this.curr2_comboBox.SelectedItem == null)
{
Debug.WriteLine("BELUM PILIH");
return;
}
string from = this.curr1_comboBox.SelectedItem.ToString().Substring(0, 3);
string to = this.curr2_comboBox.SelectedItem.ToString().Substring(0,3);
double rate = api_connection.getCurrencyRate(from, to);
double conv_res = Double.Parse(input_curr_textbox.Text) * rate;
Debug.WriteLine("HASIL ", conv_res);
this.curr1_label.Text = Double.Parse(input_curr_textbox.Text).ToString("C3", CultureInfo.CurrentCulture).Substring(1) + " " + curr1_comboBox.SelectedItem.ToString().Substring(6) + " =";
this.curr2_label.Text = conv_res.ToString("C3", CultureInfo.CurrentCulture).Substring(1);
this.curr2_name_label.Text = curr2_comboBox.SelectedItem.ToString().Substring(6) + "s";
this.curr_rate.Text = "1 " + curr1_comboBox.SelectedItem.ToString().Substring(0, 3) + " = " + rate.ToString("C5", CultureInfo.CurrentCulture).Substring(1) + " "
+ curr2_comboBox.SelectedItem.ToString().Substring(0, 3);
}
private void curr1_comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.symbol_curr_label.Text = list_cur[this.curr1_comboBox.SelectedItem.ToString().Substring(0, 3)]["currencySymbol"].ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment