Ejemplo n.º 1
0
        public void TestNpv()
        {
            double r, npv, x;

            r = 1;
            double[] v = new double[] { 100, 200, 300, 400 };
            npv = FinanceLib.npv(r, v);
            x   = 162.5;
            Assert.AreEqual(x, npv, 0.000001);

            r   = 2.5; v = new double[] { 1000, 666.66666, 333.33, 12.2768416 };
            npv = FinanceLib.npv(r, v);
            x   = 347.99232604144827;
            Assert.AreEqual(x, npv, 0.000001);

            r   = 12.33333; v = new double[] { 1000, 0, -900, -7777.5765 };
            npv = FinanceLib.npv(r, v);
            x   = 74.3742433377061;
            Assert.AreEqual(x, npv, 0.000001);

            r   = 0.05; v = new double[] { 200000, 300000.55, 400000, 1000000, 6000000, 7000000, -300000 };
            npv = FinanceLib.npv(r, v);
            x   = 11342283.4233124;
            Assert.AreEqual(x, npv, 0.0000001);
        }
Ejemplo n.º 2
0
        public void TestNper()
        {
            double f, r, y, p, x, n;
            bool   t = false;

            r = 0; y = 7; p = 2; f = 3; t = false;
            n = FinanceLib.nper(r, y, p, f, t);
            x = -0.71428571429; // can you believe it? excel returns nper as a fraction!??
            Assert.AreEqual(x, n, 0.0000000001);

            // cross check with pv
            r = 1; y = 100; p = -109.66796875; f = 10000; t = false;
            n = FinanceLib.nper(r, y, p, f, t);
            x = 10;
            Assert.AreEqual(x, n, 0.01);

            r = 1; y = 100; p = -209.5703125; f = 10000; t = true;
            n = FinanceLib.nper(r, y, p, f, t);
            x = 10;
            Assert.AreEqual(x, n, 0.1);

            // cross check with fv
            r = 2; y = 120; f = -6409178400d; p = 12000; t = false;
            n = FinanceLib.nper(r, y, p, f, t);
            x = 12;
            Assert.AreEqual(x, n, "nper ");

            r = 2; y = 120; f = -6472951200d; p = 12000; t = true;
            n = FinanceLib.nper(r, y, p, f, t);
            x = 12;
            Assert.AreEqual(x, n, "nper ");
        }
Ejemplo n.º 3
0
        public void TestPmt()
        {
            double f, r, y, p, x;
            int    n;
            bool   t = false;

            r = 0; n = 3; p = 2; f = 7; t = true;
            y = FinanceLib.pmt(r, n, p, f, t);
            x = -3;
            Assert.AreEqual(x, y, "pmt ");

            // cross check with pv
            r = 1; n = 10; p = -109.66796875; f = 10000; t = false;
            y = FinanceLib.pmt(r, n, p, f, t);
            x = 100;
            Assert.AreEqual(x, y, "pmt ");

            r = 1; n = 10; p = -209.5703125; f = 10000; t = true;
            y = FinanceLib.pmt(r, n, p, f, t);
            x = 100;
            Assert.AreEqual(x, y, "pmt ");

            // cross check with fv
            r = 2; n = 12; f = -6409178400d; p = 12000; t = false;
            y = FinanceLib.pmt(r, n, p, f, t);
            x = 120;
            Assert.AreEqual(x, y, "pmt ");

            r = 2; n = 12; f = -6472951200d; p = 12000; t = true;
            y = FinanceLib.pmt(r, n, p, f, t);
            x = 120;
            Assert.AreEqual(x, y, "pmt ");
        }
Ejemplo n.º 4
0
        public void GetPresentValue_Should_Throw_ArgumentOutOfRangeException_When_Interest_Rate_Less_Than_Zero()
        {
            //Arrange
            double     repayment    = 10000;
            double     interestRate = -4.8;
            int        loanTerm     = 3;
            FinanceLib sut          = new FinanceLib();

            //Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => { sut.GetPresentValue(repayment, interestRate, loanTerm); });
        }
Ejemplo n.º 5
0
        public void GetRepayment_Should_Throw_ArgumentOutOfRangeException_When_Loan_Term_Less_Than_Zero()
        {
            //Arrange
            double     loanAmount   = 10000;
            double     interestRate = 4.8;
            int        loanTerm     = -3;
            FinanceLib sut          = new FinanceLib();

            //Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => { sut.GetRepayment(loanAmount, interestRate, loanTerm); });
        }
Ejemplo n.º 6
0
        public void Should_Return_Present_Value_Of_Loan()
        {
            //Arrange
            double     repayment      = 250;
            double     interestRate   = 4.8;
            int        loanTerm       = 3;
            double     expectedResult = 8366.47;
            FinanceLib sut            = new FinanceLib();

            //Act
            double actualResult = sut.GetPresentValue(repayment, interestRate, loanTerm);

            //Assert
            Assert.Equal(expectedResult, Math.Round(actualResult, 2));
        }
Ejemplo n.º 7
0
        public void Should_Return_Monthly_Repayment()
        {
            //Arrange
            double     loanAmount     = 10000;
            double     interestRate   = 4.8;
            int        loanTerm       = 3;
            double     expectedResult = 298.81;
            FinanceLib sut            = new FinanceLib();

            //Act
            double actualResult = sut.GetRepayment(loanAmount, interestRate, loanTerm);

            //Assert
            Assert.Equal(expectedResult, Math.Round(actualResult, 2));
        }
Ejemplo n.º 8
0
        public void TestFv()
        {
            double f, r, y, p, x;
            int    n;
            bool   t = false;

            r = 0; n = 3; y = 2; p = 7; t = true;
            f = FinanceLib.fv(r, n, y, p, t);
            x = -13;
            Assert.AreEqual(x, f, "fv ");

            r = 1; n = 10; y = 100; p = 10000; t = false;
            f = FinanceLib.fv(r, n, y, p, t);
            x = -10342300;
            Assert.AreEqual(x, f, "fv ");

            r = 1; n = 10; y = 100; p = 10000; t = true;
            f = FinanceLib.fv(r, n, y, p, t);
            x = -10444600;
            Assert.AreEqual(x, f, "fv ");

            r = 2; n = 12; y = 120; p = 12000; t = false;
            f = FinanceLib.fv(r, n, y, p, t);
            x = -6409178400d;
            Assert.AreEqual(x, f, "fv ");

            r = 2; n = 12; y = 120; p = 12000; t = true;
            f = FinanceLib.fv(r, n, y, p, t);
            x = -6472951200d;
            Assert.AreEqual(x, f, "fv ");

            // cross tests with pv
            r = 2.95; n = 13; y = 13000; p = -4406.78544294496; t = false;
            f = FinanceLib.fv(r, n, y, p, t);
            x = 333891.230010986; // as returned by excel
            Assert.AreEqual(x, f, 0.01);

            r = 2.95; n = 13; y = 13000; p = -17406.7852148156; t = true;
            f = FinanceLib.fv(r, n, y, p, t);
            x = 333891.230102539; // as returned by excel
            Assert.AreEqual(x, f, 0.01);
        }
Ejemplo n.º 9
0
        public void TestPv()
        {
            double f, r, y, p, x;
            int    n;
            bool   t = false;

            r = 0; n = 3; y = 2; f = 7; t = true;
            f = FinanceLib.pv(r, n, y, f, t);
            x = -13;
            Assert.AreEqual(x, f, "pv ");

            r = 1; n = 10; y = 100; f = 10000; t = false;
            p = FinanceLib.pv(r, n, y, f, t);
            x = -109.66796875;
            Assert.AreEqual(x, p, "pv ");

            r = 1; n = 10; y = 100; f = 10000; t = true;
            p = FinanceLib.pv(r, n, y, f, t);
            x = -209.5703125;
            Assert.AreEqual(x, p, "pv ");

            r = 2.95; n = 13; y = 13000; f = 333891.23; t = false;
            p = FinanceLib.pv(r, n, y, f, t);
            x = -4406.78544294496;
            Assert.AreEqual(x, p, 0.0000001);

            r = 2.95; n = 13; y = 13000; f = 333891.23; t = true;
            p = FinanceLib.pv(r, n, y, f, t);
            x = -17406.7852148156;
            Assert.AreEqual(x, p, 0.0000001);

            // cross tests with fv
            r = 2; n = 12; y = 120; f = -6409178400d; t = false;
            p = FinanceLib.pv(r, n, y, f, t);
            x = 12000;
            Assert.AreEqual(x, p, "pv ");

            r = 2; n = 12; y = 120; f = -6472951200d; t = true;
            p = FinanceLib.pv(r, n, y, f, t);
            x = 12000;
            Assert.AreEqual(x, p, "pv ");
        }
Ejemplo n.º 10
0
 public override double Evaluate(double rate, double arg1, double arg2, double arg3, bool type)
 {
     return(FinanceLib.pv(rate, arg1, arg2, arg3, type));
 }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            string choice = "";

            do
            {
                //Get menu choice
                ShowMenu();
                choice = Console.ReadLine();

                //calculate monthly repayment
                if (choice == "1")
                {
                    double loanAmount;
                    do
                    {
                        Console.WriteLine("Enter the loan amount in dollars (e.g. 10000):");
                    } while (!Double.TryParse(Console.ReadLine(), out loanAmount) || loanAmount <= 0);

                    double interestRate;
                    do
                    {
                        Console.WriteLine("Enter interest rate as a percentage (e.g. 5.2):");
                    } while (!Double.TryParse(Console.ReadLine(), out interestRate) || interestRate <= 0);

                    int loanTermInYears;
                    do
                    {
                        Console.WriteLine("Enter the term of the loan in years (e.g. 5):");
                    } while (!Int32.TryParse(Console.ReadLine(), out loanTermInYears) || loanTermInYears <= 0);

                    //create new calculator object to call repayment method
                    //Note: This could also be made into a static class if little domain logic is required
                    FinanceLib calculator = new FinanceLib();

                    try
                    {
                        double monthlyRepayment = calculator.GetRepayment(loanAmount, interestRate, loanTermInYears);
                        Console.WriteLine($"Monthly Repayments: ${Math.Round(monthlyRepayment,2)}");
                        Console.WriteLine();

                        //Append details of loan calculation to loans.txt file
                        //This file is in the bin folder so will be removed if a clean of the project is performed. For a real system this data
                        //would be persisted in a proper data store
                        using (StreamWriter writer = new StreamWriter("loans.txt", true))
                        {
                            writer.WriteLine(calculator);
                        }
                    }
                    //probably never hit as UI does some valdiation
                    catch (ArgumentOutOfRangeException ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine();
                    }
                }
                //Present Value calculation
                else if (choice == "2")
                {
                    double repayment;

                    //get input and validate
                    do
                    {
                        Console.WriteLine("Enter the monthly repayment amount in dollars (e.g. 250):");
                    } while (!Double.TryParse(Console.ReadLine(), out repayment) || repayment <= 0);

                    double interestRate;
                    do
                    {
                        Console.WriteLine("Enter interest rate as a percentage (e.g. 5.2):");
                    } while (!Double.TryParse(Console.ReadLine(), out interestRate) || interestRate <= 0);

                    int loanTermInYears;
                    do
                    {
                        Console.WriteLine("Enter the term of the loan in years (e.g. 5):");
                    } while (!Int32.TryParse(Console.ReadLine(), out loanTermInYears) || loanTermInYears <= 0);

                    //create new calculator object to call repayment method
                    //Note: This could also be made into a static class if little domain logic is required
                    FinanceLib calculator = new FinanceLib();

                    try
                    {
                        double presentValue = calculator.GetPresentValue(repayment, interestRate, loanTermInYears);
                        Console.WriteLine($"Present Value of the loan: ${Math.Round(presentValue, 2)}");
                        Console.WriteLine();

                        //Append details of loan calculation to loans.txt file
                        //This file is in the bin folder so will be removed if a clean of the project is performed. For a real system this data
                        //would be persisted in a proper data store
                        using (StreamWriter writer = new StreamWriter("loans.txt", true))
                        {
                            writer.WriteLine(calculator);
                        }
                    }
                    //probably never hit as UI does some valdiation
                    catch (ArgumentOutOfRangeException ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine();
                    }
                }
                //display calculations saved to file
                else if (choice == "3")
                {
                    try
                    {   // Open the text file using a stream reader.
                        using (StreamReader sr = new StreamReader("loans.txt"))
                        {
                            // Read the stream to a string, and write the string to the console.
                            String line = sr.ReadToEnd();
                            Console.WriteLine(line);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("The file could not be read:");
                        Console.WriteLine(e.Message);
                    }
                }
            }while (choice != "4");

            Console.WriteLine("Exiting program...");
        }