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 will 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 LearningPractical Learning: Creating the Application

  1. Start a new project
  2. Save it in a new folder named Pledge1
  3. Save the (first) unit as Exercise
  4. Save the project as Pledge1
  5. Design the form as follows:
     
    Control Text/Caption Name Additional Properties
    Form     BorderIcons: [biSystemMenu,
                       biMinimize,biMaximize]
    BorderStyle: bsDialog
    Label Amount Pledged:    
    Edit 0.00 edtAmountPledged  
    Label Rotherham College:    
    Edit 0 edtRate1  
    UpDown 50 updAmount1 Associate: edtRate1
    Label %    
    Edit 0.00 edtAmount1  
    Label Leicester University:    
    Edit 0 edtRate2  
    UpDown 25 updAmount2 Associate: edtRate2
    Label %    
    Edit 0.00 edtAmount2  
    Label Lars Union Academy:    
    Edit 0 edtRate3  
    UpDown 25 updAmount3  
    Label %    
    TextBox 0.00 edtAmount3  
    Label Message lblMessage  
    BitBtn     Kind: bkClose
  6. Double-click each up down control and the Amount Pledged edit box
  7. Implement the events as follows:
     
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Exercise.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::updAmount1Click(TObject *Sender, TUDBtnType Button)
    {
        double AmountPledged,
                    RateAmount1, RateAmount2, RateAmount3,
                    Amount1, Amount2, Amount3,
                    Rest;
    
         AmountPledged = this->edtAmountPledged->Text.ToDouble();
    
         RateAmount2 = static_cast<double>(this->updAmount2->Position);
         RateAmount3 = static_cast<double>(this->updAmount3->Position);
         this->updAmount1->Max = 100 - RateAmount2 - RateAmount3;
         RateAmount1 = static_cast<double>(this->updAmount1->Position);
    
    	 Amount1 = AmountPledged * RateAmount1 / 100;
    	 Amount2 = AmountPledged * RateAmount2 / 100;
    	 Amount3 = AmountPledged * RateAmount3 / 100;
    	 Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    
    	 this->edtAmount1->Text = FloatToStrF(Amount1, ffCurrency, 8, 2);
    	 this->edtAmount2->Text = FloatToStrF(Amount2, ffCurrency, 8, 2);
    	 this->edtAmount3->Text = FloatToStrF(Amount3, ffCurrency, 8, 2);
    
    	 if( Rest > 0 )
         	 this->lblMessage->Caption =
    		 FloatToStrF(Rest, ffCurrency, 8, 2) + " still to be used";
    	 else
    	 	 this->lblMessage->Caption = "";
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::updAmount2Click(TObject *Sender, TUDBtnType Button)
    {
        double AmountPledged,
               RateAmount1, RateAmount2, RateAmount3,
    		   Amount1, Amount2, Amount3,
    		   Rest;
    
        AmountPledged = this->edtAmountPledged->Text.ToDouble();
    
    	RateAmount1 = static_cast<double>(this->updAmount1->Position);
    	RateAmount3 = static_cast<double>(this->updAmount3->Position);
    	this->updAmount2->Max = 100 - RateAmount1 - RateAmount3;
    	RateAmount2 = static_cast<double>(this->updAmount2->Position);
    
        Amount1 = AmountPledged * RateAmount1 / 100;
    	Amount2 = AmountPledged * RateAmount2 / 100;
    	Amount3 = AmountPledged * RateAmount3 / 100;
    	Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    
    	this->edtAmount1->Text = FloatToStrF(Amount1, ffCurrency, 8, 2);
    	this->edtAmount2->Text = FloatToStrF(Amount2, ffCurrency, 8, 2);
    	this->edtAmount3->Text = FloatToStrF(Amount3, ffCurrency, 8, 2);
    
    	if( Rest > 0 )
    	    this->lblMessage->Caption =
    		 FloatToStrF(Rest, ffCurrency, 8, 2) + " still to be used";
        else
    	    this->lblMessage->Caption = "";
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::updAmount3Click(TObject *Sender, TUDBtnType Button)
    {
        double AmountPledged,
    	RateAmount1, RateAmount2, RateAmount3,
    	Amount1, Amount2, Amount3,
    	Rest;
    
        AmountPledged = this->edtAmountPledged->Text.ToDouble();
    
        RateAmount1 = static_cast<double>(this->updAmount1->Position);
        RateAmount2 = static_cast<double>(this->updAmount2->Position);
        this->updAmount3->Max = 100 - RateAmount1 - RateAmount2;
        RateAmount3 = static_cast<double>(this->updAmount3->Position);
    
        Amount1 = AmountPledged * RateAmount1 / 100;
        Amount2 = AmountPledged * RateAmount2 / 100;
        Amount3 = AmountPledged * RateAmount3 / 100;
        Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    
        this->edtAmount1->Text = FloatToStrF(Amount1, ffCurrency, 8, 2);
        this->edtAmount2->Text = FloatToStrF(Amount2, ffCurrency, 8, 2);
        this->edtAmount3->Text = FloatToStrF(Amount3, ffCurrency, 8, 2);
    
        if( Rest > 0 )
            this->lblMessage->Caption =
    		FloatToStrF(Rest, ffCurrency, 8, 2), " still to be used";
        else
            this->lblMessage->Caption = "";
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::edtAmountPledgedChange(TObject *Sender)
    {
        if( this->edtAmountPledged->Text == "" )
        {
            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;
        }
    }
    //---------------------------------------------------------------------------
  8. Test the application

 

 

Home Copyright 2004-2007 FunctionX, Inc.