Skip to content

Instantly share code, notes, and snippets.

@bwedding
Created February 25, 2018 20:03
Show Gist options
  • Save bwedding/0a222fa99c00ab9d368fd2ac07d6e08f to your computer and use it in GitHub Desktop.
Save bwedding/0a222fa99c00ab9d368fd2ac07d6e08f to your computer and use it in GitHub Desktop.
Homework for someone
// ConsoleApplication9.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include<iostream>
#include<iomanip>
#include<memory>
#include <algorithm> // std::sort
using namespace std;
class Person
{
public:
Person(){};
Person( int MemNo, string fName, string lName, string dateJoined)
{
SetMemNo(MemNo);
SetFirstName(fName);
SetLastName(lName);
SetDateJoined(dateJoined);
}
void SetMemNo(int MemNo) { memno = MemNo; };
void SetFirstName(string fName) { szFirstName = fName; };
void SetLastName(string &lName) { szLastName = lName; };
string GetLastName() { return szLastName; };
void SetDateJoined(string &dateJoined) { doj = dateJoined; };
void displayPerson();
bool operator < (const Person& str) const
{
return (szLastName < str.szLastName);
}
private:
int memno = 0;
string szFirstName{};
string szLastName{};
string doj{};
};
class People
{
public:
int getPeople(int nMaxSize);
void displayPeople(void);
void AddPerson(Person &person);
//Person* quickSort(Person arr[], int length);
bool myfunction(Person &a, Person &b) { return a.GetLastName() < b.GetLastName(); }
void SortPeople();
private:
//static bool SortFunc(Person &a, Person &b) { return a.GetLastName() < b.GetLastName(); }
vector<Person> m_People{};
Person getPerson(void);
int existpersons();
};
void People::SortPeople()
{
sort(m_People.begin(), m_People.end());
}
void People::AddPerson(Person &person)
{
m_People.push_back( person);
}
Person People::getPerson()
{
Person person;
string input{};
int memNo = 0;
cout << " \nEnter another Person\n" << "Membership Number: ";
cin >> memNo;
person.SetMemNo(memNo);
cout << "Enter first name of person: ";
cin >> input;
person.SetFirstName(input);
cout << "Enter your Surname ";
cin >> input;
person.SetLastName(input);
cout << "Enter your date of joining: ";
cin >> input;
person.SetDateJoined(input);
return person;
}
int People::existpersons()
{
return m_People.size();
}
int People::getPeople( int nMaxSize)
{
int count = 0;
while (count < nMaxSize)
{
string cAnswer;
cout << "Enter another name? (Y or N): ";
cin >> cAnswer;
if ( toupper(cAnswer[0]) != 'Y' )
{
break;
}
m_People.push_back( getPerson());
count++;
}
return count;
}
void People::displayPeople()
{
for (auto person : m_People)
{
person.displayPerson();
}
}
void Person::displayPerson()
{
cout << setw(17) << "Membership number" << setw(10) << "Name" << setw(27) << "Date of Joining" << endl;
cout << setw(17) << memno << setw(10) << szFirstName << " " << szLastName << setw(18) << doj << endl;
}
int main()
{
// Prepopulate
vector<Person> Peeps =
{{
Person(3323,"abc","zzz","27/07/2012"),
Person(332,"bcd","xyz","26/07/2012"),
Person(33332,"efg","def","29/07/2011"),
Person(3332332,"klm","nop","28/09/2013")
}};
People myPeople;
for (auto peep : Peeps)
myPeople.AddPerson(peep);
myPeople.getPeople(2);
myPeople.displayPeople();
myPeople.SortPeople();
cout << endl << "Sorted by last name" << endl;
myPeople.displayPeople();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment