Alain Ghawi
Alain Ghawi

Reputation: 95

Implementing the best model for a bank module

I am implementing a bank module in c# which contains a savings account, a checkings account and an easy saving account. All accounts have an owner and a balance and all of them can withdraw and deposit money but they can't withdraw more than the balance. Till here, very easy. Now, our savings account has a new method applyInterest and checkingsAccount a method deductFees and easySavinAccount has both. What I thought about is using an abstract class Account:

 public abstract class Account
{
    protected string owner { get; set; }
    //always use decimal especially for money c# created them for that purpose :)
    protected decimal balance { get; set; }
    public void deposit(decimal money)
    {
        if (money >= 0)
        {
            balance += money;
        }
    }
    public void withdraw(decimal money)
    {
        if (money > balance)
        {
            throw new System.ArgumentException("You can't withdraw that much money from your balance");
        }
        else balance -= money;
    }
}

Which will be inherited by all 3 classes. Is there a design pattern suited to implement this in a better way? Especially for easySaveAccount maybe composition can help?

Thanks!

Upvotes: 2

Views: 652

Answers (2)

user3797387
user3797387

Reputation:

I would suggest create a class Balance that implements IBalance. All Account's may just delegate withdraw\deposit to that class, so their no code duplication, but you easily can add some additional logic around it (i. e. take taxes, сommission, add transactions, etc.)

Upvotes: 1

ashish
ashish

Reputation: 2358

I would suggest

1.implement separate interfaces declaring the methods applyInterest and deductFees.
2.You have already declared the abstract class Account.
3.Now you can implement these interfaces in your classes for savings,checkings and easy saving account.All these classes should
be implementing the abstract class.

Upvotes: 3

Related Questions