Vojtech
Vojtech

Reputation: 643

C# methods and parameters

currently I am studying in Sweden an online course Fundamental programming in C# and I have a problem with one of the examples. In the lecture there is an example of Methods and parameters and there is a mistake in the coding so when I want to try it and see what it does it doesn't work. I have written an email to the lecturer couple of days ago but he is not responding.

Here is the code: I have two classes. First is Account.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Classes_Objects_2
{
    class Account
    {
        public double Balance = 0;

        public void ShowMessage()
        {
            Console.WriteLine("Welcome to the Account Book!");
        }

        // Method Deposit.
        public double Deposit(double depositAmount)
        {
            // You get a 5% bonus.
            return depositAmount * 1.05;
        }
    }
}

And here is the second code which returns 2 errors:

Class is called Accounts.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Classes_Objects_2
{
    class Accounts
    {
        static void Main(string[] args)
        {
            Account myAccount = new Account();
            myAccount.ShowMessage();
            Console.WriteLine("Your balance is " + myAccount.Balance);
            Console.Write("Enter the deposit amount: ");
            double newBalance = myAccount.Deposit(double.Parse(Console.ReadLine()));
            newBalance = amountDep;
            Console.WriteLine("Your balance becomes " + newBalance);
            Console.Write("Enter the next deposit amount: ");
            newBalance = myAccount.Deposit(double.Parse(Console.ReadLine()));
            newBalance = amountDep;
            Console.WriteLine("Your balance becomes " + newBalance);
        }
    }
}

When user enters 100 as deposit, he gets 105 with the deposit and then when he enters 200 he gets 315. That is output I am aiming for and it should work according to the lecture.

I get error in the Accounts class because of the amountDep, it ssays it is not recognized, which is true but I have no idea how to fix this. Can you please help me figure this one out so I can continue studying? Thank you!

Upvotes: 2

Views: 333

Answers (6)

Dmitrii Erokhin
Dmitrii Erokhin

Reputation: 1347

As far as I can see, the balance does not change after you execute Deposit method. So my guess will be, that amountDep should be added to your account's balance, like this:

double amountDep = myAccount.Deposit(double.Parse(Console.ReadLine()));
double newBalance = myAccount.Balance += amountDep;
Console.WriteLine("Your balance becomes " + newBalance);
Console.Write("Enter the next deposit amount: ");
amountDep = myAccount.Deposit(double.Parse(Console.ReadLine()));
myAccount.Balance += amountDep;
Console.WriteLine("Your balance becomes " + newBalance);

Upvotes: 0

Matthias Wuttke
Matthias Wuttke

Reputation: 2032

You need to change your Deposit method to include "Balance += depositAmount * 1.05" to increment the account balance and store the deposit. Use the Balance field of your account class to retrieve the current balance in your Main method (e.g. Console.WriteLine("Balance: " + myAccount.Balance); Notes: You should not use public fields, but class properties. Fields should not be named in upper case (like "Balance"), but "balance", or "_balance", or "mBalance". Good luck!

Upvotes: 0

AakashM
AakashM

Reputation: 63340

Simply remove these two lines :

newBalance = amountDep;

It's not clear what was intended here, and there are n other mentions of the non-existent variable amountDep, so I'd say in order to get compiling code just remove those two lines and carry on.

Upvotes: 0

321X
321X

Reputation: 3185

I think the amountDep is not necessary. It is nowhere declared anyway.

It could be removed, since the assignment of newBalance is done above the assignment of amountDep.

Just a small textual mistake I guess!

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could declare the variable:

double amountDep = 1234;

Upvotes: 1

Olle89
Olle89

Reputation: 678

you have not instantiated amountDep or given it a value

ad something like

double amountDep = 0; 

or a value of choice

Upvotes: 1

Related Questions