Home

Pledge Distribution

Pledge Distribution

Introduction

In this exercise, we are asked to create an application used to raise money for three educational institutions. To proceed, we would call people on the phone or post the application in an intranet's directory. When the application starts, an amount of money will be entered. Then, the person who is pledging money will specify the percentage of money that can be allocated to each institution. We will use three up down buttons to set the percentages. We must synchronize them so that the total values of the up down buttons must not exceed 100. The amount pledged is then converted with regards to each up down.

Practical Learning: Starting the Exercise

  1. Start Notepad and type the following
     
    import System;
    import System.Drawing;
    import System.Collections;
    import System.Windows.Forms;
    
    public class Exercise extends System.Windows.Forms.Form
    {
        var Label1     : System.Windows.Forms.Label;
        var Label2     : System.Windows.Forms.Label;
        var Label3     : System.Windows.Forms.Label;
        var Label4     : System.Windows.Forms.Label;
        var Label5     : System.Windows.Forms.Label;
        var Label6     : System.Windows.Forms.Label;
        var Label7     : System.Windows.Forms.Label;
        var lblMessage : System.Windows.Forms.Label;
        var btnClose   : System.Windows.Forms.Button;
        var txtAmount3 : System.Windows.Forms.TextBox;
        var updAmount3 : System.Windows.Forms.NumericUpDown;
        var txtAmount2 : System.Windows.Forms.TextBox;
        var updAmount2 : System.Windows.Forms.NumericUpDown;
        var txtAmount1 : System.Windows.Forms.TextBox;
        var updAmount1 : System.Windows.Forms.NumericUpDown;
        var txtAmountPledged : System.Windows.Forms.TextBox;
    
        function Exercise()
        {
    	InitializeComponent();
    
            this.updAmount1.add_Click(this.updAmount1_ValueChanged);
    	this.updAmount2.add_Click(this.updAmount2_ValueChanged);
    	this.updAmount3.add_Click(this.updAmount3_ValueChanged);
    	this.btnClose.add_Click(this.btnClose_Click);
        }
    
        function updAmount1_ValueChanged(sender : Object, e : System.EventArgs)
        {
    	var AmountPledged : double;
            var RateAmount1   : double;
            var RateAmount2   : double;
            var RateAmount3   : double;
            var Amount1       : double;
            var Amount2       : double;
            var Amount3       : double;
            var Rest          : double;
    
    	AmountPledged = double.Parse(this.txtAmountPledged.Text);
    
    	RateAmount2 = parseFloat(this.updAmount2.Value.ToString());
    	RateAmount3 = parseFloat(this.updAmount3.Value.ToString());
    	this.updAmount1.Maximum = (decimal)(100 - RateAmount2 - RateAmount3);
    	RateAmount1 = parseFloat(this.updAmount1.Value.ToString());
    
    	Amount1 = AmountPledged * RateAmount1 / 100;
    	Amount2 = AmountPledged * RateAmount2 / 100;
    	Amount3 = AmountPledged * RateAmount3 / 100;
    	Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    		 
    	this.txtAmount1.Text = Amount1.ToString("C");
    	this.txtAmount2.Text = Amount2.ToString("C");
    	this.txtAmount3.Text = Amount3.ToString("C");
    
    	if( Rest > 0 )
                this.lblMessage.Text = Rest.ToString("C") + " still to be used";
    	else
    	    this.lblMessage.Text = "";
        }
    
        function updAmount2_ValueChanged(sender : Object, e : System.EventArgs)
        {
    	var AmountPledged : double;
            var RateAmount1   : double;
            var RateAmount2   : double;
            var RateAmount3   : double;
            var Amount1       : double;
            var Amount2       : double;
            var Amount3       : double;
            var Rest          : double;
    
    	AmountPledged = double.Parse(this.txtAmountPledged.Text);
    
    	RateAmount1 = parseFloat(this.updAmount1.Value.ToString());
    	RateAmount3 = parseFloat(this.updAmount3.Value.ToString());
    	this.updAmount2.Maximum = 100 - RateAmount1 - RateAmount3;
    	RateAmount2 = parseFloat(this.updAmount2.Value.ToString());
    
    	Amount1 = AmountPledged * RateAmount1 / 100;
    	Amount2 = AmountPledged * RateAmount2 / 100;
    	Amount3 = AmountPledged * RateAmount3 / 100;
    	Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    
    	this.txtAmount1.Text = Amount1.ToString("C");
    	this.txtAmount2.Text = Amount2.ToString("C");
    	this.txtAmount3.Text = Amount3.ToString("C");
    
    	if( Rest > 0 )
                this.lblMessage.Text = Rest.ToString("C") + " still to be used";
    	else
                this.lblMessage.Text = "";
        }
    
        function updAmount3_ValueChanged(sender : Object, e : System.EventArgs)
        {
    	var AmountPledged : double;
            var RateAmount1   : double;
            var RateAmount2   : double;
            var RateAmount3   : double;
            var Amount1       : double;
            var Amount2       : double;
            var Amount3       : double;
            var Rest          : double;
    
    	AmountPledged = double.Parse(this.txtAmountPledged.Text);
    
    	RateAmount1 = parseFloat(this.updAmount1.Value.ToString());
    	RateAmount2 = parseFloat(this.updAmount2.Value.ToString());
    	this.updAmount3.Maximum = 100 - RateAmount1 - RateAmount2;
    	RateAmount3 = parseFloat(this.updAmount3.Value.ToString());
    
    	Amount1 = AmountPledged * RateAmount1 / 100;
    	Amount2 = AmountPledged * RateAmount2 / 100;
    	Amount3 = AmountPledged * RateAmount3 / 100;
    	Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    
    	this.txtAmount1.Text = Amount1.ToString("C");
    	this.txtAmount2.Text = Amount2.ToString("C");
    	this.txtAmount3.Text = Amount3.ToString("C");
    
    	if( Rest > 0 )
                this.lblMessage.Text = Rest.ToString("C") + " still to be used";
    	else
    	    this.lblMessage.Text = "";
        }
    
        function txtAmountPledged_TextChanged(sender : Object, e : System.EventArgs)
        {
    	if( this.txtAmountPledged.Text.Equals("") )
    	{
    	    this.updAmount1.Enabled = false;
    	    this.updAmount2.Enabled = false;
    	    this.updAmount3.Enabled = false;
    	}
    	else
    	{
    	    this.updAmount1.Enabled = true;
    	    this.updAmount2.Enabled = true;
    	    this.updAmount3.Enabled = true;
    	}
        }
    
        function btnClose_Click(sender : Object, e : System.EventArgs)
        {
    	Close();
        }
    
        function InitializeComponent()
        {
    	this.lblMessage = new System.Windows.Forms.Label();
    	this.btnClose = new System.Windows.Forms.Button();
    	this.txtAmount3 = new System.Windows.Forms.TextBox();
    	this.Label6 = new System.Windows.Forms.Label();
    	this.updAmount3 = new System.Windows.Forms.NumericUpDown();
    	this.Label7 = new System.Windows.Forms.Label();
    	this.txtAmount2 = new System.Windows.Forms.TextBox();
    	this.Label4 = new System.Windows.Forms.Label();
    	this.updAmount2 = new System.Windows.Forms.NumericUpDown();
    	this.Label5 = new System.Windows.Forms.Label();
    	this.txtAmount1 = new System.Windows.Forms.TextBox();
    	this.Label3 = new System.Windows.Forms.Label();
    	this.updAmount1 = new System.Windows.Forms.NumericUpDown();
    	this.Label2 = new System.Windows.Forms.Label();
    	this.txtAmountPledged = new System.Windows.Forms.TextBox();
    	this.Label1 = new System.Windows.Forms.Label();
    
    	// 
    	// lblMessage
    	// 
    	this.lblMessage.Location = new System.Drawing.Point(16, 160);
    	this.lblMessage.Name = "lblMessage";
    	this.lblMessage.Size = new System.Drawing.Size(168, 23);
    	this.lblMessage.TabIndex = 47;
    	this.lblMessage.Text = "Message";
    
    	// 
    	// btnClose
    	// 
    	this.btnClose.Location = new System.Drawing.Point(200, 160);
    	this.btnClose.Name = "btnClose";
    	this.btnClose.TabIndex = 46;
    	this.btnClose.Text = "Close";
    
    	// 
    	// txtAmount3
    	// 
    	this.txtAmount3.Location = new System.Drawing.Point(200, 120);
    	this.txtAmount3.Name = "txtAmount3";
    	this.txtAmount3.Size = new System.Drawing.Size(72, 20);
    	this.txtAmount3.TabIndex = 45;
    	this.txtAmount3.Text = "0.00";
    	this.txtAmount3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    
    	// 
    	// Label6
    	// 
    	this.Label6.Location = new System.Drawing.Point(176, 128);
    	this.Label6.Name = "Label6";
    	this.Label6.Size = new System.Drawing.Size(16, 16);
    	this.Label6.TabIndex = 44;
    	this.Label6.Text = "%";
    
    	// 
    	// updAmount3
    	// 
    	this.updAmount3.Location = new System.Drawing.Point(128, 120);
    	this.updAmount3.Name = "updAmount3";
    	this.updAmount3.Size = new System.Drawing.Size(48, 20);
    	this.updAmount3.TabIndex = 43;
    	this.updAmount3.Value = 25.00; // new System.Decimal(new int[] {25, 0, 0, 0});
    
    	// 
    	// Label7
    	// 
    	this.Label7.Location = new System.Drawing.Point(16, 128);
    	this.Label7.Name = "Label7";
    	this.Label7.Size = new System.Drawing.Size(112, 20);
    	this.Label7.TabIndex = 42;
    	this.Label7.Text = "Lars Union Academy:";
    
    	// 
    	// txtAmount2
    	// 
    	this.txtAmount2.Location = new System.Drawing.Point(200, 88);
    	this.txtAmount2.Name = "txtAmount2";
    	this.txtAmount2.Size = new System.Drawing.Size(72, 20);
    	this.txtAmount2.TabIndex = 41;
    	this.txtAmount2.Text = "0.00";
    	this.txtAmount2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    
    	// 
    	// Label4
    	// 
    	this.Label4.Location = new System.Drawing.Point(176, 88);
    	this.Label4.Name = "Label4";
    	this.Label4.Size = new System.Drawing.Size(16, 16);
    	this.Label4.TabIndex = 40;
    	this.Label4.Text = "%";
    
    	// 
    	// updAmount2
    	// 
    	this.updAmount2.Location = new System.Drawing.Point(128, 88);
    	this.updAmount2.Name = "updAmount2";
    	this.updAmount2.Size = new System.Drawing.Size(48, 20);
    	this.updAmount2.TabIndex = 39;
    	this.updAmount2.Value = 25.00; // new System.Decimal(new int[] { 25, 0, 0, 0});
    
    	// 
    	// Label5
    	// 
    	this.Label5.Location = new System.Drawing.Point(16, 88);
    	this.Label5.Name = "Label5";
    	this.Label5.Size = new System.Drawing.Size(112, 20);
    	this.Label5.TabIndex = 38;
    	this.Label5.Text = "Leicester University:";
    
    	// 
    	// txtAmount1
    	// 
    	this.txtAmount1.Location = new System.Drawing.Point(200, 56);
    	this.txtAmount1.Name = "txtAmount1";
    	this.txtAmount1.Size = new System.Drawing.Size(72, 20);
    	this.txtAmount1.TabIndex = 37;
    	this.txtAmount1.Text = "0.00";
    	this.txtAmount1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    
    	// 
    	// Label3
    	// 
    	this.Label3.Location = new System.Drawing.Point(176, 64);
    	this.Label3.Name = "Label3";
    	this.Label3.Size = new System.Drawing.Size(16, 16);
    	this.Label3.TabIndex = 36;
    	this.Label3.Text = "%";
    
    	// 
    	// updAmount1
    	// 
    	this.updAmount1.Location = new System.Drawing.Point(128, 56);
    	this.updAmount1.Name = "updAmount1";
    	this.updAmount1.Size = new System.Drawing.Size(48, 20);
    	this.updAmount1.TabIndex = 35;
    	this.updAmount1.Value = 50.00; // new System.Decimal(new int[] {50, 0, 0, 0});
    	
    	// 
    	// Label2
    	// 
    	this.Label2.Location = new System.Drawing.Point(16, 56);
    	this.Label2.Name = "Label2";
    	this.Label2.Size = new System.Drawing.Size(112, 20);
    	this.Label2.TabIndex = 34;
    	this.Label2.Text = "Rotherham College:";
    
    	// 
    	// txtAmountPledged
    	// 
    	this.txtAmountPledged.Location = new System.Drawing.Point(128, 16);
    	this.txtAmountPledged.Name = "txtAmountPledged";
    	this.txtAmountPledged.Size = new System.Drawing.Size(144, 20);
    	this.txtAmountPledged.TabIndex = 33;
    	this.txtAmountPledged.Text = "0.00";
    	this.txtAmountPledged.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    
    	// 
    	// Label1
    	// 
    	this.Label1.Location = new System.Drawing.Point(16, 16);
    	this.Label1.Name = "Label1";
    	this.Label1.Size = new System.Drawing.Size(96, 16);
    	this.Label1.TabIndex = 32;
    	this.Label1.Text = "Amount Pledged:";
    	// 
    	// Form1
    	// 
    	this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    	this.ClientSize = new System.Drawing.Size(288, 198);
    	this.Controls.Add(this.lblMessage);
    	this.Controls.Add(this.btnClose);
    	this.Controls.Add(this.txtAmount3);
    	this.Controls.Add(this.Label6);
    	this.Controls.Add(this.updAmount3);
    	this.Controls.Add(this.Label7);
    	this.Controls.Add(this.txtAmount2);
    	this.Controls.Add(this.Label4);
    	this.Controls.Add(this.updAmount2);
    	this.Controls.Add(this.Label5);
    	this.Controls.Add(this.txtAmount1);
    	this.Controls.Add(this.Label3);
    	this.Controls.Add(this.updAmount1);
    	this.Controls.Add(this.Label2);
    	this.Controls.Add(this.txtAmountPledged);
    	this.Controls.Add(this.Label1);
    
    	this.Name = "Exercise";
    	this.Text = "Pledge Distribution";
      	this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
    	this.MaximizeBox = false;
    	this.MinimizeBox = false;
        }		
    }
    
    var frmPledge : Exercise = new Exercise();
    
    frmPledge.ShowDialog();
    
  2. Save the file as Exercise.js in a folder named Pledge1
  3. Open the Command Prompt and switch to the folder that contains the above file
  4. To compile it, type jsc /target:winexe Exercise.js
  5. To execute it, type Exercise
  6. Close
 

Home Copyright 2004 FunctionX, Inc.