/// <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)); }
/// <summary> /// The handler method for all of the ATM's states. /// </summary> /// <param name="s">The string taken from the input text box on the ATM from.</param> /// <returns>A tuple containing the next state, as well as the balance of the account when applicable.</returns> public Tuple <AtmStatus, double> handle(string s) { //int acctNum = 0; switch (state) { case AtmStatus.Start: { int acctNum = 0; bool intOK = int.TryParse(s, out acctNum); if (intOK) { if (aDatabase.doesAccountExist(acctNum) == true) { if (aDatabase.logInAccount(acctNum) == true) { balance = 0; state = AtmStatus.LoggedIn; accountNumber = acctNum; } else { state = AtmStatus.AccountAlreadyInUse; } } else { state = AtmStatus.AccountNotFound; } } else { state = AtmStatus.AccountNotFound; } break; } case AtmStatus.AccountNotFound: { state = AtmStatus.Start; break; } case AtmStatus.LoggedIn: { int input = 0; bool intOK = int.TryParse(s, out input); if (intOK) { if (input == 1) { state = AtmStatus.ChooseBalanceInquiry; balance = checkBalance(accountNumber); } else if (input == 2) { state = AtmStatus.ChoseWithdrawalNeedAmount; balance = checkBalance(accountNumber); } else { //Add an invalid input state here. state = AtmStatus.InvalidInput; } } else { state = AtmStatus.InvalidInput; } break; } case AtmStatus.ChooseBalanceInquiry: { state = AtmStatus.Start; aDatabase.logOutAccount(accountNumber); break; } case AtmStatus.ChoseWithdrawalNeedAmount: { double withAmmt = 0; bool doubleOk = Double.TryParse(s, out withAmmt); if (doubleOk == true) { if ((balance - withAmmt) >= 0) { completeWithdrawal(accountNumber, withAmmt); balance = checkBalance(accountNumber); state = AtmStatus.CompletedWithdrawal; MessageBox.Show("Here's $" + withAmmt.ToString("00.00") + ", just for you.", "Dolla Dolla Bills Ya'll"); } else { state = AtmStatus.InvalidInput; } } else { //Add the invalid input state here. state = AtmStatus.InvalidInput; } break; } case AtmStatus.CompletedWithdrawal: { state = AtmStatus.Start; aDatabase.logOutAccount(accountNumber); break; } case AtmStatus.InvalidInput: { state = AtmStatus.Start; aDatabase.logOutAccount(accountNumber); break; } case AtmStatus.AccountAlreadyInUse: { state = AtmStatus.Start; break; } default: { break; } } return(new Tuple <AtmStatus, double>(state, balance)); }