Home

Bethesda Car Rental: Cars Records

 

Introduction

Cars are at the center of the rental transactions of our company. A car is the main reason a customer comes to the business. In our application, we will provide all the necessary information related to a car such as its make, model, year, picture, and whether it is available. Because we know that sometimes when renting or choosing a car, a customer may want to know the options available on a particular car, we will also list these basic pieces of information. Finally, we will mark a car as available or not. This will allow the clerk processing an order to know whether the customer can rent the car or not.

We will create two forms related to cars. One form will be used to enter a new car when the company acquires one. On the other hand, when interviewing a customer, if the clerk wants to see a list of the company cars, we will create a form that can help with this, allowing the clerk to navigate among cars for a review.

 

Practical Learning Practical Learning: Processing Cars

  1. Copy the following pictures to the folder of the current project (Save them with their default names):
     
  2. Return to your programming environment
  3. To add a new form to the application, on the main menu, click Project -> Add New Item...
  4. In the Templates list of the Add New Item dialog box, click Windows Forms (.NET)
  5. Set the Name to NewCar and press Enter
  6. Design the form as follows: 
     
    Bethesda Car Rental - New Car
    Control Text Name Other Properties
    Label Text #    
    TextBox   txtTagNumber  
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtYear  
    Label Category:    
    ComboBox   cboCategory DropDownStyle: DropDownList
    Items: Economy
    Compact
    Standard
    Full Size
    Mini Van
    SUV
    Truck
    Van
    CheckBox Cassete Player chkK7Player CheckAlign: MiddleRight
    CheckBox DVD Player chkDVDPlayer CheckAlign: MiddleRight
    CheckBox CD Player chkCDPlayer CheckAlign: MiddleRight
    CheckBox Available chkAvailable CheckAlign: MiddleRight
    PictureBox   pctCar SizeMode: CenterImage
    Label Select Car Picture:    
    ComboBox none.gif cboPictures  
    Button Add Car btnAddCar  
    Button Close btnClose DialogResult: OK
    Form     FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  7. To add a new class to the project, on the main menu, click Project -> Add Class...
  8. In the Templates list, click Generic C++ Class and click Open
  9. Set the Class Name to CCar and click Finish
  10. Access the Car.h file and change it as follows:
     
    #pragma once
    
    using namespace System;
    
    [Serializable]
    __sealed public __gc class CCar
    {
    public:
    	String *TagNumber;
    	String *Make;
    	String *Model;
    	int     Year;
    	String *Category;
    	int    HasK7Player;
    	int    HasCDPlayer;
    	int    HasDVDPlayer;
    	String *PictureName;
    	int    IsAvailable;
    
    public:
    	CCar(void);
    	CCar(String *tag, String *mk, String *mdl, 
    		 int Year, String *cat, int k7, int cd,
    		 int dvd, String *pct, int avl);
    	~CCar(void);
    };
  11. Change the Car.cpp file as follows:
     
    #include "StdAfx.h"
    #include ".\car.h"
    #using <mscorlib.dll>
    
    CCar::CCar(void)
    {
    	TagNumber    = S"000-000";
    	Make         = S"Make";
    	Model        = S"Model";
    	Year         = 1960;
    	Category     = S"Small";
    	HasK7Player  = 0;
    	HasCDPlayer  = 0;
    	HasDVDPlayer = 0;
    	PictureName  = "";
    	IsAvailable  = 0;
    }
    CCar::CCar(String *tag, String *mk, String *mdl, 
    		 int yr, String *cat, int k7, int cd,
    		 int dvd, String *pct, int avl)
    {
    	TagNumber    = tag;
    	Make         = mk;
    	Model        = mdl;
    	Year         = yr;
    	Category     = cat;
    	HasK7Player  = k7;
    	HasCDPlayer  = cd;
    	HasDVDPlayer = dvd;
    	PictureName  = pct;
    	IsAvailable  = avl;
    }
    
    CCar::~CCar(void)
    {
    }
  12. Display the NewCar form. Right-click it and click View Code
  13. In the top section of the file, type the following:
     
    #pragma once
    
    #using <System.Runtime.Serialization.Formatters.Soap.dll>
    
    #include "Car.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
    {
  14. Declare an ArrayList variable and name it lstCars:
     
    private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    		ArrayList *lstCars;
    
  15. Return to the NewCar form. Double-click an unoccupied area of its body to generate its Load event and implement it as follows:
     
    System::Void NewCar_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	 lstCars = new ArrayList;
    	 String *strFilename = S"Cars.bcr";
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    			 
    	 if( File::Exists(strFilename) )
    	 {
    FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    		 lstCars = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    
    		 bcrStream->Close();
    	 }
    	 else
    	 {
      FileStream *bcrStream = new FileStream(strFilename, FileMode::OpenOrCreate, FileAccess::Write, FileShare::Write);
    			 bcrSoap->Serialize(bcrStream, lstCars);
    			 bcrStream->Close();
    	 }
    
    	 // Locate the director that contains the current application
    	 DirectoryInfo* di = new DirectoryInfo(S".\\");
    
        	// Get a reference to each file in that directory
        	FileInfo* fiArr[] = di->GetFiles();
    
    	// Display the names of the graphics files
    	for(int i = 0; i < fiArr->Count; i++)
    	{
    		if( fiArr[i]->Extension->Equals(S".gif") ||
    			fiArr[i]->Extension->Equals(S".jpeg") ||
    			fiArr[i]->Extension->Equals(S".jpg") ||
    			fiArr[i]->Extension->Equals(S".bmp") ||
    			fiArr[i]->Extension->Equals(S".png") )
    			cboPictures->Items->Add(fiArr[i]->Name);
        }
    
    	cboPictures->Text = S"none.gif";
    }
  16. Return to the NewCar form and double-click the combo box on then right side of Select Car Picture
  17. Implement its SelectedIndexChanged event as follows:
     
    System::Void cboPictures_SelectedIndexChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 this->pctCar->Image = Image::FromFile(cboPictures->Text);
    }
  18. Return to the NewCar form and double-click the Add Car button to generate its Click event
  19. Implement it as follows:
     
    System::Void btnAddCar_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    	 CCar *car = new CCar;
    
    	 car->TagNumber = this->txtTagNumber->Text;
    	 car->Make      = this->txtMake->Text;
    	 car->Model     = this->txtModel->Text;
    	 car->Year      = this->txtYear->Text->ToInt32(0);
    	 car->Category  = this->cboCategory->Text;
    	 if( this->chkK7Player->Checked == true )
    		 car->HasK7Player  = 1;
    	 else
    		 car->HasK7Player  = 0;
    	 if( this->chkCDPlayer->Checked == true )
    		 car->HasCDPlayer  = 1;
    	 else
    		 car->HasCDPlayer  = 0;
    	 if( this->chkDVDPlayer->Checked == true )
    		 car->HasDVDPlayer = 1;
    	 else
    		 car->HasDVDPlayer = 0;
    	 car->PictureName  = cboPictures->Text;
    	 if( this->chkAvailable->Checked == true )
    		 car->IsAvailable  = 1;
    	 else
    		 car->IsAvailable  = 0;
    
    	 lstCars->Add(car);
    
    	 String *strFilename = S"Cars.bcr";
    	 SoapFormatter *bcrSoap = new SoapFormatter();
      FileStream *bcrStream = new FileStream(strFilename, FileMode::OpenOrCreate, FileAccess::Write, FileShare::Write);
    			 bcrSoap->Serialize(bcrStream, lstCars);
    			 bcrStream->Close();
    
    			 this->txtTagNumber->Text = S"";
    			 this->txtMake->Text      = S"";
    			 this->txtModel->Text     = S"";
    			 this->txtYear->Text      = S"1960";
    			 this->cboCategory->SelectedIndex = 0;
    			 this->chkK7Player->Checked = false;
    			 this->chkCDPlayer->Checked  = false;
    			 this->chkDVDPlayer->Checked = false;
    			 cboPictures->Text = S"none.gif";			 
    			 this->chkAvailable->Checked = false;
    			 this->pctCar->Image = Image::FromFile(S"none.gif");
    			 this->txtTagNumber->Focus();
    }
  20. Display the first form, Form1.h [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnNewCar
    Text: New Car
  21. Double-click the New Car button to generate its Click event
  22. In the top section of the file and under the #pragma once line, type
     
    #include "NewCar.h"
  23. Implement the event as follows:
     
    System::Void btnNewCar_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 NewCar *dlgCar = new NewCar;	 
    	 dlgCar->ShowDialog();
    }
  24. Execute the application
  25. Create a few cars as follows:
     
  26. Close the forms and return to your programming environment
  27. To add a new form to the application, on the main menu, click Project -> Add New Item...
  28. In the Templates list of the Add New Item dialog box, click Windows Forms (.NET)
  29. Set the Name to Cars and press Enter
  30. Design the form as follows: 
     
    Control Text Name Other Properties
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtYear  
    Label Category:    
    TextBox   txtCategory  
    CheckBox Cassete Player chkK7Player CheckAlign: MiddleRight
    CheckBox DVD Player chkDVDPlayer CheckAlign: MiddleRight
    CheckBox CD Player chkCDPlayer CheckAlign: MiddleRight
    CheckBox Available chkAvailable CheckAlign: MiddleRight
    Label Tag #:    
    TextBox   txtTagNumber  
    PictureBox   pctCar  
    Button First btnFirst  
    Button Previous btnPrevious  
    Button Next btnNext  
    Button Last btnLast  
    Button Close btnClose  
    Form     MaximizeBox: False
    StartPosition: CenterScreen
  31. Right-click the form and click View Code
  32. In the top section of the file, type the following:
     
    #pragma once
    
    #include "Car.h"
    
    #using <System.Runtime.Serialization.Formatters.Soap.dll>
    
    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;
  33. In the class, declare a pointer to ArrayList and name it lstCars:
     
    private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container* components;
    		ArrayList *lstCars;
    		int CurrentPosition;
  34. Return to the Cars form and double- click an empty area of its body
  35. Implement the event as follows:
     
    System::Void Cars_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	 lstCars = new ArrayList();
    	 CurrentPosition = 0;
    	 
    	 String *strFilename = S"Cars.bcr";
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    			 
    	 if( File::Exists(strFilename) )
    	 {
    		FileStream *bcrStream = new FileStream(strFilename, 
    						FileMode::Open,
    						FileAccess::Read, 
    						FileShare::Read);
    		 lstCars = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    
    		 bcrStream->Close();
    		 this->btnFirst_Click(sender, e);
    	 }
    }
    
    void ShowCarInformation(CCar *car)
    {
    	 this->txtTagNumber->Text = car->TagNumber;
    	 this->txtMake->Text = car->Make;
    	 this->txtModel->Text = car->Model;
    	 this->txtYear->Text  = car->Year.ToString();
    	 this->txtCategory->Text  = car->Category;
    
    	 if( car->HasK7Player == 1 )
    		 this->chkK7Player->Checked = true;
    	 else
    		 this->chkK7Player->Checked = false;
    
    	 if( car->HasCDPlayer == 1 )
    		 this->chkCDPlayer->Checked = true;
    	 else
    		 this->chkCDPlayer->Checked = false;
    
    	 if( car->HasDVDPlayer == 1 )
    		 this->chkDVDPlayer->Checked = true;
    	 else 
    		 this->chkDVDPlayer->Checked = false;
    
    	 String *strPictureName = car->PictureName;
    
    	 try {
    		 this->pctCar->Image = Image::FromFile(car->PictureName);
    	 }
    	 catch(OutOfMemoryException *)
    	 {
    		 this->pctCar->Image = Image::FromFile(S"none.gif");
    	 }
    
    	 if( car->IsAvailable == 1 )
    		 this->chkAvailable->Checked = true;
    	 else
    		 this->chkAvailable->Checked = false;
    }
  36. Return to the Cars form and double-click the First button
  37. Implement its Click event as follows:
     
    System::Void btnFirst_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( lstCars->Count == 0 )
    		 return;
    			 
    	 CurrentPosition = 0;
    	 CCar *car = new CCar();
    
    	 car = dynamic_cast<CCar *>(this->lstCars->Item[CurrentPosition]);
    
    	ShowCarInformation(car);
    }
  38. Return to the Cars form
  39. Double-click the Previous button and implement its Click event as follows:
     
    private: System::Void btnPrevious_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( lstCars->Count == 0 )
    		 return;
    			 
    	 if( CurrentPosition == 0 )
    		 return;
    	 
    	 CurrentPosition--;
    	 CCar *car = new CCar();
    
    	 car = dynamic_cast<CCar *>(lstCars->Item[CurrentPosition]);
    
    	ShowCarInformation(car);
    }
  40. Return to the Cars form. Double-click the Next button and implement its Click event as follows:
     
    private: System::Void btnNext_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( lstCars->Count == 0 )
    		 return;
    
    	 if( CurrentPosition == lstCars->Count - 1 )
    		 return;
    	 else
    	 {
    	 CurrentPosition++;
    	 
    	 CCar *car = new CCar();
    
    	 car = dynamic_cast<CCar *>(lstCars->Item[CurrentPosition]);
    
    	ShowCarInformation(car);
    	 }
    }
  41. Return to the Cars form. Double-click the Last button and implement its Click event as follows:
     
    private: System::Void btnLast_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( lstCars->Count == 0 )
    		 return;
    			 
    	 CurrentPosition = lstCars->Count - 1;
    	 CCar *car = new CCar();
    
    	 car = dynamic_cast<CCar *>(this->lstCars->Item[CurrentPosition]);
    
    	ShowCarInformation(car);
    }
  42. Return to the Cars form. Double-click the Close button and implement its Click event as follows:
     
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  43. Display the first form, Form1.h [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnCarsReview
    Text: Cars Review
  44. Double-click the New Car button to generate its Click event
  45. In the top section of the file and under the #pragma once line, type
     
    #include "Cars.h"
  46. Implement the event as follows:
     
    System::Void btnCarsReview_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Cars *frmCars = new Cars;
    	 frmCars->Show();
    }
  47. Execute the application and review the list of cars using the Cars form
     
  48. Close the forms and return to your programming environment
 

Previous Copyright © 2005-2010 FunctionX, Inc. Next