- Press Ctrl + W to access the ClassWizard. Click the Member Variables
property sheet.
- Double-click each item in the Control IDs list and create the
variables as follows:
- When you have finished, click OK.
- To display default values for some of the controls, in the
Workspace, click the ClassView property sheet and expand the
CDanilloPizzaDlg folder.
- Double-click the CDanilloPizzaDlg constructor to display its section
in the DanilloPizzaDlg.cpp file.
- Set the default values of the controls as follows:
CDaniloPizzaDlg::CDaniloPizzaDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDaniloPizzaDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDaniloPizzaDlg)
m_CheckOlives = FALSE;
m_CheckOnions = FALSE;
m_CheckPepper = FALSE;
m_CheckSausage = FALSE;
m_CheckExtraCheese = FALSE;
m_PriceLarge = _T("14.55");
m_PriceMedium = _T("12.75");
m_PriceOlives = _T("0.55");
m_PriceOnions = _T("0.25");
m_PricePepperoni = _T("0.45");
m_PriceSausage = _T("0.35");
m_PriceSmall = _T("8.95");
m_PriceExtraCheese = _T("0.45");
m_PriceBreads = _T("2.15");
m_PriceBuffWings = _T("3.25");
m_PriceDPepsi = _T("1.45");
m_PriceOJ = _T("2.25");
m_PricePepsi = _T("1.45");
m_PriceRootBeer = _T("1.25");
m_PriceSprite = _T("1.45");
m_QtyBreads = _T("0");
m_QtyBuffWings = _T("0");
m_QtyDPepsi = _T("0");
m_QtyOJ = _T("0");
m_QtyPepsi = _T("0");
m_QtyRootBeer = _T("0");
m_QtySprite = _T("0");
m_TotalBreads = _T("0.00");
m_TotalBuffWings = _T("0.00");
m_TotalDPepsi = _T("0.00");
m_TotalOJ = _T("0.00");
m_TotalPepsi = _T("0.00");
m_TotalRootBeer = _T("0.00");
m_TotalSprite = _T("0.00");
m_PriceSize = 1; // This sets the default pizza size to Medium
m_TotalOrder = _T("12.75"); // This is the same price as the Medium
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
|
- Now, we will need a function that can carry the calculations and let
other controls call it when they need to update the total price after
a change.
In the Workspace, right-click the CDanilloPizzaDlg folder and click
Add Member Function...
- Set the Function Type to void and the Function Name to Calculate
- Since this function doesn't need to be accessed outside of the
class, we will make it private. Click the Private radio button and
click OK.
- Implement the function as follows:
void CDaniloPizzaDlg::Calculate()
{
double PriceSize, Pepperoni, Sausage,
ExtraCheese, Onions, Olives, BWings, Bread,
Pepsi, DietPepsi, Sprite, OJ, RootBeer, TotalOrder;
UpdateData();
// Get the price of pizza depending on the selected size
if( m_PriceSize == 0 )
PriceSize = atof(m_PriceSmall);
else if(m_PriceSize == 1 )
PriceSize = atof(m_PriceMedium);
else
PriceSize = atof(m_PriceLarge);
// Get the price of a topping if it was selected
if( m_CheckPepper == TRUE)
Pepperoni = atof(m_PricePepperoni);
else
Pepperoni = 0;
if(m_CheckSausage == TRUE)
Sausage = atof(m_PriceSausage);
else
Sausage = 0;
if(m_CheckExtraCheese == TRUE)
ExtraCheese = atof(m_PriceExtraCheese);
else
ExtraCheese = 0;
if(m_CheckOnions == TRUE)
Onions = atof(m_PriceOnions);
else
Onions = 0;
if(m_CheckOlives == TRUE)
Olives = atof(m_PriceOlives);
else
Olives = 0;
// Calculate the price of the side dishes
// depending on the quantity entered
BWings = atof(m_TotalBuffWings);
Bread = atof(m_TotalBreads);
// Calculate the price of the drink(s)
Pepsi = atof(m_TotalPepsi);
DietPepsi = atof(m_TotalDPepsi);
Sprite = atof(m_TotalSprite);
OJ = atof(m_TotalOJ);
RootBeer = atof(m_TotalRootBeer);
TotalOrder = PriceSize
+ Pepperoni + Sausage + ExtraCheese + Onions + Olives
+ BWings + Bread
+ Pepsi + DietPepsi + Sprite + OJ + RootBeer;
m_TotalOrder.Format("%.2f", TotalOrder);
UpdateData(FALSE);
}
|
- When a customer adds one of the side items or a drink to his or her order,
the clerk types the new quantity. We need to retrieve that quantity,
multiply it by the net price of the item and display the sub-total in
the corresponding edit box. To change an item's quantity, the clerk
clicks in the desired Qty edit box and types a number. After entering
the desired value, the user presses Tab or click somewhere else. This
means that the edit box loses focus. We will use this LostFocus event
to find out if the quantity has changed and to act accordingly.
Every time a (new) calculation has occured, we will need to call the
Calculate() function to update the total price and display it. This
makes sure that the clerk doesn't have to perform any calculcation.
Press Ctrl + W to access the ClassWizard and click the Message Maps
property sheet.
- In the Object IDs list, click the IDC_QTY_BREADS to select it.
- In the Messages list box, double-click EN_KILLFOCUS and change the
name of the function to OnQtyBreadsLostFocus:
- Click OK.
- Repeat the last three steps to add the following functions:
Object
ID |
Message |
Function |
IDC_QTY_BUFFWINGS |
EN_KILLFOCUS |
OnQtyBuffWingsLostFocus |
IDC_QTY_DPEPSI |
EN_KILLFOCUS |
OnQtyDPepsiLostFocus |
IDC_QTY_OJ |
EN_KILLFOCUS |
OnQtyOJLostFocus |
IDC_QTY_PEPSI |
EN_KILLFOCUS |
OnQtyPepsiLostFocus |
IDC_QTY_RBEER |
EN_KILLFOCUS |
OnQtyRBeerLostFocus |
IDC_QTY_SPRITE |
EN_KILLFOCUS |
OnQtySpriteLostFocus |
- Click Edit Code and implement the functions as follows:
void CDaniloPizzaDlg::OnQtyBreadsLostFocus()
{
int Qty;
double Price, Total;
UpdateData();
if(m_QtyBreads.IsEmpty())
{
Qty = 0;
}
else
Qty = atoi(m_QtyBreads);
Price = atof(m_PriceBreads);
Total = Qty * Price;
m_TotalBreads.Format("%.2f", Total);
UpdateData(FALSE);
Calculate();
}
void CDaniloPizzaDlg::OnQtyBuffWingsLostFocus()
{
int Qty;
double Price, Total;
UpdateData();
if(m_QtyBuffWings.IsEmpty())
{
Qty = 0;
}
else
Qty = atoi(m_QtyBuffWings);
Price = atof(m_PriceBuffWings);
Total = Qty * Price;
m_TotalBuffWings.Format("%.2f", Total);
UpdateData(FALSE);
Calculate();
}
void CDaniloPizzaDlg::OnQtyDPepsiLostFocus()
{
int Qty;
double Price, Total;
UpdateData();
if(m_QtyDPepsi.IsEmpty())
{
Qty = 0;
}
else
Qty = atoi(m_QtyDPepsi);
Price = atof(m_PriceDPepsi);
Total = Qty * Price;
m_TotalDPepsi.Format("%.2f", Total);
UpdateData(FALSE);
Calculate();
}
void CDaniloPizzaDlg::OnQtyOJLostFocus()
{
int Qty;
double Price, Total;
UpdateData();
if(m_QtyOJ.IsEmpty())
{
Qty = 0;
}
else
Qty = atoi(m_QtyOJ);
Price = atof(m_PriceOJ);
Total = Qty * Price;
m_TotalOJ.Format("%.2f", Total);
UpdateData(FALSE);
Calculate();
}
void CDaniloPizzaDlg::OnQtyPepsiLostFocus()
{
int Qty;
double Price, Total;
UpdateData();
if(m_QtyPepsi.IsEmpty())
{
Qty = 0;
}
else
Qty = atoi(m_QtyPepsi);
Price = atof(m_PricePepsi);
Total = Qty * Price;
m_TotalPepsi.Format("%.2f", Total);
UpdateData(FALSE);
Calculate();
}
void CDaniloPizzaDlg::OnQtyRBeerLostFocus()
{
int Qty;
double Price, Total;
UpdateData();
if(m_QtyRootBeer.IsEmpty())
{
Qty = 0;
}
else
Qty = atoi(m_QtyRootBeer);
Price = atof(m_PriceRootBeer);
Total = Qty * Price;
m_TotalRootBeer.Format("%.2f", Total);
UpdateData(FALSE);
Calculate();
}
void CDaniloPizzaDlg::OnQtySpriteLostFocus()
{
int Qty;
double Price, Total;
UpdateData();
if(m_QtySprite.IsEmpty())
{
Qty = 0;
}
else
Qty = atoi(m_QtySprite);
Price = atof(m_PriceSprite);
Total = Qty * Price;
m_TotalSprite.Format("%.2f", Total);
UpdateData(FALSE);
Calculate();
}
|
- All we have to do now is to call the Calculate() function whenever
the user clicks one of the radio buttons. You could also do this for
each LostFocus event of a net price of a side item or drink.
Press Ctrl + W to access the ClassWizard.
- In the Message Maps property sheet, click the ID of each radio
button. Then, in the Messages list, double-click BN_CLICKED. Accept
the suggested name for each function.
- In the same way, create a BN_CLICKED function for the ID of each
check box and accept the suggested name.
- Click Edit Code and implement the functions as follows:
void CDaniloPizzaDlg::OnRdoSmall()
{
Calculate();
}
void CDaniloPizzaDlg::OnRdoMedium()
{
Calculate();
}
void CDaniloPizzaDlg::OnRdoLarge()
{
Calculate();
}
void CDaniloPizzaDlg::OnCheckOlives()
{
Calculate();
}
void CDaniloPizzaDlg::OnCheckOnions()
{
Calculate();
}
void CDaniloPizzaDlg::OnCheckPepperoni()
{
Calculate();
}
void CDaniloPizzaDlg::OnCheckSausage()
{
Calculate();
}
void CDaniloPizzaDlg::OnCheckXcheese()
{
Calculate();
}
|
- Test your program.
|