fabbe680

list.h

Feb 10th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. //
  2. // Created by Fabian on 2019-02-09.
  3. //
  4.  
  5. #ifndef DT019G_LIST_H
  6. #define DT019G_LIST_H
  7.  
  8. #include <string>
  9. #include <vector>
  10. #include <algorithm>
  11. #include <fstream>
  12. #include <iostream>
  13. #include "person.h"
  14.  
  15. using namespace std;
  16.  
  17. class PersonList
  18. {
  19. private:
  20. vector<Person> list;
  21. string filename;
  22.  
  23. public:
  24. void setFilename(string name);
  25. void addPerson(Person &person);
  26. string getFilename() const;
  27. void getPerson(int i) const;
  28. int listSize() const;
  29. void sortName();
  30. void sortPersnr();
  31. void sortShoenr();
  32. void readFromFile();
  33. void writeToFile();
  34. };
  35.  
  36. bool compareName(const Person &a, const Person &b);
  37. bool comparePersnr(const Person &a, const Person &b);
  38. bool compareShoenr(const Person &a, const Person &b);
  39.  
  40. //------------------------------------------------------------------------------
  41. // List
  42. //------------------------------------------------------------------------------
  43.  
  44. void PersonList::addPerson(Person &person)
  45. {
  46. int i = list.size();
  47. string first, last, ssn;
  48. string street, zip, city;
  49. int shoe;
  50.  
  51. first = person.getFirstName();
  52. last = person.getLastName();
  53.  
  54. street = person.getStreet();
  55. zip = person.getZip();
  56. city = person.getCity();
  57.  
  58. ssn = person.getSsn();
  59. shoe = person.getShoeSize();
  60.  
  61. list.push_back(Person());
  62. list[i].setFirstName(first);
  63. list[i].setLastName(last);
  64.  
  65. list[i].setStreet(street);
  66. list[i].setZip(zip);
  67. list[i].setCity(city);
  68.  
  69. list[i].setSsn(ssn);
  70. list[i].setShoeSize(shoe);
  71.  
  72. return;
  73. }
  74.  
  75. void PersonList::getPerson(int i) const
  76. {
  77. cout << list[i].getFirstName() + " " + list[i].getLastName() << endl;
  78. cout << list[i].getSsn() + " " << list[i].getShoeSize() << endl;
  79. cout << list[i].getStreet() + ", " + list[i].getZip() + " " + list[i].getCity() << endl;
  80.  
  81. return;
  82. }
  83.  
  84. bool compareName(const Person &a, const Person &b)
  85. {
  86. string name1, name2;
  87. string first1 = a.getFirstName(), first2 = b.getFirstName();
  88. string last1 = a.getLastName(), last2 = b.getLastName();
  89.  
  90. for(int i = 0; i < first1.length(); i++) {
  91. char c = first1[i];
  92. first1[i] = tolower(c);
  93. }
  94.  
  95. for(int i = 0; i < first2.length(); i++) {
  96. char c = first2[i];
  97. first2[i] = tolower(c);
  98. }
  99.  
  100. for(int i = 0; i < last1.length(); i++) {
  101. char c = last1[i];
  102. last1[i] = tolower(c);
  103. }
  104.  
  105. for(int i = 0; i < last2.length(); i++) {
  106. char c = last2[i];
  107. last2[i] = tolower(c);
  108. }
  109.  
  110. name1 = (last1 + first1);
  111. name2 = (last2 + first2);
  112.  
  113. return name1 < name2;
  114. }
  115.  
  116. bool comparePersnr(const Person &a, const Person &b)
  117. {
  118. string ssn1 = a.getSsn();
  119. string ssn2 = b.getSsn();
  120.  
  121. return ssn1 < ssn2;
  122. }
  123.  
  124. bool compareShoenr(const Person &a, const Person &b)
  125. {
  126. int shoe1 = a.getShoeSize();
  127. int shoe2 = b.getShoeSize();
  128.  
  129. return shoe1 > shoe2;
  130. }
  131.  
  132. void PersonList::sortName()
  133. {
  134. sort(list.begin(),list.end(),compareName);
  135.  
  136. return;
  137. }
  138.  
  139. void PersonList::sortPersnr()
  140. {
  141. sort(list.begin(),list.end(),comparePersnr);
  142.  
  143. return;
  144. }
  145.  
  146. void PersonList::sortShoenr()
  147. {
  148. sort(list.begin(),list.end(),compareShoenr);
  149.  
  150. return;
  151. }
  152.  
  153. void PersonList::setFilename(string name)
  154. {
  155. filename = (name + ".txt");
  156.  
  157. return;
  158. }
  159.  
  160. string PersonList::getFilename() const
  161. {
  162. return filename;
  163. }
  164.  
  165. void PersonList::readFromFile()
  166. {
  167. fstream inFile(filename,ios::in);
  168. int i = list.size();
  169. string line, tmp1;
  170. int pos1 = 0, pos2 = 0, length, tmp2;
  171.  
  172. while(getline(inFile,line))
  173. {
  174. list.push_back(Person());
  175.  
  176. pos2 = line.find(DELIM,pos1);
  177. length = (pos2 - pos1);
  178. tmp1 = line.substr(pos1,length);
  179. list[i].setFirstName(tmp1);
  180. pos1 = (pos2 + 1);
  181.  
  182. pos2 = line.find(DELIM,pos1);
  183. length = (pos2 - pos1);
  184. tmp1 = line.substr(pos1,length);
  185. list[i].setLastName(tmp1);
  186. pos1 = (pos2 + 1);
  187.  
  188. pos2 = line.find(DELIM,pos1);
  189. length = (pos2 - pos1);
  190. tmp1 = line.substr(pos1,length);
  191. list[i].setSsn(tmp1);
  192. pos1 = (pos2 + 1);
  193.  
  194. pos2 = line.find(DELIM,pos1);
  195. length = (pos2 - pos1);
  196. tmp2 = stoi(line.substr(pos1,length));
  197. list[i].setShoeSize(tmp2);
  198. pos1 = (pos2 + 1);
  199.  
  200. pos2 = line.find(DELIM,pos1);
  201. length = (pos2 - pos1);
  202. tmp1 = line.substr(pos1,length);
  203. list[i].setStreet(tmp1);
  204. pos1 = (pos2 + 1);
  205.  
  206. pos2 = line.find(DELIM,pos1);
  207. length = (pos2 - pos1);
  208. tmp1 = line.substr(pos1,length);
  209. list[i].setZip(tmp1);
  210. pos1 = (pos2 + 1);
  211.  
  212. pos2 = line.find(DELIM,pos1);
  213. length = (pos2 - pos1);
  214. tmp1 = line.substr(pos1,length);
  215. list[i].setCity(tmp1);
  216. pos1 = (pos2 + 1);
  217.  
  218. pos1 = 0, pos2 = 0;
  219. i++;
  220. }
  221.  
  222. inFile.close();
  223.  
  224. return;
  225. }
  226.  
  227. void PersonList::writeToFile()
  228. {
  229. fstream outFile(filename,ios::out);
  230.  
  231. for(int i = 0; i < list.size(); i++)
  232. {
  233. outFile << list[i].getFirstName() << DELIM;
  234. outFile << list[i].getLastName() << DELIM;
  235. outFile << list[i].getSsn() << DELIM;
  236. outFile << list[i].getShoeSize() << DELIM;
  237. outFile << list[i].getStreet() << DELIM;
  238. outFile << list[i].getZip() << DELIM;
  239. outFile << list[i].getCity() << DELIM;
  240. }
  241.  
  242. outFile.close();
  243.  
  244. return;
  245. }
  246.  
  247. int PersonList::listSize() const
  248. {
  249. int size = list.size();
  250.  
  251. return size;
  252. }
  253.  
  254. #endif //DT019G_LIST_H
Advertisement
Add Comment
Please, Sign In to add comment