Beispiel #1
0
        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            string acctId = BankControlForm.Bank.GetNewAcctId();

            Vm.ErrorString = String.Empty;
            try
            {
                string      name = acctTextBox.Text;
                int         idx  = acctTypeComboBox.SelectedIndex;
                AccountType type = (AccountType)Enum.Parse(typeof(AccountType), idx.ToString());
                float       amt  = (float)Convert.ToDouble(entryTextBox.Text);
                if (type == AccountType.SIMPLE_CHECKING)
                {
                    Bank.PerformAction(acctId, name, "deposit", amt, type, true);
                }
                else if (type == AccountType.INTEREST_CHECKING)
                {
                    Bank.PerformAction(acctId, name, "accrue", amt, type, true);
                }
                else if (type == AccountType.SAVINGS)
                {
                    Bank.PerformAction(acctId, name, "accrue", amt, type, true);
                }
                else
                {
                    Vm.ErrorString = "Unsupported Checking Type. Choose Simple, Interest or Savings Account Type.";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Vm.ErrorString = ex.Message;
            }
        }
        public override int Display(Bank bank)
        {
            if (bank == null)
            {
                return(1);
            }

            string      entry    = String.Empty;
            string      name     = String.Empty;
            string      acctId   = Bank.InitialID;
            float       amt      = 0F;
            float       balance  = 0F;
            AccountType acctType = AccountType.UNINIT;

            Console.Write("Enter a name: ");
            entry = Console.ReadLine();
            name  = entry;
            while ((String.Compare(entry, "quit", true) != 0) && (String.Compare(entry, "q", true) != 0))
            {
                Console.WriteLine("Account for {0}. Balance: [{1}]" + Environment.NewLine, name, balance);
                Console.WriteLine("What would you like to do? Type:");
                Console.WriteLine("[o]pen - Open/Create a new account.");
                Console.WriteLine("[d]eposit - Deposit entered amount into an Account.");
                Console.WriteLine("[w]ithdraw - Withdraw entered amount from an Account.");
                Console.WriteLine("[b]alance - Print out the balance of an Account.");
                Console.WriteLine("[a]ccrue -- Accrue interest on the Account.");
                Console.WriteLine("[s]witch - Switch to a named Account.");
                Console.WriteLine("[p]rint - Print out details of an Account.");
                Console.WriteLine("[du]mp -- Dump details of all Accounts");
                Console.WriteLine("[q]uit - Quit the program.");
                Console.Write("====> ");
                entry = Console.ReadLine();
                try
                {
                    switch (entry)
                    {
                    case "open":
                    case "o":
                        Console.WriteLine("Opening an Account. What Type? [s]imple, [i]nterest or [sa]vings");
                        string type = Console.ReadLine();
                        Console.Write("Initial Deposit? ");
                        entry  = Console.ReadLine();
                        amt    = (float)Convert.ToDouble(entry);
                        acctId = bank.GetNewAcctId();
                        string cmd = "uninit";
                        if (type == "s")
                        {
                            acctType = AccountType.SIMPLE_CHECKING;
                            cmd      = "deposit";
                        }
                        else if (type == "i")
                        {
                            acctType = AccountType.INTEREST_CHECKING;
                            cmd      = "accrue";
                        }
                        else if (type == "sa")
                        {
                            acctType = AccountType.SAVINGS;
                            cmd      = "accrue";
                        }
                        else
                        {
                            Console.WriteLine("Undefined action: " + type + ". Type one of [s] or [i].");
                        }
                        try
                        {
                            balance = bank.PerformAction(acctId, name, cmd, amt, acctType, true);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        break;

                    case "deposit":
                    case "d":
                        Console.Write("How much do you want to deposit? ");
                        entry = Console.ReadLine();
                        amt   = (float)Convert.ToDouble(entry);
                        try
                        {
                            balance = bank.PerformAction(acctId, name, "deposit", amt);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        break;

                    case "withdraw":
                    case "w":
                        Console.Write("How much do you want to withdraw? Balance: [" + balance + "] ");
                        entry = Console.ReadLine();
                        amt   = (float)Convert.ToDouble(entry);
                        try
                        {
                            balance = bank.PerformAction(acctId, name, "withdraw", amt);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        break;

                    case "balance":
                    case "b":
                        try
                        {
                            Console.WriteLine("Balance is: " + bank.PerformAction(acctId, name, "balance", amt));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        break;

                    case "accrue":
                    case "a":
                        AccountDetailsViewModel details = bank.AccountDetailsByAccountId(acctId);
                        float defaultInterest           = ((details.Type == AccountType.INTEREST_CHECKING) ? bank.CheckingInterest : bank.SavingsInterest);
                        Console.WriteLine("Accruing interest on Account: Default is {0}%", (defaultInterest * 100.0F));
                        Console.WriteLine("Do you want to change it? [y] or [n]");
                        float interest = defaultInterest;
                        entry = Console.ReadLine();
                        if (entry == "y")
                        {
                            Console.Write("Enter a percent value: ");
                            entry    = Console.ReadLine();
                            interest = (float)(Convert.ToDouble(entry) / 100.0D);
                            Console.WriteLine("Interest changed to: [{0}]", (interest * 100.0F));
                        }
                        try
                        {
                            balance = bank.PerformAction(acctId, name, "accrue", interest);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        break;

                    case "switch":
                    case "s":
                        Console.Write("Enter the Account Name to Switch to: ");
                        name = Console.ReadLine();
                        amt  = 0;
                        try
                        {
                            AccountDetailsViewModel[] vm = bank.GetDetailsByName(name);
                            string choice = String.Empty;
                            if (vm.Count() > 1)
                            {
                                Console.Write("Which Acct? ");
                                foreach (var v in vm.Select((x, i) => new { x, i }))
                                {
                                    Console.Write("[{0}]: {1} - {2} ; ", v.i, v.x.AccountId, v.x.Type.ToString());
                                }
                                Console.Write(Environment.NewLine + "Choose One: ==> ");
                                choice = Console.ReadLine();
                            }
                            int chosenIdx = 0;
                            if (!String.IsNullOrEmpty(choice))
                            {
                                chosenIdx = Convert.ToInt32(choice);
                            }
                            string id = "0";
                            if (vm != null)
                            {
                                id = vm[chosenIdx].AccountId;
                            }
                            acctId  = id;
                            balance = bank.PerformAction(acctId, name, "balance", 0);
                        }
                        catch (Exception e)
                        {
                            balance = 0;
                            Console.WriteLine(e.Message);
                        }
                        break;

                    case "print":
                    case "p":
                        Console.WriteLine(bank.PerformAction(acctId, name, "print", amt));
                        break;

                    case "dump":
                    case "du":
                        bank.DumpAccounts();
                        break;

                    case "quit":
                    case "q":
                        Console.WriteLine("Exiting...");
                        break;

                    default:
                        Console.WriteLine("Undefined action: " + entry + ". Type one of [balance], [withdraw] or [deposit].");
                        break;
                    }
                }
                catch (MethodSelector.AccountNotFoundException ae)
                {
                    Console.WriteLine(ae.Message);
                    balance = 0;
                }
                catch (MethodSelector.BankingException e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Open an Account by Typing \'open\'");
                }
            }
            Console.Write("...Goodbye. Hit ENTER to Exit.");
            Console.ReadLine();

            return(0);
        }
Beispiel #3
0
        private void ProcessTransaction(MessageData data, Client client)
        {
            System.Diagnostics.Debug.WriteLine("Processing Transaction message for client {0}", client.ClientHandle);
            if (data.id != MessageTypes.TxMsgType)
            {
                throw new BankingException("Invalid Transaction Type: " + data.id + " from Client: " + client.ClientHandle);
            }
            Transaction tx = data.message as Transaction;

            if (tx != null)
            {
                switch (tx.txOperation)
                {
                case "open":
                {
                    string id = tx.acctId.ToString();
                    if (String.IsNullOrEmpty(id) || (String.Compare(id, "0") == 0))
                    {
                        id = BankControlForm.Bank.GetNewAcctId();
                    }
                    try
                    {
                        float balance = _bank.PerformAction(id, tx.acctLastName, tx.txOperation, tx.txAmount, tx.acctType, true);
                        if (balance >= 0)
                        {
                            // Send response of Tx back to client
                            Transaction txBack = new Transaction();
                            txBack.acctFirstName = tx.acctFirstName;
                            txBack.acctLastName  = tx.acctLastName;
                            txBack.txAmount      = (float)Convert.ToDouble(tx.txAmount);
                            txBack.acctId        = Convert.ToInt32(id);
                            txBack.acctType      = tx.acctType;
                            txBack.txOperation   = "open-response";
                            txBack.balance       = balance;
                            txBack.response      = true;

                            // The data should send across in the library sender class
                            client.SetData(txBack);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("Open Tx Exception: " + e.Message);
                    }
                }
                break;

                case "deposit":
                {
                    try
                    {
                        float balance = _bank.PerformAction(tx.acctId.ToString(), tx.acctLastName, tx.txOperation, tx.txAmount, tx.acctType);
                        if (balance >= 0)
                        {
                            Transaction txBack = new Transaction();
                            txBack.acctFirstName = tx.acctFirstName;
                            txBack.acctId        = tx.acctId;
                            txBack.acctLastName  = tx.acctLastName;
                            txBack.acctType      = tx.acctType;
                            txBack.balance       = balance;
                            txBack.txAmount      = tx.txAmount;
                            txBack.txOperation   = "deposit-response";
                            txBack.response      = true;

                            client.SetData(txBack);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("Deposit Tx Exception: " + e.Message);
                    }
                }
                break;

                case "withdraw":
                {
                    try
                    {
                        float balance = _bank.PerformAction(tx.acctId.ToString(), tx.acctLastName, tx.txOperation, tx.txAmount, tx.acctType);
                        if (balance >= 0)
                        {
                            Transaction txBack = new Transaction();
                            txBack.acctFirstName = tx.acctFirstName;
                            txBack.acctId        = tx.acctId;
                            txBack.acctLastName  = tx.acctLastName;
                            txBack.acctType      = tx.acctType;
                            txBack.balance       = balance;
                            txBack.txAmount      = tx.txAmount;
                            txBack.txOperation   = "withdraw-response";
                            txBack.response      = true;

                            client.SetData(txBack);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("Withdraw Tx Exception: " + e.Message);
                    }
                }
                break;

                default:
                    throw new BankingException("Invalid Transaction Name: " + data.name + " from Client: " + client.ClientHandle);
                    break;
                }
            }
        }