Hey my question is less about a problem and more about knowing if there is a more efficient way of implementing my program(a currency converter) I am new at C# so I have used what I know to make this code bu as you can see if the converter had to cover more things the code would get ridiculously long so I was wondering is there any better/ more efficient way of doing this?(Sorry there is no pic of the form i dont have high enough rep to add one)
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 _158._212_assignment_2
{
public partial class Form1 : Form
{
private double amountToConvert = 0;
private string convertingTo = "";
private string convertingFrom = "";
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void NZDOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: NZD";
convertingFrom = "NZ";
}
private void AUDOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: AUD";
convertingFrom = "AU";
}
private void EUROC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: EUD";
convertingFrom = "EU";
}
private void GBPOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: GBP";
convertingFrom = "GB";
}
private void CADOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: CAD";
convertingFrom = "CA";
}
private void USDOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: USD";
convertingFrom = "US";
}
//Buttons for the currency you are converting to
private void NZDCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: NZD";
convertingTo = "NZD";
}
private void AUDCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: AUD";
convertingTo = "AUD";
}
private void EURCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: EUR";
convertingTo = "EUR";
}
private void GBPCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: GBP";
convertingTo = "GBP";
}
private void CADCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: CAD";
convertingTo = "CAD";
}
private void USDCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: USD";
convertingTo = "USD";
}
private void Convert_Click(object sender, EventArgs e)
{
double check;
string Amount = currencyInput.Text;
bool result = double.TryParse(Amount, out check);//checks if user input is a integer
if (result)//if input is a integer the code proceeds
{
inputWarning.Text = ("");//removes previous error message if it was triggered
if (convertingFrom == "NZ")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 1.36;
}
else if (convertingFrom == "AU")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 1.31;
}
else if (convertingFrom == "GB")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 0.68;
}
else if (convertingFrom == "EU")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 0.95;
}
else if (convertingFrom == "CA")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 1.28;
}
else if (convertingFrom == "US")
{
amountToConvert = double.Parse(Amount);
}
else
{
convertFromWarning.Text = "Please select the currency you are converting from";
}
if (convertingTo == "USD")
{
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "CAD")
{
amountToConvert = amountToConvert * 1.28;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "NZD")
{
amountToConvert = amountToConvert * 1.36;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "AUD")
{
amountToConvert = amountToConvert * 1.31;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "GBP")
{
amountToConvert = amountToConvert * 0.68;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "EUR")
{
amountToConvert = amountToConvert * 0.95;
output.Text = amountToConvert.ToString("F2");
}
else
{
convertToWarning.Text = "Please select the currency to convert to";
}
}
else
{
inputWarning.Text = " Please enter a valid amount";
}
}
private void Reset_Click(object sender, EventArgs e)
{
amountToConvert = 0;
convertingTo = "";
convertingFrom = "";
CCDisplay.Text = "Converting to:";
OCDisplay.Text = "Converting from:";
output.Text = ("");
currencyInput.Text = ("");
inputWarning.Text = ("");
convertToWarning.Text = ("");
convertFromWarning.Text = ("");
}
}
}
Aucun commentaire:
Enregistrer un commentaire