Home

VCL Examples: The CD Publisher

 

Introduction

The application we are about to develop is for a CD publishing small business. This company manufactures compact discs for self-promoting musicians and small business that want to sell their own CDs. When taking an order of a new CD, the company charges:

  • $20/CD if the customer is ordering less than 20 units
  • $15/CD if the customer is ordering up to 50 units
  • $12/CD if the customer is ordering up to 100 units
  • $8/CD if the customer is ordering up to 500 units
  • $5/CD for any order over 500 units

 

Practical Learning Practical Learning: Introducing the Spin Button

  1. Start a new project with its default form
  2. Save it in a new folder named CDPublisher1
  3. Save the unit as Exercise and save the project as CDPublisher
  4. Change the Caption of the form Compact Disc Publisher
  5. Change its Name to frmMain and set its BorderStyle to bsDialog
  6. Design the form by adding the following controls:
     
    Control Caption/Text Name
    Bevel     
    Label Number of Items:  
    Edit 0 edtQuantity
    Label Unit Price:  
    Edit 20.00 edtUnitPrice
    Label Total Price:  
    Edit 0.00 EdtTotal
    Button Kind = bkClose  
  7. Save all
  8. On the Component Palette, click the Samples tab
  9. Click CSpinButton and flick on right side of the Number of Items edit box on the form
  10. On the Object Inspector, change its name to spnQuantity
  11. Display the header file of the form. In the private section of the header file, declare a Value variable of type int. Also, declare a method named EvaluatePrice() of type void that uses __fastcall:
     
    private:
    	int Value;
    	void __fastcall EvaluatePrice(); // User declarations
  12. In the formís source file, initialize the value to 0 and implement the new method as follows:
     
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
    : TForm(Owner)
    {
    	Value = 0;
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::EvaluatePrice()
    {
    	int Quantity;
    	double UnitPrice, TotalPrice;
    
    	Quantity = edtQuantity->Text.ToInt();
    
    	if( Quantity < 20 )
    		UnitPrice = 20;
    	else if( Quantity < 50 )
    		UnitPrice = 15;
    	else if( Quantity < 100 )
    		UnitPrice = 12;
    	else if( Quantity < 500 )
    		UnitPrice = 8;
    	else
    		UnitPrice = 5;
    
    	TotalPrice = Quantity * UnitPrice;
    
    edtUnitPrice->Text = edtUnitPrice->Text.FormatFloat("#,##0.00", UnitPrice);
    edtTotal->Text = edtTotal->Text.FormatFloat("#,##0.00", TotalPrice);
    }
    //---------------------------------------------------------------------------
  13. On the form, click the SpinButton control to select it. On the Object Inspector, click the Events tab
  14. Double-click the empty field on the right side of OnUpClick and implement the event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::spnQuantityUpClick(TObject *Sender)
    {
    	edtQuantity->Text = Value++;
    	EvaluatePrice();
    }
    //---------------------------------------------------------------------------
  15. On the Object Inspector, double-click the field of the OnDownClick event and implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::spnQuantityDownClick(TObject *Sender)
    {
    	if( Value > 0 )
    		edtQuantity->Text = Value--;
    	else
    		edtQuantity->Text = 0;
    
    	EvaluatePrice();
    }
    //---------------------------------------------------------------------------
  16. To test the form, on the main menu, click Run -> Run

  17. After experimenting with the spin button, close the form
 
Home Copyright © 2005-2007 FunctionX, Inc.