//public static double getAverage() //{ // //my original version accumulated the total account numbers as they were input into the class // return sum / (double)count; //} public static double getAverage(account[] accts) { double avg = 0D; int count = 0; for (int i = 0; i < accts.Length; i++) if (accts[i] != null)//user can input null accounts { avg += accts[i].getAcctBal(); count++;//determine the true number of accounts } return avg / (double)count; }
//public static account[] promptAccountAutoNum() //{ // //This method gets input from the user and makes an array out of the results // ArrayList accounts = new ArrayList(); // do // { // //int acctNum = Input.promptInt32("Enter the integer account number: ", // // "Please Enter a valid integer... "); // double bal = Input.promptDouble("Enter the account balance: ", // "Please enter a valid double... "); // string name = Input.promptString("Enter the account holder last name: ", // "Please enter a valid string... "); // accounts.Add(new account(name, bal)); // //return new account(name, bal);//autonumber the account number // //the likelihood of this method erroring out is not very high // } while (char.ToUpper(Input.promptChar("Would you like to continue?: ", // "I don't know what you did, but that was awesome!!! ")) != 'N'); // //was a little hard to find how to do this // return (account[])accounts.ToArray(typeof(account)); //}//end method public static void searchPrompt(account[] accts) { int searchNum = Input.promptInt32( "Enter an account number to search for: ", "\nPlease enter valid integer data... \n"); Console.WriteLine(); int acctIndex = account.unorderedSearch(accts, searchNum); if (acctIndex == -1) Console.WriteLine("You entered an invalid account... "); else if (accts[acctIndex] == null)//there can be null entries Console.WriteLine("You entered an invalid account... "); else Console.WriteLine( accts[acctIndex].ToString());//find the account and print it }
public static int unorderedSearch(account[] accounts, int acctNum) { for (int i = 0; i < accounts.Length; i++) if (accounts[i].CompareTo(acctNum) == 0) return i; return -1; }
//static methods //public static account[] promptAccount() //{ // //This method gets input from the user and makes an array out of the results // ArrayList accounts = new ArrayList(); // bool firstRun = true; // do // { // Console.Write(firstRun ? "" : "\n"); // int acctNum = Input.promptInt32("Enter the integer account number: ", // "Please Enter a valid integer... "); // double bal = Input.promptDouble("Enter the account balance: ", // "Please enter a valid double... "); // string name = Input.promptString("Enter the account holder last name: ", // "Please enter a valid string... "); // accounts.Add(new account(acctNum, name, bal)); // firstRun = false; // //the likelihood of this method erroring out is not very high // } while (char.ToUpper(Input.promptChar("Would you like to continue?: ", // "I don't know what you did, but that was awesome!!! ")) != 'N'); // //was a little hard to find how to do this // return (account[])accounts.ToArray(typeof(account)); //}//end method public static void promptAccount(ref account[] accts) { //This method gets input from the user and makes an array out of the results ArrayList accounts; if (accts == null) accounts = new ArrayList(); else accounts = new ArrayList(accts);//send the input array to the ArrayList bool firstRun = true; do { Console.Write(firstRun ? "" : "\n"); int acctNum = Input.promptInt32("Enter the integer account number: ", "Please Enter a valid integer... "); double bal = Input.promptDouble("Enter the account balance: ", "Please enter a valid double... "); string name = Input.promptString("Enter the account holder last name: ", "Please enter a valid string... "); accounts.Add(new account(acctNum, name, bal)); firstRun = false; //the likelihood of this method erroring out is not very high } while (char.ToUpper(Input.promptChar("Would you like to continue?: ", "I don't know what you did, but that was awesome!!! ")) != 'N'); //was a little hard to find how to do this accts = (account[])accounts.ToArray(typeof(account)); }