Hi all I am a student and a beginner at C#. I have to build a simple solution which would simulate a very basic banking system. Everything was going fine except for one method, which is supposed to add interest to the account balance; public void AddInterest(). The problem is that it is not changing the balance whatsoever; not adding the interest! Here's the code for the Accounts hierarchy that I have created and below it the tests to ensure all works. As I have mentioned there are no compilation errors, only the interest is not being added and the balance not altered.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceSimpleBank
{
publicabstractclassAccount
{
// instance variablesprivatestaticlong num = 0; // for the account numberprivatelong number; // account numberprivatedecimal balance; // account balance// public propertiespubliclong Number
{
get { return number; }
}
publicdecimal Balance
{
get { return balance; }
}
// constructorspublicAccount()
{
balance = 0;
number = ++num;
}
publicAccount(decimal amount)
{
balance = amount;
number = ++num;
}
// methodspublicoverridestringToString()
{
string strout = "Account Number: " + Number + "\tBalance: " + Balance.ToString("c");
return strout;
}
// adding funds to the accountpublicvoidCredit(decimal amount)
{
balance = Balance + amount;
}
// debiting funds from accountvirtualpublicvoidDebit(decimal amount)
{
balance = Balance - amount;
}
}// end of class AccountpublicclassCreditAccount : Account
{
// instance variablesprivatedecimal ODLimit; // overdraft limit// propertiespublicdecimal Limit
{
get { return ODLimit; }
}
// constructorspublicCreditAccount(decimal amount)
: base(amount)
{
ODLimit = 100;
}
publicCreditAccount()
: base()
{
ODLimit = 100;
}
publicCreditAccount(decimal amount, decimal limit)
: base(amount)
{
ODLimit = limit;
}
publicoverridestringToString()
{
string strout = base.ToString();
strout = strout + "\tOverdraft Limit: " + Limit;
return strout;
}
publicoverridevoidDebit(decimal amount)
{
if (amount > base.Balance + Limit)
thrownew Exception("Insufficient funds - transaction cancelled");
elsebase.Debit(amount);
}
}// End of CreditAccount classpublicclassDepositAccount : Account
{
// instance variablesprivatedouble rate; // rate of interest//public properties publicdouble Rate
{
get { return rate; }
set { rate = value; }
}
// Constructors publicDepositAccount(decimal amount)
: base(amount)
{
rate = 0.0;
}
publicDepositAccount()
: base()
{
rate = 0.0;
}
publicDepositAccount(decimal amount, double rt)
: base(amount)
{
rate = rt;
}
publicoverridestringToString()
{
string strout;
strout = base.ToString();
strout = strout + string.Format("\tRate of Interest: {0:f2}", this.Rate);
return strout;
}
publicoverridevoidDebit(decimal amount)
{
if ((base.Balance - amount) thrownew Exception("Insufficient funds - transaction cancelled");
elsebase.Debit(amount);
}
publicvoidAddInterest()
{
decimal balance = base.Balance;
decimal interest;
double i = ((double)balance * this.Rate) / 100;
interest = (decimal)i;
balance = balance + interest;
}
}
}// end of DepositAccount class// end of file// start of Program.cs file where test isusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceSimpleBank
{
classProgram
{
staticvoidMain(string[] args)
{
Tester();
}
staticvoidTester()
{
CreditAccount cacc;
DepositAccount dacc;
cacc = new CreditAccount(100, 100);
dacc = new DepositAccount(1200, 2.0);
Console.WriteLine("Current account acc1 created with 100 and 100 OD");
Console.WriteLine(cacc);
Console.WriteLine("Deposit account acc2 created with 1200 and 2.0 rate");
Console.WriteLine(dacc);
Console.WriteLine("withdraw 150 from credit account cacc");
cacc.Debit(150);
Console.WriteLine(cacc);
try
{
Console.WriteLine("Try withdraw 200 from credit account cacc");
Console.WriteLine("expect error");
cacc.Debit(150);
Console.WriteLine("no error " + cacc);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
Console.WriteLine("balance should be same as before");
Console.WriteLine(cacc);
}
try
{
Console.WriteLine("Try withdraw 2000 from debit account dacc");
Console.WriteLine("expect error");
dacc.Debit(2000);
Console.WriteLine("no error " + dacc);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
Console.WriteLine("balance should be same as before");
Console.WriteLine(dacc);
}
Console.WriteLine("testing apply interest");
dacc.AddInterest();
Console.WriteLine(dacc);
Console.ReadLine();
}
}
6 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 30, 2010, 7:16 AM EDT
0
HI..
if your not getting error at runtime then thats neithere arithmetic nor syntax error
so might be your logic is wrong for adding intrest..
so trace your project and run it by pressing F11 key.. you will get the solution
Ankur GuptaRank #1,017 · 1.9K reputationPosted May 31, 2010, 2:02 AM EDT
0
Declare a public property in DepositAccount class
public decimal ToatlBalance;
and then The Add Intrest function look like this
public void AddInterest()
{
decimal balance = base.Balance;
decimal interest;
double i = ((double)balance * this.Rate) / 100;
interest = (decimal)i;
balance = balance + interest;
ToatlBalance = balance;
}
and at last you can print the intrest using
Console.WriteLine(dacc.ToatlBalance);
I think This will solve your problem
0
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 31, 2010, 6:44 AM EDT
0
just press F11 key..
this way you will get how your program is executing,,
Its neither arithmetic or syntax error. I can say implementation problem. Your derived class has no way to set the new balance, whatever new balance you calculate in AddInterest() method is stored in local variable "balance" (which is local variable to the function itself). You can solve your problem by just adding the following protected function "SetBalance" in your Base class "Account"
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 30, 2010, 7:16 AM EDT
Thierry14 reputationPosted May 30, 2010, 3:48 PM EDT
Ankur GuptaRank #1,017 · 1.9K reputationPosted May 31, 2010, 2:02 AM EDT
public decimal ToatlBalance;
and then The Add Intrest function look like this
public void AddInterest()
{
decimal balance = base.Balance;
decimal interest;
double i = ((double)balance * this.Rate) / 100;
interest = (decimal)i;
balance = balance + interest;
ToatlBalance = balance;
}
and at last you can print the intrest using
Console.WriteLine(dacc.ToatlBalance);
I think This will solve your problem
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 31, 2010, 6:44 AM EDT
mubashir hafeez5 reputationPosted May 31, 2010, 9:57 AM EDT
"Account"protected void SetBalance(decimal NewBalance)
{
balance = NewBalance;
}
Update your
AddInterest() function as mentioned below.
public void AddInterest()
{
decimal balance = base.Balance;
decimal interest;
double i = ((double)balance * this.Rate) / 100;
interest = (decimal)i;
balance = balance + interest;
base.SetBalance(balance); // sets new balance
}
Run your program your balance will be updated.
Thierry14 reputationPosted May 31, 2010, 12:40 PM EDT