XML-Based Applications: |
|
Order processing consists of receiving requests from a customer and finding the items that the customer wants. To make this easy, as mentioned already, we created a form that allows the user to select the year, the make, the model, and the item's category. Once these selections are made, a list view displays the items that are available based on these criteria. To select an item and make it part of the order, the user can double-click it in the list view. This action copies the item by its item number, its name, and its unit price. It also sets its quantity to 1. The user can also change the quantity. The calculations are made automatically so the user doesn't have to click a button or use a calculator. |
Practical Learning: Processing Orders |
System::Void cboYears_SelectedIndexChanged(System::Object * sender, System::EventArgs * e) { String *strYearSelected = this->cboYears->Text; String *strMake = 0; XmlDocument *docXML = new XmlDocument; // Open the Parts.xml file docXML->Load(S"Parts.xml"); // We will empty the Make combo box to add a new list this->cboMakes->Items->Clear(); // We will empty the Model combo box because the car is about to be changed this->cboModels->Items->Clear(); // Also empty the list of available parts this->lvwAvailableParts->Items->Clear(); // Create a list of the nodes whose names are CarYear XmlNodeList *nodYears = docXML->GetElementsByTagName(S"CarYear"); // Create a list of the nodes whose names are Make XmlNodeList *nodMakes = docXML->GetElementsByTagName(S"Make"); // Check every CarYear node to see if its value matches the selected year for(int i = 0; i < nodYears->Count; i++) { // If the CarYear of the current node is the same as the // selected year, add its corresponding make to the Make combo box if( nodYears->ItemOf[i]->InnerXml->Equals(strYearSelected) ) { strMake = nodMakes->ItemOf[i]->InnerText; // Before adding the Make to the list, make sure that it doesn't // exist already in the combo box if( !this->cboMakes->Items->Contains(strMake) ) this->cboMakes->Items->Add(strMake); } } } |
System::Void cboMakes_SelectedIndexChanged(System::Object * sender, System::EventArgs * e) { // Get the year that was selected String *strYearSelected = this->cboYears->Text; // Get the make that is selected String *strMakeSelected = this->cboMakes->Text; // We will need a model string String *strModel = 0; // We will use a reference to the XML document XmlDocument *docXML = new XmlDocument; // Open the Parts.xml file docXML->Load(S"Parts.xml"); // We will empty the Model combo box to add a new list this->cboModels->Items->Clear(); // Also empty the list of available parts this->lvwAvailableParts->Items->Clear(); // Create a list of the nodes we will need to locate XmlNodeList *nodYears = docXML->GetElementsByTagName(S"CarYear"); XmlNodeList *nodMakes = docXML->GetElementsByTagName(S"Make"); XmlNodeList *nodModels = docXML->GetElementsByTagName(S"Model"); // Check every CarYear node to see if its // value matches the selected year for(int i = 0; i < nodYears->Count; i++) { // If the CarYear of the current node is the same as the // selected year, check its corresponding make to see whether // it matches the selected Make // If both the year and the make selected match, then add the // corresponding model to the Model combo box if( (nodYears->ItemOf[i]->InnerXml->Equals(strYearSelected)) && (nodMakes->ItemOf[i]->InnerXml->Equals(strMakeSelected)) ) { strModel = nodModels->ItemOf[i]->InnerText; // Before adding the model to the Model combo box, make sure // that it doesn't exist already in the list if( this->cboModels->Items->Contains(strModel) ) break; else // If it doesn't, then add it this->cboModels->Items->Add(strModel); } } } |
System::Void cboModels_SelectedIndexChanged(System::Object * sender, System::EventArgs * e) { // Get the year that was selected String *strYearSelected = this->cboYears->Text; // Get the make that is selected String *strMakeSelected = this->cboMakes->Text; // We will need a model string String *strModelSelected = this->cboModels->Text; // We will use a reference to the XML document XmlDocument *docXML = new XmlDocument; // Open the Parts.xml file docXML->Load(S"Parts.xml"); // Empty the Category combo box this->cboCategories->Items->Clear(); // Empty the list of available parts this->lvwAvailableParts->Items->Clear(); // Create a list of the nodes we will need to locate XmlNodeList *nodYears = docXML->GetElementsByTagName(S"CarYear"); XmlNodeList *nodMakes = docXML->GetElementsByTagName(S"Make"); XmlNodeList *nodModels = docXML->GetElementsByTagName(S"Model"); XmlNodeList *nodPartNames = docXML->GetElementsByTagName(S"PartName"); // Check every CarYear node to see if its // value matches the selected year for(int i = 0; i < nodYears->Count; i++) { // If the CarYear of the current node is the same as the // selected year, check its corresponding make and model to see whether // they match the selected Make and Model if( (nodYears->ItemOf[i]->InnerXml->Equals(strYearSelected)) && (nodMakes->ItemOf[i]->InnerXml->Equals(strMakeSelected)) && (nodModels->ItemOf[i]->InnerXml->Equals(strModelSelected)) ) { // If you find a part that match the year, the make, and the model selected, // then retrieve the (first/only) attribute of the Part Name element XmlAttribute *nodCategory = nodPartNames->ItemOf[i]->Attributes->ItemOf[S"Category"]; // Add the category to the Categories combo box String *strCategory = nodCategory->InnerText; // Before adding the model to the Model combo box, make sure // that it doesn't exist already in the list if( this->cboCategories->Items->Contains(strCategory) ) break; else // If it doesn't, then add it this->cboCategories->Items->Add(strCategory); } } } |
System::Void cboCategories_SelectedIndexChanged(System::Object * sender, System::EventArgs * e) { // Get the values the user has selected String *strYearSelected = this->cboYears->Text; String *strMakeSelected = this->cboMakes->Text; String *strModelSelected = this->cboModels->Text; String *strCategorySelected = this->cboCategories->Text; // Open the Parts.xml file XmlDocument *docXML = new XmlDocument; docXML->Load(S"Parts.xml"); // Empty the list of available parts this->lvwAvailableParts->Items->Clear(); // Create a list of the nodes we will need to locate XmlNodeList *nodYears = docXML->GetElementsByTagName(S"CarYear"); XmlNodeList *nodMakes = docXML->GetElementsByTagName(S"Make"); XmlNodeList *nodModels = docXML->GetElementsByTagName(S"Model"); XmlNodeList *nodPartNbr = docXML->GetElementsByTagName(S"PartNumber"); XmlNodeList *nodPartNames = docXML->GetElementsByTagName(S"PartName"); XmlNodeList *nodPrices = docXML->GetElementsByTagName(S"UnitPrice"); // Check every CarYear node to see if its // value matches the selected year for(int i = 0; i < nodYears->Count; i++) { XmlAttribute *nodCategory = nodPartNames->ItemOf[i]->Attributes->ItemOf[S"Category"]; // Find the year, make, model, and category that match the selected if( (nodYears->ItemOf[i]->InnerXml->Equals(strYearSelected)) && (nodMakes->ItemOf[i]->InnerXml->Equals(strMakeSelected)) && (nodModels->ItemOf[i]->InnerXml->Equals(strModelSelected)) && (nodCategory->InnerText->Equals(strCategorySelected)) ) { // Create a list view item of the part of the current model ListViewItem *itmPart = new ListViewItem(nodPartNbr->ItemOf[i]->InnerText, 0); itmPart->SubItems->Add(nodPartNames->ItemOf[i]->InnerText); itmPart->SubItems->Add(nodPrices->ItemOf[i]->InnerText); // And display that list to the list view control this->lvwAvailableParts->Items->Add(itmPart); } } } |
void CalculateTotalOrder(void) { double subTotal1, subTotal2, subTotal3, subTotal4, subTotal5, subTotal6; double orderTotal; // Retrieve the value of each sub total subTotal1 = this->txtSubTotal1->Text->ToDouble(0); subTotal2 = this->txtSubTotal2->Text->ToDouble(0); subTotal3 = this->txtSubTotal3->Text->ToDouble(0); subTotal4 = this->txtSubTotal4->Text->ToDouble(0); subTotal5 = this->txtSubTotal5->Text->ToDouble(0); subTotal6 = this->txtSubTotal6->Text->ToDouble(0); // Calculate the total value of the sub totals orderTotal = subTotal1 + subTotal2 + subTotal3 + subTotal4 + subTotal5 + subTotal6; // Display the total order in the appropriate text box this->txtTotalOrder->Text = orderTotal.ToString(S"F"); } |
System::Void lvwAvailableParts_DoubleClick(System::Object * sender, System::EventArgs * e) { ListViewItem *itmSelectedPart = this->lvwAvailableParts->SelectedItems->Item[0]; // Check the first empty row in the Order Processing section // and fill it up with the newly selected item if( this->txtPartNumber1->Text->Equals(S"") ) { this->txtPartNumber1->Text = itmSelectedPart->Text; this->txtPartName1->Text = itmSelectedPart->SubItems->Item[1]->Text; this->txtUnitPrice1->Text = itmSelectedPart->SubItems->Item[2]->Text; this->txtQuantity1->Text = S"1"; this->txtSubTotal1->Text = itmSelectedPart->SubItems->Item[2]->Text; this->chkKeepRemove1->Enabled = true; this->chkKeepRemove1->Checked = true; this->txtQuantity1->Focus(); } else if( this->txtPartNumber2->Text->Equals(S"") ) { this->txtPartNumber2->Text = itmSelectedPart->Text; this->txtPartName2->Text = itmSelectedPart->SubItems->Item[1]->Text; this->txtUnitPrice2->Text = itmSelectedPart->SubItems->Item[2]->Text; this->txtQuantity2->Text = S"1"; this->txtSubTotal2->Text = itmSelectedPart->SubItems->Item[2]->Text; this->chkKeepRemove2->Enabled = true; this->chkKeepRemove2->Checked = true; this->txtQuantity2->Focus(); } else if( this->txtPartNumber3->Text->Equals(S"") ) { this->txtPartNumber3->Text = itmSelectedPart->Text; this->txtPartName3->Text = itmSelectedPart->SubItems->Item[1]->Text; this->txtUnitPrice3->Text = itmSelectedPart->SubItems->Item[2]->Text; this->txtQuantity3->Text = S"1"; this->txtSubTotal3->Text = itmSelectedPart->SubItems->Item[2]->Text; this->chkKeepRemove3->Enabled = true; this->chkKeepRemove3->Checked = true; this->txtQuantity3->Focus(); } else if( this->txtPartNumber4->Text->Equals(S"") ) { this->txtPartNumber4->Text = itmSelectedPart->Text; this->txtPartName4->Text = itmSelectedPart->SubItems->Item[1]->Text; this->txtUnitPrice4->Text = itmSelectedPart->SubItems->Item[2]->Text; this->txtQuantity4->Text = S"1"; this->txtSubTotal4->Text = itmSelectedPart->SubItems->Item[2]->Text; this->chkKeepRemove4->Enabled = true; this->chkKeepRemove4->Checked = true; this->txtQuantity4->Focus(); } else if( this->txtPartNumber5->Text->Equals(S"") ) { this->txtPartNumber5->Text = itmSelectedPart->Text; this->txtPartName5->Text = itmSelectedPart->SubItems->Item[1]->Text; this->txtUnitPrice5->Text = itmSelectedPart->SubItems->Item[2]->Text; this->txtQuantity5->Text = S"1"; this->txtSubTotal5->Text = itmSelectedPart->SubItems->Item[2]->Text; this->chkKeepRemove5->Enabled = true; this->chkKeepRemove5->Checked = true; this->txtQuantity5->Focus(); } else if( this->txtPartNumber6->Text->Equals(S"") ) { this->txtPartNumber6->Text = itmSelectedPart->Text; this->txtPartName6->Text = itmSelectedPart->SubItems->Item[1]->Text; this->txtUnitPrice6->Text = itmSelectedPart->SubItems->Item[2]->Text; this->txtQuantity6->Text = S"1"; this->txtSubTotal6->Text = itmSelectedPart->SubItems->Item[2]->Text; this->chkKeepRemove6->Enabled = true; this->chkKeepRemove6->Checked = true; this->txtQuantity6->Focus(); } else return; // Calculate the current total order and update the order CalculateTotalOrder(); } |
System::Void txtQuantity1_Leave(System::Object * sender, System::EventArgs * e) { int qty; double unitPrice, subTotal; try { // Get the quantity of the current item qty = this->txtQuantity1->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"The value you provided for the quantity of the item is invalid" S"\nPlease try again"); } try { // Get the unit price of the current item unitPrice = this->txtUnitPrice1->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"The unit price you provided for item is invalid" S"\nPlease try again"); } // Calculate the current sub total subTotal = qty * unitPrice; // Display the new sub total in the corresponding text box this->txtSubTotal1->Text = subTotal.ToString(S"F"); // Update the order CalculateTotalOrder(); } private: System::Void txtQuantity2_Leave(System::Object * sender, System::EventArgs * e) { int qty; double unitPrice, subTotal; try { qty = this->txtQuantity2->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"The value you provided for the quantity of the item is invalid" S"\nPlease try again"); } try { unitPrice = this->txtUnitPrice2->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"The unit price you provided for item is invalid" S"\nPlease try again"); } subTotal = qty * unitPrice; this->txtSubTotal2->Text = subTotal.ToString(S"F"); CalculateTotalOrder(); } private: System::Void txtQuantity3_Leave(System::Object * sender, System::EventArgs * e) { int qty; double unitPrice, subTotal; try { qty = this->txtQuantity3->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"The value you provided for the quantity of the item is invalid" S"\nPlease try again"); } try { unitPrice = this->txtUnitPrice3->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"The unit price you provided for item is invalid" S"\nPlease try again"); } subTotal = qty * unitPrice; this->txtSubTotal3->Text = subTotal.ToString(S"F"); CalculateTotalOrder(); } private: System::Void txtQuantity4_Leave(System::Object * sender, System::EventArgs * e) { int qty; double unitPrice, subTotal; try { qty = this->txtQuantity4->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"The value you provided for the quantity of the item is invalid" S"\nPlease try again"); } try { unitPrice = this->txtUnitPrice4->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"The unit price you provided for item is invalid" S"\nPlease try again"); } subTotal = qty * unitPrice; this->txtSubTotal4->Text = subTotal.ToString(S"F"); CalculateTotalOrder(); } private: System::Void txtQuantity5_Leave(System::Object * sender, System::EventArgs * e) { int qty; double unitPrice, subTotal; try { qty = this->txtQuantity5->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"The value you provided for the quantity of the item is invalid" S"\nPlease try again"); } try { unitPrice = this->txtUnitPrice5->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"The unit price you provided for item is invalid" S"\nPlease try again"); } subTotal = qty * unitPrice; this->txtSubTotal5->Text = subTotal.ToString(S"F"); CalculateTotalOrder(); } private: System::Void txtQuantity6_Leave(System::Object * sender, System::EventArgs * e) { int qty; double unitPrice, subTotal; try { qty = this->txtQuantity6->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"The value you provided for the quantity of the item is invalid" S"\nPlease try again"); } try { unitPrice = this->txtUnitPrice6->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"The unit price you provided for item is invalid" S"\nPlease try again"); } subTotal = qty * unitPrice; this->txtSubTotal6->Text = subTotal.ToString(S"F"); CalculateTotalOrder(); } |
System::Void chkKeepRemove1_CheckedChanged(System::Object * sender, System::EventArgs * e) { // If the check box was unchecked if( this->chkKeepRemove1->Checked == false ) { // Reset the controls of the current item this->txtPartNumber1->Text = S""; this->txtPartName1->Text = S""; this->txtUnitPrice1->Text = S""; this->txtQuantity1->Text = S"0"; this->txtSubTotal1->Text = S"0.00"; this->chkKeepRemove1->Checked = false; this->chkKeepRemove1->Enabled = false; // Re-calculate the total order to update it CalculateTotalOrder(); } } private: System::Void chkKeepRemove2_CheckedChanged(System::Object * sender, System::EventArgs * e) { // If the check box was unchecked if( this->chkKeepRemove2->Checked == false ) { // Reset the controls of the current item this->txtPartNumber2->Text = S""; this->txtPartName2->Text = S""; this->txtUnitPrice2->Text = S""; this->txtQuantity2->Text = S"0"; this->txtSubTotal2->Text = S"0.00"; this->chkKeepRemove2->Checked = false; this->chkKeepRemove2->Enabled = false; // Re-calculate the total order to update it CalculateTotalOrder(); } } private: System::Void chkKeepRemove3_CheckedChanged(System::Object * sender, System::EventArgs * e) { // If the check box was unchecked if( this->chkKeepRemove3->Checked == false ) { // Reset the controls of the current item this->txtPartNumber3->Text = S""; this->txtPartName3->Text = S""; this->txtUnitPrice3->Text = S""; this->txtQuantity3->Text = S"0"; this->txtSubTotal3->Text = S"0.00"; this->chkKeepRemove3->Checked = false; this->chkKeepRemove3->Enabled = false; // Re-calculate the total order to update it CalculateTotalOrder(); } } private: System::Void chkKeepRemove4_CheckedChanged(System::Object * sender, System::EventArgs * e) { // If the check box was unchecked if( this->chkKeepRemove4->Checked == false ) { // Reset the controls of the current item this->txtPartNumber4->Text = S""; this->txtPartName4->Text = S""; this->txtUnitPrice4->Text = S""; this->txtQuantity4->Text = S"0"; this->txtSubTotal4->Text = S"0.00"; this->chkKeepRemove4->Checked = false; this->chkKeepRemove4->Enabled = false; // Re-calculate the total order to update it CalculateTotalOrder(); } } private: System::Void chkKeepRemove5_CheckedChanged(System::Object * sender, System::EventArgs * e) { // If the check box was unchecked if( this->chkKeepRemove5->Checked == false ) { // Reset the controls of the current item this->txtPartNumber5->Text = S""; this->txtPartName5->Text = S""; this->txtUnitPrice5->Text = S""; this->txtQuantity5->Text = S"0"; this->txtSubTotal5->Text = S"0.00"; this->chkKeepRemove5->Checked = false; this->chkKeepRemove5->Enabled = false; // Re-calculate the total order to update it CalculateTotalOrder(); } } private: System::Void chkKeepRemove6_CheckedChanged(System::Object * sender, System::EventArgs * e) { // If the check box was unchecked if( this->chkKeepRemove6->Checked == false ) { // Reset the controls of the current item this->txtPartNumber6->Text = S""; this->txtPartName6->Text = S""; this->txtUnitPrice6->Text = S""; this->txtQuantity6->Text = S"0"; this->txtSubTotal6->Text = S"0.00"; this->chkKeepRemove6->Checked = false; this->chkKeepRemove6->Enabled = false; // Re-calculate the total order to update it CalculateTotalOrder(); } } |
|
||
Previous | Copyright © 2004-2016, FunctionX, Inc. | Next |
|