Skip to content

Instantly share code, notes, and snippets.

@mimukit
Created June 21, 2016 12:41
Show Gist options
  • Save mimukit/47a5ae696fa283ecf6505f404a6eee82 to your computer and use it in GitHub Desktop.
Save mimukit/47a5ae696fa283ecf6505f404a6eee82 to your computer and use it in GitHub Desktop.
Basic Bank Management Program by M MUKIT
using System;
using System.Collections;
namespace Bank_Management
{
class DOB
{
private int day = 0;
private int month = 0;
private int year = 0;
public bool set(int d, int m, int y)
{
string dateString = d.ToString() + "-" + m.ToString() + "-" + y.ToString();
DateTime dateValue;
if (DateTime.TryParse(dateString, out dateValue))
{
this.day = d;
this.month = m;
this.year = y;
return true;
}
else
{
return false;
}
}
public string getDOB()
{
string dateString = day.ToString() + "-" + month.ToString() + "-" + year.ToString();
return dateString;
}
}
static class IDGENERATOR
{
private static int[] serial_no = new int[12];
public static string generate()
{
DateTime currentDate = DateTime.Now;
string id = currentDate.Year.ToString("D4") + "-" + currentDate.Month.ToString("D2") + "-" + (serial_no[currentDate.Month] + 1).ToString("D5");
//Console.WriteLine(id);
serial_no[currentDate.Month]++;
return id;
}
}
abstract class Account
{
public readonly string name;
public readonly string ID;
public readonly DOB DOB;
public readonly string nominee;
protected double balance;
protected string type;
public abstract bool diposit(double amount);
public abstract bool withdraw(double amount);
public double getBalance()
{
return this.balance;
}
public string getAccType()
{
return this.type;
}
public void printAccount()
{
Console.WriteLine("\n======================");
Console.WriteLine("Account Details:");
Console.WriteLine("======================");
Console.WriteLine("Name : " + this.name);
Console.WriteLine("ID : " + this.ID);
Console.WriteLine("Acc Type: " + this.type);
Console.WriteLine("DOB : " + this.DOB.getDOB());
Console.WriteLine("Nominee : " + this.nominee);
Console.WriteLine("Balance : " + this.balance);
Console.WriteLine("\n======================\n");
}
public Account()
{
this.name = "Unknown";
this.ID = IDGENERATOR.generate();
this.DOB = new DOB();
this.nominee = "Unknown";
this.balance = 0;
}
public Account(string name, DOB DOB, string nominee, double balance)
{
this.name = name;
this.ID = IDGENERATOR.generate();
this.DOB = DOB;
this.nominee = nominee;
this.balance = balance;
}
}
class Debit : Account
{
private double maxBalance = 100000;
private double dailyTransLimit = 20000;
private DateTime prevTransDate = new DateTime(1950, 1, 1);
private DateTime currentTransDate;
private double dayTrans = 0;
public Debit() : base()
{
base.type = "Debit";
Console.WriteLine("Debit Account created successfully with default values...!");
}
public Debit(string name, DOB DOB, string nominee, double balance) : base(name, DOB, nominee, balance)
{
base.type = "Debit";
Console.WriteLine("Debit Account created successfully with input values...!");
}
// Daily Transaction Limit Check
private bool isDailyTransLimitOver(double amount)
{
currentTransDate = DateTime.Now.Date;
if (amount > 0 && currentTransDate.CompareTo(prevTransDate) != 0)
{
this.dayTrans = 0;
this.dayTrans += amount;
this.prevTransDate = currentTransDate;
}
else
{
this.dayTrans += amount;
}
return (this.dayTrans <= dailyTransLimit && this.balance+this.dayTrans < maxBalance) ? false : true;
}
public override bool diposit(double amount)
{
if (amount > 0 && !isDailyTransLimitOver(amount) && this.balance <= maxBalance)
{
this.balance += amount;
return true;
}
else
{
return false;
}
}
public override bool withdraw(double amount)
{
if (amount > 0 && !isDailyTransLimitOver(amount) && this.balance > amount)
{
this.balance -= amount;
return true;
}
else
{
return false;
}
}
}
class Credit : Account
{
private double minBalance = -100000;
private double dailyWithdrawLimit = 20000;
private DateTime prevTransDate = new DateTime(1950, 1, 1);
private DateTime currentTransDate;
private double dayWithdraw = 0;
public Credit() : base()
{
base.type = "Credit";
Console.WriteLine("Credit Account created successfully with default values...!");
}
public Credit(string name, DOB DOB, string nominee, double balance) : base(name, DOB, nominee, balance)
{
base.type = "Credit";
Console.WriteLine("Credit Account created successfully with input values...!");
}
// Daily Withdraw Limit Check
private bool isDailyWithdrawLimitOver(double amount)
{
currentTransDate = DateTime.Now.Date;
if (amount > 0 && currentTransDate.CompareTo(prevTransDate) != 0)
{
this.dayWithdraw = 0;
this.dayWithdraw += amount;
this.prevTransDate = currentTransDate;
}
else
{
this.dayWithdraw += amount;
}
return (this.dayWithdraw <= dailyWithdrawLimit && (this.balance - amount) > minBalance) ? false : true;
}
public override bool diposit(double amount)
{
if (amount > 0)
{
this.balance += amount;
return true;
}
else
{
return false;
}
}
public override bool withdraw(double amount)
{
if (amount > 0 && !isDailyWithdrawLimitOver(amount))
{
this.balance -= amount;
return true;
}
else
{
return false;
}
}
}
class Savings : Account
{
public Savings() : base()
{
base.type = "Savings";
Console.WriteLine("Savings Account created successfully with default values...!");
}
public Savings(string name, DOB DOB, string nominee, double balance) : base(name, DOB, nominee, balance)
{
base.type = "Savings";
Console.WriteLine("Savings Account created successfully with input values...!");
}
public override bool diposit(double amount)
{
if (amount > 0)
{
this.balance += amount;
return true;
}
else
{
return false;
}
}
public override bool withdraw(double amount)
{
if (amount > 0 && this.balance > amount)
{
this.balance -= amount;
return true;
}
else
{
return false;
}
}
}
class Bank
{
public ArrayList accArr = new ArrayList();
private Account GetAcc(string ID)
{
foreach (Account acc in accArr)
{
if (acc.ID == ID)
{
return acc;
}
}
throw new System.ArgumentOutOfRangeException();
}
public Account this[string ID]
{
get
{
try
{
return (GetAcc(ID));
}
catch (ArgumentOutOfRangeException ae)
{
Console.WriteLine("Error: Account can not be found with id: " + ID);
return null;
}
}
}
public void create_account()
{
int accType;
string name;
int d, m, y;
string nominee;
double balance;
Console.WriteLine("Enter account Type : ");
Console.WriteLine("1. Debit Account");
Console.WriteLine("2. Credit Account");
Console.WriteLine("3. Savings Account");
accType = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter account name : ");
name = Console.ReadLine();
Console.WriteLine("Enter Date of Birth (dd-mm-yyyy) : ");
d = Int32.Parse(Console.ReadLine());
m = Int32.Parse(Console.ReadLine());
y = Int32.Parse(Console.ReadLine());
DOB dob = new DOB();
dob.set(d, m, y);
Console.WriteLine("Enter Nominee name : ");
nominee = Console.ReadLine();
Console.WriteLine("Enter account Balance : ");
balance = Double.Parse(Console.ReadLine());
switch (accType)
{
case 1:
accArr.Add(new Debit(name, dob, nominee, balance));
break;
case 2:
accArr.Add(new Credit(name, dob, nominee, balance));
break;
case 3:
accArr.Add(new Savings(name, dob, nominee, balance));
break;
default:
accArr.Add(new Debit(name, dob, nominee, balance));
break;
}
}
public void deposit(string ID, double amount)
{
try
{
Account acc = GetAcc(ID);
acc.diposit(amount);
Console.WriteLine("Deposit Successful.");
}
catch (ArgumentOutOfRangeException ae)
{
Console.WriteLine("Error: Account can not be found with id: " + ID);
}
}
public void withdraw(string ID, double amount)
{
try
{
Account acc = GetAcc(ID);
acc.withdraw(amount);
Console.WriteLine("Withdraw Successful.");
}
catch (ArgumentOutOfRangeException ae)
{
Console.WriteLine("Error: Account can not be found with id: " + ID);
}
}
public void print(string ID)
{
try
{
GetAcc(ID).printAccount();
}
catch (ArgumentOutOfRangeException ae)
{
Console.WriteLine("Error: Account can not be found with id: " + ID);
}
}
public void printAllAccount()
{
if (accArr.Count < 1)
{
Console.WriteLine("\nError: No account found. Create an account first");
return;
}
Console.WriteLine("ID\t\tName\t\t\tBalance\t\tAccType");
Console.WriteLine("==\t\t====\t\t\t=======\t\t=======");
foreach (Account acc in accArr)
{
Console.WriteLine("{0}\t{1}\t\t{2}\t\t{3}", acc.ID, acc.name, acc.getBalance(), acc.getAccType());
}
}
}
class Program
{
static void Main(string[] args)
{
// Bank Class Test
//==========================
int choice;
Bank bank = new Bank();
DOB dob1 = new DOB();
dob1.set(31, 12, 1995);
bank.accArr.Add(new Debit("Rony Ahmed", dob1, "M Mukit", 5000));
bank.accArr.Add(new Credit("Anamul Haque", dob1, "M Mukit", 2000));
bank.accArr.Add(new Savings("Anik Hasan", dob1, "M Mukit", 1000));
Console.WriteLine("**** Welcome to Bank Management System ***");
while (true)
{
Console.WriteLine("\nWhat you want to do:\n");
Console.WriteLine("1. Create account");
Console.WriteLine("2. Show account information");
Console.WriteLine("3. Deposit from account");
Console.WriteLine("4. Withdraw from account");
Console.WriteLine("5. Show all account with id");
Console.WriteLine("6. Clear screen");
Console.WriteLine("7. Exit");
choice = Int32.Parse(Console.ReadLine());
string id = "";
double amount = 0;
switch (choice)
{
case 1:
bank.create_account();
break;
case 2:
Console.WriteLine("Enter Account ID: ");
id = Console.ReadLine();
bank.print(id);
//bank[id].printAccount();
break;
case 3:
Console.WriteLine("Enter Account ID: ");
id = Console.ReadLine();
Console.WriteLine("Enter Deposit Amount: ");
amount = Double.Parse(Console.ReadLine());
bank.deposit(id, amount);
bank.print(id);
break;
case 4:
Console.WriteLine("Enter Account ID: ");
id = Console.ReadLine();
Console.WriteLine("Enter Withdraw Amount: ");
amount = Double.Parse(Console.ReadLine());
bank.withdraw(id, amount);
bank.print(id);
break;
case 5:
bank.printAllAccount();
break;
case 6:
Console.Clear();
Console.WriteLine("**** Welcome to Bank Management System ***");
break;
case 7:
return;
}
}
// DOB Class Test.
//========================
//DOB dob = new DOB();
//if (dob.set(6, 5, 1995))
//{
// Console.WriteLine("DOB set successful.");
//}
//else
//{
// Console.WriteLine("Error: DOB can not be set...!");
//}
// IDGENERATOR Class Test
//========================
//IDGENERATOR.generate();
//IDGENERATOR.generate();
//IDGENERATOR.generate();
//IDGENERATOR.generate();
// Debit Account Class Test
//========================
//DOB dob1 = new DOB();
//dob1.set(31, 12, 1995);
//Debit d1 = new Debit("Rony Ahmed", dob1, "M Mukit", 1000);
//d1.printAccount();
//if (d1.diposit(10000))
//{
// Console.WriteLine("Deposite Successful.");
//}
//else
//{
// Console.WriteLine("Error: Transiction Limit exceeds or Maximum Balance exceeds.");
//}
//d1.printAccount();
// Daily trans exceed test
//========================
//if (d1.diposit(20000))
//{
// Console.WriteLine("Deposite Successful.");
//}
//else
//{
// Console.WriteLine("Error: Transiction Limit exceeds or Maximum Balance exceeds.");
//}
//d1.printAccount();
//Debit d2 = new Debit();
//d2.printAccount();
//DOB dob2 = new DOB();
//dob2.set(1, 1, 1995);
//Debit d3 = new Debit("Anamul Haque", dob2, "M Mukit", 5000);
//d3.printAccount();
// Credit Account Class Test
//========================
//DOB dob1 = new DOB();
//dob1.set(31, 12, 1995);
//Credit c1 = new Credit("Rony Ahmed", dob1, "M Mukit", -2000000);
//c1.printAccount();
//if (c1.diposit(10000))
//{
// Console.WriteLine("Deposite Successful.");
//}
//else
//{
// Console.WriteLine("Error: Transiction Limit exceeds or Minimum Balance exceeds.");
//}
//c1.printAccount();
// Daily withdraw exceed test
//========================
//if (c1.withdraw(1000))
//{
// Console.WriteLine("Withdraw Successful.");
//}
//else
//{
// Console.WriteLine("Error: Daily Withdraw Limit exceeds or Minimum Balance exceeds.");
//}
//c1.printAccount();
//Debit c2 = new Debit();
//c2.printAccount();
//DOB dob2 = new DOB();
//dob2.set(1, 1, 1995);
//Debit c3 = new Debit("Anamul Haque", dob2, "M Mukit", 5000);
//c3.printAccount();
// Saving Account Class Test
//==========================
//DOB dob1 = new DOB();
//dob1.set(31, 12, 1995);
//Savings s1 = new Savings("Rony Ahmed", dob1, "M Mukit", 1000);
//s1.printAccount();
//if (s1.diposit(2000))
//{
// Console.WriteLine("Deposite Successful.");
//}
//else
//{
// Console.WriteLine("Error: Invalid amount.");
//}
//s1.printAccount();
//if (s1.withdraw(1000))
//{
// Console.WriteLine("Withdraw Successful.");
//}
//else
//{
// Console.WriteLine("Error: Daily Withdraw Limit exceeds or Minimum Balance exceeds.");
//}
//s1.printAccount();
}
}
}
@rhysevans371a
Copy link

I'm trying to do a similar program and when I'm writing the classes I'm getting errors with regards to variable double amount. Where does it come from, where is it declared?

@mimukit
Copy link
Author

mimukit commented Jul 11, 2019

@rhysevans371a the double amount used as a parameter of those deposit and withdraw functions. You should pass the amount value as parameter when you call those function. That's why double amount is not declared anywhere separately like variable. I hope you understand now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment