0

I have a private method named RentingOrBuying(); that gathers information about the property price and interest rate. I want to create another private method that calculates the monthly home loan repayment but I need the information in RentingOrBuying().

Ive tried using get and set methods but the initialization doesn't seem to work. Maybe I did it wrong. Any help is appreciated. I am calling all the methods in the main class. Is it worth leaving them private or should I be making the methods public. How would you go about this?

//this method uses the information gathered in RentingOrBuying and calculates the monthly home loan repayment
    private static void CalculateMonthlyLoanAmount()
    { 
        //accumulated amount = propertyPrice(1 + interestRate)^monthsToRepay
        double repaymentAmount = propertyPrice(1 + interestRate);
    }

    //A method that asks whether the user is buying or renting accommodation and gathers necessary information
    private static void RentingOrBuying()
    {
        Console.WriteLine("Are you going to be renting accommodation or buying a property? \n Please enter either [rent] or [buy]");
        //variable to store user input
        string buyingRenting = Console.ReadLine();
        if (buyingRenting.ToLower() == "rent")
        {
            Console.WriteLine("We see you want to rent, please can you enter the monthly rental amount: ");
            //variable that stores the monthly rental amount.
            double monthlyRental = double.Parse(Console.ReadLine());
        }
        else
        {
            Console.WriteLine("So you've chosen to buy, I will just need the following ionformation:");

            Console.WriteLine("Please enter the purchase price of property: ");
            //variable stores the price of the property the user wants to buy
            double propertyPrice = double.Parse(Console.ReadLine());

            Console.WriteLine("Please enter the total deposit: ");
            //variable stores the total deposit
            double totalDeposit = double.Parse(Console.ReadLine());

            Console.WriteLine("Please enter the interest rate (percentage): ");
            //variable store the interest rate in percentage form
            int interestRate = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Please enter the number of months to repay: ");
            //variable stores the amount of months to repay
            int monthsToRepay = Int32.Parse(Console.ReadLine());
        }
    }
JazzaWild
  • 13
  • 3
  • From outside, you cannot access variables declared inside a method. The method should either return a value or it should assign the value to a global/class-scope variable – Xerillio Mar 18 '21 at 10:41
  • `Int32.Parse(Console.ReadLine())` is quite error-prone. It fails if the user types something which can't be parsed as integer. Please prefer `TryParse` to make your app more robust. – Peter Csala Mar 18 '21 at 11:30

1 Answers1

0

you should take any variables needed as input parameters, and return any results from the calculations, i.e.

private static double CalculateMonthlyLoanAmount(double interestRate, double propertyPrice)
{ 
    // do whatever calculation you need do to here

    // return the result
    return result;
}

More complex scenarios would involve encapsulating the variables in a class, to avoid needing to specify them if you call a method multiple times, or multiple methods share many of the variables.

JonasH
  • 28,608
  • 2
  • 10
  • 23