Home

Introduction

 

Fundamentals

 

Introduction

 

 

 
   
 
 

 

 
 
   
 

Previous Copyright © 2015 FunctionX Next

Home

Visual F# Example: Compound Interest

   

Introduction

This is an example of a graphical application that calculates the compound interest of a loan:

Compound Interest

open System;
open System.Drawing;
open System.Windows.Forms;

let Exercise:Form = new Form();

let btnCloseClick (e : EventArgs) =
    Exercise.Close()
    
let lblPrincipal      : Label       = new Label();
let txtPrincipal      : TextBox     = new TextBox();
let txtInterestRate   : TextBox     = new TextBox();
let txtPeriods        : TextBox     = new TextBox();
let rdoMonthly        : RadioButton = new RadioButton();
let rdoQuarterly      : RadioButton = new RadioButton();
let rdoSemiannually   : RadioButton = new RadioButton();
let rdoAnnually       : RadioButton = new RadioButton();
let txtFutureValue    : TextBox     = new TextBox();
let txtInterestEarned : TextBox     = new TextBox();

let btnCalculateClick (e : EventArgs) =
    let mutable principal      : double = 0.00;
    let mutable interestRate   : double = 0.00;
    let mutable periods        : double = 0.00;
    let mutable compoundType   : double = 0.00;

    // Retrieve the value of the principal
    try
        principal <- (double txtPrincipal.Text)
    with
    | :? FormatException as exc -> (MessageBox.Show "There was a problem when trying to convert the principal") |> ignore;

    interestRate <- (double txtInterestRate.Text) / 100.00
     
    // Find out what radio button was clicked to apply the compound frequency
    if rdoMonthly.Checked then
        compoundType <- 12.00
    elif rdoQuarterly.Checked then
        compoundType <- 4.00
    elif rdoSemiannually.Checked then
        compoundType <- 2.00
    else
        compoundType <- 1.00

    // Get the number of periods
    try
        periods <- (double txtPeriods.Text)
    with
    | :? FormatException as exc -> (MessageBox.Show "The value you entered for the number of periods is not valid\nPlease try again") |> ignore

        // These values will make the calculation easier to read
    let i : double = interestRate / compoundType;
    let n : double = compoundType * periods;

    // Perform the necessary calculations
    let futureValue    = principal * ((1.00 + i) ** n);
    let interestEarned = futureValue - principal;

    // Display the values in the appropriate text boxes
    txtInterestEarned.Text <- interestEarned.ToString("C");
    txtFutureValue.Text <- futureValue.ToString("C");

// Group Box: Loan Preparation
let pbxLoanPreparation : GroupBox = new GroupBox();
pbxLoanPreparation.Location <- new System.Drawing.Point(12, 12);
pbxLoanPreparation.Size <- new System.Drawing.Size(212, 153);
pbxLoanPreparation.TabIndex <- 0;
pbxLoanPreparation.TabStop <- false;
pbxLoanPreparation.Text <- "Loan Preparation";
Exercise.Controls.Add(pbxLoanPreparation);

// Label: : Principal
lblPrincipal.AutoSize <- true;
lblPrincipal.Location <- new System.Drawing.Point(11, 28);
lblPrincipal.Size <- new System.Drawing.Size(50, 13);
lblPrincipal.TabIndex <- 0;
lblPrincipal.Text <- "Principal:";
pbxLoanPreparation.Controls.Add(lblPrincipal);

// Text Box: Principal
txtPrincipal.Location <- new System.Drawing.Point(97, 28);
txtPrincipal.Size <- new System.Drawing.Size(62, 20);
txtPrincipal.TabIndex <- 1;
txtPrincipal.Text <- "0.00";
txtPrincipal.TextAlign <- HorizontalAlignment.Right;
pbxLoanPreparation.Controls.Add(txtPrincipal);

// Label: : Interest Rate
let lblInterestRate : Label = new Label();
lblInterestRate.AutoSize <- true;
lblInterestRate.Location <- new System.Drawing.Point(11, 65);
lblInterestRate.Size <- new System.Drawing.Size(71, 13);
lblInterestRate.TabIndex <- 2;
lblInterestRate.Text <- "Interest Rate:";
pbxLoanPreparation.Controls.Add(lblInterestRate);

// Text Box: Interest Rate
txtInterestRate.Location <- new System.Drawing.Point(97, 62);
txtInterestRate.Size <- new System.Drawing.Size(62, 20);
txtInterestRate.TabIndex <- 3;
txtInterestRate.Text <- "0.00";
txtInterestRate.TextAlign <- HorizontalAlignment.Right;
pbxLoanPreparation.Controls.Add(txtInterestRate);

// Label: Percent %
let lblPercent : Label = new Label();
lblPercent.AutoSize <- true;
lblPercent.Location <- new System.Drawing.Point(162, 67);
lblPercent.Size <- new System.Drawing.Size(15, 13);
lblPercent.TabIndex <- 4;
lblPercent.Text <- "%";
pbxLoanPreparation.Controls.Add(lblPercent);

// Label: Periods
let lblPeriods : Label = new Label();
lblPeriods.AutoSize <- true;
lblPeriods.Location <- new System.Drawing.Point(11, 105);
lblPeriods.Size <- new System.Drawing.Size(45, 13);
lblPeriods.TabIndex <- 5;
lblPeriods.Text <- "Periods:";
pbxLoanPreparation.Controls.Add(lblPeriods);

// Text Box: Periods
txtPeriods.Location <- new System.Drawing.Point(97, 100);
txtPeriods.Size <- new System.Drawing.Size(62, 20);
txtPeriods.TabIndex <- 6;
txtPeriods.Text <- "1";
txtPeriods.TextAlign <- HorizontalAlignment.Right;
pbxLoanPreparation.Controls.Add(txtPeriods);

// Label: Years
let lblYears : Label = new Label();
lblYears.AutoSize <- true;
lblYears.Location <- new System.Drawing.Point(163, 105);
lblYears.Size <- new System.Drawing.Size(34, 13);
lblYears.TabIndex <- 7;
lblYears.Text <- "Years";
pbxLoanPreparation.Controls.Add(lblYears);

// Group Box: 
let pbxCompoundFrequency : GroupBox = new GroupBox();
pbxCompoundFrequency.Location <- new System.Drawing.Point(240, 12);
pbxCompoundFrequency.Size <- new System.Drawing.Size(133, 153);
pbxCompoundFrequency.TabIndex <- 1;
pbxCompoundFrequency.TabStop <- false;
pbxCompoundFrequency.Text <- "Compound Frequency";
Exercise.Controls.Add(pbxCompoundFrequency);

// Radio Button: Monthly
rdoMonthly.AutoSize <- true;
rdoMonthly.Location <- new System.Drawing.Point(18, 29);
rdoMonthly.Checked  <- true;
rdoMonthly.Size <- new System.Drawing.Size(62, 17);
rdoMonthly.TabIndex <- 0;
rdoMonthly.TabStop <- true;
rdoMonthly.Text <- "Monthly";
rdoMonthly.UseVisualStyleBackColor <- true;
pbxCompoundFrequency.Controls.Add(rdoMonthly);

// Radio Button: Quarterly
rdoQuarterly.AutoSize <- true;
rdoQuarterly.Location <- new System.Drawing.Point(18, 59);
rdoQuarterly.Size <- new System.Drawing.Size(67, 17);
rdoQuarterly.TabIndex <- 1;
rdoQuarterly.TabStop <- true;
rdoQuarterly.Text <- "Quarterly";
rdoQuarterly.UseVisualStyleBackColor <- true;
pbxCompoundFrequency.Controls.Add(rdoQuarterly);

// Radio Button: Semiannually
rdoSemiannually.AutoSize <- true;
rdoSemiannually.Location <- new System.Drawing.Point(18, 89);
rdoSemiannually.Size <- new System.Drawing.Size(87, 17);
rdoSemiannually.TabIndex <- 2;
rdoSemiannually.TabStop <- true;
rdoSemiannually.Text <- "Semiannually";
rdoSemiannually.UseVisualStyleBackColor <- true;
pbxCompoundFrequency.Controls.Add(rdoSemiannually);

// Radio Button: Annually
rdoAnnually.AutoSize <- true;
rdoAnnually.Location <- new System.Drawing.Point(18, 119);
rdoAnnually.Size <- new System.Drawing.Size(65, 17);
rdoAnnually.TabIndex <- 3;
rdoAnnually.TabStop <- true;
rdoAnnually.Text <- "Annually";
rdoAnnually.UseVisualStyleBackColor <- true;
pbxCompoundFrequency.Controls.Add(rdoAnnually);

// Broup Box: Results
let gbxResults : GroupBox = new GroupBox();
gbxResults.Location <- new System.Drawing.Point(12, 182);
gbxResults.Size <- new System.Drawing.Size(361, 64);
gbxResults.TabIndex <- 2;
gbxResults.TabStop <- false;
gbxResults.Text <- "Results";
Exercise.Controls.Add(gbxResults);
gbxResults.Controls.Add(txtFutureValue);

// Label: Future Value
let lblFutureValue : Label = new Label();
lblFutureValue.AutoSize <- true;
lblFutureValue.Location <- new System.Drawing.Point(192, 29);
lblFutureValue.Name <- "lblFutureValue";
lblFutureValue.Size <- new System.Drawing.Size(70, 13);
lblFutureValue.TabIndex <- 2;
lblFutureValue.Text <- "Future Value:";
gbxResults.Controls.Add(lblFutureValue);

// Text Box: Future Value
txtFutureValue.Location <- new System.Drawing.Point(268, 26);
txtFutureValue.Size <- new System.Drawing.Size(65, 20);
txtFutureValue.TabIndex <- 3;
txtFutureValue.Text <- "0.00";
txtFutureValue.TextAlign <- HorizontalAlignment.Right;

// Label: Interest Earned
let lblInterestEarned : Label = new Label();
lblInterestEarned.AutoSize <- true;
lblInterestEarned.Location <- new System.Drawing.Point(11, 26);
lblInterestEarned.Size <- new System.Drawing.Size(82, 13);
lblInterestEarned.TabIndex <- 0;
lblInterestEarned.Text <- "Interest Earned:";
gbxResults.Controls.Add(lblInterestEarned);

// Text Box: Interest Earned
txtInterestEarned.Location <- new System.Drawing.Point(99, 23);
txtInterestEarned.Size <- new System.Drawing.Size(65, 20);
txtInterestEarned.TabIndex <- 1;
txtInterestEarned.Text <- "0.00";
txtInterestEarned.TextAlign <- HorizontalAlignment.Right;
gbxResults.Controls.Add(txtInterestEarned);

// Button: Calculate
let btnCalculate : Button = new Button();
btnCalculate.Location <- new System.Drawing.Point(388, 22);
btnCalculate.Size <- new System.Drawing.Size(75, 23);
btnCalculate.TabIndex <- 3;
btnCalculate.Text <- "Calculate";
btnCalculate.UseVisualStyleBackColor <- true;
btnCalculate.Click.Add(btnCalculateClick);
Exercise.Controls.Add(btnCalculate);

// Button Close
let btnClose : Button = new Button();
btnClose.Location <- new System.Drawing.Point(388, 51);
btnClose.Size <- new System.Drawing.Size(75, 23);
btnClose.TabIndex <- 4;
btnClose.Text <- "Close";
btnClose.UseVisualStyleBackColor <- true;
btnClose.Click.Add(btnCloseClick);
Exercise.Controls.Add(btnClose);

// Form: Exercise
Exercise.ClientSize <- new System.Drawing.Size(474, 259);
Exercise.MaximizeBox <- false;
Exercise.StartPosition <- FormStartPosition.CenterScreen;
Exercise.Text <- "Compound Interest";

[<EntryPoint>]
let main arg =
    Application.EnableVisualStyles();
    Application.Run(Exercise);
    0
             

Home Copyright © 2015 FunctionX Home