1 //#include "stdafx.h"
  2 innclude <string>
  3 #include <iostream>
  4 #include <sstream>
  5 #include "unique_tree.h"
  6 using namespace tcl;
  7 // forward declaration
  8 class cRental;
  9
 10 namespace utility
 11 {
 12 	void populate_tree(tcl::unique_tree<cRental>& rental_tree);
 13 	void print_tree(const tcl::unique_tree<cRental>& rental_tree, const int depth);
 14 	bool is_last_child(const tcl::unique_tree<cRental>* node);
 15 }
 16 
 17 class cRental
 18 {
 19 public:
 20 	cRental() : rental_no(0) {}
 21 	cRental(const int rental_no_) : rental_no(rental_no_) {}
 22 	cRental(const int rental_no_, const std::string& rental_name_)
 23 		: rental_no(rental_no_), rental_name(rental_name_) {}
 24 	virtual ~cRental() {}
 25
 26 	virtual std::string get_rental_type() const { return "unknown"; }
 27 	virtual cRental* clone() const { return new cRental(*this); }
 28 
 29 	friend bool operator < (const cRental& lhs, const cRental& rhs) { return lhs.rental_no < rhs.rental_no; }
 30 	int get_rental_no() const { return rental_no; }
 31 	const std::string& get_name() const { return rental_name; }
 32 	std::string print() const
 33 	{
 34 		std::ostringstream ostr;
 35 		ostr << get_rental_type() << ": " << get_name();
 36 		return ostr.str();
 37 	}
 38 
 39 private:
 40 	int rental_no;
 41 	std::string rental_name;
 42 };
 43
 44 class cProperty : public cRental
 45 {
 46 public:
 47 	cProperty() : acreage(0.0), land_value(0.0) {}
 48 	cProperty(	int rental_no_, 
 49 				const std::string& property_name_,
 50 				double acreage_,
 51 				double land_value_)
 52 				:	cRental(rental_no_, property_name_),
 53 					acreage(acreage_),
 54 					land_value(land_value_)  {}
 55 	virtual ~cProperty() {}
 56 
 57 	virtual std::string get_rental_type() const { return "Property"; }
 58 	virtual cRental* clone() const { return new cProperty(*this); }
 59 
 60 private:
 61 	double acreage;
 62 	double land_value;
 63 };
 64
 65 class cBuilding : public cRental
 66 {
 67 public:
 68 	cBuilding()  {}
 69 	cBuilding(	int rental_no_, 
 70 				const std::string& building_name_,
 71 				const std::string& address_)
 72 				:	cRental(rental_no_, building_name_),
 73 					address(address_)  {}
 74 	virtual ~cBuilding() {}
 75
 76 	virtual std::string get_rental_type() const { return "Building"; }
 77 	virtual cRental* clone() const { return new cBuilding(*this); }
 78
 79 private:
 80 	std::string address;
 81 };
 82
 83 class cUnit : public cRental
 84 {
 85 public:
 86 	cUnit() : rent_amt(0.0) {}
 87 	cUnit(	int rental_no_, 
 88 				const std::string& unit_name_,
 89 				double rent_amt_)
 90 				:	cRental(rental_no_, unit_name_),
 91 					rent_amt(rent_amt_)  {}
 92 	~cUnit() {}
 93 
 94 	virtual std::string get_rental_type() const { return "Unit"; }
 95 	virtual cRental* clone() const { return new cUnit(*this); }
 96
 97 private:
 98 	double rent_amt;
 99 	std::string unit_code;
100 };
101
102 cRental* clone_fcn(const cRental& rental_obj)
103 {
104 	return rental_obj.clone();
105 }
106
107 int main(int argc, char* argv[])
108 {
109 	tcl::unique_tree<cRental> rental_tree(cRental(0, "My Properties"));
110 	
111 
112 	// populate and print tree without supplying clone function
113 	std::cout << "Tree without clone function" << std::endl << std::endl;
114 	utility::populate_tree(rental_tree);
115 	utility::print_tree(rental_tree, 0);
116 	std::cout << std::endl << std::endl << std::endl;
117
118 	// populate and print tree supplying a clone function
119 	std::cout << "Tree with clone function" << std::endl << std::endl;
120 	rental_tree.clear();
121 	rental_tree.set_clone(&clone_fcn);
122 	utility::populate_tree(rental_tree);
123 	utility::print_tree(rental_tree, 0);
124 	std::cout << std::endl << std::endl;
125
126 	return 0;
127 }
128
129 void utility::populate_tree(tcl::unique_tree<cRental>& rental_tree)
130 {
131 	// insert properties
132 	rental_tree.insert( cRental(0), cProperty(100, "Riverside Property", 35.5, 1000.00));
133 	rental_tree.insert( 0, cProperty(200, "Main Street Property", 21.6, 2500.00));
134 	rental_tree.insert( 0, cProperty(300, "Boston Ave Property", 52.1, 4300.00));
135 
136 	// insert buildings
137 	rental_tree.insert( 100, cBuilding(110, "Tech Building", "4636 Riverside Road" ));
138 	rental_tree.insert( 100, cBuilding(120, "Riverside Hotel", "4690 Riverside Road"));
139
140 	rental_tree.insert( 200, cBuilding(210, "Main Street Building", "322 Main Street"));
141
142 	rental_tree.insert( 300, cBuilding(310, "Boston Ave Tech Center", "3200 Boston Ave"));
143 	rental_tree.insert( 300, cBuilding(320, "Mike's Pool Hall", "3350 Boston Ave"));
144 	rental_tree.insert( 300, cBuilding(330, "Porter Apartment Bldg", "3300 Boston Ave"));
145
146 	// insert units
147 	rental_tree.insert( 110, cUnit(111, "Suite G1", 1400.00));
148 	rental_tree.insert( 110, cUnit(112, "Suite G2", 2150.00));
149 	rental_tree.insert( 110, cUnit(113, "Suite G3", 1530.00));
150 	rental_tree.insert( 110, cUnit(114, "Suite G4", 2300.00));
151 	rental_tree.insert( 110, cUnit(115, "Suite G5", 2640.00));
152
153 	rental_tree.insert( 120, cUnit(121, "Room 1A", 600.00));
154 	rental_tree.insert( 120, cUnit(122, "Room 1B", 550.00));
155 	rental_tree.insert( 120, cUnit(123, "Room 1C", 650.00));
156 	rental_tree.insert( 120, cUnit(124, "Room 2A", 700.00));
157 	rental_tree.insert( 120, cUnit(125, "Room 2B", 630.00));
158 	rental_tree.insert( 120, cUnit(126, "Room 2C", 800.00));
159
160 	rental_tree.insert( 210, cUnit(211, "Apt 1", 550.00));
161 	rental_tree.insert( 210, cUnit(212, "Apt 2", 600.00));
162 	rental_tree.insert( 210, cUnit(213, "Apt 3", 730.00));
163 	rental_tree.insert( 210, cUnit(214, "Apt 4", 660.00));
164
165 	rental_tree.insert( 310, cUnit(311, "Suite A", 2200.00));
166 	rental_tree.insert( 310, cUnit(312, "Suite B", 1900.00));
167 	rental_tree.insert( 310, cUnit(313, "Suite C", 2500.00));
168 
169 	rental_tree.insert( 330, cUnit(331, "Apt 101", 900.00));
170 	rental_tree.insert( 330, cUnit(332, "Apt 102", 680.00));
171 	rental_tree.insert( 330, cUnit(333, "Apt 103", 800.00));
172 	rental_tree.insert( 330, cUnit(334, "Apt 201", 880.00));
173 	rental_tree.insert( 330, cUnit(335, "Apt 202", 930.00));
174 }
175
176 void utility::print_tree(const tcl::unique_tree<cRental>& rental_tree, const int depth )
177 {
178 	std::cout << rental_tree.get()->print() << std::endl;
179
180 	tcl::unique_tree<cRental>::const_iterator it = rental_tree.begin(), it_end = rental_tree.end();
181 	for ( ; it != it_end; ++it ) {
182 		for ( int i = 0; i < depth; ++i ) {
183 			const tcl::unique_tree<cRental>* parent = &rental_tree;
184 			for ( int j = depth - 1; j > i; --j )
185 				parent = parent->parent();
186
187 			std::cout <<  (is_last_child(parent) ? " " : "|");
188 
189 			std::cout << std::string(2, ' ');
190 		}
191 		std::cout << "|" << std::endl;
192 		std::cout << std::string(depth * 3 + 1, ' ') << "- ";
193 		print_tree(*it.node(), depth + 1);
194 	}
195 }
196 
197 bool utility::is_last_child(const tcl::unique_tree<cRental>* node)
198 {
199 	const tcl::unique_tree<cRental>* parent = node->parent();
200 	tcl::unique_tree<cRental>::const_iterator it = parent->end();
201 	--it;
202 	return it->get_rental_no() == node->get()->get_rental_no();
203 }