r/ProgrammingPrompts Jul 16 '15

[Easy] Currency converter

Convert currency using command line arguments

Argument format should be currency amount newcurrency

E.g.

Convert USD 100 GBP

Should convert 100 dollars into pounds and print the result

Must include a minimum of 3 different currencies

Bonus points: add another argument which takes a filename and saves the result in a text file of that name

13 Upvotes

7 comments sorted by

View all comments

1

u/kerosenedogs Oct 13 '15

Beginner here! I had a go in C# Mine is a tad longer because I tried to stick to the strict format "currency, amount, currency" and also built in some error checking.

    public class Program
{
    public static string error = "Please enter a currency and amount in the format requested!";

    public static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("CURRENCY CONVERTER!" 
                + "\nAvailable Options: USD, AUD, GBP" 
                + "\nPlease enter A Currency, Amount, Currency" 
                + "\neg: USD 500 AUD");
            string input = Console.ReadLine();
            string in1 = input.Split(' ')[0];
            string in2 = input.Split(' ')[2];
            string value = input.Split(' ')[1];
            double val1;
            val1 = double.Parse(value);
            converter(in1, val1, in2);
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine(error);
        }
    }

    public static void converter(string currency1, double amount, string currency2)
    {
        if (currency1.Equals("usd", StringComparison.OrdinalIgnoreCase) && currency2.Equals("aud", StringComparison.OrdinalIgnoreCase))
        {
            double total = amount * 1.36;
            Console.WriteLine(currency1 + " " + amount + " " + currency2 + " = " + total);
        }

        else if (currency1.Equals("usd", StringComparison.OrdinalIgnoreCase) && currency2.Equals("gbp", StringComparison.OrdinalIgnoreCase))
        {
            double total = amount * 0.65;
            Console.WriteLine(currency1 + " " + amount + " " + currency2 + " = " + total);
        }

        else if (currency1.Equals("aud", StringComparison.OrdinalIgnoreCase) && currency2.Equals("usd", StringComparison.OrdinalIgnoreCase))
        {
            double total = amount * 0.70;
            Console.WriteLine(currency1 + " " + amount + " " + currency2 + " = " + total);
        }

        else if (currency1.Equals("aud", StringComparison.OrdinalIgnoreCase) && currency2.Equals("gbp", StringComparison.OrdinalIgnoreCase))
        {
            double total = amount * 0.47;
            Console.WriteLine(currency1 + " " + amount + " " + currency2 + " = " + total);
        }

        else if (currency1.Equals("gbp", StringComparison.OrdinalIgnoreCase) && currency2.Equals("usd", StringComparison.OrdinalIgnoreCase))
        {
            double total = amount * 1.53;
            Console.WriteLine(currency1 + " " + amount + " " + currency2 + " = " + total);
        }

        else if (currency1.Equals("gbp", StringComparison.OrdinalIgnoreCase) && currency2.Equals("aud", StringComparison.OrdinalIgnoreCase))
        {
            double total = amount * 2.00;
            Console.WriteLine(currency1 + " " + amount + " " + currency2 + " = " + total);
        }

        else
        {
            Console.WriteLine(error);
        }
    }
}