SavingsBank.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericImplementation
{
class SavingsBank
{
protected string name;
protected int age;
private const float MIN_AMT_LMT = 10000;
private float accBalance;
public SavingsBank(string AccName, int age, string opType, float balanceAmt)
{
this.Name = AccName;
this.Age = age;
if (opType.ToLower().Equals("credit"))
{
this.DoCredit(balanceAmt);
}
else
{
this.DoDebit(balanceAmt);
}
}
public string Name
{
get
{
return name;
}
set
{ name = value;
}
}
public int Age
{
get
{
return age;
}
set
{ age = value;
}
}
public float TotalBalance
{
get
{
return accBalance;
}
set
{
accBalance = value;
}
}
public void DoCredit(float credBal)
{
TotalBalance = TotalBalance + credBal;
}
public void DoDebit(float debBal)
{
if (TotalBalance - debBal > MIN_AMT_LMT)
{
TotalBalance = TotalBalance - debBal;
}
else
{
throw new Exception("Balance should not be less than minimum amount " + MIN_AMT_LMT.ToString());
}
}
public override string ToString()
{
return " Account name :" + this.Name + " Age:" + this.age.ToString() + " Balance : " + this.TotalBalance.ToString();
}
}
}
Current Bank A/C:
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericImplementation
{
class CurrentBank
{
protected string name;
protected int age;
private float accBalance;
private const float MIN_AMT_LMT = 20000;
public CurrentBank(string AccName, int age,string opType,float balanceAmt)
{
this.Name = AccName;
this.Age = age;
if (opType.ToLower().Equals("credit"))
{
this.DoCredit(balanceAmt);
}
else
{
this.DoDebit(balanceAmt);
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public float GetCreditLimit
{
get
{
return MIN_AMT_LMT;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public float TotalBalance
{
get
{
return accBalance;
}
set
{
accBalance = value;
}
}
public void DoCredit(float credBal)
{
TotalBalance = TotalBalance + credBal;
}
public void DoDebit(float debBal)
{
if (TotalBalance - debBal > MIN_AMT_LMT)
{
TotalBalance = TotalBalance - debBal;
}
else
{
throw new Exception("Balance should not be less than minimum amount " + MIN_AMT_LMT.ToString());
}
}
public override string ToString()
{
return " Account name :" +this.Name + " Age:" +this.age.ToString() + " Balance : " + this.TotalBalance.ToString();
}
}
}
Now create a Generic bank account collection which can hold the collection of either Savings Bank account or Current bank account.
Generic Bank Collection:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace GenericImplementation
{
class GenericBankCollection<AccountType> : CollectionBase
{
public void Add(AccountType GenericObject)
{
InnerList.Add(GenericObject);
}
public void Remove(int index)
{
InnerList.RemoveAt(index);
}
public AccountType Item(int index)
{
return (AccountType)InnerList[index];
}
}
}
Usage:
Now we can use the Generic bank collection to hold either the savingsBankAccount or CurrentBankAccount collection.
Program.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericImplementation
{
class Program
{
static void Main(string[] args)
{
GenericBankCollection<SavingsBank> sbAccs = new GenericBankCollection<SavingsBank>();
sbAccs.Add(new SavingsBank("Sriram",34,"credit",1000));
sbAccs.Add(new SavingsBank("Saik",30,"debit",1000));
GenericBankCollection<CurrentBank> curAccs = new GenericBankCollection<CurrentBank>();
curAccs.Add(new CurrentBank("Mohan", 34, "credit", 1000));
curAccs.Add(new CurrentBank("Krishna", 30, "credit", 1000));
System.Console.WriteLine("Savings Accounts");
System.Console.WriteLine("=========");
foreach (SavingsBank savingsBank in sbAccs)
{
System.Console.WriteLine(savingsBank.ToString());
}
System.Console.WriteLine("Current Accounts");
System.Console.WriteLine("=========");
foreach (CurrentBank CurrentBank in curAccs)
{
System.Console.WriteLine(CurrentBank.ToString());
}
System.Console.ReadLine();
}
}
}