Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("enter the type of currency and amount of currency ");
                Money input1 = new Money(Console.ReadLine());
                Console.WriteLine("enter the Target currency");
                string targetCurrency = Console.ReadLine();
                targetCurrency = targetCurrency.ToUpper();
                var ans = input1.ConvertCurrency(targetCurrency);
                //Money ans = new Money(string);
                //ans.ConvertCurrency(targetCurrency);
                Console.WriteLine("the converted amount is: {0}", ans);
                /*Console.WriteLine("enter the type of currency and amount of currency ");
                Money input1 = new Money(Console.ReadLine());
                Console.WriteLine("enter the type of currency and amount of currency");
                Money input2 = new Money(Console.ReadLine());
                Money output = input1 + input2;
                Console.WriteLine("the total amount is: " + Output.Currency + " " + Output.Amount);*/

            }
            catch (Exception e)
            {
                Console.WriteLine("the exception is: {0}", e.Message);

            }
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            double money = 0.0;
            string sourceCurrency = null;
            try
            {
                Console.WriteLine("\nEnter Amount : ");
                while (double.TryParse(Console.ReadLine(), out money) == false)
                {
                    Console.WriteLine(" Enter valid entry for money");
                }
                Console.WriteLine("Enter Currency");
                sourceCurrency = Console.ReadLine();

                Money money1 = new Money(money,sourceCurrency);

                Console.WriteLine("\nEnter target Currency : ");
                string target = Console.ReadLine();
                Console.WriteLine("\nExchanged Amount : ");
                var money2 = new Money(target);

                double exchangedRate = money1.Convert(money1.Amount, money1.Currency, money2.Currency);
                Console.WriteLine("{0} {1}", exchangedRate, target.ToUpper());
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Format : 100 USD");
                Console.WriteLine("\nEnter Amount : "); // Enter Amount in Format '100 USD'
                var m1 = new Money(Console.ReadLine());

                Console.WriteLine("\nEnter target Currency : "); // Enter target currency
                var m2 = new Money(0, Console.ReadLine());

                var exchangedRate = m1.Convert(m2.Currency);
                Console.WriteLine("\nExchanged Amount : {0} {1} ", exchangedRate.Amount, exchangedRate.Currency);
            }

            catch (ArgumentException a)
            {
                Console.WriteLine(a.Message);
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Money money1 = new Money();

            while (true)// condition to check currency is not empty
            {
                Console.WriteLine("Enter 1st Currency.");
                try
                {
                    money1.Currency = Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
                break;
            }

            while (true) //this while keep on taking the input unless you enter valid amount.
            {
                Console.WriteLine("Enter amount for that Currency.");

                string input = Console.ReadLine();
                double temp = 0.0;
                if (double.TryParse(input, out temp))
                {
                    money1.Amount = temp;
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid Amount, Enter again: ");
                    continue;
                }

            }

            Console.WriteLine("Enter currency to which you want to convert: ");
            string toCurrency = Console.ReadLine();

            Money money2 = new Money();
            try
            {
                money2 = money1.Convert(toCurrency);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
                return;
            }

            Console.WriteLine();

            Console.WriteLine("Converted Amount:");
            Console.WriteLine(money2);

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        //this is operator overloaded function
        public static Money operator +(Money money1, Money money2)
        {
            if (money1 == null || money2 == null)
            {
                 throw new ArgumentException(Messages.InvalidArgument);
            }
            Money money3 = new Money();

            if (string.Equals(money1.Currency, money2.Currency, StringComparison.OrdinalIgnoreCase)) //here we r checking whether the currencies of both money is same or not.. if not throw InvalidCurrencyException exception
            {
                money3.Amount = money1.Amount + money2.Amount;
                if(double.IsInfinity(money3.Amount))// here we r checking whether range of double is exceeded or not....if yes then throw out of range exception
                {
                    throw new OverflowException();
                }

            }
            else
            {
                throw new CurrencyMismatchException();

            }
            money3.Currency = money1.Currency;
            return money3;
        }
        public void TestMethod1()
        {
            var money = new Money(100, "USD");
            var amount = money.Convert("INR");

            Assert.IsTrue(amount == 6347.345);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Enter the amount and the Curreny type,, ex:  100 USD ");
                //Calling the Parameterized constructor of Money Class
                var moneyOne = new Money(Console.ReadLine());

                Console.WriteLine("Enter the amount and the Curreny type,, ex:  100 USD");
                //Calling the Parameterized constructor of Money Class.
                var moneyTwo = new Money(Console.ReadLine());

                //Overloading the Plus operator.
                Console.WriteLine(moneyTwo + moneyOne);

                Console.WriteLine("Enter the amount and Currency type ex: 100 USD");
                var money = new Money(Console.ReadLine());
                Console.WriteLine("Enter the currency you want to convert into ex :INR ");

                var convertedMoney = money.Convert(Console.ReadLine());
                Console.WriteLine(convertedMoney);
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
        }
Ejemplo n.º 8
0
        public static void Main(string[] args)
        {
            try
            {
            //Adding comment for changes. Making changes in this comment
                //Console.Write("Enter Amount 1 (<Amt> CType): ");
                //Money amount1 = new Money(Console.ReadLine());

                //Console.Write("Enter Amount 2 (<Amt> CType): ");
                //Money amount2 = new Money(Console.ReadLine());

                //Console.Write("The Total Amount is: ");
                //Console.WriteLine(amount1 + amount2);

                Console.Write("Enter Amount 3 (<Amt> CType): ");
                Money amount3 = new Money(Console.ReadLine());
                Console.Write("Enter Currency to Convert to: ");
                string convertTo = Console.ReadLine();
                Money convertedAmount = amount3.Convert(convertTo);

                Console.WriteLine("The Converted Amount is: " + convertedAmount);

            }

            catch (Exception e)
            {
                Console.WriteLine("Exception Occured.");
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }
        public void InvalidLengthSourceCurrency()
        {
            var money1 = new Money("100 UD");
            var money2 = money1.Convert("INR");

            var money3 = new Money("100 USDF");
            var money4 = money3.Convert("INR");
        }
        public void ValidTest()
        {
            var money1 = new Money("100 USD");
            var money2 = money1.Convert("INR");

            //MOQ can be used for same purpose
            string filePath = ConfigurationManager.AppSettings["Path"];
            string json = System.IO.File.ReadAllText(filePath);
            int indexSource = json.IndexOf("USDINR");
            int indexStart = json.IndexOf(':', indexSource) + 1;
            int indexEnd = json.IndexOf(',', indexStart);
            string value = json.Substring(indexStart, indexEnd - indexStart);
            double checkValue;
            double.TryParse(value, out checkValue);
            Assert.IsTrue(money2.Amount == checkValue * money1.Amount);
        }
Ejemplo n.º 11
0
        public static void Main(string[] args)
        {
            try
            {
                Console.Write("Enter first Amount (Amount Currency) ");
                var Money1 = new Money(Console.ReadLine());
                Console.WriteLine("Enter the Amount to Convert ");
                string toCurrency = Console.ReadLine();
                var output = Money1.Convert(toCurrency);
                Console.WriteLine("Your Converted amount is " + output);
                Console.ReadKey();

            }

            catch (Exception e)
            {
                Console.WriteLine("Exception Occured.");
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }
Ejemplo n.º 12
0
        public static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Enter the amount in the form of <100 USD>");
                var amount = new Money(Console.ReadLine());
                Console.WriteLine("Enter the currency in which you want to convert");
                string toCurrency = Console.ReadLine();
                var output = amount.Convert(toCurrency.ToUpper());
                Console.WriteLine("your Converted amount is " + output);
                Console.ReadKey();

            }
            catch (Exception e)
            {
                Console.WriteLine("OOPS..!! There occur some error");
                Console.WriteLine(e.Message);
                Console.ReadKey();

            }
        }
Ejemplo n.º 13
0
        public static void Main(string[] args)
        {
            try
            {
                Console.Write("Enter first Amount (Amount Currency) ");
                var amount1 = new Money(Console.ReadLine());

                Console.Write("Enter second  Amount (Amount Currency) ");
                var amount2 = new Money(Console.ReadLine());

                Console.Write(amount1 + amount2+" is the Total Amount  ");

            }

            catch (Exception e)
            {
                Console.WriteLine("Exception Occured.");
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Performing Currency Conversion");
                Console.WriteLine("Enter Amount to be converted and Source Currency");

                var moneyOne = new Money(Console.ReadLine());//in format "100 USD"

                Console.WriteLine("Enter Target Currency");

                string targetCurrency = Console.ReadLine();//in format "INR"

                var getexchangeRates1 = moneyOne.Convert(targetCurrency);

                Console.WriteLine("Converted Amount in " + targetCurrency + "= " + moneyOne.Amount + " " + targetCurrency);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
Ejemplo n.º 15
0
        public static void Main(string[] args)
        {
            Money money = new Money();
            string sourceCurrency;
            string targetCurrency;

               Console.WriteLine("Enter Source Currency in \"100 USD\" Format");
               string temporaryString = Console.ReadLine();

               string[] splitInput = temporaryString.Split(' ');
                double temporaryAmount;
                 if((double.TryParse(splitInput[0],out temporaryAmount)==false))
                     throw new Exception(Messages.InvalidParsing);

                 Money money1=new Money( temporaryAmount,splitInput[1]);

            Console.WriteLine("Enter Target Currency");
            if (IsValidCurrency(Console.ReadLine().ToUpper(), out targetCurrency) == false)
                throw new Exception(Messages.InvalidCurrency);
            Money money2 = new Money(targetCurrency);

            double multiplier = money.ConvertCurrency(money1.Currency, money2.Currency);
            Console.WriteLine(multiplier*money1.Amount);
        }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            try
            {
                // Input and calling constructor
                Console.WriteLine(Messages.InputMessage);
                var money1 = new Money(Console.ReadLine());

                // Input and calling constructor
                Console.WriteLine(Messages.InputMessage);
                var money2 = new Money(Console.ReadLine());

                //this statement can throw exception
                var money3 = money1 + money2;
                Console.WriteLine("The Amount and Currency is : {1} {0}", money3.Currency, money3.Amount);
            }
            catch (Exception e)
            {
                //Handle the exception in this section
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            Console.ReadKey();
        }
Ejemplo n.º 17
0
        //operator + is overloaded
        public static Money operator +(Money Money1, Money Money2)
        {
            string currency;

            if (Money1 == null || Money2 == null)   //Null checked for money objects
            {
                throw new NullReferenceException();
            }
            Money Money3 = new Money();

            if (Money1.Currency.Equals(Money2.Currency, StringComparison.OrdinalIgnoreCase))  // checking whether both currencies are equal or not
            {
                Money3.Amount = Money1.Amount + Money2.Amount;
                Money3.Currency = Money1.Currency.ToUpper();
                if (double.IsInfinity(Money3.Amount))     //cheked infinity for amount for exception
                {
                    throw new OverflowException(Messages.Overfolw);
                }
            }
            currency = Money1.Currency.ToUpper(); // used to convert the currency into Uppercase
            Money3.Amount = Money1.Amount + Money2.Amount;
            Money3.Currency = Money1.Currency;
            return Money3;
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Converts the Money into desired Currency 
        /// </summary>
        /// <param name="INR"></param>
        /// <returns>100 INR</returns>
        public Money Convert(string toCurrency)
        {
            if (string.IsNullOrWhiteSpace(toCurrency) || toCurrency.Length != 3 || Regex.IsMatch(toCurrency, @"^[a-zA-Z]+$") == false)
            {
                throw new System.Exception(Resources.InvalidCurrency);
            }
            CurrencyConverter currencyObject = new CurrencyConverter();
            var exchangeRate = currencyObject.GetConversion(this.Currency, toCurrency);
            var convertedAmount = exchangeRate * this.Amount;
            if (double.IsPositiveInfinity(Amount) || Amount > double.MaxValue)
            {
                throw new System.Exception(Resources.InvalidSum);

            }
            Money Money2 = new Money(convertedAmount, toCurrency);
            return (Money2);
        }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            double temporaryAmount;
            string temporaryCurrency;
            Money moneyOne = null;
            Money moneyTwo = null;

            //Take the input from user for first money object
            try
            {

                Console.WriteLine("Enter amount for first money object:");
                while (double.TryParse(Console.ReadLine(), out  temporaryAmount) == false)
                {
                    Console.WriteLine("Please enter proper amount for first money object:");
                }

                while (true)
                {
                    try
                    {
                        Console.WriteLine("Enter currency for first money object:");
                        temporaryCurrency = Console.ReadLine();
                        moneyOne = new Money(temporaryAmount, temporaryCurrency);
                        break;
                    }
                    catch (NullReferenceException e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                }
                //Take the input from user for second money object
                Console.WriteLine("Enter amount for second money object:");
                while (double.TryParse(Console.ReadLine(), out  temporaryAmount) == false)
                {
                    Console.WriteLine("Please enter proper amount for second money object:");
                }

                while (true)
                {
                    try
                    {
                        Console.WriteLine("Enter currency for second money object:");
                        temporaryCurrency = Console.ReadLine();
                        moneyTwo = new Money(temporaryAmount, temporaryCurrency);
                        break;
                    }
                    catch (NullReferenceException e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                }
                Money moneyThree = moneyOne + moneyTwo;
                Console.WriteLine("Third money object(sum of the two that you just entered) is:");
                Console.WriteLine("Total amount: {0} {1}", moneyThree.Amount, moneyThree.Currency);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
 public void TestMethod5()
 {
     var money = new Money(100, "USD");
     var amount = money.Convert("\0");
 }
 public void TestMethod10()
 {
     var money = new Money(100, "YEN");
     var amount = money.Convert("INR");
 }
Ejemplo n.º 22
0
        public Money Convert(String toCurrency)
        {
            //  Console.WriteLine(""+toCurrency+"      "+this.Currency);

            ExchangeRateProvider converter = new ExchangeRateProvider();
            double exchangeRate=0.0;
            try
            {
                exchangeRate = converter.GetExchangeRate(this.Currency, toCurrency);
            }
            catch (Exception e)
            {
                throw e;
            }
            Money convertedMoney = new Money();
            convertedMoney.Currency = toCurrency;
            convertedMoney.Amount = this.Amount * exchangeRate;
            return convertedMoney;
        }
 public void CurrencyNotFound()
 {
     var money1 = new Money("100 USD");
     var money2 = money1.Convert("YEN");
 }
Ejemplo n.º 24
0
        private static void Validate(Money money1, Money money2)
        {
            if (money1 != null && money2 != null)
            {

                if (string.Equals(money1.Currency, money2.Currency, StringComparison.OrdinalIgnoreCase))
                {

                    if (double.IsInfinity(money1.Amount + money2.Amount) == true)
                    {
                        CustomExceptions.ExceptionThrower.ThrowAmountOverflow();
                    }
                }
                else
                {
                    CustomExceptions.ExceptionThrower.ThrowCurrencyMismatch();
                }
            }
            else
            {
                CustomExceptions.ExceptionThrower.ThrowNullObjectPassed();
            }
        }
 public void CurrencyToConvertInvalid()
 {
     var money1 = new Money("100 USD");
     var money2 = money1.Convert("100");
 }
 public void CurrencyToConvertNull()
 {
     var money1 = new Money("100 USD");
     var money2 = money1.Convert(null);
 }
 public void SourceNull()
 {
     var money1 = new Money(null);
     var money2 = money1.Convert("INR");
 }