Exemplo n.º 1
0
        public bool RevertFundsTransfer(Bank banKSource, Bank bankDest, FundsTransfer transaction)
        {
            string   sourceId = transaction.SourceId;
            string   destinationId = transaction.destinationAccId;
            Customer sender = null, reciever = null;
            float    amountFlow = transaction.Amountflow;

            sender   = this.CustomerExistsOrNotByAccId(banKSource, sourceId);
            reciever = this.CustomerExistsOrNotByAccId(banKSource, destinationId);

            if (reciever != null && sender != null)
            {
                if (reciever.GetAccount().IsBalanceSufficient(-1 * amountFlow))
                {
                    sender.AddBalance(-1 * amountFlow);
                    reciever.AddBalance(amountFlow);
                    bankDest.transactions.Add(new FundsTransfer(destinationId, bankDest.Id, amountFlow, sourceId, banKSource.Id));
                    if (sender.bankId != reciever.bankId)
                    {
                        banKSource.transactions.Add(new FundsTransfer(destinationId, transaction.destinationBankId, amountFlow * -1, sourceId, transaction.SourceBankId));
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public void DebitInSourceAccountAndCreditInTarjetAccountRefactored()
        {
            /*
             * // Not Relevant stuff for this test case
             * var firstName = "John";
             * var lastName = "Doe";
             * var fiscalIdentifier = Guid.NewGuid().ToString("N");
             * var accountOwner = new Customer(firstName, lastName, fiscalIdentifier);
             * var branch = new Branch("MainBranch", 1);
             */

            // var sourceAccount = new BankAccount(accountOwner, branch);
            // sourceAccount.Credit(100);
            var sourceAccount = ObjectMother.CreateBankAccount().WithBalance(500m);

            // var targetAccount = new BankAccount(accountOwner, branch);;
            // targetAccount.Credit(100);
            var targetAccount = ObjectMother.CreateBankAccount().WithBalance(100m);

            var fundsTransfer = new FundsTransfer(sourceAccount, targetAccount, 100m);

            fundsTransfer.Execute();

            Assert.That(sourceAccount.Balance, Is.EqualTo(400));
            Assert.That(targetAccount.Balance, Is.EqualTo(200));
            Assert.That(fundsTransfer.State, Is.EqualTo(FundsTransferState.Completed));
        }
Exemplo n.º 3
0
        public ActionResult PublishMessage(FundsTransfer fundsTransfer)
        {
            var factory = ObjectFactory.GetBusManager <FundsTransfer>();

            factory.SendMessage(fundsTransfer, new ApplicationInfo {
                ApplicationName = "WebApp"
            });
            return(View(Index_VIEW, fundsTransfer));
        }
Exemplo n.º 4
0
        public void FundTransferStateIsPendingWhenCreated_Refactored()
        {
            var sourceAccount = ObjectMother.CreateBankAccount();
            var targetAccount = ObjectMother.CreateBankAccount();

            var fundsTransfer = new FundsTransfer(sourceAccount, targetAccount, 100m);

            fundsTransfer.State.Should().Be(FundsTransferState.Pending);
        }
Exemplo n.º 5
0
        public void StatsIsPendingWhenCreated()
        {
            var firstName        = "John";
            var lastName         = "Doe";
            var fiscalIdentifier = Guid.NewGuid().ToString("N");
            var accountOwner     = new Customer(firstName, lastName, fiscalIdentifier);
            var address          = new Address("Long Avenue", 1234, "London");
            var branch           = new Branch("MainBranch", 1, address);
            var sourceAccount    = new BankAccount(accountOwner, branch);
            var targetAccount    = new BankAccount(accountOwner, branch);
            var transferAmount   = 100m;
            var fundsTransfer    = new FundsTransfer(sourceAccount, targetAccount, transferAmount);

            Assert.That(fundsTransfer.State, Is.EqualTo(FundsTransferState.Pending));
        }
        public void FundTransferStateIsPendingWhenCreated()
        {
            // arrange
            var fiscalIdentifier = TestDataGenerator.NewGuid();
            var accountOwner     = new Customer("John", "Doe", fiscalIdentifier);
            var address          = new Address("Long Avenue", 1234, "London");
            var branch           = new Branch("MainBranch", 1, address);
            var sourceAccount    = new BankAccount(accountOwner, branch);
            var targetAccount    = new BankAccount(accountOwner, branch);

            // act
            var fundsTransfer = new FundsTransfer(sourceAccount, targetAccount, 100m);

            // assert
            Assert.That(fundsTransfer.State, Is.EqualTo(FundsTransferState.Pending));
        }
Exemplo n.º 7
0
        public void StateChangesToFailedWhenExecuteFails()
        {
            var firstName        = "John";
            var lastName         = "Doe";
            var fiscalIdentifier = Guid.NewGuid().ToString("N");
            var accountOwner     = new Customer(firstName, lastName, fiscalIdentifier);

            accountOwner.SetBlocked();
            var address        = new Address("Long Avenue", 1234, "London");
            var branch         = new Branch("MainBranch", 1, address);
            var sourceAccount  = new BankAccount(accountOwner, branch);
            var targetAccount  = new BankAccount(accountOwner, branch);
            var transferAmount = 100m;
            var fundsTransfer  = new FundsTransfer(sourceAccount, targetAccount, transferAmount);

            Assert.Throws <InvalidBankOperationException>(() => fundsTransfer.Execute());

            Assert.That(fundsTransfer.State, Is.EqualTo(FundsTransferState.Failed));
        }
        public void FundsTransferDebitInSourceAccountAndCreditInTarjetAccount()
        {
            var fiscalIdentifier = TestDataGenerator.NewGuid();
            var accountOwner     = new Customer("John", "Doe", fiscalIdentifier);
            var address          = new Address("Long Avenue", 1234, "London");
            var branch           = new Branch("MainBranch", 1, address);
            var sourceAccount    = new BankAccount(accountOwner, branch);

            sourceAccount.Credit(500);
            var targetAccount = new BankAccount(accountOwner, branch);;

            targetAccount.Credit(100);
            var fundsTransfer = new FundsTransfer(sourceAccount, targetAccount, 100m);

            // act
            fundsTransfer.Execute();

            Assert.That(sourceAccount.Balance, Is.EqualTo(400));
            Assert.That(targetAccount.Balance, Is.EqualTo(200));
            Assert.That(fundsTransfer.State, Is.EqualTo(FundsTransferState.Completed));
        }
Exemplo n.º 9
0
        public void FundsTransferDebitInSourceAccountAndCreditInTarjetAccount_Refactored()
        {
            var sourceAccount = ObjectMother.CreateBankAccount().WithBalance(500m);
            var targetAccount = ObjectMother.CreateBankAccount().WithBalance(100m);

            var fundsTransfer = new FundsTransfer(sourceAccount, targetAccount, 100m);

            fundsTransfer.Execute();

            // traditional nunit assertion
            Assert.AreEqual(sourceAccount.Balance, 400m);

            // "more readable" nunit assertion
            Assert.That(sourceAccount.Balance, Is.EqualTo(400m));

            // fluent assertion
            sourceAccount.Balance.Should().Be(400m);

            targetAccount.Balance.Should().Be(200m);
            fundsTransfer.State.Should().Be(FundsTransferState.Completed);
        }
Exemplo n.º 10
0
        public void DebitInSourceAccountAndCreditInTarjetAccount()
        {
            var firstName        = "John";
            var lastName         = "Doe";
            var fiscalIdentifier = Guid.NewGuid().ToString("N");
            var accountOwner     = new Customer(firstName, lastName, fiscalIdentifier);
            var address          = new Address("Long Avenue", 1234, "London");
            var branch           = new Branch("MainBranch", 1, address);
            var sourceAccount    = new BankAccount(accountOwner, branch);

            sourceAccount.Credit(500);
            var targetAccount = new BankAccount(accountOwner, branch);;

            targetAccount.Credit(100);
            var transferAmount = 100m;

            var fundsTransfer = new FundsTransfer(sourceAccount, targetAccount, transferAmount);

            fundsTransfer.Execute();

            Assert.That(sourceAccount.Balance, Is.EqualTo(400));
            Assert.That(targetAccount.Balance, Is.EqualTo(200));
            Assert.That(fundsTransfer.State, Is.EqualTo(FundsTransferState.Completed));
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter The Number OF Bank To SetUp : ");
            int num = Int32.Parse(Console.ReadLine());

            Bank[] bankArray = new Bank[num];
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("Enter " + _8th(i + 1) + " Bank Name : ");
                string name = Console.ReadLine().ToUpper();
                bankArray[i] = new Bank(name);
            }
            while (true)
            {
                Console.WriteLine("There Are Following Bank That Exists : ");
                for (int i = 0; i < num; i++)
                {
                    Console.WriteLine(_8th(i + 1) + " Bank : " + bankArray[i].name);
                }
                Console.WriteLine("Please Enter Bank Name You Are Associated With : ");
                string name     = Console.ReadLine().ToUpper();
                Bank   userBank = null;
                for (int i = 0; i < num; i++)
                {
                    if (name.Equals(bankArray[i].name))
                    {
                        userBank = bankArray[i];
                        break;
                    }
                }
                if (userBank == null)
                {
                    Console.WriteLine("Please, Enter A Valid Bank Name . . .");
                    continue;
                }
                //Enter In A Bank
                while (true)
                {
                    Console.WriteLine("<<< Welcome, To " + userBank.name + " Bank >>>");
                    Console.WriteLine("1. Staff\n2. Customer\nEnter(1/2) : ");
                    char choice = Console.ReadLine()[0];
                    if (choice == '1')
                    {
                        Console.WriteLine("Hello Staff, Enter Your User Id ");
                        string stfId = Console.ReadLine();
                        Console.WriteLine("Enter Your LogIn Password ");
                        string pword = Console.ReadLine();
                        Staff  staff = (new BankService()).StaffExistsOrNot(userBank, stfId, pword);
                        if (staff == null)
                        {
                            Console.WriteLine("Please Enter Valid Credentials . . .");
                            continue;
                        }
                        bool staffWish = true;
                        while (staffWish)
                        {
                            Console.WriteLine("1.Add Account\n2.DeleteAccount\n3.Update Account\n4.ChangeCurrency\n5.ChangeServiceCharge\n6.View Transaction History\n7.Revert A Transaction\nE. Exit\nYour Choice . . . ");
                            char options = Console.ReadLine()[0];
                            switch (options)
                            {
                            case '1':
                                Console.WriteLine("Enter Customer Name : ");
                                string ename = Console.ReadLine();
                                Console.WriteLine("Enter Password : "******"The User Id is : " + customer.UserId);
                                    Console.WriteLine("The Account Id Is : " + customer.GetAccount().AccountId);
                                }
                                else
                                {
                                    Console.WriteLine("Account Already Exists . . .");
                                }
                                break;

                            case '2':
                                Console.WriteLine("Enter Account Id To Delete ->> ");
                                string accId     = Console.ReadLine();
                                bool   doneOrNot = (new StaffService().DeleteAccount(userBank, accId));
                                if (doneOrNot)
                                {
                                    Console.WriteLine("Account Successfully Deleted");
                                }
                                else
                                {
                                    Console.WriteLine("Account Do Not Exists");
                                }
                                break;

                            case '3':
                                Console.WriteLine("Enter The UserId To Update : ");
                                string UserId = Console.ReadLine();
                                Console.WriteLine("Enter The New Password : "******"<<<-- Password Updated -->>>");
                                }
                                else
                                {
                                    Console.WriteLine("No Such User Exists");
                                }
                                break;

                            case '4':
                                Console.WriteLine("Enter New Currency Name : ");
                                string currencyName = Console.ReadLine();
                                Console.WriteLine("Enter Rate : ");
                                float rate = float.Parse(Console.ReadLine());
                                new StaffService().ChangeCurrency(userBank, currencyName, rate);
                                break;

                            case '5':
                                Console.WriteLine("Enter IMPS and RTGS Rate : ");
                                int IMPS = Int32.Parse(Console.ReadLine());
                                int RTGS = Int32.Parse(Console.ReadLine());
                                Console.WriteLine("Enter Foreign IMPS and RTGS Rate : ");
                                int fIMPS = Int32.Parse(Console.ReadLine());
                                int fRTGS = Int32.Parse(Console.ReadLine());
                                new StaffService().ChangeRates(userBank, IMPS, RTGS, fIMPS, fRTGS);
                                break;

                            case '6':
                            {
                                Console.WriteLine("Enter Account Id For Which Trnsaction History To Look Up -->> ");
                                string AccountId      = Console.ReadLine();
                                bool   recieverExists = false;
                                for (int i = 0; i < bankArray.Length; i++)
                                {
                                    if ((new BankService().CustomerExistsOrNotByAccId(bankArray[i], AccountId)) != null)
                                    {
                                        recieverExists = true;
                                        break;
                                    }
                                }
                                if (!recieverExists)
                                {
                                    Console.WriteLine("Account Do Not Exists . . .");
                                }
                                else
                                {
                                    List <Transaction> transactions = new StaffService().ViewTransactionHistory(userBank, AccountId);
                                    if (transactions == null)
                                    {
                                        Console.WriteLine("No Transaction Exists . . .");
                                        break;
                                    }
                                    Console.WriteLine("There Are Following Transactions : ");
                                    for (int i = 0; i < transactions.Count; i++)
                                    {
                                        Console.WriteLine(transactions[i].ToString());
                                    }
                                }
                            }
                            break;

                            case '7':
                            {
                                Console.WriteLine("Enter Account Id For Which Trnsaction History To Look Up -->> ");
                                string AccountId = Console.ReadLine();
                                if (new BankService().CustomerExistsOrNotByAccId(userBank, AccountId) == null)
                                {
                                    Console.WriteLine("No Such Account Exists In This Bank ! ! !");
                                    break;
                                }
                                List <Transaction> transactions = new StaffService().ViewTransactionHistory(userBank, AccountId);
                                if (transactions == null)
                                {
                                    Console.WriteLine("No Transaction Exists . . .");
                                    break;
                                }
                                for (int i = 0; i < transactions.Count; i++)
                                {
                                    Console.WriteLine(transactions[i].ToString());
                                }
                                int  tchoice = 0;
                                bool eflag   = false;
                                while (true)
                                {
                                    try
                                    {
                                        Console.WriteLine("Enter Your Choice = ");
                                        tchoice = Int32.Parse(Console.ReadLine());
                                        if (tchoice > transactions.Count)
                                        {
                                            Console.WriteLine(">>-- Enter A Valid Choice --<<");
                                            continue;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("Aborting Reverting A Transaction. . .");
                                        eflag = true;
                                        break;
                                    }
                                }
                                if (eflag)
                                {
                                    continue;
                                }
                                Transaction tType = transactions[tchoice - 1];
                                if ((tType is Deposit) || (tType is WithDraw))
                                {
                                    if (!tType.alive)
                                    {
                                        Console.WriteLine("Cannot Revert, Aborting Reverting !!!");
                                        break;
                                    }
                                    bool IsReverted = new StaffService().RevertATransaction(userBank, tType);
                                    if (IsReverted)
                                    {
                                        Console.WriteLine("SuccessFully Reverted . . .");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Insufficient Balance !!!");
                                    }
                                    tType.alive = false;
                                }
                                else
                                {
                                    if (!tType.alive)
                                    {
                                        Console.WriteLine("Cannot Revert, Aborting Reverting !!!");
                                        break;
                                    }
                                    tType.alive = false;
                                    FundsTransfer fT = tType as FundsTransfer;
                                    string        sourceId = fT.SourceId;
                                    string        destId = fT.destinationAccId;
                                    string        sourceBankId = fT.SourceBankId;
                                    string        destBankId = fT.destinationBankId;
                                    Bank          bankSource = null, bankDest = null;
                                    for (int i = 0; i < bankArray.Length; i++)
                                    {
                                        if (bankArray[i].Id.Equals(destBankId))
                                        {
                                            bankDest = bankArray[i];
                                        }
                                        if (bankArray[i].Id.Equals(sourceBankId))
                                        {
                                            bankSource = bankArray[i];
                                        }
                                    }
                                    bool value = new StaffService().RevertFundsTransfer(bankSource, bankDest, tType);
                                    if (value)
                                    {
                                        Console.WriteLine("<<< Transaction Reverted SuccessFully >>>");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Cannot Revert, Aborting Reverting . . .");
                                    }
                                }
                            }
                            break;

                            case 'e':
                            case 'E':
                                staffWish = false;
                                break;
                            }
                        }
                    }
                    else if (choice == '2')
                    {
                        Console.WriteLine("Enter Your Username...");
                        string username = Console.ReadLine();
                        Console.WriteLine("Enter Your Password...");
                        string pword = Console.ReadLine();

                        Customer accHolder  = new BankService().CustomerLogIn(userBank, username, pword);
                        char     choiceUser = '******';
                        bool     exitFlag   = false;
                        while (!exitFlag)
                        {
                            if (accHolder != null)
                            {
                                Console.WriteLine("Enter A Choice\n1.Deposit Amount \n2.Withdraw Amount \n3.Transfer Funds \n4.View Transaction History E.Exit . . .\n");
                                choice = Console.ReadLine()[0];
                            }
                            else
                            {
                                Console.WriteLine("Username Do Not Exists . . .");
                                choice = 'e';
                            }
                            switch (choice)
                            {
                            case '1':
                            {
                                Console.WriteLine("Enter Amount To Deposit : ");
                                float amount = float.Parse(Console.ReadLine());

                                Transaction transaction = new CustomerService().DepositMoney(userBank, accHolder, amount);

                                if (transaction != null)
                                {
                                    Console.WriteLine("SuccessFully Depoisted");
                                }
                                else
                                {
                                    Console.WriteLine("Not Deposited . . .");
                                }
                                break;
                            }

                            case '2':
                            {
                                Console.WriteLine("Enter Amount To Withdraw : ");
                                float       amtF        = float.Parse(Console.ReadLine());
                                Transaction transaction = new CustomerService().WithDrawAmount(userBank, accHolder, amtF);
                                if (transaction != null)
                                {
                                    Console.WriteLine("SuccessFully WithDrawed");
                                }
                                else
                                {
                                    Console.WriteLine("Insufficient Balance <<<< ");
                                }
                                break;
                            }

                            case '4':
                                List <Transaction> transactions = new StaffService().ViewTransactionHistory(userBank, accHolder.GetAccount().AccountId);
                                if (transactions == null)
                                {
                                    Console.WriteLine("No Transactions Exists . . .");
                                    break;
                                }
                                for (int i = 0; i < transactions.Count; i++)
                                {
                                    Console.WriteLine(transactions[i].ToString());
                                }
                                break;

                            case '3':
                                while (true)
                                {
                                    Console.WriteLine("Enter Account Id Of The Reciever : ");
                                    string destAccountId  = Console.ReadLine();
                                    bool   recieverExists = false;
                                    Bank   receiverBank   = null;
                                    Console.WriteLine("Enter Amount To Transfer -->> ");
                                    float amt = float.Parse(Console.ReadLine());

                                    Customer reciever = null;
                                    for (int i = 0; i < bankArray.Length; i++)
                                    {
                                        if ((reciever = new BankService().CustomerExistsOrNotByAccId(bankArray[i], destAccountId)) != null)
                                        {
                                            receiverBank   = bankArray[i];
                                            recieverExists = true;
                                            break;
                                        }
                                    }

                                    if (recieverExists)
                                    {
                                        if (accHolder == reciever)
                                        {
                                            Console.WriteLine("Please, Enter Others Account Id . . .");
                                            continue;
                                        }
                                        bool IsTransfer = new StaffService().FundsTransfer(userBank, receiverBank, amt, accHolder, reciever);
                                        if (IsTransfer)
                                        {
                                            Console.WriteLine("<<<< Transfer SuccessFully >>>>");
                                        }
                                        else
                                        {
                                            Console.WriteLine(">>>> InSufficient Balance <<<<");
                                        }

                                        break;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Reciver Do Not Exists!!!");
                                        break;
                                    }
                                }

                                break;

                            case 'e':
                            case 'E':
                                exitFlag = true;
                                break;
                            }
                        }
                    }
                    else if (choice == 'e')
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Please Enter A Valid Choice . . .");
                        continue;
                    }
                }
            }
        }