Home

File-Based Applications:
Bethesda Car Rental

 

Introduction

Imagine you get a contract or an assignment to create an application used to process business transactions for a car rental company. In this case, we will call it Bethesda Car Rental. If you decide to use then .NET Framework to develop the application, you have various options. You can use file processing for a file-based database. You can proceed with XML to save company files in a format that can be accessed by different types of applications, including the World Wide Web. Or you can create a formal database using Microsoft SQL Server, Borland/Corel Paradox, or MS Access, etc. In this example, we will create our application using regular files.

This application assumes that you have some knowledge or a basic understanding of file processing and serialization as it is featured in the .NET Framework but we will be as clear as possible with this exercise and its different parts.

 

Practical Learning Practical Learning: Processing Cars 

  • Start Microsoft Visual C++ .NET or Visual Studio .NET and create a new Windows Forms Application named BCR2

Employees

An employee will be anybody who works for this company, whether the person is part of the management or is one of those who process transactions. In a formal company, various pieces of information are provided for each employee. In our example, we will get only the strict minimum.

 

Practical Learning Practical Learning: Creating Employees

  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 Employees 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
    colFirstName First Name 80
    colLastName Last Name 80
    colTitle Title 200
    colGender Gender 80
  6. Design the form as follows: 
     
     
    Control Text Name Other Properties
    ListView   lvwEmployees Anchor: Top, Bottom, Left, Right
    FullRowSelect: True
    GridLines: True
    View: Details
    Button New Employee btnNewEmployee  
    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 NewEmployee and press Enter
  10. Design the form as follows: 
     
     
    Control Text Name Properties
    Label First Name:    
    TextBox   txtFirstName Modifiers: public
    Label Last Name:    
    TextBox   txtLastName Modifiers: public
    Label Title:    
    TextBox   txtTitle Modifiers: public
    Label Gender:    
    ComboBox   cboGenders Modifiers: public
    Items: Female
    Male
    Unknown
    Button Add btnAdd DialogResult: OK
    Button Cancel btnCancel DialogResult: Cancel
    Form     AcceptButton: btnAdd
    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 Employee and click Finish
  14. In the Solution Explorer, double-click Employee.h
  15. Change the Employee.h file as follows:
     
    #pragma once
    
    using namespace System;
    
    [Serializable]
    public __gc class Employee
    {
    public:
    	String *FirstName;
    	String *LastName;
    	String *Title;
    	String *Gender;
    
    public:
    	Employee(void);
    	Employee(String *fname, String *lname,
    			 String *title, String *sex);
    	~Employee(void);
    };
  16. Change the Employee.cpp file as follows:
     
    #include "StdAfx.h"
    #include ".\employee.h"
    #using <mscorlib.dll>
    
    Employee::Employee(void)
    {
    	FirstName = S"Unknown";
    	LastName  = S"Unknown";
    	Title     = S"N/A";
    	Gender    = S"Unknown";
    }
    
    Employee::Employee(String *fname, String *lname,
    		   String *title, String *sex)
    {
    	FirstName = fname;
    	LastName  = lname;
    	Title     = title;
    	Gender    = sex;
    }
    
    Employee::~Employee(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 ShowEmployees and its Access to private
  21. Click Finish 
  22. Return to the Employees form and double-click an empty area of its body to generate its Load event
  23. Return to the Employees form and double-click its New Employee button
  24. Return to the Employees form and double-click its Close button
  25. Change the file as follows:
     
    #pragma once
    
    #using <System.Runtime.Serialization.Formatters.Soap.dll>
    
    #include "NewEmployee.h"
    #include "Employee.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 Employees : public System::Windows::Forms::Form
    	{
    	public: 
    		. . .
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container* components;
    		ArrayList *lstEmployees;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			. . . 
    		}		
    	
    void ShowEmployees(void)
    {
    	 SoapFormatter *bcrSoap = new SoapFormatter();
    	 String *strFilename = S"Employees.bcr";
    			 
    	 if( File::Exists(strFilename) )
    	 {
    FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
    		 ArrayList *lstEmpl = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
    
    		 bcrStream->Close();
    
    		 Employee* empl;
    
    		 lvwEmployees->Items->Clear();
    
    		 for(int i = 0; i < lstEmpl->Count; i++)
    		 {
    			 empl = dynamic_cast<Employee *>(lstEmpl->Item[i]);
    			 ListViewItem *lviEmployee = new ListViewItem(empl->FirstName);
    			 lviEmployee->Font = new Drawing::Font("Georgia", 8, FontStyle::Bold);
    		 
    			if( i % 2 == 0 )
    		 	{
    			 	lviEmployee->BackColor = Color::Blue;
    			 	lviEmployee->ForeColor = Color::White;
    		 	}
    		 	else
    		 	{
    			 	lviEmployee->BackColor = Color::LightBlue;
    			 	lviEmployee->ForeColor = Color::Blue;
    		 	}
    
    			 lviEmployee->SubItems->Add(empl->LastName);
    			 lviEmployee->SubItems->Add(empl->Title);
    			 lviEmployee->SubItems->Add(empl->Gender);
    			 lvwEmployees->Items->Add(lviEmployee);
    		 }
    	}
    }
    
    private: System::Void Employees_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	 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();
    	 }
    
    	 ShowEmployees();
    }
    
    private: System::Void btnNewEmployee_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 NewEmployee *dlgEmpl = new NewEmployee;
    				 
    	 if( dlgEmpl->ShowDialog() == DialogResult::OK )
    	 {
    		 if( dlgEmpl->txtLastName->Text->Equals(S"") )
    		 {
    		 MessageBox::Show(S"You must provide at least a last name to create a new employee");
    			 return;
    		 }
    		 else
    		 {
    			 String *strFilename = S"Employees.bcr";
    			 Employee *empl = new Employee;
    
    			 empl->FirstName = dlgEmpl->txtFirstName->Text;
    			 empl->LastName  = dlgEmpl->txtLastName->Text;
    			 empl->Title     = dlgEmpl->txtTitle->Text;
    			 empl->Gender    = dlgEmpl->cboGenders->Text;
    
    			 lstEmployees->Add(empl);
    			 
     FileStream *bcrStream = new FileStream(strFilename, FileMode::OpenOrCreate, FileAccess::Write, FileShare::Write);
    			 SoapFormatter *bcrSoap = new SoapFormatter();
    			 bcrSoap->Serialize(bcrStream, lstEmployees);
    			 bcrStream->Close();
    						 
    			 ShowEmployees();
    		 }
    	 }
    }
    
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
    
    };
    }
  26. Display the first form, Form1.h [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnEmployees
    Text: Employees
  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 "Employees.h"
  29. Implement the event as follows:
     
    private: System::Void btnEmployees_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Employees *frmEmpl = new Employees;
    
    	 frmEmpl->Show();
    }
  30. Execute the application
  31. Create a few employees as follows:
     
    First Name Last Name Title Gender
    Patricia Katts Store Manager  
    Henry Larson Sales Representative Male
    Gertrude Palau Sales Representative Female
    Helene Sandt Intern Female
    Melanie Karron Sales Representative Unknown
    Ernest Chisen Sales Manager Male
    Melissa Roberts Administrative Assistant Female
     
  32. Close the forms and return to your programming environment
 

Home Copyright © 2005-2010 FunctionX, Inc. Next