Advertisement
Guest User

Untitled

a guest
May 5th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
  2. //
  3.  
  4. #include <fstream>
  5. #include <vector>
  6. #include <iostream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. class User
  11. {
  12. private:
  13. string name;
  14. string password;
  15. bool is_admin;
  16.  
  17. public:
  18. User(string name_a, string password_a, bool admin)
  19. {
  20. name = name_a;
  21. password = password_a;
  22. is_admin = admin;
  23. }
  24.  
  25. void setname(string new_name)
  26. {
  27. name = new_name;
  28. }
  29.  
  30. string getpassword()
  31. {
  32. return password;
  33. }
  34.  
  35. void setpassword(string new_password)
  36. {
  37. password = new_password;
  38. }
  39.  
  40. string getname()
  41. {
  42. return name;
  43. }
  44.  
  45.  
  46. bool getis_admin()
  47. {
  48. return is_admin;
  49. }
  50.  
  51. void setis_admin(bool new_isadmin)
  52. {
  53. is_admin = new_isadmin;
  54. }
  55. };
  56.  
  57. class Part
  58. {
  59. private:
  60. int price;
  61. string manufacture_date;
  62. string expiry_date;
  63. string part_name;
  64. string company_name;
  65.  
  66. public:
  67.  
  68. Part(string part_name_, string company_name_, string manufacture_date_, string expiry_date_, int price_)
  69. {
  70. part_name = part_name_;
  71. company_name = company_name_;
  72. expiry_date = expiry_date_;
  73. manufacture_date = manufacture_date_;
  74. price = price_;
  75. }
  76. void setcompany_name(string new_company_name)
  77. {
  78. company_name = new_company_name;
  79. }
  80.  
  81. string getmanufacture_date()
  82. {
  83. return manufacture_date;
  84. }
  85.  
  86. void setmanufacture_date(string new_manufacture_date)
  87. {
  88. manufacture_date = new_manufacture_date;
  89. }
  90.  
  91. string getexpiry_date()
  92. {
  93. return expiry_date;
  94. }
  95.  
  96. string getpart_name()
  97. {
  98. return part_name;
  99. }
  100.  
  101. void setpart_name(string new_part_name)
  102. {
  103. part_name = new_part_name;
  104. }
  105.  
  106. string getcompany_name()
  107. {
  108. return company_name;
  109. }
  110.  
  111. void setexpiry_date(string new_expiry_date)
  112. {
  113. expiry_date = new_expiry_date;
  114. }
  115.  
  116. int getprice()
  117. {
  118. return price;
  119. }
  120.  
  121. void setprice(int new_price)
  122. {
  123. price = new_price;
  124. }
  125. };
  126.  
  127. void list_parts(vector<Part>& vector_part)
  128. {
  129. cout << endl;
  130. int i = 0;
  131. for (; i < vector_part.size(); i++)
  132. {
  133. cout << endl << "part number: " << i << endl;
  134. cout << "part name : " << vector_part[i].getpart_name() << endl;
  135. cout << "Company name : " << vector_part[i].getcompany_name() << endl;
  136. cout << "Manufacture date : " << vector_part[i].getmanufacture_date() << endl;
  137. cout << "Expiry date : " << vector_part[i].getexpiry_date() << endl;
  138. cout << "Price : " << vector_part[i].getprice() << endl;
  139. }
  140. }
  141.  
  142. void add_user(vector<User>& vector_user)
  143. {
  144. string temp_username, temp_password;
  145. bool temp_admin;
  146. cout << endl;
  147. cout << "username : ";
  148. cin >> temp_username;
  149. cout << "password : ";
  150. cin >> temp_password;
  151. cout << "Admin (1/0) : ";
  152. cin >> temp_admin;
  153. vector_user.push_back(User(temp_username, temp_password, temp_admin));
  154. cout << endl;
  155. }
  156.  
  157.  
  158. void modify_part(vector<Part>& vector_part)
  159. {
  160.  
  161. string p_name, nc_name, nm_date, ne_date;
  162. int p_number, n_price;
  163. cout << endl;
  164. cin >> p_number;
  165. int i = 0;
  166. for (; i < vector_part.size(); i++)
  167. {
  168. if (i == p_number)
  169. {
  170. break;
  171. }
  172. }
  173. if (i == vector_part.size())
  174. {
  175. cout << endl << "part not found." << endl << endl;
  176. }
  177. else
  178. {
  179.  
  180.  
  181. cout << endl << "new part name : ";
  182. cin >> p_name;
  183. cout << "company name : ";
  184. cin >> nc_name;
  185. cout << "manufacture date : ";
  186. cin >> nm_date;
  187. cout << "expiry date : ";
  188. cin >> ne_date;
  189. cout << "price : ";
  190. cin >> n_price;
  191.  
  192. vector_part[i].setpart_name(p_name);
  193.  
  194. vector_part[i].setcompany_name(nc_name);
  195.  
  196. vector_part[i].setmanufacture_date(nm_date);
  197.  
  198. vector_part[i].setexpiry_date(ne_date);
  199.  
  200. vector_part[i].setprice(n_price);
  201. cout << endl;
  202. }
  203.  
  204. }
  205.  
  206. void add_to_cab(vector<pair<int, Part> >& vector_cab, vector<Part>& vector_part)
  207. {
  208. int prt;
  209. int qty;
  210. cout << endl;
  211. cout << "part number : ";
  212. cin >> prt;
  213.  
  214. int i = 0;
  215. for (; i < vector_part.size(); i++)
  216. {
  217. if (prt == i)
  218. {
  219. cout << "quantity : ";
  220. cin >> qty;
  221. vector_cab.push_back(make_pair(qty, vector_part[i]));
  222. break;
  223. }
  224. }
  225. }
  226.  
  227. void display_bill(vector<pair<int, Part> >& vector_cab)
  228. {
  229. int final_bill = 0;
  230. cout << endl;
  231.  
  232. for (int i = 0; i < vector_cab.size(); i++)
  233. {
  234. cout << "unit price : " << vector_cab[i].second.getprice() << endl;
  235. cout << "quantity : " << vector_cab[i].first << endl;
  236. cout << endl << "part name : " << vector_cab[i].second.getpart_name() << endl;
  237. cout << "company name : " << vector_cab[i].second.getcompany_name() << endl;
  238. final_bill += vector_cab[i].first * vector_cab[i].second.getprice();
  239. }
  240. cout << endl << "total price : " << final_bill << endl << endl;
  241. }
  242.  
  243.  
  244. void delete_part(vector<Part>& vector_part)
  245. {
  246. string p_name;
  247. cout << endl;
  248. cout << "part name : ";
  249. cin >> p_name;
  250. int i = 0;
  251. for (; i < vector_part.size(); i++)
  252. {
  253. if (vector_part[i].getpart_name() == p_name)
  254. {
  255. vector_part.erase(vector_part.begin() + i);
  256. break;
  257. }
  258. }
  259. if (i == vector_part.size())
  260. {
  261. cout << endl << "part not found." << endl << endl;
  262. }
  263. }
  264.  
  265. void delete_user(vector<User> & vector_user)
  266. {
  267. string n_name;
  268. cout << endl;
  269. cout << "username : ";
  270. cin >> n_name;
  271. int i = 0;
  272. for (; i < vector_user.size(); i++)
  273. {
  274. if (vector_user[i].getname() == n_name)
  275. {
  276. vector_user.erase(vector_user.begin() + i);
  277. break;
  278. }
  279. }
  280. if (i == vector_user.size())
  281. {
  282. cout << endl << "user not found." << endl << endl;
  283. }
  284. }
  285.  
  286. void modify_user(vector<User> & vector_user)
  287. {
  288. string nu_name, n_name, n_password;
  289. cout << endl;
  290. cout << "username : ";
  291. cin >> n_name;
  292. int i = 0;
  293. for (; i < vector_user.size(); i++)
  294. {
  295. if (vector_user[i].getname() == n_name)
  296. {
  297. break;
  298. }
  299. }
  300. if (i == vector_user.size())
  301. {
  302. cout << endl << "user not found." << endl << endl;
  303. }
  304. else
  305. {
  306.  
  307. cout << endl << "new username : ";
  308. cin >> nu_name;
  309. cout << "new password : ";
  310. cin >> n_password;
  311. vector_user[i].setname(nu_name);
  312. vector_user[i].setpassword(n_password);
  313. cout << endl;
  314. }
  315. }
  316.  
  317.  
  318. void add_part(vector<Part> & vector_part)
  319. {
  320. string p_name, c_name, m_date, e_date;
  321. int price;
  322. cout << endl;
  323. cout << "part name : ";
  324. cin >> p_name;
  325. cout << "company name : ";
  326. cin >> c_name;
  327. cout << "manufacture date : ";
  328. cin >> m_date;
  329. cout << "expiry date : ";
  330. cin >> e_date;
  331. cout << "price : ";
  332. cin >> price;
  333. vector_part.push_back(Part(p_name, c_name, m_date, e_date, price));
  334. cout << endl;
  335. }
  336.  
  337.  
  338. void admin_menu(bool& exit, bool& login, vector<User> & vector_user, vector<Part> & vector_part)
  339. {
  340. while (!exit && login) {
  341. cout << endl << "1) logout" << endl;
  342. cout << "2) exit" << endl;
  343. cout << "3) list parts" << endl;
  344. cout << "4) add new part" << endl;
  345. cout << "5) modify spare part" << endl;
  346. cout << "6) delete spare part" << endl;
  347. cout << "7) add new user" << endl;
  348. cout << "8) modify user" << endl;
  349. cout << "9) delete user" << endl;
  350.  
  351. int choice;
  352. cout << "your choice (integer) : ";
  353. cin >> choice;
  354. cin.clear();
  355. cin.ignore();
  356. switch (choice)
  357. {
  358. case 1:
  359. login = false;
  360. break;
  361. case 2:
  362. exit = true;
  363. break;
  364. case 3:
  365. list_parts(vector_part);
  366. break;
  367. case 4:
  368. add_part(vector_part);
  369. break;
  370. case 5:
  371. modify_part(vector_part);
  372. break;
  373. case 6:
  374. delete_part(vector_part);
  375. break;
  376. case 7:
  377. add_user(vector_user);
  378. break;
  379. case 8:
  380. modify_user(vector_user);
  381. break;
  382. case 9:
  383. delete_user(vector_user);
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. }
  390.  
  391. void customer_menu(bool& exit, bool& login, vector<Part> & vector_part, vector<pair<int, Part> > & vector_cab)
  392. {
  393. while (!exit && login) {
  394. cout << endl << "1) logout" << endl;
  395. cout << "2) exit" << endl;
  396. cout << "3) list parts" << endl;
  397. cout << "4) add a part to the cab" << endl;
  398. cout << "5) display bill" << endl;
  399.  
  400. int choice;
  401. cout << "your choice (integer) : ";
  402. cin >> choice;
  403.  
  404. cout << choice;
  405. cin.clear();
  406. cin.ignore();
  407. switch (choice)
  408. {
  409. case 1:
  410. login = false;
  411. vector_cab.clear();
  412. break;
  413. case 2:
  414. exit = true;
  415. break;
  416. case 3:
  417. list_parts(vector_part);
  418. break;
  419. case 4:
  420. add_to_cab(vector_cab, vector_part);
  421. break;
  422. case 5:
  423. display_bill(vector_cab);
  424. break;
  425. default:
  426. break;
  427. }
  428. }
  429. }
  430.  
  431. void save_user(vector<User> & vector_user)
  432. {
  433. ofstream output("user.txt");
  434. for (int i = 0; i < vector_user.size(); i++)
  435. {
  436. output << vector_user[i].getname() << endl << vector_user[i].getpassword() << endl << boolalpha << vector_user[i].getis_admin() << endl;
  437. }
  438. output.close();
  439. }
  440.  
  441. void save_part(vector<Part> & vector_part)
  442. {
  443. ofstream output("part.txt");
  444. for (int i = 0; i < vector_part.size(); i++)
  445. {
  446. output << vector_part[i].getpart_name() << endl << vector_part[i].getcompany_name() << endl << vector_part[i].getmanufacture_date() << endl << vector_part[i].getexpiry_date() << endl << vector_part[i].getprice() << endl;
  447. }
  448. output.close();
  449. }
  450.  
  451. void load_user(vector<User> & vector_user)
  452. {
  453. ifstream input("user.txt");
  454. string usr, pwd, admin;
  455. while (input >> usr >> pwd >> admin) {
  456. bool admin_bool = !admin.compare("true");
  457. vector_user.push_back(User(usr, pwd, admin_bool));
  458. }
  459. input.close();
  460. }
  461.  
  462. void load_part(vector<Part> & vector_part)
  463. {
  464. ifstream input("part.txt");
  465. string p_name, c_name, m_date, e_date;
  466. int price;
  467. while (input >> p_name >> c_name >> m_date >> e_date >> price)
  468. {
  469. vector_part.push_back(Part(p_name, c_name, m_date, e_date, price));
  470.  
  471. }
  472. input.close();
  473. }
  474.  
  475.  
  476. int loginf(string username, string password, vector<User> vector_user, bool& login)
  477. {
  478. int i = 0;
  479. for (; i < vector_user.size(); i++)
  480. {
  481. if (vector_user[i].getname() == username && vector_user[i].getpassword() == password)
  482. {
  483. cout << "welcome " << username << endl;
  484. login = true;
  485. break;
  486. }
  487. }
  488. if (i == vector_user.size())
  489. cout << "incorrect login !" << endl;
  490.  
  491. return i;
  492. }
  493.  
  494. int main(int argc, char** argv)
  495. {
  496.  
  497. cout << endl << "Hello, please login :" << endl;
  498.  
  499. bool exit = false;
  500. bool login = false;
  501.  
  502. vector<User> vector_user;
  503. vector<Part> vector_part;
  504. vector<pair<int, Part> > vector_cab;
  505.  
  506. load_user(vector_user);
  507. load_part(vector_part);
  508.  
  509. int index = 0;
  510.  
  511. while (!exit) {
  512. while (!login) {
  513. string temp_username;
  514. string temp_password;
  515. cout << endl << "username : ";
  516. cin >> temp_username;
  517. cout << "password : ";
  518. cin >> temp_password;
  519. index = loginf(temp_username, temp_password, vector_user, login);
  520. }
  521.  
  522. if (vector_user[index].getis_admin())
  523. {
  524. admin_menu(exit, login, vector_user, vector_part);
  525. }
  526. else
  527. {
  528. customer_menu(exit, login, vector_part, vector_cab);
  529. }
  530. }
  531. save_user(vector_user);
  532. save_part(vector_part);
  533. return 0;
  534. }
  535.  
  536. // Exécuter le programme : Ctrl+F5 ou menu Déboguer > Exécuter sans débogage
  537. // Déboguer le programme : F5 ou menu Déboguer > Démarrer le débogage
  538.  
  539. // Conseils pour bien démarrer :
  540. // 1. Utilisez la fenêtre Explorateur de solutions pour ajouter des fichiers et les gérer.
  541. // 2. Utilisez la fenêtre Team Explorer pour vous connecter au contrôle de code source.
  542. // 3. Utilisez la fenêtre Sortie pour voir la sortie de la génération et d'autres messages.
  543. // 4. Utilisez la fenêtre Liste d'erreurs pour voir les erreurs.
  544. // 5. Accédez à Projet > Ajouter un nouvel élément pour créer des fichiers de code, ou à Projet > Ajouter un élément existant pour ajouter des fichiers de code existants au projet.
  545. // 6. Pour rouvrir ce projet plus tard, accédez à Fichier > Ouvrir > Projet et sélectionnez le fichier .sln.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement