Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Fabian on 2019-02-09.
- //
- #ifndef DT019G_LIST_H
- #define DT019G_LIST_H
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <iostream>
- #include "person.h"
- using namespace std;
- class PersonList
- {
- private:
- vector<Person> list;
- string filename;
- public:
- void setFilename(string name);
- void addPerson(Person &person);
- string getFilename() const;
- void getPerson(int i) const;
- int listSize() const;
- void sortName();
- void sortPersnr();
- void sortShoenr();
- void readFromFile();
- void writeToFile();
- };
- bool compareName(const Person &a, const Person &b);
- bool comparePersnr(const Person &a, const Person &b);
- bool compareShoenr(const Person &a, const Person &b);
- //------------------------------------------------------------------------------
- // List
- //------------------------------------------------------------------------------
- void PersonList::addPerson(Person &person)
- {
- int i = list.size();
- string first, last, ssn;
- string street, zip, city;
- int shoe;
- first = person.getFirstName();
- last = person.getLastName();
- street = person.getStreet();
- zip = person.getZip();
- city = person.getCity();
- ssn = person.getSsn();
- shoe = person.getShoeSize();
- list.push_back(Person());
- list[i].setFirstName(first);
- list[i].setLastName(last);
- list[i].setStreet(street);
- list[i].setZip(zip);
- list[i].setCity(city);
- list[i].setSsn(ssn);
- list[i].setShoeSize(shoe);
- return;
- }
- void PersonList::getPerson(int i) const
- {
- cout << list[i].getFirstName() + " " + list[i].getLastName() << endl;
- cout << list[i].getSsn() + " " << list[i].getShoeSize() << endl;
- cout << list[i].getStreet() + ", " + list[i].getZip() + " " + list[i].getCity() << endl;
- return;
- }
- bool compareName(const Person &a, const Person &b)
- {
- string name1, name2;
- string first1 = a.getFirstName(), first2 = b.getFirstName();
- string last1 = a.getLastName(), last2 = b.getLastName();
- for(int i = 0; i < first1.length(); i++) {
- char c = first1[i];
- first1[i] = tolower(c);
- }
- for(int i = 0; i < first2.length(); i++) {
- char c = first2[i];
- first2[i] = tolower(c);
- }
- for(int i = 0; i < last1.length(); i++) {
- char c = last1[i];
- last1[i] = tolower(c);
- }
- for(int i = 0; i < last2.length(); i++) {
- char c = last2[i];
- last2[i] = tolower(c);
- }
- name1 = (last1 + first1);
- name2 = (last2 + first2);
- return name1 < name2;
- }
- bool comparePersnr(const Person &a, const Person &b)
- {
- string ssn1 = a.getSsn();
- string ssn2 = b.getSsn();
- return ssn1 < ssn2;
- }
- bool compareShoenr(const Person &a, const Person &b)
- {
- int shoe1 = a.getShoeSize();
- int shoe2 = b.getShoeSize();
- return shoe1 > shoe2;
- }
- void PersonList::sortName()
- {
- sort(list.begin(),list.end(),compareName);
- return;
- }
- void PersonList::sortPersnr()
- {
- sort(list.begin(),list.end(),comparePersnr);
- return;
- }
- void PersonList::sortShoenr()
- {
- sort(list.begin(),list.end(),compareShoenr);
- return;
- }
- void PersonList::setFilename(string name)
- {
- filename = (name + ".txt");
- return;
- }
- string PersonList::getFilename() const
- {
- return filename;
- }
- void PersonList::readFromFile()
- {
- fstream inFile(filename,ios::in);
- int i = list.size();
- string line, tmp1;
- int pos1 = 0, pos2 = 0, length, tmp2;
- while(getline(inFile,line))
- {
- list.push_back(Person());
- pos2 = line.find(DELIM,pos1);
- length = (pos2 - pos1);
- tmp1 = line.substr(pos1,length);
- list[i].setFirstName(tmp1);
- pos1 = (pos2 + 1);
- pos2 = line.find(DELIM,pos1);
- length = (pos2 - pos1);
- tmp1 = line.substr(pos1,length);
- list[i].setLastName(tmp1);
- pos1 = (pos2 + 1);
- pos2 = line.find(DELIM,pos1);
- length = (pos2 - pos1);
- tmp1 = line.substr(pos1,length);
- list[i].setSsn(tmp1);
- pos1 = (pos2 + 1);
- pos2 = line.find(DELIM,pos1);
- length = (pos2 - pos1);
- tmp2 = stoi(line.substr(pos1,length));
- list[i].setShoeSize(tmp2);
- pos1 = (pos2 + 1);
- pos2 = line.find(DELIM,pos1);
- length = (pos2 - pos1);
- tmp1 = line.substr(pos1,length);
- list[i].setStreet(tmp1);
- pos1 = (pos2 + 1);
- pos2 = line.find(DELIM,pos1);
- length = (pos2 - pos1);
- tmp1 = line.substr(pos1,length);
- list[i].setZip(tmp1);
- pos1 = (pos2 + 1);
- pos2 = line.find(DELIM,pos1);
- length = (pos2 - pos1);
- tmp1 = line.substr(pos1,length);
- list[i].setCity(tmp1);
- pos1 = (pos2 + 1);
- pos1 = 0, pos2 = 0;
- i++;
- }
- inFile.close();
- return;
- }
- void PersonList::writeToFile()
- {
- fstream outFile(filename,ios::out);
- for(int i = 0; i < list.size(); i++)
- {
- outFile << list[i].getFirstName() << DELIM;
- outFile << list[i].getLastName() << DELIM;
- outFile << list[i].getSsn() << DELIM;
- outFile << list[i].getShoeSize() << DELIM;
- outFile << list[i].getStreet() << DELIM;
- outFile << list[i].getZip() << DELIM;
- outFile << list[i].getCity() << DELIM;
- }
- outFile.close();
- return;
- }
- int PersonList::listSize() const
- {
- int size = list.size();
- return size;
- }
- #endif //DT019G_LIST_H
Advertisement
Add Comment
Please, Sign In to add comment