|
Collection-Based Application: Altair Realtors |
|
|
A real estate company or agency is a business that
presents or sells houses (properties) to prospective customers. This is an
application that addresses some of the issues with a real estate company.
|
Practical Learning: Introducing the Collection Class
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Forms Application
- Change the Name to AltairRealtors1
- Click OK
- To create a new class, on the main menu, click Project -> Add
Class...
- In the middle list, click C++ Class and click Add
- Set the Name to CRealEstateProperty
- Click Finish
- Change the header file as follows:
#pragma once
using namespace System;
[Serializable]
public ref class CRealEstateProperty
{
public:
CRealEstateProperty(void);
private:
String ^ nbr;
String ^ type;
String ^ adrs;
String ^ ct;
String ^ stt;
String ^ code;
short levels;
int year;
short beds;
float baths;
String ^ cond;
String ^ status;
double price;
String ^ file;
public:
property String ^ PropertyNumber
{
String ^ get() { return nbr; }
void set(String ^ value) { nbr = value; }
}
property String ^ PropertyType
{
String ^ get() { return type; }
void set(String ^ value) { type = value; }
}
property String ^ Address
{
String ^ get() { return adrs; }
void set(String ^ value) { adrs = value; }
}
property String ^ City
{
String ^ get() { return ct; }
void set(String ^ value) { ct = value; }
}
property String ^ State
{
String ^ get() { return stt; }
void set(String ^ value) { stt = value; }
}
property String ^ ZIPCode
{
String ^ get() { return code; }
void set(String ^ value) { code = value; }
}
property short Stories
{
short get() { return levels; }
void set(short value) { levels = value; }
}
property int YearBuilt
{
int get() { return year; }
void set(int value) { year = value; }
}
property short Bedrooms
{
short get() { return beds; }
void set(short value) { beds = value; }
}
property float Bathrooms
{
float get() { return baths; }
void set(float value) { baths = value; }
}
property String ^ Condition
{
String ^ get() { return cond; }
void set(String ^ value) { cond = value; }
}
property String ^ SaleStatus
{
String ^ get() { return status; }
void set(String ^ value) { status = value; }
}
property double MarketValue
{
double get() { return price; }
void set(double value) { price = value; }
}
property String ^ PictureFile
{
String ^ get() { return file; }
void set(String ^ value) { file = value; }
}
virtual bool Equals(Object ^ obj) override;
virtual int CRealEstateProperty::GetHashCode() override;
};
- Access the source file and change it as follows:
#include "StdAfx.h"
#include "RealEstateProperty.h"
CRealEstateProperty::CRealEstateProperty(void)
{
}
// To determine that two properties are the same,
// we will test only the property number.
// We assume that if two properties have the same number,
// then it is the same property
bool CRealEstateProperty::Equals(Object ^ obj)
{
CRealEstateProperty ^ rep = reinterpret_cast<CRealEstateProperty ^>(obj);
if (rep->PropertyNumber == PropertyNumber)
return true;
else
return false;
}
// To avoid a compiler warning
int CRealEstateProperty::GetHashCode()
{
return GetHashCode();
}
- In the Solution Explorer, right-click Form1.h and click Rename
- Type AltairRealtors.h and press Enter twice to
display the form
- From the Toolbox, add a list view to the form
- Right-click that list box on the click Edit Columns...
- Create the columns as follows:
(Name) |
Text |
TextAlign |
Width |
colPropertyNumber |
Property # |
|
65 |
colCity |
City |
|
75 |
colStories |
Stories |
Right |
45 |
colYearBuilt |
Year |
Right |
40 |
colBedrooms |
Beds |
Right |
38 |
colBathrooms |
Baths |
Right |
40 |
colCondition |
Condition |
|
80 |
colSaleStatus |
Status |
|
70 |
colMarketValue |
Value |
Right |
75 |
- Click OK
- Design the form as follows:
|
Control |
(Name) |
Anchor |
BorderStyle |
SizeMode |
Text |
ListView |
|
lvwProperties |
Top, Bottom, Left, Right |
|
|
|
Button |
|
btnNewProperty |
Bottom, Left |
|
|
New Real Estate Property... |
PictureBox |
|
pbxPicture |
Bottom, Right |
FixedSingle |
Zoom |
|
Button |
|
btnClose |
Bottom, Right |
|
|
Close |
|
Form |
Text: |
Altair Realtors - Properties Listing |
StartPosition: |
CenterScreen |
- Right-click the form and click Edit Groups...
- Create the groups as follows:
Header |
Name |
Condominium |
lvgCondominium |
Townhouse |
lvgTownhouse |
Single Family |
lvgSingleFamily |
- Click OK
- To create a new form, on the main menu, click Projects -> Add New
Item...
- In the middle list, make sure Windows Form is selected.
Set the
Name to PropertyEditor
- Click Add
- In the Dialogs section of the Toolbox, click OpenFileDialog
- Click the form
- In the Properties window, change its characteristics as follows:
(Name): dlgPicture DefaultExt: jpg Filter: JPEG
Files (*.jpg,*.jpeg)|*.jpg Title: Select House Picture
- Design the form as follows:
|
Control |
(Name) |
DropDownStyle |
Text |
Items |
Modifiers |
Other Properties |
Label |
|
|
|
Property Type: |
|
|
|
ComboBox |
|
cbxPropertyTypes |
DropDownList |
|
Condominium Townhouse Single Family
Unknown |
Public |
|
Label |
|
|
|
Property #: |
|
|
|
TextBox |
|
txtPropertyNumber |
|
|
|
Public |
|
Label |
|
|
|
Address: |
|
|
|
TextBox |
|
txtAddress |
|
|
|
Public |
|
Label |
|
|
|
City: |
|
|
|
TextBox |
|
txtCity |
|
|
|
Public |
|
Label |
|
|
|
State: |
|
|
|
ComboBox |
|
cbxStates |
DropDownList |
|
AL, AK, AZ, AR, CA, CO, CT, DE, DC, FL, GA, HI,
ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS,
MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR,
PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WY |
Public |
|
Label |
|
|
|
ZIP Code: |
|
|
|
TextBox |
|
txtZIPCode |
|
|
|
Public |
|
Label |
|
|
|
Stories: |
|
|
|
TextBox |
|
txtStories |
|
|
|
Public |
|
Label |
|
|
|
Year Built: |
|
|
|
TextBox |
|
txtYearBuilt |
|
|
|
Public |
|
Label |
|
|
|
Condition: |
|
|
|
ComboBox |
|
cbxConditions |
DropDownList |
|
Excellent Good Shape Needs Fixing |
Public |
|
Label |
|
|
|
Bedrooms: |
|
|
|
TextBox |
|
txtBedrooms |
|
0 |
|
Public |
|
Label |
|
|
|
Bathrooms: |
|
|
|
TextBox |
|
txtBathrooms |
|
0.00 |
|
Public |
|
Label |
|
|
|
Market Value: |
|
|
|
TextBox |
|
txtMarketValue |
|
0.00 |
|
Public |
|
Label |
|
|
|
Sale Status: |
|
|
|
ComboBox |
|
cbxSaleStatus |
DropDownList |
|
Unspecified Available Sold |
Public |
|
Button |
|
btnPicture |
|
Picture... |
|
|
|
PictureBox |
|
pbxProperty |
|
|
|
|
BorderStyle: FixedSingle SizeMode: Zoom |
Button |
|
btnOK |
|
OK |
|
|
DialogResult: OK |
Button |
|
btnCancel |
|
Cancel |
|
|
DialogResult: Cancel |
|
Form |
FormBorderStyle: |
FixedDialog |
Text: |
Altair Realtors - Property Editor |
StartPosition: |
CenterScreen |
AcceptButton: |
btnOK |
CancelButton: |
btnCancel |
MaximizeBox: |
False |
MinimizeBox: |
False |
ShowInTaskBar: |
False |
- Double-click an unoccupied area of the form
- Return to the form
- Double-click the Picture button
- Change the file as follows:
public:
bool pictureChanged;
String ^ pictureFile;
private:
System::Void PropertyEditor_Load(System::Object^ sender, System::EventArgs^ e)
{
pictureChanged = false;
pictureFile = L"C:\\Altair Realtors1\000-000.jpg";
}
System::Void btnPicture_Click(System::Object^ sender, System::EventArgs^ e)
{
if (dlgPicture->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
pbxProperty->Image = Image::FromFile(dlgPicture->FileName);
pictureFile = dlgPicture->FileName;
pictureChanged = true;
}
}
};
}
Practical Learning: Starting a Linked List Class
|
|
- In the Solution Explorer, double-click AltairRealtors.h
- Double-click an unoccupied area of the form
- Change the file as follows:
#pragma once
#include "RealEstateProperty.h"
#include "PropertyEditor.h"
namespace AltairRealtors1 {
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::Collections::Generic;
using namespace System::Runtime::Serialization::Formatters::Binary;
. . . No Change
#pragma endregion
private:
LinkedList<CRealEstateProperty ^> ^properties;
void ShowProperties()
{
// Get a reference to the file that holds the records of properties
String ^ Filename = "C:\\Altair Realtors1\\properties.atr";
// Make sure the file exists
if( File::Exists(Filename) == true )
{
// if so, create a file stream
FileStream ^ stmProperties = gcnew FileStream(Filename,
FileMode::Open,
FileAccess::Read);
// Create a binary formatter
BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter;
// If some properties were created already,
// get them and store them in the collection
properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperty->Deserialize(stmProperties));
// First, empty the list view
lvwProperties->Items->Clear();
ListViewItem ^lviProperty = nullptr;
// Visit each property in the collection and add it to the list view
for each( CRealEstateProperty ^ house in properties )
{
if( house->PropertyType->Equals(L"Condominium") )
lviProperty = gcnew ListViewItem(house->PropertyNumber, lvwProperties->Groups[0]);
else if (house->PropertyType->Equals(L"Townhouse"))
lviProperty = gcnew ListViewItem(house->PropertyNumber, lvwProperties->Groups[1]);
else // if (house->PropertyType->Equals(L"Single Family"))
lviProperty = gcnew ListViewItem(house->PropertyNumber, lvwProperties->Groups[2]);
lviProperty->SubItems->Add(house->City);
lviProperty->SubItems->Add(house->Stories.ToString());
lviProperty->SubItems->Add(house->YearBuilt.ToString());
lviProperty->SubItems->Add(house->Bedrooms.ToString());
lviProperty->SubItems->Add(house->Bathrooms.ToString(L"F"));
lviProperty->SubItems->Add(house->Condition);
lviProperty->SubItems->Add(house->SaleStatus);
lviProperty->SubItems->Add(house->MarketValue.ToString());
lvwProperties->Items->Add(lviProperty);
}
// Close the file stream
stmProperties->Close();
}
}
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
properties = gcnew LinkedList<CRealEstateProperty ^>;
ShowProperties();
}
};
}
- Return to the Altair Realtors - Properties Listing form
Practical Learning: Adding the First Node
|
|
- On the form, double-click the New Real Estale Property button
- Implement its event as follows:
System::Void btnNewProperty_Click(System::Object^ sender, System::EventArgs^ e)
{
PropertyEditor ^ editor = gcnew PropertyEditor;
Random ^ rndNumber = gcnew Random(DateTime::Now.Millisecond);
int number1 = rndNumber->Next(100, 999);
int number2 = rndNumber->Next(100, 999);
String ^ propNumber = number1 + L"-" + number2;
editor->txtPropertyNumber->Text = propNumber;
// Check that the directory that contains the list of properties exists.
// If it doesn't exist, create it
DirectoryInfo ^ dirInfo = Directory::CreateDirectory(L"C:\\Altair Realtors1");
// Get a reference to the file that holds the properties
String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
// First check if the file was previously created
if( File::Exists(Filename) == true )
{
// If the list of properties exists already,
// get it and store it in a file stream
FileStream ^ stmProperties = gcnew FileStream(Filename,
FileMode::Open,
FileAccess::Read);
BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
// Store the list of properties in the collection
properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperty->Deserialize(stmProperties));
// Close the file stream
stmProperties->Close();
}
if( editor->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
CRealEstateProperty ^ prop = gcnew CRealEstateProperty;
prop->PropertyNumber = editor->txtPropertyNumber->Text;
prop->PropertyType = editor->cbxPropertyTypes->Text;
prop->Address = editor->txtAddress->Text;
prop->City = editor->txtCity->Text;
prop->State = editor->cbxStates->Text;
prop->ZIPCode = editor->txtZIPCode->Text;
prop->Stories = short::Parse(editor->txtStories->Text);
prop->YearBuilt = int::Parse(editor->txtYearBuilt->Text);
prop->Bedrooms = short::Parse(editor->txtBedrooms->Text);
prop->Bathrooms = float::Parse(editor->txtBathrooms->Text);
prop->Condition = editor->cbxConditions->Text;
prop->SaleStatus = editor->cbxSaleStatus->Text;
prop->MarketValue = double::Parse(editor->txtMarketValue->Text);
if( !editor->pictureFile->Equals(L"") )
{
FileInfo ^ flePicture = gcnew FileInfo(editor->pictureFile);
flePicture->CopyTo(L"C:\\Altair Realtors1\\" +
editor->txtPropertyNumber->Text +
flePicture->Extension);
prop->PictureFile = L"C:\\Altair Realtors1\\" +
editor->txtPropertyNumber->Text +
flePicture->Extension;
}
else
prop->PictureFile = L"C:\\Altair Realtors1\\000-000.jpg";
// Add the property in the collection
properties->AddFirst(prop);
// Get a reference to the properties file
String ^ strFilename = dirInfo->FullName + "\\properties.atr";
// Create a file stream to hold the list of properties
FileStream ^ stmProperties = gcnew FileStream(strFilename,
FileMode::Create,
FileAccess::Write);
BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
// Serialize the list of properties
bfmProperty->Serialize(stmProperties, properties);
// Close the file stream
stmProperties->Close();
// Show the list of properties
ShowProperties();
}
}
- Return to the form and click the list view
- In the Properties window, click Events and double-click
ItemSelectionChanged
- Implement the event as follows:
System::Void lvwProperties_ItemSelectionChanged(System::Object^ sender,
System::Windows::Forms::ListViewItemSelectionChangedEventArgs^ e)
{
CRealEstateProperty ^ currentProperty = gcnew CRealEstateProperty;
for each (CRealEstateProperty ^ prop in properties)
{
if (prop->PropertyNumber->Equals(e->Item->SubItems[0]->Text))
pbxProperty->Image = Image::FromFile(prop->PictureFile);
}
}
- To execute, press F5
- Click the New Real Estate Property button and
create a property:
- Click OK
- Copy the following picture and paste (or save it) in the Altair
Realtors folder on the C: drive (or the folder that contains the file
properties of this project):
- Create some properties:
- Close the form and return to your programming environment
|
|