Saturday, August 2, 2014

how to write a code for withdrawal using C#

Hello,


The following is not meant to provide a solution for input but food for thought and might want to explore what happens when a balance before a withdraw is possible. So with that said it's best to think about creating a class such as the one below which I am not going to go over as this as said above is food for thought. If it's of interest either learn the about it or do your own.



using System;

namespace Sample
{
public class Account
{
private decimal mWarningLevel;
private decimal mBalance;

public event AccountBalanceWarningEventHandler AccountBalanceWarningEvent;

public event AccountDenyingEventHandler AccountDenialEvent;

public string Number { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public Account()
{
mWarningLevel = 10M;
}

/// <summary>
/// Warning level is when to send an alert via our delegate
/// </summary>
/// <param name="warningLevel"></param>
/// <remarks></remarks>
public Account(decimal warningLevel)
{
mWarningLevel = warningLevel;
mInsufficientFunds = mBalance <= 0M;
}

/// <summary>
/// Current balance of account
/// </summary>
/// <value></value>
/// <returns></returns>
/// <remarks></remarks>
public decimal Balance
{
get
{
return mBalance;
}
}

/// <summary>
/// Deposit money into account
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
/// <remarks></remarks>
public decimal Deposit(decimal amount)
{
mBalance += amount;
if (mBalance < mWarningLevel)
{
if (AccountBalanceWarningEvent != null)
AccountBalanceWarningEvent(this, new AccountBalanceWarningEventArgs(Number, mWarningLevel, mBalance));
}

if (mBalance - amount < 0M)
{
mInsufficientFunds = true;
if (AccountDenialEvent != null)
AccountDenialEvent(this, new AccountDenialEventArgs(DenialReasons.InsufficientFunds));
}
else
{
mInsufficientFunds = false;
}

return mBalance;
}

/// <summary>
/// Withdraw from account
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
/// <remarks></remarks>
public decimal Debit(decimal amount)
{
if (mBalance - amount < 0M)
{
// Deny withdraw
mInsufficientFunds = true;
if (AccountDenialEvent != null)
AccountDenialEvent(this, new AccountDenialEventArgs(DenialReasons.InsufficientFunds));
return mBalance;
}

mBalance -= amount;

if (mBalance < mWarningLevel)
{
if (AccountBalanceWarningEvent != null)
AccountBalanceWarningEvent(this, new AccountBalanceWarningEventArgs(Number, mWarningLevel, mBalance));
}

return mBalance;
}

private bool mInsufficientFunds;

public bool InsufficientFunds
{
get
{
return mInsufficientFunds;
}
}

public override string ToString()
{
return Balance.ToString("c2");
}
}

public class AccountBalanceWarningEventArgs : EventArgs
{
private decimal Level;
private decimal Current;
private string mAccountNumber;

public AccountBalanceWarningEventArgs(string Number, decimal warningLevel, decimal currentBalance)
{
this.Level = warningLevel;
this.Current = currentBalance;
this.mAccountNumber = Number;
}

public string AccountNumber
{
get
{
return mAccountNumber;
}
}

public decimal WarningLevel
{
get
{
return Level;
}
}

public decimal Balance
{
get
{
return Current;
}
}
}

/// <summary>
///
/// </summary>
/// <remarks>
/// SuspiciousActivity is not used here yet
/// in real life banks monitor activity and
/// may flag an account inactive if say five
/// purchases where made in a very short time
/// period.
/// </remarks>
public enum DenialReasons
{
InsufficientFunds,
SuspiciousActivity
}

public class AccountDenialEventArgs : EventArgs
{
private DenialReasons mReason;

public AccountDenialEventArgs(DenialReasons Reason)
{
mReason = Reason;
}

public DenialReasons Reason
{
get
{
return mReason;
}
}
}

public delegate void AccountBalanceWarningEventHandler(object sender, AccountBalanceWarningEventArgs e);

public delegate void AccountDenyingEventHandler(object sender, AccountDenialEventArgs e);
}





Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


No comments:

Post a Comment