Calculator Program and Currency Converter Program
Sitti Chofifah - 05111840000039 - PBKK B
Pada kesempatan kali ini, saya akan membuat calculator program menggunakan WinForm dan currency converter program dengan menggunakan API dari Currency Converter API.
- Calculator Program
Calculator Program merupakan aplikasi kalkulator sederhana yang dapat melakukan berbagai operasi matematika seperti pertambahan, pengurangan, perkalian dan pembagian dengan cara melakukan input 2 buah angka dan memilih 1 operasi matematika. Berikut code untuk calculator program
Calculator Design Code :
namespace Kalkulator {
partial class Kalkulator {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.inputValue1 = new System.Windows.Forms.NumericUpDown();
this.inputValue2 = new System.Windows.Forms.NumericUpDown();
this.labelValue1 = new System.Windows.Forms.Label();
this.labelValue2 = new System.Windows.Forms.Label();
this.outputResult = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.plusButton = new System.Windows.Forms.Button();
this.minusButton = new System.Windows.Forms.Button();
this.mulButton = new System.Windows.Forms.Button();
this.divButton = new System.Windows.Forms.Button();
this.clearButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.inputValue1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.inputValue2)).BeginInit();
this.SuspendLayout();
//
// inputValue1
//
this.inputValue1.Location = new System.Drawing.Point(76, 15);
this.inputValue1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.inputValue1.Name = "inputValue1";
this.inputValue1.Size = new System.Drawing.Size(151, 22);
this.inputValue1.TabIndex = 0;
//
// inputValue2
//
this.inputValue2.Location = new System.Drawing.Point(76, 47);
this.inputValue2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.inputValue2.Name = "inputValue2";
this.inputValue2.Size = new System.Drawing.Size(151, 22);
this.inputValue2.TabIndex = 1;
//
// labelValue1
//
this.labelValue1.AutoSize = true;
this.labelValue1.Location = new System.Drawing.Point(12, 17);
this.labelValue1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labelValue1.Name = "labelValue1";
this.labelValue1.Size = new System.Drawing.Size(54, 17);
this.labelValue1.TabIndex = 2;
this.labelValue1.Text = "value 1";
//
// labelValue2
//
this.labelValue2.AutoSize = true;
this.labelValue2.Location = new System.Drawing.Point(12, 49);
this.labelValue2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labelValue2.Name = "labelValue2";
this.labelValue2.Size = new System.Drawing.Size(54, 17);
this.labelValue2.TabIndex = 3;
this.labelValue2.Text = "value 2";
//
// outputResult
//
this.outputResult.Location = new System.Drawing.Point(67, 79);
this.outputResult.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.outputResult.Name = "outputResult";
this.outputResult.ReadOnly = true;
this.outputResult.Size = new System.Drawing.Size(159, 22);
this.outputResult.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 82);
this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(43, 17);
this.label3.TabIndex = 5;
this.label3.Text = "result";
//
// plusButton
//
this.plusButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.plusButton.Location = new System.Drawing.Point(16, 111);
this.plusButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.plusButton.Name = "plusButton";
this.plusButton.Size = new System.Drawing.Size(100, 85);
this.plusButton.TabIndex = 6;
this.plusButton.Text = "+";
this.plusButton.UseVisualStyleBackColor = true;
//
// minusButton
//
this.minusButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.minusButton.Location = new System.Drawing.Point(124, 111);
this.minusButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.minusButton.Name = "minusButton";
this.minusButton.Size = new System.Drawing.Size(100, 85);
this.minusButton.TabIndex = 7;
this.minusButton.Text = "-";
this.minusButton.UseVisualStyleBackColor = true;
//
// mulButton
//
this.mulButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.mulButton.Location = new System.Drawing.Point(16, 203);
this.mulButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.mulButton.Name = "mulButton";
this.mulButton.Size = new System.Drawing.Size(100, 85);
this.mulButton.TabIndex = 8;
this.mulButton.Text = "x";
this.mulButton.UseVisualStyleBackColor = true;
//
// divButton
//
this.divButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.divButton.Location = new System.Drawing.Point(124, 203);
this.divButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.divButton.Name = "divButton";
this.divButton.Size = new System.Drawing.Size(100, 85);
this.divButton.TabIndex = 9;
this.divButton.Text = "/";
this.divButton.UseVisualStyleBackColor = true;
//
// clearButton
//
this.clearButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.clearButton.Location = new System.Drawing.Point(16, 295);
this.clearButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.clearButton.Name = "clearButton";
this.clearButton.Size = new System.Drawing.Size(208, 57);
this.clearButton.TabIndex = 10;
this.clearButton.Text = "c";
this.clearButton.UseVisualStyleBackColor = true;
//
// Kalkulator
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(248, 367);
this.Controls.Add(this.clearButton);
this.Controls.Add(this.divButton);
this.Controls.Add(this.mulButton);
this.Controls.Add(this.minusButton);
this.Controls.Add(this.plusButton);
this.Controls.Add(this.label3);
this.Controls.Add(this.outputResult);
this.Controls.Add(this.labelValue2);
this.Controls.Add(this.labelValue1);
this.Controls.Add(this.inputValue2);
this.Controls.Add(this.inputValue1);
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Kalkulator";
this.Text = "Kalkulator";
((System.ComponentModel.ISupportInitialize)(this.inputValue1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.inputValue2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.NumericUpDown inputValue1;
private System.Windows.Forms.NumericUpDown inputValue2;
private System.Windows.Forms.Label labelValue1;
private System.Windows.Forms.Label labelValue2;
private System.Windows.Forms.TextBox outputResult;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button plusButton;
private System.Windows.Forms.Button minusButton;
private System.Windows.Forms.Button mulButton;
private System.Windows.Forms.Button divButton;
private System.Windows.Forms.Button clearButton;
}
}
Calculator Code :
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 Kalkulator { public partial class Kalkulator : Form { public Kalkulator() { InitializeComponent(); outputResult.Text = "0"; InitializeButtonEvents(); } private void Plus(object sender, EventArgs e) { outputResult.Text = "" + (inputValue1.Value + inputValue2.Value); } private void Minus(object sender, EventArgs e) { outputResult.Text = "" + (inputValue1.Value - inputValue2.Value); } private void Multiply(object sender, EventArgs e) { outputResult.Text = "" + (inputValue1.Value * inputValue2.Value); } private void Division(object sender, EventArgs e) { outputResult.Text = "" + (inputValue1.Value / inputValue2.Value); } private void Clear(object sender, EventArgs e) { inputValue1.Value = 0; inputValue2.Value = 0; outputResult.Text = "0"; } private void InitializeButtonEvents() { plusButton.Click += Plus; minusButton.Click += Minus; mulButton.Click += Multiply; divButton.Click += Division; clearButton.Click += Clear; } } }
Berikut hasil dari calculator program
- Currency Converter Program
Program code :
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace Currency_Conversion_App { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Currency List code :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace Currency_Conversion_App { class CurrencyList { public Dictionary<string, CurrencyData> results; public CurrencyData getCurrencyByID(string id) { return results[id]; } public CurrencyData GetCurrencyByIndex(int index) { return results.ElementAt(index).Value; } public CurrencyData[] ToArray() { return results.Values.ToArray(); } public static CurrencyList Deserialize(string data) { return JsonConvert.DeserializeObject<CurrencyList>(data); } } class CurrencyData { public string currencyName; public string currencySymbol; public string id; } }
Currency code :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Currency_Conversion_App { class Currency { public string name { get; } private float compareUnit = -1; public float CompareUnit { get { if(compareUnit == -1) { APIRequester currencyRequest = new APIRequester(string.Format("https://free.currconv.com/api/v7/convert?apiKey=acb1dfa8644dc094dc15&q={0}_{1}&compact=y", name, "USD")); ConversionData conversionData = ConversionData.Deserialize(currencyRequest.SendAndGetResponse()); this.compareUnit = 1/(float)conversionData.val; } return compareUnit; } } public Currency(string name) { this.name = name; } public Currency(string name, float compareUnit) { this.name = name; this.compareUnit = compareUnit; } public float ConvertTo(float money, Currency currency) { return money / CompareUnit * currency.CompareUnit; } } }
Currency Conversion code :
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 Currency_Conversion_App { public partial class Form1 : Form { private List<Currency> currencies; public Form1() { InitializeComponent(); InitializeCurrency(); SetUpComponent(); Convert(); } private void InitializeCurrency() { APIRequester currencyListRequest = new APIRequester("https://free.currconv.com/api/v7/currencies?apiKey=acb1dfa8644dc094dc15"); CurrencyList currencyList = CurrencyList.Deserialize(currencyListRequest.SendAndGetResponse()); currencies = new List<Currency>(); CurrencyData[] datas = currencyList.ToArray(); foreach(CurrencyData currency in datas) { CreateNewCurrency(currency.id); ; } } private void CreateNewCurrency(string name) { currencies.Add(new Currency(name)); } private void SetUpComponent() { foreach(Currency currency in currencies) { inputCurrency.Items.Add(currency.name); outputCurrency.Items.Add(currency.name); } inputMoney.Minimum = 0; inputMoney.Maximum = decimal.MaxValue; inputMoney.DecimalPlaces = 2; inputCurrency.SelectedIndex = 0; outputCurrency.SelectedIndex = 0; convertButton.Click += ConvertButtonClick; } private void ConvertButtonClick(object sender, EventArgs arg) { Convert(); } private Currency GetCurrencyByName(string name) { for(int i = 0; i < currencies.Count; i++) { if(currencies[i].name == name) { return currencies[i]; } } return null; } private void Convert() { float money = (float)inputMoney.Value; Currency inCurrency = GetCurrencyByName(inputCurrency.Text); Currency outCurrency = GetCurrencyByName(outputCurrency.Text); float output = inCurrency.ConvertTo(money, outCurrency); outputMoney.Text = "" + output; } } }
Conversion Data code :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace Currency_Conversion_App { class ConversionData { public decimal val; public static ConversionData Deserialize(string data) { return JsonConvert.DeserializeObject<Dictionary<String, ConversionData>>(data).ElementAt(0).Value; } } }
API Request code :
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Currency_Conversion_App { class APIRequester { private string url; private WebClient client; public APIRequester(string url) { this.url = url; this.client = new WebClient(); } public string SendAndGetResponse() { return client.DownloadString(url); } } }
Berikut hasil dari Currency Converter Program
Comments
Post a Comment