134 lines
4.5 KiB
C#
134 lines
4.5 KiB
C#
using Newtonsoft.Json;
|
|
using System.Collections.Generic;
|
|
|
|
namespace CurrencyConvert
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
|
|
private readonly HashSet<string> supportedCurrencies = new HashSet<string>
|
|
{
|
|
"AUD", "BGN", "BRL", "CAD", "CHF", "CNY", "CZK", "DKK",
|
|
"EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "ISK",
|
|
"JPY", "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PLN",
|
|
"RON", "SEK", "SGD", "THB", "TRY", "USD", "ZAR"
|
|
};
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
|
|
|
|
comboBox1.Items.Clear();
|
|
comboBox2.Items.Clear();
|
|
foreach (string currency in supportedCurrencies)
|
|
{
|
|
comboBox1.Items.Add(currency);
|
|
comboBox2.Items.Add(currency);
|
|
}
|
|
|
|
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("EUR");
|
|
comboBox2.SelectedIndex = comboBox2.Items.IndexOf("USD");
|
|
|
|
|
|
label4.AutoSize = true;
|
|
label4.Font = new Font(label4.Font.FontFamily, 12, FontStyle.Bold);
|
|
}
|
|
|
|
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (!char.IsDigit(e.KeyChar) && e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)Keys.Back)
|
|
{
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
private async void button1_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
|
|
label4.Text = "Berechne...";
|
|
button1.Enabled = false;
|
|
|
|
if (string.IsNullOrWhiteSpace(textBox1.Text))
|
|
{
|
|
MessageBox.Show("Bitte geben Sie einen Betrag ein.");
|
|
return;
|
|
}
|
|
|
|
|
|
string amount = textBox1.Text.Replace(',', '.');
|
|
if (!double.TryParse(amount, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out double menge))
|
|
{
|
|
MessageBox.Show("Bitte geben Sie einen gültigen Betrag ein.");
|
|
return;
|
|
}
|
|
|
|
if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
|
|
{
|
|
MessageBox.Show("Bitte wählen Sie beide Währungen aus.");
|
|
return;
|
|
}
|
|
|
|
string von = comboBox1.SelectedItem.ToString().Trim().ToUpper();
|
|
string zu = comboBox2.SelectedItem.ToString().Trim().ToUpper();
|
|
|
|
if (!supportedCurrencies.Contains(von) || !supportedCurrencies.Contains(zu))
|
|
{
|
|
MessageBox.Show($"Eine oder beide Währungen werden nicht unterstützt.\nUnterstützte Währungen: {string.Join(", ", supportedCurrencies)}");
|
|
return;
|
|
}
|
|
|
|
|
|
string url = $"https://api.frankfurter.app/latest?amount={amount}&from={von}&to={zu}";
|
|
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
var response = await client.GetAsync(url);
|
|
string json = await response.Content.ReadAsStringAsync();
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
MessageBox.Show("Fehler beim Abrufen der Wechselkurse.");
|
|
return;
|
|
}
|
|
|
|
var data = JsonConvert.DeserializeObject<FrankfurterResponse>(json);
|
|
|
|
if (data?.rates == null || !data.rates.ContainsKey(zu))
|
|
{
|
|
MessageBox.Show("Fehler beim Abrufen der Wechselkurse.");
|
|
return;
|
|
}
|
|
|
|
double ergebnis = data.rates[zu];
|
|
label4.Text = $"{menge} {von} = {ergebnis:0.00} {zu}";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show("Ein Fehler ist aufgetreten. Bitte versuchen Sie es später erneut.");
|
|
}
|
|
finally
|
|
{
|
|
button1.Enabled = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class FrankfurterResponse
|
|
{
|
|
public double amount { get; set; }
|
|
public string base_currency { get; set; }
|
|
public string date { get; set; }
|
|
public Dictionary<string, double> rates { get; set; }
|
|
}
|
|
}
|
|
|