TUGAS 3 - Membuat Aplikasi Currency Converter Online Menggunakan API JSON
Achmad Zidan Akbar
05111840000005
1. Buat akun pada web penyedia Currency API
daftar pada alamat berikut : https://free.currencyconverterapi.com/
sehingga mendapat api key yang selanjutnya akan digunakan.
2. Buat New Project
Ubah Nama project menjadi CurrencyExchange
3. Desain Antarmuka.
Menggunakan widget yang telah tersedia pada toolbox.
4. Code
Terdapat kelas yang dibuat untuk mengakomodir Koneksi API. yaitu ApiConn.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Diagnostics; | |
using System.Net; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace CurrencyExchange | |
{ | |
class ApiConn | |
{ | |
private string all_curr_url = "https://free.currconv.com/api/v7/currencies?apiKey=a0cc285385352baf9649"; | |
IDictionary<string, string> numberNames = new Dictionary<string, string>(); | |
//<id,currencySymbol | |
public ApiConn() | |
{ | |
Debug.WriteLine("Api conn created"); | |
getAllCurrency(); | |
getCurrencyRate("USD", "PHP"); | |
} | |
public JToken getAllCurrency() | |
{ | |
string json_str = new WebClient().DownloadString(all_curr_url); | |
Debug.WriteLine(json_str); | |
JObject o = JObject.Parse(json_str); | |
Debug.WriteLine(o["results"]["ALL"]); | |
JToken res_o = o["results"]; | |
string coba = "IDR - I Dont know"; | |
Debug.WriteLine(coba.Substring(0,3)); | |
Debug.WriteLine(res_o[coba.Substring(0, 3)]); | |
return res_o; | |
} | |
public Double getCurrencyRate(string from_, string to_) | |
{ | |
string url_api = "https://free.currconv.com/api/v7/convert?q=" + from_ + "_" + to_ + | |
"&compact=ultra&apiKey=a0cc285385352baf9649"; | |
string json_str = new WebClient().DownloadString(url_api); | |
var id_convert = from_ + "_" + to_; | |
JToken j_o = JToken.Parse(json_str); | |
Debug.WriteLine(j_o[id_convert]); | |
return j_o[id_convert].ToObject<double>(); | |
} | |
} | |
} |
Lalu untuk Form1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Comments
Post a Comment