summary refs log tree commit diff
path: root/future_value2/FutureValue/frmFutureValue.cs
diff options
context:
space:
mode:
author1970-01-01 00:00:00 +0000
committer2025-01-08 04:44:47 +0000
commit1f317d3eb564e463a78cd0e47f4bcf95838c60ec (patch)
tree2c36aebaaf84c22cc3ddaa81809f01f95e326e08 /future_value2/FutureValue/frmFutureValue.cs
parentinvoice total 2 (diff)
downloadcs-1f317d3eb564e463a78cd0e47f4bcf95838c60ec.tar
cs-1f317d3eb564e463a78cd0e47f4bcf95838c60ec.tar.gz
cs-1f317d3eb564e463a78cd0e47f4bcf95838c60ec.tar.bz2
cs-1f317d3eb564e463a78cd0e47f4bcf95838c60ec.tar.xz
cs-1f317d3eb564e463a78cd0e47f4bcf95838c60ec.zip
future value 2
Diffstat (limited to 'future_value2/FutureValue/frmFutureValue.cs')
-rw-r--r--future_value2/FutureValue/frmFutureValue.cs128
1 files changed, 128 insertions, 0 deletions
diff --git a/future_value2/FutureValue/frmFutureValue.cs b/future_value2/FutureValue/frmFutureValue.cs
new file mode 100644
index 0000000..f301573
--- /dev/null
+++ b/future_value2/FutureValue/frmFutureValue.cs
@@ -0,0 +1,128 @@
+using System;

+using System.Collections.Generic;

+using System.ComponentModel;

+using System.Data;

+using System.Drawing;

+using System.Linq;

+using System.Text;

+using System.Threading.Tasks;

+using System.Windows.Forms;

+

+namespace FutureValue

+{

+	public partial class frmFutureValue : Form

+	{

+		public frmFutureValue()

+		{

+			InitializeComponent();

+		}

+

+		private bool IsDecimal(string txt, string name)

+		{

+			if (Decimal.TryParse(txt, out _))

+				return true;

+			MessageBox.Show(name + " must be a decimal");

+			return false;

+		}

+

+		private bool IsInt32(string txt, string name)

+		{

+			if (Int32.TryParse(txt, out _))

+				return true;

+			MessageBox.Show(name + " must be an integer");

+			return false;

+		}

+

+		private bool IsWithinRange(string txt, string name, decimal min, decimal max)

+		{

+			if (Decimal.TryParse(txt, out decimal i))

+				if (i < min || i > max) {

+					MessageBox.Show(name + " must be in the range of "

+					+ min + " to " + max + "\n");

+					return false;

+				}

+			return true;

+		}

+

+		private bool IsValidData()

+		{

+			bool valid = true;

+

+			if(!IsDecimal(txtMonthlyInvestment.Text,

+				txtMonthlyInvestment.Name))

+				valid = false;

+			if(!IsWithinRange(txtMonthlyInvestment.Text,

+				txtMonthlyInvestment.Name, 0, 10000))

+				valid = false;

+

+			if(!IsDecimal(txtInterestRate.Text,

+				txtInterestRate.Name))

+				valid = false;

+			if(!IsWithinRange(txtInterestRate.Text,

+				txtInterestRate.Name, 0, 10000))

+				valid = false;

+

+			if(!IsInt32(txtYears.Text,

+				txtYears.Name))

+				valid = false;

+			if(!IsWithinRange(txtYears.Text,

+				txtYears.Name, 0, 10000))

+				valid = false;

+

+			return valid;

+		}

+

+		private void btnCalculate_Click(object sender, EventArgs e)

+		{

+			try {

+				if (!IsValidData())

+					return;

+

+				decimal monthlyInvestment = Convert.ToDecimal(txtMonthlyInvestment.Text);

+				decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text);

+				int years = Convert.ToInt32(txtYears.Text);

+

+				int months = years * 12;

+				decimal monthlyInterestRate = yearlyInterestRate / 12 / 100;

+

+				decimal futureValue = this.CalculateFutureValue(

+					monthlyInvestment, monthlyInterestRate, months);

+				txtFutureValue.Text = futureValue.ToString("c");

+				txtMonthlyInvestment.Focus();

+

+				/* throw new Exception("There was an unknown error"); */

+			}

+			/* catch (FormatException) {

+				MessageBox.Show("Invalid format: Try entering " +

+					"numbers between 1 and 9999", "Entry Error");

+			}

+			catch (OverflowException) {

+				MessageBox.Show("Overflow Error: Try entering"

+					+ "smaller numbers", "Entry Error");

+			} */

+			catch (Exception ex){

+				MessageBox.Show(ex.Message + "\n" +

+				ex.GetType().ToString() +  "\n" +

+				ex.StackTrace, "Entry Error");

+			}

+		}

+

+		private decimal CalculateFutureValue(decimal monthlyInvestment,

+			decimal monthlyInterestRate, int months)

+		{

+			decimal futureValue = 0m;

+			for (int i = 0; i < months; i++)

+			{

+				futureValue = (futureValue + monthlyInvestment)

+							* (1 + monthlyInterestRate);

+			}

+			return futureValue;

+		}

+

+		private void btnExit_Click(object sender, EventArgs e)

+		{

+			this.Close();

+		}

+

+	}

+}