Thierry

Thierry

  • NA
  • 14
  • 0

Is this a syntax problem or an arithmetic error?

May 30 2010 9:45 AM

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.
 
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
 
namespace
SimpleBank
{
   public abstract class Account
   {
      // instance variables
      private static long num = 0; // for the account number
      private long number; // account number
      private decimal balance; // account balance
 
      // public properties
      public long Number
      {
         get { return number; }
      }
      public decimal Balance
      {
         get { return balance; }
      } 
 
      // constructors
      public Account()
      {
         balance = 0;
         number = ++num;
      }
      public Account(decimal amount)
      {
         balance = amount;
         number = ++num;
      }
 
      // methods
      public override string ToString()
      {
         string strout = "Account Number: " + Number + "\tBalance: " + Balance.ToString("c");
         return strout;
      }
      
      // adding funds to the account
      public void Credit(decimal amount)
      {
         balance = Balance + amount;
      }
      
      // debiting funds from account

      virtual public void Debit(decimal amount)
      {
         balance = Balance - amount;
      }
   }
// end of class Account
 
   public class CreditAccount : Account
   {
      // instance variables
      private decimal ODLimit; // overdraft limit
 
      // properties
      public decimal Limit
      {
         get { return ODLimit; }
      }
      // constructors
      public CreditAccount(decimal amount)
         :
base(amount)
      {
         ODLimit = 100;
      }
 
      public CreditAccount()
         :
base()
      {
         ODLimit = 100;
      }
 
      public CreditAccount(decimal amount, decimal limit)
         :
base(amount)
      {
         ODLimit = limit;
      }
 
      public override string ToString()
      {
         string strout = base.ToString();
         strout = strout +
"\tOverdraft Limit: " + Limit;
         return strout;
      }
 
      public override void Debit(decimal amount)
      {
         if (amount > base.Balance + Limit)
            throw new Exception("Insufficient funds - transaction cancelled");
         else
            base.Debit(amount);
      }
   }// End of CreditAccount class
 
   public class DepositAccount : Account
   {
      // instance variables
      private double rate; // rate of interest
      
      //public properties              
      public double Rate
      {
         get { return rate; }
         set { rate = value; }
      }
 
      // Constructors
      
      public DepositAccount(decimal amount)
         :
base(amount)
      {
         rate = 0.0;
      }
 
      public DepositAccount()
         :
base()
      {
         rate = 0.0;
      }
 
      public DepositAccount(decimal amount, double rt)
         :
base(amount)
      {
         rate = rt;
      }
      
      public override string ToString()
      {
         string strout;
         strout =
base.ToString();
         strout = strout +
string.Format("\tRate of Interest: {0:f2}", this.Rate);
         return strout;
      }
 
      public override void Debit(decimal amount)
      {
         if ((base.Balance - amount) < 0)
            throw new Exception("Insufficient funds - transaction cancelled");
         else
            base.Debit(amount);
      }
 
      public void AddInterest()
      {
         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 is
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
 
namespace
SimpleBank
{
   class Program
   {
      static void Main(string[] args)
   {

      Tester();

   }
 
   static void Tester()
   {
      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();
   }

}

 

Attachment: SimpleBank.rar

Answers (6)