Advertisement
eerrtt

Untitled

Feb 6th, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <conio.h>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <sstream>
  7. #include <vector>
  8. #include <time.h>
  9. #include <iomanip>
  10.  
  11. using namespace std;
  12.  
  13. const char* DATABASE_FILENAME = "baza.txt";
  14. const string DELIMITER = "|";
  15.  
  16. //dostΔ™pne akcje
  17. const string ACTION_INSERT_ITEM = "dodaj";
  18. const string ACTION_UPDATE_ITEM = "edytuj";
  19. const string ACTION_VIEW_ITEM = "pokaz";
  20. const string ACTION_DELETE_ITEM = "usun";
  21. const string ACTION_LIST_ITEMS = "lista";
  22. const string ACTION_SEARCH_ITEMS = "szukaj";
  23. const string ACTION_EXIT = "zamknij";
  24.  
  25.  
  26. struct item {
  27. int id;
  28. string name;
  29. float quantity;
  30. string unit;
  31. string location;
  32. string add_date;
  33. string del_date;
  34. };
  35.  
  36.  
  37. string structToString(item itemStruct){
  38. stringstream ss;
  39. ss << itemStruct.id << DELIMITER
  40. << itemStruct.name << DELIMITER
  41. << itemStruct.quantity << DELIMITER
  42. << itemStruct.unit << DELIMITER
  43. << itemStruct.location << DELIMITER
  44. << itemStruct.add_date << DELIMITER
  45. << itemStruct.del_date << DELIMITER;
  46.  
  47. string str = ss.str();
  48. cout << str << endl;
  49. return str;
  50. }
  51. bool stringToStruct(istream &in, item &tempItem) {
  52. string temp;
  53. //ID
  54. if (!getline(in, temp, '|')) { return false; }
  55. tempItem.id = atoi(temp.c_str());
  56. //NAZWA
  57. if (!getline(in, temp, '|')) { return false; }
  58. tempItem.name = temp;
  59. //ILOSC
  60. if (!getline(in, temp, '|')) { return false; }
  61. tempItem.quantity = atoi(temp.c_str());
  62. //JEDNOSTKA
  63. if (!getline(in, temp, '|')) { return false; }
  64. tempItem.unit = temp;
  65. //LOKALIZACJA
  66. if (!getline(in, temp, '|')) { return false; }
  67. tempItem.location = temp;
  68. //DATA DODANIA
  69. if (!getline(in, temp, '|')) { return false; }
  70. tempItem.add_date = temp;
  71. //KIEDY USUNAC
  72. if (!getline(in, temp, '|')) { return false; }
  73. tempItem.del_date = temp;
  74.  
  75. return true;
  76. }
  77.  
  78. bool stringToStruct(const string &s, item &tempItem) {
  79. istringstream stream(s);
  80. return stringToStruct(stream, tempItem);
  81. }
  82.  
  83. int getNewItemId(){
  84. int max = 0;
  85. item tempItem;
  86.  
  87. ifstream file(DATABASE_FILENAME, ios::in);
  88. if (!file){
  89. return 1;
  90. }
  91. string line;
  92. while (!file.eof()){
  93. getline(file, line);
  94. if(stringToStruct(line, tempItem) == true){
  95. if(tempItem.id > max)
  96. max = tempItem.id;
  97. }
  98. }
  99.  
  100. file.close();
  101.  
  102. return max + 1;
  103.  
  104. }
  105.  
  106. string getDate(){
  107. time_t currentTime;
  108. struct tm * date;
  109. char currentDate[10];
  110. time(&currentTime);
  111. date = localtime(&currentTime);
  112. strftime(currentDate, 80, "%Y-%m-%d", date);
  113. return currentDate;
  114. }
  115.  
  116. time_t strToTime(string str){
  117. size_t pos = 0;
  118. string delimiter = "-";
  119. string dateParts[3];
  120. int i = 0;
  121. while ((pos = str.find(delimiter)) != string::npos) {
  122. dateParts[i] = str.substr(0, pos);
  123. str.erase(0, pos + delimiter.length());
  124. i++;
  125. }
  126. dateParts[i] = str;
  127.  
  128. tm timeStruct = { 0 };
  129. timeStruct.tm_year = atoi(dateParts[0].c_str()) - 1900;
  130. timeStruct.tm_mon = atoi(dateParts[1].c_str()) - 1;
  131. timeStruct.tm_mday = atoi(dateParts[2].c_str());
  132.  
  133. return mktime(&timeStruct);
  134. }
  135.  
  136.  
  137. void insertItem(item &newItem){
  138. newItem.id = getNewItemId();
  139. newItem.add_date = getDate();
  140. ofstream file(DATABASE_FILENAME, ios::app | ios::out);
  141. file << endl << structToString(newItem);
  142. file.close();
  143. return;
  144. }
  145.  
  146. void updateItem(item itemToUpdate, int position){
  147. vector <string> vec;
  148. ifstream in(DATABASE_FILENAME);
  149. string tmp;
  150.  
  151. while(getline(in, tmp)) vec.push_back(tmp);
  152.  
  153. in.close();
  154.  
  155. ofstream out(DATABASE_FILENAME);
  156. string newLine = structToString(itemToUpdate);
  157.  
  158. for(int i = 0; i < vec.size(); ++i) {
  159. if(i + 1 != position) {
  160. out << vec[i] << endl;
  161. } else {
  162. out << newLine << endl;
  163. }
  164. }
  165. out.close();
  166. }
  167.  
  168. void deleteItem(int position){
  169. vector <string> vec;
  170. ifstream in(DATABASE_FILENAME);
  171. string tmp;
  172.  
  173. while(getline(in, tmp)) vec.push_back(tmp);
  174.  
  175. in.close();
  176.  
  177. ofstream out(DATABASE_FILENAME);
  178.  
  179. for(int i = 0; i < vec.size(); ++i) {
  180. if(i + 1 != position) {
  181. out << vec[i] << endl;
  182. }
  183. }
  184. out.close();
  185. }
  186.  
  187. item getItemById(int id, int &result, int &position){
  188. ifstream file(DATABASE_FILENAME, ios::in);
  189. item tempItem;
  190. result = 0;
  191. position = 1;
  192. string line;
  193. while (!file.eof()){
  194. getline(file, line);
  195. stringToStruct(line, tempItem);
  196.  
  197. if(tempItem.id == id){
  198. result = 1;
  199. break;
  200. }
  201. position++;
  202. }
  203. file.close();
  204.  
  205. return tempItem;
  206. }
  207.  
  208. void form(item &formItem){
  209. cout << "----------------------------------------------------------------" << endl;
  210. cout << "| Podaj nazwe pozycji: ";
  211. cin.ignore();
  212. getline(cin, formItem.name);
  213. cout << "----------------------------------------------------------------" << endl;
  214. cout << "| Podaj ilosc: ";
  215. cin >> formItem.quantity;
  216. cout << "----------------------------------------------------------------" << endl;
  217. cout << "| Podaj jednostke: ";
  218. cin >> formItem.unit;
  219. cout << "----------------------------------------------------------------" << endl;
  220. cout << "| Podaj date usuniecia z listy (format: RRRR-MM-DD): ";
  221. cin >> formItem.del_date;
  222. cout << "----------------------------------------------------------------" << endl;
  223. cout << "| Podaj lokalizacje: ";
  224. cin.ignore();
  225. getline(cin, formItem.location);
  226. cout << "----------------------------------------------------------------" << endl;
  227. }
  228.  
  229. void viewItem(item itemToView){
  230.  
  231. cout << "----------------------------------------------------------------" << endl;
  232. cout << "|" << endl;
  233. cout << "| Szczegoly pozycji " << itemToView.id << endl;
  234. cout << "|" << endl;
  235. cout << "----------------------------------------------------------------" << endl;
  236.  
  237. cout << "----------------------------------------------------------------" << endl;
  238. cout << "| ID: " << itemToView.id << endl;
  239. cout << "----------------------------------------------------------------" << endl;
  240. cout << "| Nazwa: " << itemToView.name << endl;
  241. cout << "----------------------------------------------------------------" << endl;
  242.  
  243. char qty[20];
  244. sprintf(qty, "%4.2f", itemToView.quantity);
  245. cout << "| Ilosc: " << qty << " " << itemToView.unit << endl;
  246. cout << "----------------------------------------------------------------" << endl;
  247. cout << "| Lokalizacja: " << itemToView.location << endl;
  248. cout << "----------------------------------------------------------------" << endl;
  249. cout << "| Dodano: " << itemToView.add_date << endl;
  250. cout << "----------------------------------------------------------------" << endl;
  251. cout << "| Usunac: " << itemToView.del_date << endl;
  252. cout << "----------------------------------------------------------------" << endl;
  253.  
  254. }
  255.  
  256.  
  257. void viewRowHeader(){
  258. cout << fixed << setiosflags(ios::left);
  259. cout << setw(4) << "ID";
  260. cout << setw(40) << "Nazwa";
  261. cout << setw(20) << "Ilosc";
  262. cout << setw(20) << "Lokalizacja";
  263. cout << setw(20) << "Dodano";
  264. cout << endl;
  265. return;
  266. }
  267.  
  268. void viewItemRow(item itemToView){
  269. char qty[20];
  270. sprintf(qty, "%4.2f", itemToView.quantity);
  271. cout << fixed << setiosflags(ios::left);
  272. cout << setw(4) << itemToView.id;
  273. cout << setw(40) << itemToView.name;
  274. string space = " ";
  275. cout << setw(20) << qty + space + itemToView.unit;
  276. cout << setw(20) << itemToView.location;
  277. cout << setw(20) << itemToView.add_date;
  278. cout << endl;
  279. return;
  280. }
  281.  
  282. void actionInsertItem(){
  283. system("cls");
  284. cout << "----------------------------------------------------------------" << endl;
  285. cout << "|" << endl;
  286. cout << "| Tworzenie nowej pozycji" << endl;
  287. cout << "|" << endl;
  288. cout << "----------------------------------------------------------------" << endl;
  289.  
  290. item newItem;
  291. form(newItem);
  292. insertItem(newItem);
  293. system("cls");
  294.  
  295. cout << "----------------------------------------------------------------" << endl;
  296. cout << "|" << endl;
  297. cout << "| Pomyslnie dodano nowa pozycje" << endl;
  298. cout << "|" << endl;
  299. cout << "----------------------------------------------------------------" << endl;
  300.  
  301. viewItem(newItem);
  302. cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
  303. getch();
  304.  
  305. system("cls");
  306. }
  307.  
  308. void actionViewItem(int id){
  309. system("cls");
  310. item itemToView;
  311. int result = 0, position = 0;
  312. itemToView = getItemById(id, result, position);
  313. if(result == 1){ // znaleziono pozycje o wskazanym id
  314. viewItem(itemToView);
  315. } else {
  316. cout << "Nie znaleziono pozycji o wskazanym numerze" << endl;
  317. }
  318.  
  319. cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
  320. getch();
  321.  
  322. system("cls");
  323. return;
  324. }
  325.  
  326. void actionUpdateItem(int id){
  327. system("cls");
  328. cout << "----------------------------------------------------------------" << endl;
  329. cout << "|" << endl;
  330. cout << "| Edycja pozycji " << id << endl;
  331. cout << "|" << endl;
  332. cout << "----------------------------------------------------------------" << endl;
  333. item itemToUpdate;
  334. int result = 0, position = 0;
  335. itemToUpdate = getItemById(id, result, position);
  336.  
  337. if(result == 1){ // znaleziono pozycje o wskazanym id
  338. viewItem(itemToUpdate);
  339. form(itemToUpdate);
  340.  
  341. cout << "----------------------------------------------------------------" << endl;
  342. cout << "|" << endl;
  343. cout << "| Wprowadz dane" << endl;
  344. cout << "|" << endl;
  345. cout << "----------------------------------------------------------------" << endl;
  346.  
  347. updateItem(itemToUpdate, position);
  348. system("cls");
  349.  
  350. cout << "----------------------------------------------------------------" << endl;
  351. cout << "| Pomyslnie zaktualizowano pozycje " << itemToUpdate.id << endl;
  352. cout << "----------------------------------------------------------------" << endl;
  353. } else {
  354. cout << "----------------------------------------------------------------" << endl;
  355. cout << "| Nie znaleziono pozycji o wskazanym numerze " << endl;
  356. cout << "----------------------------------------------------------------" << endl;
  357. }
  358.  
  359. cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
  360. getch();
  361.  
  362. system("cls");
  363. return;
  364. }
  365.  
  366. void actionDeleteItem(int id){
  367. system("cls");
  368. item itemToDelete;
  369. int result = 0, position = 0;
  370. itemToDelete = getItemById(id, result, position);
  371. if(result == 1){ // znaleziono pozycje o wskazanym id
  372. deleteItem(position);
  373. cout << "----------------------------------------------------------------" << endl;
  374. cout << "| Pomyslnie usunieto pozycje " << itemToDelete.id << endl;
  375. cout << "----------------------------------------------------------------" << endl;
  376. } else {
  377. cout << "----------------------------------------------------------------" << endl;
  378. cout << "| Nie znaleziono pozycji o wskazanym numerze " << endl;
  379. cout << "----------------------------------------------------------------" << endl;
  380. }
  381.  
  382. cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
  383. getch();
  384.  
  385. system("cls");
  386. return;
  387. }
  388.  
  389. void actionListItems(){
  390. system("cls");
  391. item tempItem;
  392.  
  393. cout << "----------------------------------------------------------------" << endl;
  394. cout << "| Lista pozycji inwentarza" << endl;
  395. cout << "----------------------------------------------------------------" << endl;
  396.  
  397. ifstream file(DATABASE_FILENAME, ios::in);
  398. if (!file){
  399. cout << "Nieudane otwarcie pliku" << endl;
  400. //exit( 1 );
  401. return;
  402. }
  403.  
  404. string line, temp;
  405. viewRowHeader();
  406. while (file.good()){
  407.  
  408. getline(file, line);
  409. temp = line;
  410. temp.erase(temp.find_last_not_of(" \n\r\t")+1);
  411. if(!temp.empty())
  412. if(stringToStruct(line, tempItem) == true){
  413. viewItemRow(tempItem);
  414. }
  415.  
  416. }
  417.  
  418. file.close();
  419. cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
  420. getch();
  421. system("cls");
  422.  
  423. return;
  424. }
  425.  
  426. void actionSearchItems(){
  427. system("cls");
  428. string query, temp;
  429. string queryParams[10] = {""}; // wieksza aby zmiescic wszystkie parametry razem
  430. string delimiter = " ";
  431. struct search {
  432. string add_date_from, add_date_to, name, location;
  433. } searchParams;
  434.  
  435. cout << "----------------------------------------------------------------" << endl;
  436. cout << "|" << endl;
  437. cout << "| Wyszukaj pozycje" << endl;
  438. cout << "|" << endl;
  439. cout << "----------------------------------------------------------------" << endl;
  440. cout << "| Dostepne kryteria wyszukiwania: " << endl;
  441. cout << "| Nazwa: " << endl;
  442. cout << "| nazwa biurko (nazwa nie moze zawierac spacji)" << endl;
  443. cout << "| Lokalizacja: " << endl;
  444. cout << "| lokalizacja biuro (nazwa nie moze zawierac spacji)" << endl;
  445. cout << "| Od daty dodania: " << endl;
  446. cout << "| od RRRR-MM-DD" << endl;
  447. cout << "| Do daty dodania: " << endl;
  448. cout << "| od RRRR-MM-DD" << endl;
  449. cout << "| Wybrane kryteria mozna laczyc, np.:" << endl;
  450. cout << "| nazwa biurko od 2012-12-05" << endl;
  451. cout << "----------------------------------------------------------------" << endl;
  452. cout << "| Podaj kryteria wyszukiwania: " << endl;
  453. cout << "----------------------------------------------------------------" << endl;
  454. cout << "Szukaj: ";
  455. cin.ignore();
  456. getline(cin, query);
  457. temp = query;
  458. size_t pos = 0;
  459. string param;
  460. int i = 0;
  461.  
  462. while ((pos = query.find(delimiter)) != string::npos) {
  463. queryParams[i] = query.substr(0, pos);
  464. query.erase(0, pos + delimiter.length());
  465. i++;
  466. }
  467. queryParams[i] = query; // w ostatniej iteracji nie ma spacji
  468.  
  469. for(i = 0; i < 10; i++){
  470. cout << queryParams[i] << endl;
  471. if(queryParams[i] == "od"){
  472. searchParams.add_date_from = queryParams[i + 1]; //nastepny element to wartosc dla tego parametru
  473. } else if(queryParams[i] == "do"){
  474. searchParams.add_date_to = queryParams[i + 1];
  475. } else if(queryParams[i] == "lokalizacja"){
  476. searchParams.location = queryParams[i + 1];
  477. } else if(queryParams[i] == "nazwa"){
  478. searchParams.name = queryParams[i + 1];
  479. }
  480. }
  481. system("cls");
  482.  
  483. item tempItem;
  484. cout << "----------------------------------------------------------------" << endl;
  485. cout << "|" << endl;
  486. cout << "| Wyniki wyszukiwania: " << temp << endl;
  487. cout << "|" << endl;
  488. cout << "----------------------------------------------------------------" << endl;
  489.  
  490. ifstream file(DATABASE_FILENAME, ios::in);
  491. if (!file){
  492. cout << "Nieudane otwarcie pliku" << endl;
  493. return;
  494. }
  495.  
  496. string line;
  497. cout << endl;
  498. viewRowHeader();
  499. i = 0;
  500. time_t from, to;
  501. from = strToTime(searchParams.add_date_from);
  502. to = strToTime(searchParams.add_date_to);
  503.  
  504.  
  505. while (file.good()){
  506.  
  507. getline(file, line);
  508. temp = line;
  509. temp.erase(temp.find_last_not_of(" \n\r\t")+1);
  510. if(!temp.empty()){
  511. if(stringToStruct(line, tempItem) == true){
  512. if(searchParams.name != "" && (tempItem.name.find(searchParams.name) == string::npos)){
  513. continue;
  514. }
  515. if(searchParams.location != "" && (tempItem.location.find(searchParams.location) == string::npos)){
  516. continue;
  517. }
  518. if(searchParams.add_date_from != "" && (strToTime(tempItem.add_date) < from)){
  519. continue;
  520. }
  521. if(searchParams.add_date_to != "" && (strToTime(tempItem.add_date) > to)){
  522. continue;
  523. }
  524. viewItemRow(tempItem);
  525. i++;
  526. }
  527. }
  528.  
  529. }
  530.  
  531. file.close();
  532. cout << endl;
  533. cout << "----------------------------------------------------------------" << endl;
  534. cout << "| Znaleziono " << i << " pozycji" << endl;
  535. cout << "----------------------------------------------------------------" << endl;
  536.  
  537. cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
  538. getch();
  539. system("cls");
  540. return;
  541. }
  542.  
  543. void deleteOldItems(){
  544. item tempItem;
  545.  
  546. ifstream file(DATABASE_FILENAME, ios::in);
  547. if (!file){
  548. return;
  549. }
  550.  
  551. string line, temp;
  552. int i = 1;
  553. time_t today;
  554. today = strToTime(getDate());
  555. bool deleted = false;
  556. while (file.good()){
  557. deleted = false;
  558. getline(file, line);
  559. temp = line;
  560. temp.erase(temp.find_last_not_of(" \n\r\t")+1);
  561. if(!temp.empty())
  562. if(stringToStruct(line, tempItem) == true){
  563. cout << strToTime(tempItem.del_date) << endl;
  564. if(strToTime(tempItem.del_date) < today){
  565. cout << "usun" << i << " id " << tempItem.id << endl;
  566. deleteItem(i);
  567. deleted = true;
  568. }
  569. }
  570. if(deleted == false)
  571. i++;
  572.  
  573. }
  574.  
  575. file.close();
  576. return;
  577. }
  578.  
  579. void menu(){
  580.  
  581. string action;
  582. while(1){
  583. cout << "----------------------------------------------------------------" << endl;
  584. cout << "| Inwentarz" << endl;
  585. cout << "| Autor: Rafal Kot" << endl;
  586. cout << "----------------------------------------------------------------" << endl;
  587. cout << "| Wybierz akcje do wykonania:" << endl;
  588. cout << "----------------------------------------------------------------" << endl;
  589. cout << "| dodaj - utworzenie nowej pozycji" << endl;
  590. cout << "----------------------------------------------------------------" << endl;
  591. cout << "| lista - lista wszystkich pozycji" << endl;
  592. cout << "----------------------------------------------------------------" << endl;
  593. cout << "| szukaj - wyszukiwarka pozycji" << endl;
  594. cout << "----------------------------------------------------------------" << endl;
  595. cout << "| pokaz ID - wyswietla pozycje o wskazanym numerze, np. pokaz 23" << endl;
  596. cout << "----------------------------------------------------------------" << endl;
  597. cout << "| edytuj ID - edycja pozycji o wskazanym numerze, np. edytuj 23" << endl;
  598. cout << "----------------------------------------------------------------" << endl;
  599. cout << "| usun ID - usuniecie pozycji o wskazanym numerze, np. usun 23" << endl;
  600. cout << "----------------------------------------------------------------" << endl;
  601. cout << "Akcja: ";
  602. cin >> action;
  603.  
  604. if(action == ACTION_INSERT_ITEM){
  605. actionInsertItem();
  606. } else if(action == ACTION_UPDATE_ITEM){
  607. int id;
  608. cin >> id;
  609. actionUpdateItem(id);
  610. } else if(action == ACTION_VIEW_ITEM){
  611. int id;
  612. cin >> id;
  613. actionViewItem(id);
  614. } else if(action == ACTION_DELETE_ITEM){
  615. int id;
  616. cin >> id;
  617. actionDeleteItem(id);
  618. } else if(action == ACTION_LIST_ITEMS){
  619. actionListItems();
  620. } else if(action == ACTION_SEARCH_ITEMS){
  621. actionSearchItems();
  622. } else if(action == ACTION_EXIT){
  623. break;
  624. }
  625. }
  626. return;
  627. }
  628.  
  629. int main(int argc, char** argv) {
  630.  
  631. deleteOldItems();
  632.  
  633. menu();
  634.  
  635. return 0;
  636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement