I am currently making a console program about buying property in a city. Properties can be bought from the city by someone, transferred to another person, or returned to the city if the person who has ownership dies.
I have created 3 classes: Person, City, and Property. My Person and City classes both have vectors of unique_ptrs of Property. My main.cpp has a City unique_ptr and a vector of Person unique_ptrs and does basic initialization.
class Person { string name; vector<unique_ptr<Property>> ownedProperties; public: string getName(); vector<unique_ptr<Property>> const& getOwnedProperties(); }; class City { string name; vector<unique_ptr<Property>> ownedProperties; public: string getName(); vector<unique_ptr<Property>> const& getOwnedProperties(); }; class Property { string name; int id; public: int getID(); string getName(); } int main() { int numProperties; vector<unique_ptr<Person>> people; unique_ptr<City> city; // initializing everything printAllProperties(); }
I decided to use unique_ptr because I am thinking of making subclasses of Property later and because I want to make sure that a Property only has one owner. I am using move() to transfer ownership between the Property vectors of City and Person. I am also printing out information like this:
City: San Johncisco ID | Property Name | Owner 0 | A | John 1 | B | San Johncisco 2 | C | Johnny 3 | D | Johnathan 4 | E | San Johncisco 5 | F | Johnalina 6 | G | Johnatha
The problem is that because I want to print the properties in order, I end up having something like this:
for (int currentID = 0; currentID < numProperties; currentID++) { for (auto property: city->getOwnedProperties()) { if (property->getID() == currentID) { // print info } } for (auto person: people) { for (auto property: person->getOwnedProperties()) { if (property->getID() == currentID) { // print info } } } }
This looks really ugly and I feel as though I could’ve designed the program / method better. Any advice?