static void withdrawFunds() { if (customerList.Count == 0) { Console.WriteLine("There are no customers in the system.\nPlease create a customer or load saved customers.\n"); } else { SavingsAccount currentCust = new SavingsAccount(); bool quitNow = false; while (!quitNow) { showCustomers(true); Console.WriteLine("Please choose the account from which you would like to make a withdrawal."); int custIndex = inputIndex(); if (custIndex < 1 || custIndex > customerList.Count) { Console.Clear(); Console.WriteLine("That is not a valid customer. Please try again.\n"); } else if (customerList[custIndex - 1].Balance == 0) { Console.WriteLine("There are no funds in that account to withdraw."); quitNow = true; } else { currentCust = customerList[custIndex - 1]; Console.Clear(); displayCustomer(currentCust); bool gotWithdrawal = false; double withdrawal; while (!gotWithdrawal) { Console.WriteLine("How much would you like to withdraw?"); string withdrawalInput = Console.ReadLine(); gotWithdrawal = double.TryParse(withdrawalInput, out withdrawal); if (!gotWithdrawal) { Console.WriteLine("That is not a valid value. Please try again."); } else { Console.WriteLine("You entered {0:c}. Is this correct? Y/N", withdrawal); bool withdrawalCorrect = confirm(); if (!withdrawalCorrect) { gotWithdrawal = false; } else if (withdrawal > currentCust.Balance) { Console.WriteLine("The withdrawal amount is greater than the account balance. Please try again."); gotWithdrawal = false; } else { currentCust.makeWithdrawal(withdrawal); currentCust.updateBalance(); Console.WriteLine("Withdrawal made successfully."); Console.WriteLine("The current balance in this account is {0:C}.", currentCust.Balance); Console.WriteLine("The balance in 1 year, compounded monthly at {0:p} will be {1:C}", currentCust.Interest, currentCust.FutureBalance); } } } quitNow = true; } } } }
static void addFunds(SavingsAccount currentCust) { bool quitNow = false; while (!quitNow) { string depositInput = Console.ReadLine(); double deposit; bool gotDeposit = double.TryParse(depositInput, out deposit); if (!gotDeposit) { Console.WriteLine("That is not a valid value. Please try again."); } else { Console.WriteLine("You entered {0:c}. Is this correct? Y/N", deposit); bool depositCorrect = confirm(); if (!depositCorrect) { gotDeposit = false; } else { currentCust.makeDeposit(deposit); currentCust.updateBalance(); quitNow = true; } } } }
static void setPhone(SavingsAccount customer) { string phoneInput = null; string[] phoneArray = new string[3]; bool isPhone = false; while (!isPhone) { Console.WriteLine("Please enter the customer's phone #"); try { phoneInput = Console.ReadLine(); Match match = Regex.Match(phoneInput, @"\(?\b([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})\b"); if (match.Success) { phoneArray[0] = match.Groups[1].Value; phoneArray[1] = match.Groups[2].Value; phoneArray[2] = match.Groups[3].Value; customer.Phone = ("(" + phoneArray[0] + ") " + phoneArray[1] + " " + phoneArray[2]); Console.WriteLine("You entered ({0}) {1}-{2}. Is that correct? Y/N", phoneArray[0], phoneArray[1], phoneArray[2]); isPhone = confirm(); } else { Console.WriteLine("That is not a valid phone number. Please try again."); } } catch { Console.WriteLine("Invalid input. Please try again."); } } }
//static void setInterest(SavingsAccount customer) //{ // string interestInput = null; // double setInterest; // bool isInterest = false; // while (!isInterest) // { // Console.WriteLine("Please enter the interest on this customer's account."); // try // { // interestInput = Console.ReadLine(); // } // finally // { // double.TryParse(interestInput, out setInterest); // setInterest = setInterest / 100; // customer.Interest = setInterest; // } // Console.Write("You entered {0:P}, is that correct? Y/N\n", setInterest); // isInterest = confirm(); // } //} static void setSIN(SavingsAccount customer) { string sinInput = null; bool isSIN = false; bool unique = true; int socialInsurance; while (isSIN == false || unique == false) { unique = true; Console.Write("Please enter the customer's social insurance number. E.g. 123456789: "); sinInput = Console.ReadLine(); if (sinInput.Length != 9) { Console.WriteLine("\nThat is not a valid SIN. Please try again."); } else { int.TryParse(sinInput, out socialInsurance); if (customerList.Count > 0) { foreach (SavingsAccount cust in customerList) { if (cust.SIN == socialInsurance) { Console.WriteLine("That SIN is already assigned to another customer. Please try again"); unique = false; break; } } } else if (customerList.Count == 0) { unique = true; } if (unique) { customer.SIN = socialInsurance; isSIN = true; } } } }
static void setLastName(SavingsAccount customer) { bool last = false; string lastName = null; while (last == false) { Console.Write("Please enter the customer's last name: "); try { lastName = Console.ReadLine(); customer.LastName = lastName.Trim(); Console.Write("You entered {0}. Is this correct? Y/N\n", customer.LastName); last = confirm(); } finally { if (lastName == null) { Console.WriteLine("Unable to read input. Please try again."); } } } }
static void setOpeningBalance(SavingsAccount customer) { Console.WriteLine("Please enter the customer's opening balance."); addFunds(customer); }
static void setAddress(SavingsAccount customer) { bool strAddress = false; bool successInput = false; string addressInput = null; string[] splitAddress = new string[3]; string[] splitRR = new string[2]; while (strAddress == false) { Console.WriteLine("Please enter the customer's street address. Example: 34 Belmont Dr."); try { addressInput = Console.ReadLine(); } finally { if (Regex.IsMatch(addressInput, @"[rR]\W?[rR]\W?.*\d")) // test for a rural route address { if (Regex.IsMatch(addressInput, @"^([-\d]+) ([\w ']+) (\w+)\.?"))// test whether there is also a street address { splitAddress = parseAddress(addressInput); // extract street address splitRR = parseRR(addressInput); // extract RR address (e.g. RR 6 Stn Main) if (splitAddress[0] != null && splitAddress[1] != null && splitAddress[2] != null && splitRR[0] != null && splitRR[1] != null) { // Assign address parts to the SavingsAccount object customer.StreetNumber = splitAddress[0]; customer.StreetName = splitAddress[1]; StreetAddressConverter convert = new StreetAddressConverter(); customer.StreetType = convert.convertToAbbr(splitAddress[2]); customer.Rroute = splitRR[0]; customer.RrStation = splitRR[1]; successInput = true; } else { Console.WriteLine("There was a problem with your input. Please try again."); } } else // there is only a rr address { splitRR = parseRR(addressInput); // extract RR address (e.g. RR 6 Stn Main) if (splitRR[0] != null && splitRR[1] != null) { // Assign address parts to the SavingsAccount object customer.Rroute = splitRR[0]; customer.RrStation = splitRR[1]; successInput = true; } else { Console.WriteLine("There was a problem with your input. Please try again."); } } } else { //extract street address, divide into parts splitAddress = parseAddress(addressInput); if (splitAddress[0] != null && splitAddress[1] != null && splitAddress[2] != null) { customer.StreetNumber = splitAddress[0]; customer.StreetName = splitAddress[1]; StreetAddressConverter convert = new StreetAddressConverter(); string convertme = splitAddress[2]; string streetAbbr = convert.convertToAbbr(convertme); customer.StreetType = streetAbbr; successInput = true; } else { Console.WriteLine("There was a problem with your input. Please try again."); } } if (successInput == false) { Console.WriteLine("No address was entered. Please try again."); } else if (customer.Rroute == null) // there is no rr { Console.Write("\nYou entered {0} {1} {2}. Is this correct? Y/N\n", customer.StreetNumber, ti.ToTitleCase(customer.StreetName), ti.ToTitleCase(customer.StreetType)); strAddress = confirm(); } else if (customer.StreetName == null) // there is no street address { Console.Write("\nYou entered {0} {1}. Is this correct? Y/N\n", ti.ToTitleCase(customer.Rroute), ti.ToTitleCase(customer.RrStation)); strAddress = confirm(); } else // there is both a street address and a rr { Console.Write("\nYou entered {0} {1} {2} {3} {4}. Is this correct? Y/N\n", customer.StreetNumber, ti.ToTitleCase(customer.StreetName), ti.ToTitleCase(customer.StreetType), ti.ToTitleCase(customer.Rroute), ti.ToTitleCase(customer.RrStation)); strAddress = confirm(); } } } }
static SavingsAccount newCustomer() { SavingsAccount newCust = new SavingsAccount(); Console.WriteLine("Welcome to Customer Creation.\nTo create a new customer entry, please follow the instructions.\n"); setFirstName(newCust); setLastName(newCust); setAddress(newCust); setPhone(newCust); setOpeningBalance(newCust); setSIN(newCust); Console.Write("Customer created successfully. Press any key to return to the menu..."); Console.ReadKey(); return newCust; }
static void displayCustomer(SavingsAccount cust) { Console.WriteLine("*****************************************************************"); Console.WriteLine("* {0, -10} * {1, -10} * {2, -9} * {3, -23} *", "Last Name", "First Name", "SIN", "Address"); Console.WriteLine("* {0, -10} * {1, -10} * {2, -9} * {3, -23} *", "", "", "", ""); Console.WriteLine("* {0, -10} * {1, -10} * {2, -9} * {3, -23} *", cust.LastName, cust.FirstName, cust.SIN, cust.FullAddress); Console.WriteLine("*****************************************************************"); Console.WriteLine("* {0,-14} * {1, -7} * {2, -8} * {3,-24}*", "Phone #", "Balance", "Interest", "Balance After 1 Year"); Console.WriteLine("* {0,-14} * {1, -7} * {2, -8} * {3, 24}*", "","", "", ""); Console.WriteLine("* {0,-14} * {1, -7:c} * {2, -8:p2} * {3,-24:c}*", cust.Phone, cust.Balance, cust.Interest, cust.FutureBalance); Console.WriteLine("*****************************************************************"); }
static void depositFunds() { if (customerList.Count == 0) { Console.WriteLine("There are no customers in the system.\nPlease create a customer or load saved customers.\n"); } else { SavingsAccount currentCust = new SavingsAccount(); bool quitNow = false; while (!quitNow) { showCustomers(true); Console.WriteLine("Please choose the account to which you would like to make a deposit."); int custIndex = inputIndex(); if (custIndex < 1 || custIndex > customerList.Count) { Console.Clear(); Console.WriteLine("That is not a valid customer. Please try again.\n"); } else { currentCust = customerList[custIndex - 1]; Console.Clear(); displayCustomer(currentCust); Console.WriteLine("How much would you like to deposit?"); addFunds(currentCust); Console.WriteLine("Deposit made successfully."); Console.WriteLine("The current balance in this account is {0:C}.", currentCust.Balance); Console.WriteLine("The balance in 1 year, compounded monthly at {0:p} will be {1:C}", currentCust.Interest, currentCust.FutureBalance); quitNow = true; } } } }
// adapted from the msdn page on List<T>.Sort private static int compareByLastName(SavingsAccount name1, SavingsAccount name2) { string x = name1.LastName; string y = name2.LastName; if (x == null) { if (y == null) { // If x is null and y is null, they're // equal. return 0; } else { // If x is null and y is not null, y // is greater. return -1; } } else { // If x is not null... // if (y == null) // ...and y is null, x is greater. { return 1; } else { return x.CompareTo(y); } } }