// Geoff Hagopian doing PPP9 ex 2
/*2. Design and implement a Name_pairs class holding (name,age) 
pairs where name is a string and age is a double. Represent that 
as a vector<string> (called name) and a vector<double> (called 
age) member. Provide an input operation read_names() that reads
a series of names. Provide a read_ages() operation that prompts 
the user for an age for each name. Provide a print() operation 
that prints out the (name[i],age[i]) pairs (one per line) in
the order determined by the name vector. Provide a sort() 
operation that sorts the name vector in alphabetical order and 
reorganizes the age vector to match. Implement all “operations” 
as member functions. Test the class (of course: test early and 
often).
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class NamePairs {
private:
	vector<string> name;
	vector<double> age;
public:
	void read_names() {
		string temp;
		cout << "Enter names (\"end\" to stop): ";
		while (getline(cin, temp) && temp != "end") {
			name.push_back(temp);
		}
	}
	void read_ages() {
		double a;
		cout << "Enter their ages: ";
		for(string s : name) {
			cout << "How old is " << s << "? ";
			cin >> a;
			age.push_back(a);
		}
	}
	void print() {
		cout << "The names and ages are\n";
		for (int i = 0; i < name.size(); ++i)
			cout << '(' << name[i] << ", " << age[i] << ")\n";
	}
	void sortNamePairs() {
		vector<string> sortedNames;
		vector<double> sortedAges;
		//https://www.thecrazyprogrammer.com/2011/11/c-program-to-sort-array-by-using-bubble.html
		for (int i = 1; i < name.size(); ++i)
		{
			for (int j = 0; j < name.size()-i; ++j)
				if (name[j] > name[j + 1])
				{
					swap(name[j], name[j+1]);
					swap(age[j], age[j+1]);
				}
		}
	}
};

int main() {
	NamePairs nP;
	nP.read_names();
	nP.read_ages();
	nP.print();
	nP.sortNamePairs();
	nP.print();
}