//form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CPRG253.RealEstate.Domain;
namespace CPRG253.RealEstate.App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PopulateTree();
}
private void PopulateTree()
{
TreeNode citiesNode = new TreeNode("Cities");
TreeNode cityNode = null;
TreeNode repNode = null;
TreeNode propertyNode = null;
uxTree.Nodes.Add(citiesNode);
List<City> cities = FileAccessObject.GetCities();
citiesNode.Tag = cities;
foreach(City city in cities)
{
cityNode = new TreeNode(city.Name);
cityNode.Tag = city;
citiesNode.Nodes.Add(cityNode);
foreach(SalesRep rep in city.SalesReps)
{
repNode = new TreeNode(rep.FullName);
repNode.Tag = rep;
cityNode.Nodes.Add(repNode);
foreach(Property property in rep.Properties)
{
propertyNode = new TreeNode(property.Address);
propertyNode.Tag = property;
repNode.Nodes.Add(propertyNode);
}
}
}
}
private void uxTree_AfterSelect(object sender, TreeViewEventArgs e)
{
switch (e.Node.Level)
{
case 0:
FormatCities(e.Node.Tag);
break;
case 1:
FormatSalesReps(e.Node.Tag);
break;
case 2:
FormatProperties(e.Node.Tag);
break;
case 3:
FormatProperty(e.Node.Tag);
break;
}
}
private void FormatCities(object taggedObject)
{
//cast the taggedObject to List<City>
List<City> cities = (List<City>)taggedObject;
//the list view will have 2 columns
uxDetails.Columns.Clear();
uxDetails.Columns.Add("City", 200);
uxDetails.Columns.Add("Sales Reps Count", 200);
//add the items to the listview
uxDetails.Items.Clear();
ListViewItem item;
foreach(City city in cities)
{
item = new ListViewItem();
item.Text = city.Name;
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = city.SalesReps.Count.ToString() });
uxDetails.Items.Add(item);
}
}
private void FormatSalesReps(object taggedObject)
{
City city = (City)taggedObject;
//the list view will have 2 columns
uxDetails.Columns.Clear();
uxDetails.Columns.Add("Sales Representative", 200);
uxDetails.Columns.Add("Property Count", 200);
//add the items to the listview
uxDetails.Items.Clear();
ListViewItem item;
foreach(SalesRep rep in city.SalesReps)
{
item = new ListViewItem();
item.Text = rep.FullName;
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = rep.Properties.Count.ToString() });
uxDetails.Items.Add(item);
}
}
private void FormatProperties(object taggedObject)
{
SalesRep rep = (SalesRep)taggedObject;
//the list view will have 3 columns
uxDetails.Columns.Clear();
uxDetails.Columns.Add("Address", 200);
uxDetails.Columns.Add("Property Type", 150);
uxDetails.Columns.Add("Owner", 150);
//add the items to the listview
uxDetails.Items.Clear();
ListViewItem item;
foreach (Property property in rep.Properties)
{
item = new ListViewItem();
item.Text = property.Address;
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = property.PropertyType });
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = property.OwnerName });
uxDetails.Items.Add(item);
}
}
private void FormatProperty(object taggedObject)
{
Property property = (Property)taggedObject;
//the list view will have 3 columns
uxDetails.Columns.Clear();
uxDetails.Columns.Add("Address", 200);
uxDetails.Columns.Add("Property Type", 150);
uxDetails.Columns.Add("Owner", 150);
//add the items to the listview
uxDetails.Items.Clear();
ListViewItem item;
item = new ListViewItem();
item.Text = property.Address;
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = property.PropertyType });
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = property.OwnerName });
uxDetails.Items.Add(item);
}
private void expandAllToolStripMenuItem_Click(object sender, EventArgs e)
{
uxTree.ExpandAll();
}
private void collapseAllToolStripMenuItem_Click(object sender, EventArgs e)
{
uxTree.CollapseAll();
}
}
}
---------------------------------------------------------------------------------------------
//FileAccessObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CPRG253.RealEstate.Domain
{
//This object handles the file access
public class FileAccessObject
{
//set class variables to path to csv files
const string CityFileName = "cities.csv";
const string SalesRepFileName = "salesreps.csv";
const string propertyFileName = "properties.csv";
//only public method returns collection of city objects (bueach city contains nested collections)
public static List<City> GetCities()
{
var cities = new List<City>();
City city = null;
//get the cities from the csv file as a string array
var cityArray = File.ReadAllLines(CityFileName);
//for each city csv record in file...
foreach (string cityData in cityArray)
{
//...split the csv record by the comma into a string array
var items = cityData.Split(',');
//instantiate the city object with items from string array
city = new City { Id = int.Parse(items[0]), Name = items[1] };
//city = new City (items[0], items[1]);
//pass the city object to another method to populate the sales rep for that city
GetRepsByCity(city);
//then add the city object to the collections that will be returned after this loop
cities.Add(city);
}
return cities;
}
//This private method is responsible for populating the city object with the sales reps for that city
private static void GetRepsByCity(City city)
{
var reps = new List<SalesRep>();
SalesRep rep = null;
//get salesreps from the csv file as a string array(one in each row in the file)
var repArray = File.ReadAllLines(SalesRepFileName);
//loop through each sales rep csv record in the string array
foreach (string repData in repArray)
{
//split the csv record by the comma into a string array
var items = repData.Split(',');
//convert string id value from csv record to integer for testing value of city id
int id = int.Parse(items[2]);
//if we have a match...
if(id == city.Id)
{
//instantiate the Sales Rep with items from csv record
rep = new SalesRep { Id = int.Parse(items[0]), FullName = items[1], City = city };
//then pass the sales rep to the method that will populate the sales rep object with its properties
GetPropertiesBySalesRep(rep);
//then add sales rep to collection
reps.Add(rep);
}
}
//the sales rep collection is then assigned to the sales rep property of the city object passed
//in to this method
city.SalesReps = reps;
}
//This private method populates the sales rep object with its properties
private static void GetPropertiesBySalesRep(SalesRep rep)
{
var props = new List<Property>();
Property prop = null;
//get preoperties from the csv file as a string array
var propArray = File.ReadAllLines(propertyFileName);
//loop through the csv records
foreach (string propData in propArray)
{
//split the csv record into a string array
var items = propData.Split(',');
//convert the string id value from csv record for testing of sales rep id
int id = int.Parse(items[4]);
//test to see if sales rep id of property matches the sales rep id passed in
if (id == rep.Id)
{
//if match, instantiate the property with items of csv record
prop = new Property { Id = int.Parse(items[0]), Address = items[1], PropertyType = items[2], OwnerName = items[3] };
//then assign sales rep object to sales rep property of property
prop.SalesRep = rep;
//then add property to collection
props.Add(prop);
}
}
//then assign properties collection to properties property of he sales rep object passed in.
rep.Properties = props;
}
}
}
No comments:
Post a Comment