示例#1
0
        /// <summary>
        /// Handles all of states that are passed to the controller.
        /// </summary>
        /// <param name="s">The string passed from the input text box on the form.</param>
        /// <param name="stat">The status passed in by the form.</param>
        /// <returns>A tuple containing the new status, as well as a double value that will be used for a variety of purposes.</returns>
        public Tuple <TellerStatus, double> handle(string s, TellerStatus stat)
        {
            state = stat;
            switch (state)
            {
            case TellerStatus.AttemptClosingCustomerAccount:
            {
                int  acctNum = 0;
                bool intOK   = int.TryParse(s, out acctNum);
                if (intOK)
                {
                    if (aDatabase.doesAccountExist(acctNum))
                    {
                        if (aDatabase.logInAccount(acctNum) == true)
                        {
                            currentAccountNum = acctNum;
                            output            = aDatabase.returnAccountBalance(currentAccountNum);
                            completeWithdrawal(currentAccountNum, output);
                            MessageBox.Show("The account provided was emptied. Here's $" + output.ToString("00.00") + ", just for you.", "Dolla Dolla Bills Ya'll");
                            aDatabase.logOutAccount(currentAccountNum);
                            cDatabase.removeCustomer(aDatabase.lookUpAccountReturnCustomerNumber(currentAccountNum));
                            aDatabase.removeAccount(currentAccountNum);
                            state = TellerStatus.ClosedCustomerAccount;
                        }
                        else
                        {
                            state = TellerStatus.AccountAlreadyInUse;
                        }
                    }
                    else
                    {
                        state = TellerStatus.AccountNotFound;
                    }
                }
                else
                {
                    //figure out what to do if invalid input.
                    state = TellerStatus.AccountNotFound;
                }
                break;
            }

            case TellerStatus.ClosedCustomerAccount:
            {
                state = TellerStatus.Start;
                break;
            }

            case TellerStatus.AttemptClosingBankAccount:
            {
                int  acctNum = 0;
                bool intOK   = int.TryParse(s, out acctNum);
                if (intOK)
                {
                    if (aDatabase.doesAccountExist(acctNum))
                    {
                        if (aDatabase.logInAccount(acctNum) == true)
                        {
                            currentAccountNum = acctNum;
                            output            = aDatabase.returnAccountBalance(currentAccountNum);
                            completeWithdrawal(currentAccountNum, output);
                            MessageBox.Show("Account emptied. Here's $" + output.ToString("00.00") + ", just for you.", "Dolla Dolla Bills Ya'll");
                            aDatabase.logOutAccount(currentAccountNum);
                            aDatabase.removeAccount(currentAccountNum);
                            state = TellerStatus.ClosedBankAccount;
                        }
                        else
                        {
                            state = TellerStatus.AccountAlreadyInUse;
                        }
                    }
                    else
                    {
                        state = TellerStatus.AccountNotFound;
                    }
                }
                else
                {
                    //figure out what to do if invalid input.
                    state = TellerStatus.AccountNotFound;
                }
                break;
            }

            case TellerStatus.ClosedBankAccount:
            {
                state = TellerStatus.Start;
                break;
            }

            case TellerStatus.AttemptCreatingNewCustomerAccount:
            {
                currentName = s;
                state       = TellerStatus.NeedValidAddress;
                break;
            }

            case TellerStatus.NeedValidAddress:
            {
                currentAddress = s;
                state          = TellerStatus.CreatedNewCustomerAccount;
                output         = (double)(cDatabase.addNewCustomer(currentName, currentAddress));
                break;
            }

            case TellerStatus.CreatedNewCustomerAccount:
            {
                state = TellerStatus.Start;
                break;
            }

            case TellerStatus.AttemptCreatingNewBankAccount:
            {
                int  custNum = 0;
                bool intOK   = int.TryParse(s, out custNum);
                if (intOK)
                {
                    if (cDatabase.lookUpCustomer(custNum) != null)
                    {
                        //Customer newCust = cDatabase.lookUpCustomer(custNum);
                        int newNum = aDatabase.addNewAccount(custNum, 0.0);
                        cDatabase.addAccountToCustomer(custNum, newNum);
                        output = newNum;
                        state  = TellerStatus.CreatedNewBankAccount;
                    }
                    else
                    {
                        state = TellerStatus.AccountNotFound;
                    }
                }
                else
                {
                    state = TellerStatus.AccountNotFound;
                }
                break;
            }

            case TellerStatus.CreatedNewBankAccount:
            {
                state = TellerStatus.Start;
                break;
            }

            case TellerStatus.Depositing:
            {
                int  acctNum = 0;
                bool intOK   = int.TryParse(s, out acctNum);
                if (intOK)
                {
                    if (aDatabase.doesAccountExist(acctNum))
                    {
                        if (aDatabase.logInAccount(acctNum) == true)
                        {
                            currentAccountNum = acctNum;
                            output            = aDatabase.returnAccountBalance(currentAccountNum);
                            state             = TellerStatus.NeedValidDepositAmount;
                            currentAccountNum = acctNum;
                        }
                        else
                        {
                            state = TellerStatus.AccountAlreadyInUse;
                        }
                    }
                    else
                    {
                        state = TellerStatus.AccountNotFound;
                    }
                }
                else
                {
                    //figure out what to do if invalid input.
                    state = TellerStatus.AccountNotFound;
                }
                break;
            }

            case TellerStatus.NeedValidDepositAmount:
            {
                double withAmmt = 0;
                bool   doubleOk = Double.TryParse(s, out withAmmt);
                if (doubleOk == true)
                {
                    aDatabase.depositIntoAccount(currentAccountNum, withAmmt);
                    output = aDatabase.returnAccountBalance(currentAccountNum);
                    MessageBox.Show("Here's your new balance: $" + output.ToString("00.00") + ".", "Dolla Dolla Bills Ya'll");
                    state = TellerStatus.DepositSuccess;
                }
                else
                {
                    //Add the invalid input state here.
                    state = TellerStatus.InvalidInput;
                }
                break;
            }

            case TellerStatus.DepositSuccess:
            {
                aDatabase.logOutAccount(currentAccountNum);
                state = TellerStatus.Start;
                break;
            }

            case TellerStatus.Withdrawing:
            {
                int  acctNum = 0;
                bool intOK   = int.TryParse(s, out acctNum);
                if (intOK)
                {
                    if (aDatabase.doesAccountExist(acctNum))
                    {
                        if (aDatabase.logInAccount(acctNum) == true)
                        {
                            currentAccountNum = acctNum;
                            output            = aDatabase.returnAccountBalance(currentAccountNum);
                            state             = TellerStatus.NeedValidWithdrawalAmount;
                        }
                        else
                        {
                            state = TellerStatus.AccountAlreadyInUse;
                        }
                    }
                    else
                    {
                        state = TellerStatus.AccountNotFound;
                    }
                }
                else
                {
                    //figure out what to do if invalid input.
                    state = TellerStatus.AccountNotFound;
                }
                break;
            }

            case TellerStatus.NeedValidWithdrawalAmount:
            {
                double withAmmt = 0;
                bool   doubleOk = Double.TryParse(s, out withAmmt);
                if (doubleOk == true)
                {
                    if ((output - withAmmt) >= 0)
                    {
                        completeWithdrawal(currentAccountNum, withAmmt);
                        output = checkBalance(currentAccountNum);
                        state  = TellerStatus.WithdrawalSuccess;
                        MessageBox.Show("Here's $" + withAmmt.ToString("00.00") + ", just for you.", "Dolla Dolla Bills Ya'll");
                    }
                    else
                    {
                        state = TellerStatus.InvalidInput;
                    }
                }
                else
                {
                    //If invalid input.
                    state = TellerStatus.InvalidInput;
                }
                break;
            }

            case TellerStatus.WithdrawalSuccess:
            {
                aDatabase.logOutAccount(currentAccountNum);
                state = TellerStatus.Start;
                break;
            }

            case TellerStatus.BalanceInquiry:
            {
                int  acctNum = 0;
                bool intOK   = int.TryParse(s, out acctNum);
                if (intOK)
                {
                    if (aDatabase.doesAccountExist(acctNum))
                    {
                        if (aDatabase.logInAccount(acctNum) == true)
                        {
                            currentAccountNum = acctNum;
                            output            = aDatabase.returnAccountBalance(currentAccountNum);
                            state             = TellerStatus.DisplayingBalanceInquiry;
                        }
                        else
                        {
                            state = TellerStatus.AccountAlreadyInUse;
                        }
                    }
                    else
                    {
                        state = TellerStatus.AccountNotFound;
                    }
                }
                else
                {
                    //figure out what to do if invalid input.
                    state = TellerStatus.AccountNotFound;
                }
                break;
            }

            case TellerStatus.DisplayingBalanceInquiry:
            {
                state = TellerStatus.Start;
                aDatabase.logOutAccount(currentAccountNum);
                break;
            }

            case TellerStatus.InvalidInput:
            {
                state = TellerStatus.Start;
                aDatabase.logOutAccount(currentAccountNum);
                break;
            }

            case TellerStatus.AccountAlreadyInUse:
            {
                state = TellerStatus.Start;
                break;
            }

            case TellerStatus.AccountNotFound:
            {
                state = TellerStatus.Start;
                break;
            }

            default: { break; }
            }
            return(new Tuple <TellerStatus, double>(state, output));
        }