Home

Bethesda Car Rental: Rental Orders

 

Introduction

To main purpose of a car rental company is to rent car. This is done by receiving orders from a customer and processing such an order. To proceed, a clerk would use a form to enter the customer and the car information. In our application, we will also make sure that name of the clerk who processed an order is specified. Other than than, we will enter as much information as possible to assist the user.

Practical Learning Practical Learning: Creating a Serializable Class

  1. To add a new form to the project, on the main menu, click Project -> Add New Item...
  2. In the Template section, click Windows Form
  3. Set the Name to RentalRates and press Enter
  4. Add a ListView to the form and create its Columns as follows:
     
    (Name) Text TextAlign Width
    colCategory Category   90
    colDaily Daily Right  
    colWeekly Weekly Right  
    colMonthly Monthly Right  
    colWeekend Weekend Right  
  5. Create its Items as follows:
     
    ListViewItem ListViewSubItem ListViewSubItem ListViewSubItem ListViewSubItem
      Text Text Text Text
    Economy 32.95 29.75 22.95 19.95
    Compact 39.95 34.75 29.95 24.95
    Standard 45.95 39.75 35.95 32.95
    Full Size 49.95 42.75 38.95 35.95
    Mini Van 55.95 50.75 45.95 42.95
    SUV 55.95 50.75 45.95 42.95
    Truck 42.75 38.75 35.95 32.95
    Van 69.95 62.75 55.95 52.95
  6. Complete the design of the form as follows:
     
  7. To add a new class to the project, on the main menu, click Project -> Add Class...
  8. In the Templates section, click Generic C++ Class and click Open
  9. Set the Class Name to CRentalOrder and press Enter
  10. Access the RentalOrder.h file and change it as follows:
     
    #pragma once
    
    using namespace System;
    
    [Serializable]
    __sealed __gc class CRentalOrder
    {
    public:
    	int      ReceiptNumber;
    	String  *ProcessedBy;
    	String  *CarSelected;
    	String  *Make;
    	String  *Model;
    	int	 Year;
    	String  *CarCondition;
    	String  *CustDrvLicNbr;
    	String  *CustName;
    	String  *CustAddress;
    	String  *CustCity;
    	String  *CustState;
    	String  *CustZIPCode;
    	String  *CustCountry;
    	String  *TankLevel;
    	long     Mileage;
    	DateTime StartDate;
    	DateTime EndDate;
    	int      Days;
    	double   RateApplied;
    	double   SubTotal;
    	double   TaxRate;
    	double   TaxAmount;
    	double   OrderTotal;
    
    public:
    	CRentalOrder(void);
    	~CRentalOrder(void);
    };
  11. To add a new form to the project, on the main menu, click Project -> Add New Item...
  12. In the Templates section, click Windows Form
  13. Set the Name to RentalOrders and press Enter
  14. Design the form as follows:
     
    Control Text Name Other Properties
    GroupBox Order Identification    
    Label Processed By:    
    ComboBox   cboEmployees  
    GroupBox Car Selected    
    ComboBox   cboCars  
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtCarYear  
    label Car Condition:    
    ComboBox   cboCarConditions  
    GroupBox Customer    
    ComboBox   cboCustomers  
    Label Name:    
    TextBox   txtCustName  
    Label Address:    
    TextBox   txtCustAddress  
    TextBox   txtCustCity  
    TextBox MD txtCustState  
    TextBox   txtCustZIPCode  
    TextBox USA txtCustCountry  
    GroupBox Order Evaluation    
  15. Right-click anywhere in the form and click View Code
  16. In the top section of the file, type the following:
     
    #pragma once
    
    #using <System.Runtime.Serialization.Formatters.Soap.dll>
    
    #include "Car.h"
    #include "Employee.h"
    #include "Customer.h"
    #include "RentalRates.h"
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::IO;
    using namespace System::Runtime::Serialization::Formatters::Soap;
    
    namespace BCR2
    {
  17. In the class, declare a pointer variable to ArrayList named lstRentalOrders
     
    private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container* components;
    		ArrayList *lstRentalOrders;
  18. Return to the CleaningOrders form
  19. In the combo box above the Properties window, select RentalOrders and click the Events button
  20. In the Events section, double-click Load and implement the event as follows:
     
    System::Void RentalOrders_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	  lstRentalOrders = new ArrayList;
    	 ArrayList *lstEmployees = new ArrayList;
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    	 String *strFilename = S"Employees.bcr";
    			 
    	 if( File::Exists(strFilename) )
    	 {
    FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    		 lstEmployees = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    		 bcrStream->Close();
    
    		 Employee *empl = new Employee;
    		 String *firstName, *lastName, *title;
    
    		 cboEmployees->Items->Clear();
    
    		 for(int i = 0; i < lstEmployees->Count; i++)
    		 {
    			 empl = dynamic_cast<Employee *>(lstEmployees->Item[i]);
    
    			 firstName = empl->FirstName;
    			 lastName  = empl->LastName;
    			 title     = empl->Title;
    			 
    			 cboEmployees->Items->Add(String::Concat(lastName, S", ", firstName, S" - ", title));
    		 }
    	 }
    
    	 strFilename = S"Cars.bcr";
    	 ArrayList *lstCars = new ArrayList;
    			 
    	 if( File::Exists(strFilename) )
    	 {
    FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    		 lstCars = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    		 bcrStream->Close();
    
    		 CCar *car = new CCar;
    
    		 cboCars->Items->Clear();
    
    		 for(int i = 0; i < lstCars->Count; i++)
    		 {
    			 car = dynamic_cast<CCar *>(lstCars->Item[i]);
    
    			 cboCars->Items->Add(car->TagNumber);
    		 }
    	 }
    
    	 strFilename = S"Customers.bcr";
    	 ArrayList *lstCustomers = new ArrayList;
    			 
    	 if( File::Exists(strFilename) )
    	 {
    FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    		 lstCustomers = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    		 bcrStream->Close();
    
    		 CCustomer *cust = new CCustomer;
    
    		 cboCustomers->Items->Clear();
    
    		 for(int i = 0; i < lstCustomers->Count; i++)
    		 {
    			 cust = dynamic_cast<CCustomer *>(lstCustomers->Item[i]);
    
    			 cboCustomers->Items->Add(cust->DrvLicNbr);
    		 }
    	 }
    }
  21. Return to the RentalOrders form. Double-click the Car Selected combo box and implement its SelectedIndexChanged event as follows:
     
    System::Void cboCars_SelectedIndexChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *strFilename = S"Cars.bcr";
    	 ArrayList *lstCars = new ArrayList;
    	 CCar *car = new CCar;
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    
    FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    	 lstCars = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    	 bcrStream->Close();
    
    	 for(int i = 0; i < lstCars->Count; i++)
    	 {
    		 car = dynamic_cast<CCar *>(lstCars->Item[i]);
    
    		 if( car->TagNumber->Equals(cboCars->Text) )
    		 {
    			 this->txtMake->Text    = car->Make;
    			 this->txtModel->Text   = car->Model;
    			 this->txtCarYear->Text = car->Year.ToString();
    			 return;
    		 }
    	 }
    }
  22. Return to the RentalOrders form. Double-click the Customer combo box and implement its SelectedIndexChanged event as follows:
     
    System::Void cboCustomers_SelectedIndexChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *strFilename = S"Customers.bcr";
    	 ArrayList *lstCustomers = new ArrayList;
    	 CCustomer *cust = new CCustomer;
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    
    FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    	 lstCustomers = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    	 bcrStream->Close();
    
    	 for(int i = 0; i < lstCustomers->Count; i++)
    	 {
    		 cust = dynamic_cast<CCustomer *>(lstCustomers->Item[i]);
    
    		 if( cust->DrvLicNbr->Equals(cboCustomers->Text) )
    		 {
    			 this->txtCustName->Text    = cust->FullName;
    			 this->txtCustAddress->Text = cust->Address;
    			 this->txtCustCity->Text    = cust->City;
    			 this->txtCustState->Text   = cust->State;
    			 this->txtCustZIPCode->Text = cust->ZIPCode;
    			 this->txtCustCountry->Text = cust->Country;
    			 return;
    		 }
    	 }
    }
  23. Return to the RentalOrders form and double-click the Rate Applied button to implement its Click event as follows:
     
    System::Void btnRateApplied_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 RentalRates *frmRates = new RentalRates;
    	 frmRates->Show();
    }
  24. Return to the RentalOrders form and double-click the End Date control
  25. Implement its Click event as follows:
     
    System::Void dtpEndDate_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 DateTime dteStart = this->dtpStartDate->Value;
    	 DateTime dteEnd  = this->dtpEndDate->Value;
    	 TimeSpan tme = dteEnd - dteStart;
    	 int days = tme.Days;
    
    	 this->txtDays->Text = days.ToString();
    }
  26. Return to the RentalOrders form and click the text box on the right side of the Rate Applied button
  27. In the Properties window, click Events and double-click the Leave field
  28. Implement its event as follows:
     
    System::Void txtRateApplied_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 int days = this->txtDays->Text->ToInt32(0);
    	 double rateApplied = this->txtRateApplied->Text->ToDouble(0);
    	 double subTotal = days * rateApplied;
    	 this->txtSubTotal->Text = subTotal.ToString(S"F");
    	 double taxRate = this->txtTaxRate->Text->ToDouble(0);
    	 double taxAmount = subTotal * taxRate / 100;
    	 this->txtTaxAmount->Text = taxAmount.ToString(S"F");
    	 double totalOrder = subTotal + taxAmount;
    	 this->txtOrderTotal->Text = totalOrder.ToString(S"F");
    }
  29. Return to the RentalOrders form and double-click the Save button to implement its Click event as follows:
     
    System::Void btnSave_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 static int receiptNumber = 0;
    	 ArrayList *lstRentalOrders = new ArrayList;
    	 String *strFilename = S"RentalOrders.bcr";
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    
    	 if( File::Exists(strFilename) )
    	 {
     FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    		 lstRentalOrders = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    
     CRentalOrder *order = __try_cast<CRentalOrder *>(lstRentalOrders->Item[lstRentalOrders->Count-1]);
    		 receiptNumber = order->ReceiptNumber;
    		 bcrStream->Close();
    	 }
    	 else
    	 {
     FileStream *bcrStream = new FileStream(strFilename, FileMode::OpenOrCreate, FileAccess::Write, FileShare::Write);
    		 bcrSoap->Serialize(bcrStream, lstRentalOrders);
    		 bcrStream->Close();
    	 }
    
    	 CRentalOrder *rntOrder = new CRentalOrder;
    
    	 rntOrder->ReceiptNumber = receiptNumber + 1;
    	 rntOrder->ProcessedBy   = this->cboEmployees->Text;
    	 rntOrder->CarSelected   = this->cboCars->Text;
    	 rntOrder->Make			 = this->txtMake->Text;
    	 rntOrder->Model		 = this->txtModel->Text;
    	 rntOrder->Year			 = this->txtCarYear->Text->ToInt32(0);
    	 rntOrder->CarCondition  = this->cboCarConditions->Text;
    	 rntOrder->CustDrvLicNbr = this->cboCustomers->Text;
    	 rntOrder->CustName		 = this->txtCustName->Text;
    	 rntOrder->CustAddress   = this->txtCustAddress->Text;
    	 rntOrder->CustCity		 = this->txtCustCity->Text;
    	 rntOrder->CustState	 = this->txtCustState->Text;
    	 rntOrder->CustZIPCode	 = this->txtCustZIPCode->Text;
    	 rntOrder->CustCountry	 = this->txtCustCountry->Text;
    	 rntOrder->TankLevel	 = this->cboTankLevels->Text;
    	 rntOrder->Mileage		 = this->txtMileage->Text->ToInt32(0);
    	 rntOrder->StartDate	 = this->dtpStartDate->Value;
    	 rntOrder->EndDate		 = this->dtpEndDate->Value;
    	 rntOrder->Days			 = this->txtDays->Text->ToInt32(0);
    	 rntOrder->RateApplied	 = this->txtRateApplied->Text->ToDouble(0);
    	 rntOrder->SubTotal		 = this->txtSubTotal->Text->ToDouble(0);
    	 rntOrder->TaxRate		 = this->txtTaxRate->Text->ToDouble(0);
    	 rntOrder->TaxAmount	 = this->txtTaxAmount->Text->ToDouble(0);
    	 rntOrder->OrderTotal	 = this->txtOrderTotal->Text->ToDouble(0);
    			 
    	 lstRentalOrders->Add(rntOrder);
    
     FileStream *bcrStream = new FileStream(strFilename, FileMode::OpenOrCreate, FileAccess::Write, FileShare::Write);
    	 bcrSoap->Serialize(bcrStream, lstRentalOrders);
    	 bcrStream->Close();
    
    	 this->cboEmployees->Text = S"";
    	 this->cboCars->Text = S"";
    	 this->txtMake->Text = S"";
    	 this->txtModel->Text = S"";
    	 this->txtCarYear->Text = S"2000";
    	 this->cboCarConditions->Text = S"";
    	 this->cboCustomers->Text = S"";
    	 this->txtCustAddress->Text = S"";
    	 this->txtCustAddress->Text = S"";
    	 this->txtCustCity->Text = S"";
    	 this->txtCustState->Text = S"MD";
    	 this->txtCustZIPCode->Text = S"";
    	 this->txtCustCountry->Text = S"USA";
    	 this->cboTankLevels->Text = S"";
    	 this->txtMileage->Text = S"";
    	 this->dtpStartDate->Value = DateTime::Now;
    	 this->dtpEndDate->Value = DateTime::Now;
    	 this->txtDays->Text = S"0";
    	 this->txtRateApplied->Text = S"24.95";
    	 this->txtSubTotal->Text = S"0.00";
    	 this->txtTaxRate->Text = S"7.75";
    	 this->txtTaxAmount->Text = S"0.00";
    	 this->txtOrderTotal->Text = S"0.00";
    	 this->cboEmployees->Focus();
    }
  30. Display the first form, Form1 and complete its design as follows:
     
    Control Name Text
    Button btnRentalOrders Rental Orders
    Button btnEmployees Employees
    Button btnCustomers Customers
  31. Double-click the Rental Orders button and implement its Click event as follows:
     
    System::Void btnRentalOrders_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 RentalOrders *frmOrders = new RentalOrders;
    	 frmOrders->Show();
    }
  32. Return to the Form1 form and double-click the Close button
  33. Implement it as follows:
     
    System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  34. Execute the application and create a few rental orders
     
  35. Close the forms and return to your programming environment
  36. Return to the RentalOrders form and double-click the Open button to implement its Click event as follows:
     
    System::Void btnOpen_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 ArrayList *lstRentalOrders = new ArrayList;
    	 String *strFilename = S"RentalOrders.bcr";
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    	 CRentalOrder *order;
    	 int receiptNumber = this->txtReceiptNumber->Text->ToInt32(0);
    
    	 if( File::Exists(strFilename) )
    	 {
     FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    		 lstRentalOrders = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    
    		 bcrStream->Close();
    
    		 for(int i = 0; i < lstRentalOrders->Count; i++)
    		 {
    			 order = dynamic_cast<CRentalOrder *>(lstRentalOrders->Item[i]);
    
    			 if( order->ReceiptNumber == receiptNumber )
    			 {
    				 this->cboEmployees->Text   = order->ProcessedBy;
    				 this->cboCars->Text        = order->CarSelected;
    				 this->txtMake->Text        = order->Make;
    				 this->txtModel->Text       = order->Model;
    				 this->txtCarYear->Text     = order->Year.ToString();
    				 this->cboCarConditions->Text = order->CarCondition;
    				 this->cboCustomers->Text   = order->CustDrvLicNbr;
    				 this->txtCustName->Text    = order->CustName;
    				 this->txtCustAddress->Text = order->CustAddress;
    				 this->txtCustCity->Text    = order->CustCity;
    				 this->txtCustState->Text   = order->CustState;
    				 this->txtCustZIPCode->Text = order->CustZIPCode;
    				 this->txtCustCountry->Text = order->CustCountry;
    				 this->cboTankLevels->Text  = order->TankLevel;
    				 this->txtMileage->Text     = order->Mileage.ToString();
    				 this->dtpStartDate->Value  = order->StartDate;
    				 this->dtpEndDate->Value    = order->EndDate;
    				 this->txtDays->Text        = order->Days.ToString();
    				 this->txtRateApplied->Text = order->RateApplied.ToString();
    				 this->txtSubTotal->Text    = order->SubTotal.ToString();
    				 this->txtTaxRate->Text     = order->TaxRate.ToString();
    				 this->txtTaxAmount->Text   = order->TaxAmount.ToString();
    				 this->txtOrderTotal->Text  = order->OrderTotal.ToString();
    			 }
    		 }
    	 }
    	 else
    	 {
    		 MessageBox::Show(S"There is no rental order with that receipt number!");
    		 return;
    	 }
    }
  37. Execute the application and try opening previous created orders
 

Previous Copyright © 2005-2010 FunctionX, Inc.