Skip to content

Instantly share code, notes, and snippets.

@dotted
Created May 23, 2012 08:33
Show Gist options
  • Select an option

  • Save dotted/2773923 to your computer and use it in GitHub Desktop.

Select an option

Save dotted/2773923 to your computer and use it in GitHub Desktop.
Noobie
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalcInterestTable
{
class Program
{
/// <summary>
/// Returns input string as a double, or 0 if conversion is not possible
/// </summary>
/// <param name="Expression">Numeric string to convert</param>
/// <returns>Input converted to double</returns>
public static Boolean isNumeric(String Expression)
{
double result;
return Double.TryParse(Expression, out result);
}
/// <summary>
/// Ask a question and validate the input
/// </summary>
/// <param name="question">The question to ask, for which the user provides input</param>
/// <param name="maxValue">Maximum value for input</param>
/// <returns></returns>
public static decimal input(string question, decimal maxValue = 1000000000)
{
while (true)
{
Console.Write(question);
string raw_input = Console.ReadLine();
if (!isNumeric(raw_input))
{
Console.WriteLine("Value must be numeric.");
continue;
}
decimal input = Convert.ToDecimal(raw_input);
if (input <= 0)
{
Console.WriteLine("Value can't be negative or zero! Try again.");
continue;
}
else if (input > maxValue)
{
Console.WriteLine("Value can't be over {0}", maxValue);
continue;
}
return input;
}
}
/// <summary>
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
while (true)
{
decimal principal = input("Enter principal sum: ");
decimal interest = input("Enter interest rate: ");
decimal years = input("Enter running years: ", 9999 - DateTime.Today.Year);
for (int year = 1; year <= years; year++)
{
if (year % (Console.WindowHeight - 2) == 0 || year == 1)
{
Console.WriteLine();
Console.WriteLine("Year\tPrincipal sum paid\tInterest rate paid\tTotal expenses paid");
}
Console.WriteLine(
DateTime.Today.AddYears(year).Year + "\t" +
((principal / years) * year) + "\t\t\t" +
((((interest / 100) * principal) / years) * year) + "\t\t\t" +
(((principal + (principal * (interest / 100))) / years) * year)
);
}
Console.WriteLine("Press any key to make a new calculation.");
Console.ReadKey();
Console.Clear();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment