Untitled

 avatar
unknown
csharp
2 years ago
3.0 kB
6
Indexable
using System;
using System.Collections.Generic;


namespace BankLibrary
{
    class BankException:ApplicationException
        {
            public BankException(string message):base(message)
            {}
        }

    public class SBTransaction
    {
        public int TransactionId{get;set;}
        public DateTime TransactionDate{get;set;}
        public int AccountNumber{get;set;}
        public decimal Amount{get;set;}
        public String TransactionType{get;set;}

        public SBTransaction(int TID, DateTime TD, int AN, decimal A, String TT)
        {
            TransactionId = TID;
            TransactionDate = TD;
            AccountNumber = AN;
            Amount = A;
            TransactionType = TT;
        }
    }
    public class SBAccount
    {
        public int AccountNumber{get;set;}
        public string CustomerName{get;set;}
        public string CustomerAddress{get;set;}
        public decimal CurrentBalance{get;set;}

        public SBAccount(int AN, string CN, string CA, decimal CB)
        {
            AccountNumber = AN;
            CustomerName = CN;
            CustomerAddress = CA;
            CurrentBalance = CB;   
        }
        
        public List<SBTransaction> transactions = new List<SBTransaction>();

    }


    public class BankRepository:IBankRepository
    {
        List<SBAccount> accounts = new List<SBAccount>(); 

        Random rnd  = new Random();

        public void NewAccount(SBAccount acc) { accounts.Add(acc); }
        public List<SBAccount> GetAllAccounts(){ return accounts; }
        public SBAccount GetAccountDetails(int accno)
        {
            foreach (SBAccount acc in accounts)
                if(acc.AccountNumber == accno)
                    return acc;
            throw new BankException("Account not found");
        }

        public void DepositAmount(int accno, decimal amt)
        {
            SBAccount acc = GetAccountDetails(accno);
            acc.CurrentBalance += amt;
            acc.transactions.Add(new SBTransaction(
            Convert.ToInt32(rnd.Next()),
                DateTime.Now,
                accno,
                amt,
                "Deposit"
                ));
        }

        public void WithdrawAmount(int accno, decimal amt)
        {
            SBAccount acc = GetAccountDetails(accno);

            if(acc.CurrentBalance < amt)
              throw new BankException("Not enough funds");

            acc.CurrentBalance -= amt;
            acc.transactions.Add(new SBTransaction(
            
                Convert.ToInt32(rnd.Next()),
                DateTime.Now,
                accno,
                amt,
                "Withdrawal"
                ));
        }

        public List<SBTransaction> GetTransactions(int accno)
        { 
            SBAccount acc = GetAccountDetails(accno);
            return acc.transactions;
        }


    }
}
Editor is loading...