Home

Bethesda Car Rental: Customers Records

 

Introduction

The essence of any business is to present or sell products to a customers. These are people or entities that buy what ever is on sale. In our application, we will create simple information about each customer. This information will be stored in a text file and be able to open it when needed. To make our assignment easy, we will use the same approach and steps we used for the employees.

 

Practical Learning Practical Learning: Creating Customers

  1. To add a new form to the application, on the main menu, click Project -> Add New Item...
  2. In the Templates list of the Add New Item dialog box, click Windows Forms (.NET)
  3. Set the Name to Customers and press Enter
  4. From the Toolbox, add a ListView to the form
  5. While the new list view is still selected, in the Properties window, click the ellipsis button of the Columns field and create the columns as follows:
     
    (Name) Text Width
    colDrvLicNbr Driver's Lic. # 100
    colFullName Full Name 100
    colAddress Address 160
    colCity City 100
    colState State 38
    colZIPCode ZIPCode  
    colCountry Country  
  6. Design the form as follows: 
     
     
    Control Text Name Other Properties
    ListView   lvwEmployees View: Details
    GridLines: True
    FullRowSelect: True
    Button New Customer btnNewCustomer  
    Button Close btnClose  
  7. To add another form to the application, on the main menu, click Project -> Add New Item...
  8. In the Templates list of the Add New Item dialog box, click Windows Forms (.NET)
  9. Set the Name to NewCustomer and press Enter
  10. Design the form as follows: 
     
     
    Control Text Name Properties
    Label Driver's Lic. #:    
    TextBox   txtDrvLicNbr Modifiers: public
    Label Full Name:    
    TextBox   txtFullName Modifiers: public
    Label Address:    
    TextBox   txtAddress Modifiers: public
    Label City:    
    TextBox   txtCity Modifiers: public
    Label State:    
    TextBox MD txtState Modifiers: public
    Label ZIP Code:    
    TextBox   txtZIPCode Modifiers: public
    Label Country:    
    TextBox USA txtCountry Modifiers: public
    Button New Customer btnNewCustomer DialogResult: OK
    Button Close btnClose DialogResult: Cancel
    Form     AcceptButton: btnNewCustomer
    CancelButton: btnClose
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  11. To add a new class to the project, on the main menu, click Project -> Add Class...
  12. In the Templates list, click Generic C++ Class and click Open
  13. Set the Class Name to CCustomer and click Finish
  14. In the Solution Explorer, double-click Customer.h
  15. Change the Customer.h file as follows:
     
    #pragma once
    
    using namespace System;
    
    [Serializable]
    __sealed public __gc class CCustomer
    {
    public:
    	String *DrvLicNbr;
    	String *FullName;
    	String *Address;
    	String *City;
    	String *State;
    	String *ZIPCode;
    	String *Country;
    
    public:
    	CCustomer(void);
    	CCustomer(String *DrvLic, String *fName, String *adr,
    		  String *ct, String *ste, String *zip, String *cty);
    	~CCustomer(void);
    };
  16. Change the Customer.cpp file as follows:
     
    #include "StdAfx.h"
    #include ".\customer.h"
    #using <mscorlib.dll>
    
    CCustomer::CCustomer(void)
    {
    	DrvLicNbr = S"A-000-000-000-000";
    	FullName  = S"John Doe";
    	Address   = S"123 Main Street";
    	City      = S"Good City";
    	State     = S"MD";
    	ZIPCode   = S"20900";
    	Country   = S"USA";
    }
    
    CCustomer::CCustomer(String *DrvLic, String *fName, String *adr,
    		     String *ct, String *ste, String *zip, String *cty)
    {
    	DrvLicNbr = DrvLic;
    	FullName  = fName;
    	Address   = adr;
    	City      = ct;
    	State     = ste;
    	ZIPCode   = zip;
    	Country   = cty;
    }
    
    CCustomer::~CCustomer(void)
    {
    }
  17. In the Class View, expand BCR2 and BCR2
  18. Right-click Employees -> Add -> Add Function...
  19. Set the Return Type to void
  20. Set the Function Name to ShowCustomers and its Access to private
  21. Click Finish 
  22. Return to the Customers form and double-click an empty area of its body to generate its Load event
  23. Return to the Customers form and double-click its Close button
  24. Return to the Customers form and double-click its New Customer button
  25. Change the file as follows:
     
    #pragma once
    
    #using <System.Runtime.Serialization.Formatters.Soap.dll>
    
    #include "NewCustomer.h"
    #include "Customer.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
    {
    	. . .
    	
    	public __gc class Customers : public System::Windows::Forms::Form
    	{
    
    	. . .
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container* components;
    		ArrayList *lstCustomers;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			. . . 
    		}		
    	
    void ShowCustomers(void)
    {
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    	 String *strFilename = S"Customers.bcr";
    			 
    	 if( File::Exists(strFilename) )
    	 {
    		FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, 
    							FileAccess::Read, FileShare::Read);
    		 ArrayList *lstCust = 
    			dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    
    		 bcrStream->Close();
    
    		 CCustomer* cust;
    
    		 lvwCustomers->Items->Clear();
    
    		 for(int i = 0; i < lstCust->Count; i++)
    		 {
    			 cust = dynamic_cast<CCustomer *>(lstCust->Item[i]);
    			 ListViewItem *lviCustomer = new ListViewItem(cust->DrvLicNbr);
    			 lviCustomer->Font = new 
    				Drawing::Font("Georgia", 8, FontStyle::Bold);
    		 
    			 if( i % 2 == 0 )
    		 {
    			 lviCustomer->BackColor = Color::Blue;
    			 lviCustomer->ForeColor = Color::White;
    		 }
    		 else
    		 {
    			 lviCustomer->BackColor = Color::LightBlue;
    			 lviCustomer->ForeColor = Color::Blue;
    		 }
    
    			 lviCustomer->SubItems->Add(cust->FullName);
    			 lviCustomer->SubItems->Add(cust->Address);
    			 lviCustomer->SubItems->Add(cust->City);
    			 lviCustomer->SubItems->Add(cust->State);
    			 lviCustomer->SubItems->Add(cust->ZIPCode);
    			 lviCustomer->SubItems->Add(cust->Country);
    			 lvwCustomers->Items->Add(lviCustomer);
    		 }
    	 }
    }
    
    private: System::Void Customers_Load(System::Object *  sender, System::EventArgs *  e)
    		 {
    	 lstCustomers = new ArrayList;
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    	 String *strFilename = S"Customers.bcr";
    			 
    	 if( File::Exists(strFilename) )
    	 {
    		FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, 
    							FileAccess::Read, FileShare::Read);
    		 lstCustomers = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    
    		 bcrStream->Close();
    	 }
    
    	 ShowCustomers();
    		 }
    
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
    
    private: System::Void btnNewCustomer_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 NewCustomer *dlgCust = new NewCustomer;
    				 
    	 if( dlgCust->ShowDialog() == DialogResult::OK )
    	 {
    		 String *strFilename = S"Customers.bcr";
    		 CCustomer *cust = new CCustomer;
    
    		 cust->DrvLicNbr = dlgCust->txtDrvLicNbr->Text;
    		 cust->FullName  = dlgCust->txtFullName->Text;
    		 cust->Address   = dlgCust->txtAddress->Text;
    		 cust->City      = dlgCust->txtCity->Text;
    		 cust->State     = dlgCust->txtState->Text;
    		 cust->ZIPCode   = dlgCust->txtZIPCode->Text;
    		 cust->Country   = dlgCust->txtCountry->Text;
    
    		 lstCustomers->Add(cust);
    			 
    		 FileStream *bcrStream = new FileStream(strFilename, 
    							FileMode::OpenOrCreate, 
    							FileAccess::Write, 
    							FileShare::Write);
    		 SoapFormatter *bcrSoap = new SoapFormatter();
    		 bcrSoap->Serialize(bcrStream, lstCustomers);
    		 bcrStream->Close();
    						 
    		 ShowCustomers();
    	 }
    }
    
    };
    }
  26. Display the first form, Form1.h [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnCustomers
    Text: Customers
  27. Double-click the Employees button to generate its Click event
  28. In the top section of the file and under the #pragma once line, type
     
    #include "Customers.h"
  29. Implement the event as follows:
     
    System::Void btnCustomers_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Customers *frmCust = new Customers;
    
    	 frmCust->Show();
    }
  30. Execute the application
  31. Create a few employees as follows:
     
    Driver's Lic. # Full Name Address City State ZIP Code
    M-505-862-575 Lynda Melman 4277 Jamison Ave Silver Spring   20904
    379-82-7397 John Villard 108 Hacken Rd NE Washington DC 20012
    J-938-928-274 Chris Youno 8522 Aulage Street Rockville   20852
    K-497-220-614 Pamela Ulmreck 12075 Famina Rd Hyattsville MD 20707
    922-71-8395 Helene Kapsco 806 Hyena Drive Alexandria VA 22231
    C-374-830-422 Hermine Crasson 6255 Old Georgia Ave Silver Spring   20910
    836-55-2279 Alan Pastore 4228 16th Street NW Washington DC 20004
    B-397-597-487 Phillis Buster 724 Cranston Circle College Park   20747
    K-115-802-957 Elmus Krazucki 808 Rasters Ave Chevy Chase MD 20852
    294-90-7744 Helena Weniack 10448 Great Pollard Hwy Arlington VA 22232
     
  32. Close the forms and return to your programming environment
 

Previous Copyright © 2005-2016, FunctionX Next