Advertisement
Guest User

corporation.cpp

a guest
Nov 16th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.44 KB | None | 0 0
  1. #include "Corporation.h"
  2.  
  3. using namespace std;
  4.  
  5. Utilities u1;
  6. Menu corpMenu;
  7.  
  8.  
  9.  
  10. Corporation * Corporation::instance()
  11. {
  12.  
  13. if (!singleton_instance) {
  14. singleton_instance = new Corporation;
  15. }
  16.  
  17. return singleton_instance;
  18. }
  19.  
  20. void Corporation::login() {
  21.  
  22. string password, user;
  23. bool foundUser = false;
  24. bool foundSupplier = false;
  25.  
  26. cout << "Username: "; getline(cin, user);
  27.  
  28. if (cin.eof()) {
  29. u1.cancelMessage();
  30. corpMenu.MainMenu();
  31. }
  32.  
  33. cout << "\nPassword: "; cin >> password;
  34.  
  35. u1.cinClear();
  36.  
  37. if (cin.eof()) {
  38. u1.cancelMessage();
  39. corpMenu.MainMenu();
  40. }
  41.  
  42. for (size_t i = 0; i != usersVec.size(); i++) {
  43. if (usersVec.at(i).getUsername() == user && usersVec.at(i).getPassword() == password) {
  44. Corporation::instance()->username = user;
  45. foundUser = true;
  46. u1.clearScreen();
  47. corpMenu.UsersMenu();
  48. return;
  49. }
  50. }
  51.  
  52. for (size_t i = 0; i != suppliersVec.size(); i++) {
  53. if (suppliersVec.at(i).getName() == user && suppliersVec.at(i).getPassword() == password) {
  54. Corporation::instance()->supplierName = user;
  55. foundSupplier = true;
  56. u1.clearScreen();
  57. corpMenu.SuppliersMenu();
  58. return;
  59. }
  60. }
  61.  
  62. if (!foundUser && !foundSupplier) {
  63. cout << "\n ERROR: The username/password you inserted do not exist. Please try again.";
  64. Sleep(2000);
  65. return;
  66. }
  67.  
  68. }
  69.  
  70.  
  71. //Checks existance of the users file
  72. bool Corporation::foundUsersFile(string usersFile) {
  73.  
  74. fstream f;
  75.  
  76. f.open(usersFile);
  77.  
  78. if (f.fail()) {
  79. f.close();
  80. u1.setColor(12); cerr << "\n ERROR: " << usersFile << " (users file) could not be found!\n Verify the directory!\n\n"; u1.setColor(15);
  81. return false;
  82. }
  83.  
  84. f.close();
  85.  
  86. this->usersFile = usersFile;
  87. return true;
  88. }
  89.  
  90. //Loads the users file to memory (Users vector)
  91. void Corporation::loadUsers() {
  92.  
  93. string line;
  94. fstream f;
  95.  
  96. f.open(usersFile);
  97.  
  98. while (getline(f, line)) {
  99.  
  100. string username = line.substr(0, line.find(" ; "));
  101. line.erase(0, line.find(" ; ") + 3);
  102. string password = line.substr(0, line.find(" ; "));
  103. line.erase(0, line.find(" ; ") + 3);
  104. unsigned int nif = stoi(line.substr(0, line.find(" ; ")));
  105. line.erase(0, line.find(" ; ") + 3);
  106. unsigned int points = stoi(line.substr(0, line.length()));
  107.  
  108. usersVec.push_back(Users(username, password, nif, points));
  109. }
  110. f.close();
  111. return;
  112. }
  113.  
  114. //Loads memory to the users file
  115. void Corporation::saveUsers() {
  116.  
  117. ofstream f("temp.txt");
  118.  
  119. for (size_t i = 0; i < usersVec.size(); i++) {
  120. f << usersVec[i].getUsername() << " ; " << usersVec[i].getPassword() << " ; " << usersVec[i].getNif() << " ; " << usersVec[i].getPoints() << endl;
  121. }
  122.  
  123. f.close();
  124.  
  125. remove(usersFile.c_str());
  126. rename("temp.txt", usersFile.c_str());
  127.  
  128. return;
  129. }
  130.  
  131. //Adds a user to the users vector
  132. void Corporation::registerUser() {
  133.  
  134. string user, password, nif;
  135.  
  136. cout << "\n Name: "; getline(cin, user);
  137.  
  138. if (cin.eof()) {
  139. u1.cancelMessage();
  140. corpMenu.RegisterMenu();
  141. }
  142.  
  143. for (unsigned int index = 0; index != user.size(); index++) {
  144. if (!isalpha(user[index]) && user[index] != ' ') {
  145. u1.setColor(12); cerr << " ERROR: Name must only contain alphabetic characters. "; u1.setColor(15);
  146. Sleep(3000);
  147. u1.clearScreen();
  148. corpMenu.RegisterMenu();
  149. }
  150. }
  151.  
  152. for (unsigned int index2 = 0; index2 != usersVec.size(); index2++) {
  153. if (usersVec.at(index2).getUsername() == user) {
  154. u1.setColor(12); cerr << " ERROR: The username you selected already exists. Please choose another one. "; u1.setColor(15);
  155. Sleep(3000);
  156. u1.clearScreen();
  157. corpMenu.RegisterMenu();
  158. }
  159. }
  160.  
  161. cout << "\n Password: "; cin >> password;
  162.  
  163. u1.cinClear();
  164.  
  165. for (unsigned int index3 = 0; index3 != password.size(); index3++) {
  166. if (!isalnum(password[index3])) {
  167. u1.setColor(12); cerr << " ERROR: Password cannot contain special characters. "; u1.setColor(15);
  168. Sleep(3000);
  169. u1.clearScreen();
  170. corpMenu.RegisterMenu();
  171. }
  172. }
  173.  
  174. cout << "\n NIF: "; cin >> nif;
  175. u1.cinClear();
  176.  
  177. for (unsigned int index4 = 0; index4 != nif.size(); index4++) {
  178. if (!isdigit(nif[index4])) {
  179. u1.setColor(12); cerr << " ERROR: NIF can only contain digits. "; u1.setColor(15);
  180. Sleep(3000);
  181. u1.clearScreen();
  182. corpMenu.RegisterMenu();
  183. }
  184. }
  185.  
  186. for (unsigned int index5 = 0; index5 != usersVec.size(); index5++) {
  187. if (usersVec.at(index5).getNif() == stoi(nif)) {
  188. u1.setColor(12); cerr << " ERROR: The NIF you selected was already found in our system. Probably you already have an account. "; u1.setColor(15);
  189. Sleep(3000);
  190. u1.clearScreen();
  191. corpMenu.RegisterMenu();
  192. }
  193.  
  194. }
  195.  
  196. if (nif.size() != 9) {
  197. u1.setColor(12); cerr << " ERROR: The NIF must have 9 digits. "; u1.setColor(15);
  198. Sleep(3000);
  199. u1.clearScreen();
  200. corpMenu.RegisterMenu();
  201. }
  202.  
  203. if (cin.eof()) {
  204. u1.cancelMessage();
  205. corpMenu.RegisterMenu();
  206. }
  207.  
  208. usersVec.push_back(Users(user, password, stoi(nif), 0));
  209. u1.clearScreen();
  210. return;
  211. }
  212.  
  213. bool Corporation::foundSuppliersFile(string suppliersFile) {
  214.  
  215. fstream f;
  216.  
  217. f.open(suppliersFile);
  218.  
  219. if (f.fail()) {
  220. f.close();
  221. u1.setColor(12); cerr << "\n ERROR: " << suppliersFile << " (suppliers file) could not be found!\n Verify the directory!\n\n"; u1.setColor(15);
  222. return false;
  223. }
  224.  
  225. f.close();
  226.  
  227. this->suppliersFile = suppliersFile;
  228. return true;
  229. }
  230.  
  231. void Corporation::loadSuppliers() {
  232.  
  233. string line;
  234. fstream f;
  235.  
  236. vector<Rent> rent;
  237.  
  238. f.open(suppliersFile);
  239.  
  240. while (getline(f, line)) {
  241.  
  242. string supplierName = u1.trim(line.substr(0, line.find(" ; ")));
  243. line.erase(0, line.find(" ; ") + 3);
  244. string password = u1.trim(line.substr(0, line.find(" ; ")));
  245. line.erase(0, line.find(" ; ") + 3);
  246. unsigned int nif = stoi(line.substr(0, line.find(" ; ")));
  247. line.erase(0, line.find(" ; ") + 3);
  248. string address = u1.trim(line.substr(0, line.find(" ; ")));
  249. line.erase(0, line.find(" ; ") + 3);
  250. string typeRent = u1.trim(line.substr(0, line.find(" ; ")));
  251. line.erase(0, line.find(" ; ") + 3);
  252.  
  253. if (typeRent == "Hotel") {
  254. string nameHotel = u1.trim(line.substr(0, line.find(" ; ")));
  255. line.erase(0, line.find(" ; ") + 3);
  256. string city = u1.trim(line.substr(0, line.find(" ; ")));
  257. line.erase(0, line.find(" ; ") + 3);
  258. unsigned int startDay = stoi(line.substr(0, line.find("/")));
  259. line.erase(0, line.find("/") + 1);
  260. unsigned int startMonth = stoi(line.substr(0, line.find("/")));
  261. line.erase(0, line.find("/") + 1);
  262. unsigned int startYear = stoi(line.substr(0, line.find(" ; ")));
  263. line.erase(0, line.find(" ; ") + 3);
  264. unsigned int endDay = stoi(line.substr(0, line.find("/")));
  265. line.erase(0, line.find("/") + 1);
  266. unsigned int endMonth = stoi(line.substr(0, line.find("/")));
  267. line.erase(0, line.find("/") + 1);
  268. unsigned int endYear = stoi(line.substr(0, line.find(" ; ")));
  269. line.erase(0, line.find(" ; ") + 3);
  270. string roomType = u1.trim(line.substr(0, line.find(" ; ")));
  271. line.erase(0, line.find(" ; ") + 3);
  272. float price = stof(line.substr(0, line.find(" ; ")));
  273. line.erase(0, line.find(" ; ") + 3);
  274. unsigned int numPeople = stoi(line.substr(0, line.length()));
  275.  
  276. rent.push_back(Hotel(typeRent, nameHotel, city, Date(startDay, startMonth, startYear), Date(endDay, endMonth, endYear), roomType, price, numPeople));
  277. suppliersVec.push_back(Supplier(supplierName, password, nif, address, rent));
  278.  
  279. }
  280. else if (typeRent == "Bed'n'Breakfast") {
  281. string nameBB = u1.trim(line.substr(0, line.find(" ; ")));
  282. line.erase(0, line.find(" ; ") + 3);
  283. string city = u1.trim(line.substr(0, line.find(" ; ")));
  284. line.erase(0, line.find(" ; ") + 3);
  285. unsigned int startDay = stoi(line.substr(0, line.find("/")));
  286. line.erase(0, line.find("/") + 1);
  287. unsigned int startMonth = stoi(line.substr(0, line.find("/")));
  288. line.erase(0, line.find("/") + 1);
  289. unsigned int startYear = stoi(line.substr(0, line.find(" ; ")));
  290. line.erase(0, line.find(" ; ") + 3);
  291. unsigned int endDay = stoi(line.substr(0, line.find("/")));
  292. line.erase(0, line.find("/") + 1);
  293. unsigned int endMonth = stoi(line.substr(0, line.find("/")));
  294. line.erase(0, line.find("/") + 1);
  295. unsigned int endYear = stoi(line.substr(0, line.find(" ; ")));
  296. line.erase(0, line.find(" ; ") + 3);
  297. float price = stof(line.substr(0, line.find(" ; ")));
  298. line.erase(0, line.find(" ; ") + 3);
  299. unsigned int numPeople = stoi(line.substr(0, line.length()));
  300.  
  301. rent.push_back(bedNbreakfast(typeRent, nameBB, city, Date(startDay, startMonth, startYear), Date(endDay, endMonth, endYear), price, numPeople));
  302. suppliersVec.push_back(Supplier(supplierName, password, nif, address, rent));
  303.  
  304. }
  305. else if (typeRent == "Shared House") {
  306. string nameSH = u1.trim(line.substr(0, line.find(" ; ")));
  307. line.erase(0, line.find(" ; ") + 3);
  308. string city = u1.trim(line.substr(0, line.find(" ; ")));
  309. line.erase(0, line.find(" ; ") + 3);
  310. unsigned int startDay = stoi(line.substr(0, line.find("/")));
  311. line.erase(0, line.find("/") + 1);
  312. unsigned int startMonth = stoi(line.substr(0, line.find("/")));
  313. line.erase(0, line.find("/") + 1);
  314. unsigned int startYear = stoi(line.substr(0, line.find(" ; ")));
  315. line.erase(0, line.find(" ; ") + 3);
  316. unsigned int endDay = stoi(line.substr(0, line.find("/")));
  317. line.erase(0, line.find("/") + 1);
  318. unsigned int endMonth = stoi(line.substr(0, line.find("/")));
  319. line.erase(0, line.find("/") + 1);
  320. unsigned int endYear = stoi(line.substr(0, line.find(" ; ")));
  321. line.erase(0, line.find(" ; ") + 3);
  322. float price = stof(line.substr(0, line.find(" ; ")));
  323. line.erase(0, line.find(" ; ") + 3);
  324. unsigned int numPeople = stoi(line.substr(0, line.length()));
  325.  
  326. rent.push_back(sharedHouse(typeRent, nameSH, city, Date(startDay, startMonth, startYear), Date(endDay, endMonth, endYear), price, numPeople));
  327. suppliersVec.push_back(Supplier(supplierName, password, nif, address, rent));
  328.  
  329. }
  330. else if (typeRent == "Flat") {
  331. string nameFlat = u1.trim(line.substr(0, line.find(" ; ")));
  332. line.erase(0, line.find(" ; ") + 3);
  333. string city = u1.trim(line.substr(0, line.find(" ; ")));
  334. line.erase(0, line.find(" ; ") + 3);
  335. unsigned int startDay = stoi(line.substr(0, line.find("/")));
  336. line.erase(0, line.find("/") + 1);
  337. unsigned int startMonth = stoi(line.substr(0, line.find("/")));
  338. line.erase(0, line.find("/") + 1);
  339. unsigned int startYear = stoi(line.substr(0, line.find(" ; ")));
  340. line.erase(0, line.find(" ; ") + 3);
  341. unsigned int endDay = stoi(line.substr(0, line.find("/")));
  342. line.erase(0, line.find("/") + 1);
  343. unsigned int endMonth = stoi(line.substr(0, line.find("/")));
  344. line.erase(0, line.find("/") + 1);
  345. unsigned int endYear = stoi(line.substr(0, line.find(" ; ")));
  346. line.erase(0, line.find(" ; ") + 3);
  347. float price = stof(line.substr(0, line.find(" ; ")));
  348. line.erase(0, line.find(" ; ") + 3);
  349. unsigned int numPeople = stoi(line.substr(0, line.length()));
  350.  
  351. rent.push_back(flat(typeRent, nameFlat, city, Date(startDay, startMonth, startYear), Date(endDay, endMonth, endYear), price, numPeople));
  352. suppliersVec.push_back(Supplier(supplierName, password, nif, address, rent));
  353.  
  354. }
  355. else if (typeRent == "Apartment") {
  356. string nameApartment = u1.trim(line.substr(0, line.find(" ; ")));
  357. line.erase(0, line.find(" ; ") + 3);
  358. string city = u1.trim(line.substr(0, line.find(" ; ")));
  359. line.erase(0, line.find(" ; ") + 3);
  360. unsigned int startDay = stoi(line.substr(0, line.find("/")));
  361. line.erase(0, line.find("/") + 1);
  362. unsigned int startMonth = stoi(line.substr(0, line.find("/")));
  363. line.erase(0, line.find("/") + 1);
  364. unsigned int startYear = stoi(line.substr(0, line.find(" ; ")));
  365. line.erase(0, line.find(" ; ") + 3);
  366. unsigned int endDay = stoi(line.substr(0, line.find("/")));
  367. line.erase(0, line.find("/") + 1);
  368. unsigned int endMonth = stoi(line.substr(0, line.find("/")));
  369. line.erase(0, line.find("/") + 1);
  370. unsigned int endYear = stoi(line.substr(0, line.find(" ; ")));
  371. line.erase(0, line.find(" ; ") + 3);
  372. float price = stof(line.substr(0, line.find(" ; ")));
  373. line.erase(0, line.find(" ; ") + 3);
  374. unsigned int numPeople = stoi(line.substr(0, line.find(" ; ")));
  375. int numRooms = stof(line.substr(0, line.find(" ; ")));
  376. bool k, s, l;
  377. string x = line.substr(0, line.find(" ; "));
  378. if (x == "true")
  379. k = true;
  380. else
  381. k = false;
  382. x = line.substr(0, line.find(" ; "));
  383. if (x == "true")
  384. s = true;
  385. else
  386. s = false;
  387. x = line.substr(0, line.find(" ; "));
  388. if (x == "true")
  389. l = true;
  390. else
  391. l = false;
  392.  
  393. rent.push_back(apartment(typeRent, nameApartment, city, Date(startDay, startMonth, startYear), Date(endDay, endMonth, endYear), price, numPeople, numRooms, k, s ,l));
  394. suppliersVec.push_back(Supplier(supplierName, password, nif, address, rent));
  395.  
  396. }
  397. }
  398. f.close();
  399. return;
  400.  
  401. }
  402.  
  403. //Loads suppliersVec to the .txt file
  404. void Corporation::saveSuppliers()
  405. {
  406. ofstream f("tempSuppliers.txt");
  407.  
  408. for (size_t i = 0; i < suppliersVec.size(); i++) {
  409. f << suppliersVec[i].getName() << " ; " << suppliersVec[i].getPassword() << " ; " << suppliersVec[i].getNif() << " ; " << suppliersVec[i].getAddress() << " ; ";
  410. for (int j = 0; j < suppliersVec[i].getVector().size(); j++)
  411. {
  412. if (suppliersVec[i].getVector()[j].getTypeRent() == "Hotel")
  413. {
  414. //Gajo Bo ; asdasd ; 123456789 ; Macedo crl ; Hotel ; Ibis ; Coimbra ; 1/1/2014 ; 3/3/2014 ; Single Room ; 100 ; 1
  415. //Gajo Bo; asdasd; 123456789; Macedo crl; Hotel; ; Coimbra; 1 / 1 / 2014; 3 / 3 / 2014; ; 100; 1
  416.  
  417.  
  418. cout << "Saving Hotel...\n";
  419. cout << suppliersVec[i].getVector()[j].getName() << " " << suppliersVec[i].getVector()[j].getCity() << endl;
  420. f << "Hotel ; " << suppliersVec[i].getVector()[j].getName() << " ; " << suppliersVec[i].getVector()[j].getCity() << " ; ";
  421. cout << suppliersVec[i].getVector()[j].getDataInicio().getDay() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getMonth() << suppliersVec[i].getVector()[j].getDataInicio().getYear() << endl;
  422. f << suppliersVec[i].getVector()[j].getDataInicio().getDay() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getYear() << " ; ";
  423. f << suppliersVec[i].getVector()[j].getDataFim().getDay() << "/" << suppliersVec[i].getVector()[j].getDataFim().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataFim().getYear() << " ; ";
  424. f << u1.trim(suppliersVec[i].getVector()[j].getType()) << " ; " << suppliersVec[i].getVector()[j].getPrice() << " ; " << suppliersVec[i].getVector()[j].getNumPeople() << "\n";
  425. cout << "Completed!\n";
  426. }
  427.  
  428. if (u1.trim(suppliersVec[i].getVector()[j].getTypeRent()) == "Bed'n'Breakfast")
  429. {
  430. cout << "Saving Bed'n'Breakfast...\n";
  431. f << "Bed'n'Breakfast ; " << u1.trim(suppliersVec[i].getVector()[j].getName()) << " ; " << u1.trim(suppliersVec[i].getVector()[j].getCity()) << " ; ";
  432. f << suppliersVec[i].getVector()[j].getDataInicio().getDay() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getYear() << " ; ";
  433. f << suppliersVec[i].getVector()[j].getDataFim().getDay() << "/" << suppliersVec[i].getVector()[j].getDataFim().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataFim().getYear() << " ; ";
  434. f << suppliersVec[i].getVector()[j].getPrice() << " ; " << suppliersVec[i].getVector()[j].getNumPeople() << "\n";
  435. cout << "Completed!\n";
  436. }
  437. if (u1.trim(suppliersVec[i].getVector()[j].getTypeRent()) == "Shared House")
  438. {
  439. cout << "Saving Shared House...\n";
  440. f << "Shared House ; " << u1.trim(suppliersVec[i].getVector()[j].getName()) << " ; " << u1.trim(suppliersVec[i].getVector()[j].getCity()) << " ; ";
  441. f << suppliersVec[i].getVector()[j].getDataInicio().getDay() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getYear() << " ; ";
  442. f << suppliersVec[i].getVector()[j].getDataFim().getDay() << "/" << suppliersVec[i].getVector()[j].getDataFim().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataFim().getYear() << " ; ";
  443. f << suppliersVec[i].getVector()[j].getPrice() << " ; " << suppliersVec[i].getVector()[j].getNumPeople() << "\n";
  444. cout << "Completed!\n";
  445. }
  446. if (u1.trim(suppliersVec[i].getVector()[j].getTypeRent()) == "Flat")
  447. {
  448. cout << "Saving Flat...\n";
  449. f << "Flat ; " << u1.trim(suppliersVec[i].getVector()[j].getName()) << " ; " << u1.trim(suppliersVec[i].getVector()[j].getCity()) << " ; ";
  450. f << suppliersVec[i].getVector()[j].getDataInicio().getDay() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getYear() << " ; ";
  451. f << suppliersVec[i].getVector()[j].getDataFim().getDay() << "/" << suppliersVec[i].getVector()[j].getDataFim().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataFim().getYear() << " ; ";
  452. f << suppliersVec[i].getVector()[j].getPrice() << " ; " << suppliersVec[i].getVector()[j].getNumPeople() << "\n";
  453. cout << "Completed!\n";
  454. }
  455. if (u1.trim(suppliersVec[i].getVector()[j].getTypeRent()) == "Apartment")
  456. {
  457. cout << "Saving Apartment...\n";
  458. f << "Apartment ; " << u1.trim(suppliersVec[i].getVector()[j].getName()) << " ; " << u1.trim(suppliersVec[i].getVector()[j].getCity()) << " ; ";
  459. f << suppliersVec[i].getVector()[j].getDataInicio().getDay() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataInicio().getYear() << " ; ";
  460. f << suppliersVec[i].getVector()[j].getDataFim().getDay() << "/" << suppliersVec[i].getVector()[j].getDataFim().getMonth() << "/" << suppliersVec[i].getVector()[j].getDataFim().getYear() << " ; ";
  461. f << suppliersVec[i].getVector()[j].getPrice() << " ; " << suppliersVec[i].getVector()[j].getNumPeople() << " ; ";
  462. f << suppliersVec[i].getVector()[j].getNumRooms() << " ; ";
  463. f << suppliersVec[i].getVector()[j].getKitchen() << " ; " << suppliersVec[i].getVector()[j].getSuite() << " ; " << suppliersVec[i].getVector()[j].getLivingRoom() << "\n";
  464. cout << "Completed!\n";
  465. }
  466.  
  467. }
  468. }
  469.  
  470. f.close();
  471.  
  472. remove(suppliersFile.c_str());
  473. rename("tempSuppliers.txt", suppliersFile.c_str());
  474.  
  475. return;
  476. }
  477.  
  478. //Adds a supplier to the suppliers vector
  479. void Corporation::registerSupplier() {
  480.  
  481. string user, password, nif, address;
  482. vector<Rent> rent;
  483.  
  484. cout << "\n Name: "; getline(cin, user);
  485.  
  486. if (cin.eof()) {
  487. u1.cancelMessage();
  488. corpMenu.RegisterMenu();
  489. }
  490.  
  491. for (unsigned int index = 0; index != user.size(); index++) {
  492. if (!isalpha(user.at(index)) && user.at(index) != ' ') {
  493. u1.setColor(12); cerr << " ERROR: Name must only contain alphabetic characters. "; u1.setColor(15);
  494. Sleep(3000);
  495. u1.clearScreen();
  496. corpMenu.RegisterMenu();
  497. }
  498. }
  499.  
  500. for (unsigned int index2 = 0; index2 != suppliersVec.size(); index2++) {
  501. if (suppliersVec.at(index2).getName() == user) {
  502. u1.setColor(12); cerr << " ERROR: The username you selected already exists. Please choose another one. "; u1.setColor(15);
  503. Sleep(3000);
  504. u1.clearScreen();
  505. corpMenu.RegisterMenu();
  506. }
  507. }
  508.  
  509. cout << "\n Password: "; cin >> password;
  510.  
  511. u1.cinClear();
  512.  
  513. for (unsigned int index3 = 0; index3 != password.size(); index3++) {
  514. if (!isalnum(password.at(index3))) {
  515. u1.setColor(12); cerr << " ERROR: Password cannot contain special characters. "; u1.setColor(15);
  516. Sleep(3000);
  517. u1.clearScreen();
  518. corpMenu.RegisterMenu();
  519. }
  520. }
  521.  
  522. cout << "\n NIF: "; cin >> nif;
  523. u1.cinClear();
  524.  
  525. for (unsigned int index4 = 0; index4 != nif.size(); index4++) {
  526. if (!isdigit(nif.at(index4))) {
  527. u1.setColor(12); cerr << " ERROR: NIF can only contain digits. "; u1.setColor(15);
  528. Sleep(3000);
  529. u1.clearScreen();
  530. corpMenu.RegisterMenu();
  531. }
  532. }
  533.  
  534. for (unsigned int index5 = 0; index5 != suppliersVec.size(); index5++) {
  535. if (suppliersVec.at(index5).getNif() == stoi(nif)) {
  536. u1.setColor(12); cerr << " ERROR: The NIF you selected was already found in our system. Probably you already have an account. "; u1.setColor(15);
  537. Sleep(3000);
  538. u1.clearScreen();
  539. corpMenu.RegisterMenu();
  540. }
  541.  
  542. }
  543.  
  544. if (nif.size() != 9) {
  545. u1.setColor(12); cerr << " ERROR: The NIF must have 9 digits. "; u1.setColor(15);
  546. Sleep(3000);
  547. u1.clearScreen();
  548. corpMenu.RegisterMenu();
  549. }
  550.  
  551. if (cin.eof()) {
  552. u1.cancelMessage();
  553. corpMenu.RegisterMenu();
  554. }
  555.  
  556. cout << "\n Address: "; cin >> address;
  557.  
  558. for (unsigned int i = 0; i != suppliersVec.size(); i++) {
  559. if (suppliersVec.at(i).getAddress() == address) {
  560. u1.setColor(12); cerr << " ERROR: The address you selected was already found in our system. Probably you already have an account. "; u1.setColor(15);
  561. Sleep(3000);
  562. u1.clearScreen();
  563. corpMenu.RegisterMenu();
  564. }
  565. }
  566.  
  567. if (cin.eof()) {
  568. u1.cancelMessage();
  569. corpMenu.RegisterMenu();
  570. }
  571.  
  572. suppliersVec.push_back(Supplier(user, password, stoi(nif), address, rent));
  573. u1.clearScreen();
  574. return;
  575. }
  576.  
  577. void Corporation::makeRent() {
  578.  
  579. bool isIn = true;
  580. vector<Rent> v;
  581. int choice;
  582. Date d1, d2;
  583. int numIteration;
  584.  
  585. while (isIn) {
  586.  
  587. if (u1.invalidInputRetry())
  588. continue;
  589.  
  590. if (!u1.invalidInputRetry()) { isIn = false; }
  591.  
  592. else {
  593.  
  594. cout << "\nThe program will now return.\n";
  595. isIn = false;
  596. return;
  597.  
  598. }
  599.  
  600. isIn = false;
  601.  
  602. }
  603.  
  604. isIn = true;
  605. while (isIn) {
  606.  
  607. cout << "\nHow many rents do you wish to be made available?\n";
  608. cin >> numIteration;
  609. if (u1.invalidInputRetry())
  610. continue;
  611. if (!u1.invalidInputRetry()) {
  612. numIteration = 0;
  613. isIn = false;
  614. }
  615. }
  616.  
  617. for (int i = 0; i < numIteration; i++)
  618. {
  619. u1.clearScreen();
  620. cout << "What is the type of rent? \n1 - Hotel\n2 - Bed'n'Breakfast\n3 - Apartment\n4 - Flat\n5 - Shared House";
  621. cin >> choice;
  622. if (u1.invalidInputRetry())
  623. {
  624. i--;
  625. continue;
  626. }
  627. if (!u1.invalidInputRetry())
  628. {
  629. i = numIteration;
  630. break;
  631. }
  632.  
  633. if (choice == 1)
  634. {
  635. Hotel h;
  636. v.push_back(h.buildRent());
  637. }
  638. if (choice == 2)
  639. {
  640. bedNbreakfast bnb;
  641. v.push_back(bnb.buildRent());
  642. }
  643. if (choice == 3)
  644. {
  645. flat fl;
  646. v.push_back(fl.buildRent());
  647. break;
  648. }
  649. if (choice == 4)
  650. {
  651. apartment ap;
  652. v.push_back(ap.buildRent());
  653. break;
  654. }
  655. if (choice == 5)
  656. {
  657. sharedHouse sh;
  658. v.push_back(sh.buildRent());
  659. break;
  660. }
  661. }
  662.  
  663. cout << "\n\nThe program will now return to the main menu.\n\n";
  664. Sleep(2000);
  665.  
  666. return;
  667.  
  668. }
  669.  
  670. //Checks existance of the reservations file
  671. bool Corporation::foundReservationsFile(string reservationsFile)
  672. {
  673. fstream f;
  674.  
  675. f.open(reservationsFile);
  676.  
  677. if (f.fail()) {
  678. f.close();
  679. u1.setColor(12); cerr << "\n ERROR: " << reservationsFile << " (suppliers file) could not be found!\n Verify the directory!\n\n"; u1.setColor(15);
  680. return false;
  681. }
  682.  
  683. f.close();
  684.  
  685. this->reservationsFile = reservationsFile;
  686. return true;
  687. }
  688.  
  689. //Loads the users file to memory (Reservations vector)
  690. /*void Corporation::loadReservations()
  691. {
  692. string name,name_rent,type, type_type, d1, d2;
  693. unsigned int n_people;
  694. Rent *c;
  695. fstream f;
  696. string line;
  697. f.open("reservations.txt");
  698.  
  699. while (!f.eof()) {
  700. getline(f, line);
  701.  
  702. string name= line.substr(0, line.find(" ; "));
  703. line.erase(0, line.find(" ; ") + 3);
  704. string name_rent = line.substr(0, line.find(" ; "));
  705. line.erase(0, line.find(" ; ") + 3);
  706. string type = line.substr(0, line.find(" ; "));
  707. line.erase(0, line.find(" ; ") + 3);
  708. string type_type = line.substr(0, line.find(" ; "));
  709. line.erase(0, line.find(" ; ") + 3);
  710. unsigned int n_people = stoi(line.substr(0, line.find(" ; ")));
  711. line.erase(0, line.find(" ; ") + 3);
  712. string d1 = line.substr(0, line.find(" ; "));
  713. line.erase(0, line.find(" ; ") + 3);
  714. string d2 = line.substr(0, line.length());
  715.  
  716.  
  717. for (vector<Rent>::iterator it=rentsVec.begin(); it != rentsVec.end();it++)
  718. {
  719. if (name_rent == it.getName())
  720. c = *it;
  721. }
  722.  
  723.  
  724. reservationsVec.push_back(Reservation(name, type, type_type, n_people,Date(d1),Date(d2),c));
  725. }
  726. f.close();
  727. }*/
  728.  
  729. /*void Corporation::makeReservation()
  730. {
  731. string city;
  732. cout << "City : ";
  733. getline(cin, city);
  734.  
  735. cout << "List of possibilities : " << endl;
  736. for (int i = 0; i < rentsVec; i++)
  737. {
  738. if(city == rentsVec.at(i).getCity())
  739. cout << " -> " << rentsVec.at(i) << endl;
  740. }
  741.  
  742. string d1, d2,name_rent;
  743. Rent c;
  744.  
  745. cout << " Name of the Rent Place : ";
  746. getline(cin, name_rent);
  747.  
  748. for (int i = 0; i < rentsVec.size(); i++)
  749. {
  750. int n_people;
  751.  
  752. cout << "Number of persons : ";
  753. cin >> n_people;
  754.  
  755. if (n_people > rentsVec.at(i).getNumOcupantes())
  756. {
  757. cout << "Invalid number of persons." << endl;
  758. break;
  759. }
  760.  
  761. if (name_rent == rentsVec.at(i).getName())
  762. {
  763. cout << " Date of check-in : ";
  764. getline(cin, d1);
  765. cout << "Date of check-out : ";
  766. getline(cin, d2);
  767.  
  768. Date d1 = Date(d1);
  769. Date d2 = Date(d2);
  770.  
  771. if (d1 >= rentsVec.at(i).getDataInicio() && d2 <= rentsVec.at(i).getDataFim())
  772. {
  773. string answer;
  774. cout << rentsVEc.at(i) << endl << "Do you want to confirm? ";
  775. if (answer == "yes" || answer == "Yes")
  776. {
  777. reservationsVec.push_back(Reservation(username,))
  778. break;
  779. }
  780. else
  781. continue;
  782. }
  783. else
  784. cout << "invalid period of time" << endl;
  785. }
  786. }
  787.  
  788.  
  789.  
  790. }*/
  791.  
  792.  
  793. /*void Corporation::saveReservations()
  794. {
  795. ofstream f;
  796.  
  797. f.open(reservationsFile, ofstream::app);
  798.  
  799. for (size_t i = 0; i < reservationsVec.size(); i++) {
  800. f << reservationsVec.at(i).getname() << " ; " << reservationsVec.at(i).getrent().getName() << " ; " << reservationsVec.at(i).gettype_rent << " ; " << reservationsVec.at(i).gettype_type_rent() << " ; " << reservationsVec.at(i).getn_people() << " ; " << reservationsVec.at(i).getDate1() << " ; " << reservationsVec.at(i).getDate2();
  801. }
  802.  
  803. f.close();
  804.  
  805. return;
  806. }*/
  807.  
  808.  
  809. /*void Corporation::cancelReservation(string name)
  810. {
  811. #pragma warning(disable : 4996)
  812. time_t ti = time(0);
  813. struct tm * now = localtime(&ti);
  814. unsigned int year = 1900 + now->tm_year, month = 1 + now->tm_mon, day = now->tm_mday;
  815. Date real_date = Date(day, month, year);
  816.  
  817. vector<Reservation>temp;
  818. unsigned int j = 0;
  819. cout << "List of your reservations : " << endl;
  820. for (int i = 0; i < reservationsVec.size(); i++)
  821. {
  822. if (username == reservationsVec.at(i).getname()) {
  823. cout << i + 1 << " -> " << reservationsVec.at(i) << endl;
  824. j++;
  825. temp.push_back(reservationsVec.at(i));
  826. }
  827. }
  828.  
  829. unsigned int n;
  830. cout << " Which one would you like to cancel :";
  831. cin >> n;
  832. if (n > j || n < 0) {
  833. cout << "Invalid Input" << endl;
  834. Sleep(3000);
  835. return;
  836. }
  837.  
  838. string answer;
  839. cout << "Do you want to confirm ? (yes|No)";
  840. getline(cin, answer);
  841. if (answer == "Yes" || answer == "yes")
  842. {
  843. for (int i = 0; i < temp.size(); i++)
  844. {
  845. if ((i + 1) == n) {
  846. Date x = temp.at(i).getDate1().minus(real_date);
  847. if (x.getYear() !=0 || x.getMonth >= 1)
  848. cout << " You will receive " << temp.at(i).getprice() << " euros." << endl;
  849. else if (x.getDay >= 15)
  850. cout << "You will receive " << temp.at(i).getprice() / 2 << " euros." << endl;
  851. else
  852. cout << "You will not receive any money." << endl;
  853. }
  854. }
  855.  
  856. for (int i = 0; i < reservationsVec.size(); i++)
  857. {
  858. if (reservationsVec.at(i) == temp.at(n - 1)) {
  859. reservationsVec.erase(reservationsVec.begin() + i);
  860. cout << "Operation concluded!" << endl;
  861. Sleep(2000);
  862. return;
  863. }
  864. }
  865. }
  866. else
  867. {
  868. cout << "You canceled the operation." << endl;
  869. Sleep(2000);
  870. return;
  871. }
  872.  
  873. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement