简体   繁体   中英

C# Compound Interest Calculator Using a Loop (Error CS0103)

I want to create a Compound Interest Calculator in C# using two classes in different namespaces but can't for the life of me figure out why I keep getting errors.

PSA I am a beginner, I know this code probably looks awful, but please be kind.

Here is CompoundTest.cs


namespace CompoundTest
{
       class Program
    {
        static void Main(string[] args)
        {
            CompoundClass newprogram = new CompoundClass();

            Console.Write("\nPlease enter the initial balance for your account: ");
            double balance = Convert.ToDouble(Console.ReadLine());


            Console.Write("\nPlease enter the annual interest rate: ");
            double interestRate = Convert.ToDouble(Console.ReadLine()) / 100;


            Console.Write("\nHow many years will you acrue interest? ");
            double annualAmount = Convert.ToDouble(Console.ReadLine());


            Console.WriteLine($"Your balance after {annualAmount} years is {accountBalance:C}");


            Console.ReadLine();
        }
    }
}

And here is Compound.cs


using System;

namespace Compound
{
    public class CompoundClass
    {

        private double balance;
        public int value { get; private set; }

        public CompoundClass()
        {
            Balance = value;
        }


        public double Balance
        {
            get
            {
                return balance;
            }
            private set
            {
                if (value > 0)
                {
                    balance = value;
                }
            }
        }

        public void Rate(double interestRate)
        {
          interestRate = value / 100;

        }


        public void Years(double annualAmount)
        {

         annualAmount = value * 12;


        }


        public void addMethod(double accountBalance)
        {
            for (int i = 1; i < annualAmount + 1; i++)
            {
                accountBalance = balance * Math.Pow(1 + interestRate / annualAmount, annualAmount * i);

            }
        }
    }
}

I get the error:

CS0103 C# The name '..' does not exist in the current context - in the public void addMethod(double accountBalance) method

You are not storing any data on the CompoundClass, the method

public void Rate(double interestRate)
{
    interestRate = value / 100;
}

only operates on the input parameter interestrate inside the functions scope, after that the result of the calculation is lost

If you want to reuse a variable on the entire lifetime of the CompoundClass, then define it as a member variable like:

private double _interestRate

and change your function to

public void Rate()
{
    _interestRate = value / 100;
}

and for the annualAmount as well

private double _annualAmount;

public void Years()
{
  _annualAmount = value * 12;
}

and your calculation to

public double addMethod(double accountBalance)
{
    for (int i = 1; i < annualAmount + 1; i++)
    {
                accountBalance = balance * Math.Pow(1 + _interestRate / _annualAmount, _annualAmount * i);
    }

    return accountBalance;
}

you didn't define interestRate and annualAmount

you should add them to the class:

private double interestRate;
private double annualAmount;

in this function you're basically doing nothing:

public void Rate(double interestRate)
{
    // value doesnt even exist in this scope
    interestRate = value / 100;
}

if you want it to return something, you have to

return interestRate / 100;

if you want it to set some variable in the class, then you have to create a class variable first. Then:

this.interestRate = interestRate / 100;

There is more then one thing wrong with this code. And I am honestly not sure if I even got anything close to your problem yet.

using System;

namespace Compound
{
    public class CompoundClass
    {

        private double balance;
        public int value { get; private set; }

        public CompoundClass()
        {   
            //Balance with a big B is nowhere in context
            Balance = value;
        }


        public double Balance
        {
            get
            {
                return balance;
            }
            private set
            {
                if (value > 0)
                {
                    balance = value;
                }
            }
        }

        //As remarked by somebody else, this function does nothing. Without return or out parameter, interest rate will stay at nothing.
        public void Rate(double interestRate)
        {
          interestRate = value / 100;

        }

        //The naming of this variable is bad. Did you mean "Amoung of Months"?
        //Also as someone else pointed out, you do not return or otherwise persist this value
        public void Years(double annualAmount)
        {

         annualAmount = value * 12;


        }
        //Method does not return anything.
        //accountBalance is a local value and will not persist
        public void addMethod(double accountBalance)
        {
            for (int i = 1; i < annualAmount + 1; i++)
            {
                //Avoid putting that much stuff into 1 line. It really messes with your ability to debug
                //1-2 operations + 1 assignment to a temporary variable per line
                //Anything more and you will have serious issues debugging this
                accountBalance = balance * Math.Pow(1 + interestRate / annualAmount, annualAmount * i);

            }
        }
    }
}

Generally the variables this works with should be either purely parameters (wich means it should be a static class with static functions) or mostly class variables. You have both things mixed all over the place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM