/* * Main Method */ static void Main(string[] args) { string html_js_arr; User_args user_input = new User_args(); // Begin by gathering the user input Get_user_input(ref user_input); html_js_arr = Compute_interest_rates(ref user_input); // Write the file output Write_file(ref html_js_arr); System.Console.ReadLine(); }
/********************************************************************* ** Function: Computes all the interest rates ** Description: Takes in the user struct and returns the interest rate string to write to file ** Parameters: Referenced User_args struct ** Pre-Conditions: All struct values should be non null ** Post-Conditions: string that is Javascript ready *********************************************************************/ private static string Compute_interest_rates(ref User_args user_input) { string js_arr_str = "[['Period',"; // setting up the string double[] interest_rates = new double[user_input.num_of_interest_intervals + 1]; // This will create a fixed interest rate constant from user input double interest_rate = Math.Round(((user_input.max_interest_rate - user_input.min_interest_rate) / user_input.num_of_interest_intervals), 2); for (int x = 0; x < interest_rates.Length; x++) { interest_rates[x] = interest_rate * x + user_input.min_interest_rate; js_arr_str += "'" + interest_rates[x] + "%'"; if (x + 1 != interest_rates.Length) { js_arr_str += ","; } } js_arr_str += "],"; // If this is the last loop, don't print a comma. Javascript is cool and allows this, but I'm picky. // Loop through periods gathered from user input. We want to do this first, because the Google API requires our Y axis to be rows for (int y = 0; y < user_input.num_of_periods; y++) { js_arr_str += "['" + (y + 1) + "',"; // Go through the previously stored interest rates for (int x = 0; x < interest_rates.Length; x++) { // compute the compund interest double amount = Math.Round((user_input.starting_capital * System.Math.Pow(1 + (interest_rates[x] / 100), y + 1)), 2); js_arr_str += amount; // append to string. No need to put in quotes, it is a number, and javascript does not require elements to be cast a strings //System.Console.Write("$" + amount + ", "); if (x + 1 != interest_rates.Length) { js_arr_str += ","; } } js_arr_str += "]"; // This will close the javascript array if (y + 1 != user_input.num_of_periods) // If this is the last loop, don't print a comma. Javascript is cool and allows this, but I'm picky. { js_arr_str += ","; } } return(js_arr_str + "]"); //return string with closing bracket }
/********************************************************************* ** Function: Obtain user input ** Description: will obtain user input and check if it is valid ** Parameters: message Referenced User_args struct ** Pre-Conditions: Referenced struct should be defined! ** Post-Conditions: returns nothing *********************************************************************/ private static void Get_user_input(ref User_args user_input) { bool error; // purposly left uninitalized because the do while will set it int dummy_int = 0; // dummy vars are because c# can't pass null ints or doubles float dummy_float = 0; // --^ // Get starting capital do { error = check_for_validity("Enter the starting capital [1-" + MAX_STARTING_CAPITAL + "]: ", 'f', ref user_input.starting_capital, ref dummy_int, MAX_STARTING_CAPITAL, 0); } while (error); // do while there are errors do // This do while is for when the user inputs a high interest rate lower than the low { // Get low interest rate do { error = check_for_validity("Enter the low interest rate [0-" + MAX_INTEREST_RATE + "]: ", 'f', ref user_input.min_interest_rate, ref dummy_int, MAX_INTEREST_RATE, 0); } while (error); // Get high interest rate do { error = check_for_validity("Enter the high interest rate [0-" + MAX_INTEREST_RATE + "]: ", 'f', ref user_input.max_interest_rate, ref dummy_int, MAX_INTEREST_RATE, 0); } while (error); if (user_input.max_interest_rate < user_input.min_interest_rate) { System.Console.WriteLine("Your max interest rate is less than your min! Try again..."); } } while (user_input.max_interest_rate < user_input.min_interest_rate); // Get interest intervals do { error = check_for_validity("Enter the number of interest intervals [0-" + MAX_INTEREST_INTERVALS + "]: ", 'i', ref dummy_float, ref user_input.num_of_interest_intervals, 0, MAX_INTEREST_INTERVALS); } while (error); // Get periods do { error = check_for_validity("Enter the number of periods [0-" + MAX_COMPOUND_PERIODS + "]: ", 'i', ref dummy_float, ref user_input.num_of_periods, 0, MAX_COMPOUND_PERIODS); } while (error); }