0% found this document useful (0 votes)
119 views11 pages

System System - Collections.Generic System - Text Bank (: Using Using Using Namespace

The document discusses an abstract Account class that stores common properties of bank accounts. It defines properties like account number and customer name. Subclasses like SavingsAccount inherit from this abstract class and implement additional functionality like deposit, withdraw, and calculating bank charges. The SavingsAccount class is tested using a SavingsTest class. The document also defines an IAccount interface for fixed deposit accounts to implement.

Uploaded by

Mobile LG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views11 pages

System System - Collections.Generic System - Text Bank (: Using Using Using Namespace

The document discusses an abstract Account class that stores common properties of bank accounts. It defines properties like account number and customer name. Subclasses like SavingsAccount inherit from this abstract class and implement additional functionality like deposit, withdraw, and calculating bank charges. The SavingsAccount class is tested using a SavingsTest class. The document also defines an IAccount interface for fixed deposit accounts to implement.

Uploaded by

Mobile LG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

/*

* Account.cs
*

*/

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

namespace Bank
{
/// <summary>
/// The program demonstrates the use of abstract classes in C#.
///
/// Class Account is an abstract class that stores the common properties of a bank
account.
/// </summary>
public abstract class Account
{
/// <summary>
/// Long field to store the account number.
/// </summary>
private long _accountNumber;

/// <summary>
/// String field to store the customer name.
/// </summary>
private string _customerName;

/// <summary>
/// Double field to store the balance.
/// </summary>
protected double Balance = 500;

/// <summary>
/// Method to set the details of the customer.
/// </summary>
/// <param name="name">Accepts the customer name</param>
/// <param name="number">Accepts the account number</param>
protected virtual void SetDetails(string name, long number)
{
this._customerName = name;
this._accountNumber = number;
}

/// <summary>
/// Method to withdraw money from the account.
/// </summary>
/// <param name="amount">Accepts the amount to be withdrawn</param>
protected virtual void Withdraw(double amount)
{
Balance -= amount;
}

/// <summary>
/// Method to deposit the amount into the account.
/// </summary>
/// <param name="amount">Accepts the amount to be deposited</param>
protected virtual void Deposit(double amount)
{
Balance += amount;
}

/// <summary>
/// Method to display the account details.
/// </summary>
public virtual void Display()
{
Console.WriteLine("\n\nA/C No. \tCustomer Name \t Balance ($)");
Console.WriteLine(_accountNumber + " \t\t " + _customerName + " \t " +
Balance);
}

/// <summary>
/// Abstract method to calculate the bank charges.
/// </summary>
/// <returns>Returns the bank charges applicable</returns>
protected abstract double BankCharges();
}
}
/*
* SavingsAccount.cs
*

*/

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

namespace Bank
{
/// <summary>
/// Class SavingsAccount inherits the abstract base class Account.
///
/// The class accepts the details of the customer
/// and also performs withdrawal and deposit traansactions and displays
/// the balance in the account.
/// </summary>
public class SavingsAccount : Account
{
/// <summary>
/// Float field to store the minimum balance required in the account.
/// </summary>
private float _minBalance;

/// <summary>
/// Float field to store the bank charges.
/// </summary>
private float _bankCharge;

/// <summary>
/// Boolean field to store the flag value.
/// </summary>
public bool Flag = true;
/// <summary>
/// Method to accept the details of the customer and process the transaction.
/// </summary>
/// <returns>Returns a boolean value</returns>
public bool AcceptDetails()
{
// String variable to store the customer name.
string name;

// Long variable to store the account number.


long number;

// Double variable to store the amount to be deposited or withdrawn.


double amount;

Console.Write("Enter the customer name: ");


name = Console.ReadLine();

First:
Console.Write("Enter the account number: ");
number = Convert.ToInt64(Console.ReadLine());

// Check if the account number is less than or equal to zero.


if (number <= 0)
{
Console.WriteLine("Invalid Account Number");
goto First;
}
else
{
// Invoking the overriden SetDetails method.
SetDetails(name, number);
}

Console.WriteLine("\nInitial balance available: " + Balance + "\n\n");

Console.WriteLine("Select the type of transaction: ");


Console.WriteLine(" 1. Withdraw \n 2. Deposit");
Console.Write("Select your choice : ");
int choice = Convert.ToInt32(Console.ReadLine());

switch (choice)
{
case 1:
Console.WriteLine("Minimum balance to be maintained: " + _minBalance
+ " $\n");
Console.Write("Enter the amount to be withdrawn: ");
amount = Convert.ToDouble(Console.ReadLine());
Withdraw(amount);
break;
case 2:
Console.Write("Enter the amount to be deposited: ");
amount = Convert.ToDouble(Console.ReadLine());
Deposit(amount);
break;
default:
Console.WriteLine("Invalid Choice");
return false;
}

if (!Flag)
{
return false;
}
else
{
return true;
}
}

/// <summary>
/// Method to set the details of the customer.
/// </summary>
/// <param name="name">Accepts the name of the customer</param>
/// <param name="number">Accepts the account number of the customer</param>
protected override void SetDetails(string name, long number)
{
base.SetDetails(name, number);
_minBalance = 500;
}

/// <summary>
/// Method to calculate the bank charges.
/// </summary>
/// <returns>Returns the bank charge</returns>
protected override double BankCharges()
{
if (Balance < _minBalance)
{
_bankCharge = 200;
}
return _bankCharge;
}

/// <summary>
/// Method to deposit the money in the account.
/// </summary>
/// <param name="amount">Accepts the amount to be deposited</param>
protected override void Deposit(double amount)
{
base.Deposit(amount);
}

/// <summary>
/// Method to withdraw money from the account.
/// </summary>
/// <param name="amount">Accepts the amount to be withdrawn</param>
protected override void Withdraw(double amount)
{
// Checking whether the amount to be withdrawn is less than the balance
available.
if (amount <= Balance)
{
base.Withdraw(amount);
}
else
{
Console.WriteLine("You do not have sufficient balance in your account");
Flag = false;
return;
}
}

/// <summary>
/// Method to display the account details.
/// </summary>
public override void Display()
{
base.Display();

if (Balance <= 500)


{
Console.WriteLine("\nBank Charges for less than minimum balance: " +
BankCharges());
}
}
}
}
/*
* SavingsTest.cs
*

*/

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

namespace Bank
{
/// <summary>
/// Class SavingsTest is used to test the SavingsAccount class.
/// </summary>
class SavingsTest
{
/// <summary>
/// The entry point for the application.
/// </summary>
/// <param name="args">A list of command line arguments</param>
static void Main(string[] args)
{
// Creating an object of the SavingsAccount class.
SavingsAccount objSavings = new SavingsAccount();

if (objSavings.AcceptDetails())
{
// Invoking the Display method of the SavingsAccount class.
objSavings.Display();
}
else
{
return;
}
Console.ReadKey();
}
}
}

/*
* IAccount.cs
*

*/

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

namespace Bank
{
/// <summary>
/// The program demostrates the use of an interface in C#.
///
/// Interface IAccount consists of four method declarations.
/// </summary>
public interface IAccount
{
bool AcceptDetails();
double CalculateInterest();
double GetBalance();
void Display();
}
}

/*
* FixedAccount.cs
*

*/

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

namespace Bank
{
/// <summary>
/// Class FixedAccount implements the IAccount interface.
/// The class accepts the deposit amount details, calculates the interest
/// and displays the output.
/// </summary>
class FixedAccount : IAccount
{
/// <summary>
/// String field to store the account holder name.
/// </summary>
private string _accountHolder;

/// <summary>
/// Long field to store the account number.
/// </summary>
private long _accountNumber;

/// <summary>
/// Float field to store the interest rate.
/// </summary>
private float _interestRate;

/// <summary>
/// Integer field to store the years of deposit.
/// </summary>
private int _numberOfYears;

/// <summary>
/// Double field to store the interest earned on the deposit amount.
/// </summary>
private double _interestEarned;

/// <summary>
/// Double field to store the total balance including the interest earned.
/// </summary>
private double _totalBalance;

/// <summary>
/// Double field to store the amount deposited by the user.
/// </summary>
private double _deposit;

#region IAccount Members

/// <summary>
/// Method to accept the details of account holder and the amount deposited.
/// </summary>
/// <returns>Returns a boolean value</returns>
public bool AcceptDetails()
{
// String variable to store the user input
string input;

Console.Write("Enter the name of the account holder: ");


_accountHolder = Console.ReadLine();

Number:
Console.Write("Enter the account number: ");
input = Console.ReadLine();
_accountNumber = Convert.ToInt64(input);

if (_accountNumber <= 0)
{
Console.WriteLine("Account number cannot be zero or negative\n");
goto Number;
}

Amount:
Console.Write("Enter the deposit amount ($): ");
_deposit = Convert.ToDouble(Console.ReadLine());

if (_deposit <= 0)
{
Console.WriteLine("Invalid amount");
goto Amount;
}

Console.WriteLine("Select the tenure for the deposit amount: ");


Console.WriteLine("\nA. \t1 year \nB. \t3 years \nC. \t5 years");
Console.Write("\nChoose your option (A - C): ");

char choice = Convert.ToChar(Console.ReadLine());

switch (choice)
{
case 'A':
case 'a':
_numberOfYears = 1;
break;
case 'B':
case 'b':
_numberOfYears = 3;
break;
case 'C':
case 'c':
_numberOfYears = 5;
break;
default:
Console.WriteLine("Invalid choice\a");
return false;
}
return true;
}

/// <summary>
/// Method to calculate the interest earned on the amount deposited.
/// </summary>
/// <returns>Returns the interest amount earned</returns>
public double CalculateInterest()
{
if (_deposit <= 50000)
{
if (_numberOfYears == 1)
{
_interestRate = 3.5F;
}
else if (_numberOfYears == 3)
{
_interestRate = 4;
}
else
{
_interestRate = 4.5F;
}
}
else if ((_deposit > 50000) && (_deposit <= 200000))
{
if (_numberOfYears == 1)
{
_interestRate = 5;
}
else if (_numberOfYears == 3)
{
_interestRate = 5.5F;
}
else
{
_interestRate = 6;
}
}
else
{
if (_numberOfYears == 1)
{
_interestRate = 6.5F;
}
else if (_numberOfYears == 3)
{
_interestRate = 7;
}
else
{
_interestRate = 7.5F;
}
}
return ((_deposit * _numberOfYears * _interestRate) / 100);
}

/// <summary>
/// Method to calculate the total amount received on the maturity date.
/// </summary>
/// <returns>Returns the total amount</returns>
public double GetBalance()
{
_interestEarned = CalculateInterest();
return (_deposit + _interestEarned);
}

/// <summary>
/// Method to display the account details.
/// </summary>
public void Display()
{
_totalBalance = GetBalance();

Console.WriteLine("\nAccount Holder: \t" + _accountHolder);


Console.WriteLine("Account Number: \t" + _accountNumber);
Console.WriteLine("Amount Deposited: \t" + _deposit + " $");
Console.WriteLine("Interest Rate: \t\t" + _interestRate + " %");
Console.WriteLine("Years of deposit: \t" + _numberOfYears);
Console.WriteLine("Interest Earned: \t" + _interestEarned + " $");
Console.WriteLine("Maturity Amount: \t" + _totalBalance + " $");
}

#endregion
}
}

/*
* FixedAccountTest.cs
*

*/

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

namespace Bank
{
/// <summary>
/// Class FixedAccountTest is used to test the FixedAccount class.
/// </summary>
class FixedAccountTest
{
/// <summary>
/// The entry point for the application.
/// </summary>
/// <param name="args">A list of command line arguments</param>
static void Main(string[] args)
{
// Creating an object of the FixedAccount class
FixedAccount objFixed = new FixedAccount();
Console.WriteLine("\t\t\tFIXED ACCOUNT DETAILS\n");

// Character variable to store the user choice


char choice;

do
{
// Invoking and checking the return value of the AcceptDetails
// of FixedAccount class
if (!objFixed.AcceptDetails())
{
return;
}
else
{
// Invoking the Display method of the FixedAccount class
objFixed.Display();
}
Console.Write("\nDo you wish to continue [Y | N]:");
choice = Convert.ToChar(Console.ReadLine());
Console.WriteLine();
}
while ((choice == 'y') || (choice == 'Y'));
Console.ReadKey();
}
}
}

You might also like