Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <conio.h>
- #include <fstream>
- #include <cstdlib>
- #include <sstream>
- #include <vector>
- #include <time.h>
- #include <iomanip>
- using namespace std;
- const char* DATABASE_FILENAME = "baza.txt";
- const string DELIMITER = "|";
- //dostΔpne akcje
- const string ACTION_INSERT_ITEM = "dodaj";
- const string ACTION_UPDATE_ITEM = "edytuj";
- const string ACTION_VIEW_ITEM = "pokaz";
- const string ACTION_DELETE_ITEM = "usun";
- const string ACTION_LIST_ITEMS = "lista";
- const string ACTION_SEARCH_ITEMS = "szukaj";
- const string ACTION_EXIT = "zamknij";
- struct item {
- int id;
- string name;
- float quantity;
- string unit;
- string location;
- string add_date;
- string del_date;
- };
- string structToString(item itemStruct){
- stringstream ss;
- ss << itemStruct.id << DELIMITER
- << itemStruct.name << DELIMITER
- << itemStruct.quantity << DELIMITER
- << itemStruct.unit << DELIMITER
- << itemStruct.location << DELIMITER
- << itemStruct.add_date << DELIMITER
- << itemStruct.del_date << DELIMITER;
- string str = ss.str();
- cout << str << endl;
- return str;
- }
- bool stringToStruct(istream &in, item &tempItem) {
- string temp;
- //ID
- if (!getline(in, temp, '|')) { return false; }
- tempItem.id = atoi(temp.c_str());
- //NAZWA
- if (!getline(in, temp, '|')) { return false; }
- tempItem.name = temp;
- //ILOSC
- if (!getline(in, temp, '|')) { return false; }
- tempItem.quantity = atoi(temp.c_str());
- //JEDNOSTKA
- if (!getline(in, temp, '|')) { return false; }
- tempItem.unit = temp;
- //LOKALIZACJA
- if (!getline(in, temp, '|')) { return false; }
- tempItem.location = temp;
- //DATA DODANIA
- if (!getline(in, temp, '|')) { return false; }
- tempItem.add_date = temp;
- //KIEDY USUNAC
- if (!getline(in, temp, '|')) { return false; }
- tempItem.del_date = temp;
- return true;
- }
- bool stringToStruct(const string &s, item &tempItem) {
- istringstream stream(s);
- return stringToStruct(stream, tempItem);
- }
- int getNewItemId(){
- int max = 0;
- item tempItem;
- ifstream file(DATABASE_FILENAME, ios::in);
- if (!file){
- return 1;
- }
- string line;
- while (!file.eof()){
- getline(file, line);
- if(stringToStruct(line, tempItem) == true){
- if(tempItem.id > max)
- max = tempItem.id;
- }
- }
- file.close();
- return max + 1;
- }
- string getDate(){
- time_t currentTime;
- struct tm * date;
- char currentDate[10];
- time(¤tTime);
- date = localtime(¤tTime);
- strftime(currentDate, 80, "%Y-%m-%d", date);
- return currentDate;
- }
- time_t strToTime(string str){
- size_t pos = 0;
- string delimiter = "-";
- string dateParts[3];
- int i = 0;
- while ((pos = str.find(delimiter)) != string::npos) {
- dateParts[i] = str.substr(0, pos);
- str.erase(0, pos + delimiter.length());
- i++;
- }
- dateParts[i] = str;
- tm timeStruct = { 0 };
- timeStruct.tm_year = atoi(dateParts[0].c_str()) - 1900;
- timeStruct.tm_mon = atoi(dateParts[1].c_str()) - 1;
- timeStruct.tm_mday = atoi(dateParts[2].c_str());
- return mktime(&timeStruct);
- }
- void insertItem(item &newItem){
- newItem.id = getNewItemId();
- newItem.add_date = getDate();
- ofstream file(DATABASE_FILENAME, ios::app | ios::out);
- file << endl << structToString(newItem);
- file.close();
- return;
- }
- void updateItem(item itemToUpdate, int position){
- vector <string> vec;
- ifstream in(DATABASE_FILENAME);
- string tmp;
- while(getline(in, tmp)) vec.push_back(tmp);
- in.close();
- ofstream out(DATABASE_FILENAME);
- string newLine = structToString(itemToUpdate);
- for(int i = 0; i < vec.size(); ++i) {
- if(i + 1 != position) {
- out << vec[i] << endl;
- } else {
- out << newLine << endl;
- }
- }
- out.close();
- }
- void deleteItem(int position){
- vector <string> vec;
- ifstream in(DATABASE_FILENAME);
- string tmp;
- while(getline(in, tmp)) vec.push_back(tmp);
- in.close();
- ofstream out(DATABASE_FILENAME);
- for(int i = 0; i < vec.size(); ++i) {
- if(i + 1 != position) {
- out << vec[i] << endl;
- }
- }
- out.close();
- }
- item getItemById(int id, int &result, int &position){
- ifstream file(DATABASE_FILENAME, ios::in);
- item tempItem;
- result = 0;
- position = 1;
- string line;
- while (!file.eof()){
- getline(file, line);
- stringToStruct(line, tempItem);
- if(tempItem.id == id){
- result = 1;
- break;
- }
- position++;
- }
- file.close();
- return tempItem;
- }
- void form(item &formItem){
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Podaj nazwe pozycji: ";
- cin.ignore();
- getline(cin, formItem.name);
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Podaj ilosc: ";
- cin >> formItem.quantity;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Podaj jednostke: ";
- cin >> formItem.unit;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Podaj date usuniecia z listy (format: RRRR-MM-DD): ";
- cin >> formItem.del_date;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Podaj lokalizacje: ";
- cin.ignore();
- getline(cin, formItem.location);
- cout << "----------------------------------------------------------------" << endl;
- }
- void viewItem(item itemToView){
- cout << "----------------------------------------------------------------" << endl;
- cout << "|" << endl;
- cout << "| Szczegoly pozycji " << itemToView.id << endl;
- cout << "|" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| ID: " << itemToView.id << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Nazwa: " << itemToView.name << endl;
- cout << "----------------------------------------------------------------" << endl;
- char qty[20];
- sprintf(qty, "%4.2f", itemToView.quantity);
- cout << "| Ilosc: " << qty << " " << itemToView.unit << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Lokalizacja: " << itemToView.location << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Dodano: " << itemToView.add_date << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Usunac: " << itemToView.del_date << endl;
- cout << "----------------------------------------------------------------" << endl;
- }
- void viewRowHeader(){
- cout << fixed << setiosflags(ios::left);
- cout << setw(4) << "ID";
- cout << setw(40) << "Nazwa";
- cout << setw(20) << "Ilosc";
- cout << setw(20) << "Lokalizacja";
- cout << setw(20) << "Dodano";
- cout << endl;
- return;
- }
- void viewItemRow(item itemToView){
- char qty[20];
- sprintf(qty, "%4.2f", itemToView.quantity);
- cout << fixed << setiosflags(ios::left);
- cout << setw(4) << itemToView.id;
- cout << setw(40) << itemToView.name;
- string space = " ";
- cout << setw(20) << qty + space + itemToView.unit;
- cout << setw(20) << itemToView.location;
- cout << setw(20) << itemToView.add_date;
- cout << endl;
- return;
- }
- void actionInsertItem(){
- system("cls");
- cout << "----------------------------------------------------------------" << endl;
- cout << "|" << endl;
- cout << "| Tworzenie nowej pozycji" << endl;
- cout << "|" << endl;
- cout << "----------------------------------------------------------------" << endl;
- item newItem;
- form(newItem);
- insertItem(newItem);
- system("cls");
- cout << "----------------------------------------------------------------" << endl;
- cout << "|" << endl;
- cout << "| Pomyslnie dodano nowa pozycje" << endl;
- cout << "|" << endl;
- cout << "----------------------------------------------------------------" << endl;
- viewItem(newItem);
- cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
- getch();
- system("cls");
- }
- void actionViewItem(int id){
- system("cls");
- item itemToView;
- int result = 0, position = 0;
- itemToView = getItemById(id, result, position);
- if(result == 1){ // znaleziono pozycje o wskazanym id
- viewItem(itemToView);
- } else {
- cout << "Nie znaleziono pozycji o wskazanym numerze" << endl;
- }
- cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
- getch();
- system("cls");
- return;
- }
- void actionUpdateItem(int id){
- system("cls");
- cout << "----------------------------------------------------------------" << endl;
- cout << "|" << endl;
- cout << "| Edycja pozycji " << id << endl;
- cout << "|" << endl;
- cout << "----------------------------------------------------------------" << endl;
- item itemToUpdate;
- int result = 0, position = 0;
- itemToUpdate = getItemById(id, result, position);
- if(result == 1){ // znaleziono pozycje o wskazanym id
- viewItem(itemToUpdate);
- form(itemToUpdate);
- cout << "----------------------------------------------------------------" << endl;
- cout << "|" << endl;
- cout << "| Wprowadz dane" << endl;
- cout << "|" << endl;
- cout << "----------------------------------------------------------------" << endl;
- updateItem(itemToUpdate, position);
- system("cls");
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Pomyslnie zaktualizowano pozycje " << itemToUpdate.id << endl;
- cout << "----------------------------------------------------------------" << endl;
- } else {
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Nie znaleziono pozycji o wskazanym numerze " << endl;
- cout << "----------------------------------------------------------------" << endl;
- }
- cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
- getch();
- system("cls");
- return;
- }
- void actionDeleteItem(int id){
- system("cls");
- item itemToDelete;
- int result = 0, position = 0;
- itemToDelete = getItemById(id, result, position);
- if(result == 1){ // znaleziono pozycje o wskazanym id
- deleteItem(position);
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Pomyslnie usunieto pozycje " << itemToDelete.id << endl;
- cout << "----------------------------------------------------------------" << endl;
- } else {
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Nie znaleziono pozycji o wskazanym numerze " << endl;
- cout << "----------------------------------------------------------------" << endl;
- }
- cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
- getch();
- system("cls");
- return;
- }
- void actionListItems(){
- system("cls");
- item tempItem;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Lista pozycji inwentarza" << endl;
- cout << "----------------------------------------------------------------" << endl;
- ifstream file(DATABASE_FILENAME, ios::in);
- if (!file){
- cout << "Nieudane otwarcie pliku" << endl;
- //exit( 1 );
- return;
- }
- string line, temp;
- viewRowHeader();
- while (file.good()){
- getline(file, line);
- temp = line;
- temp.erase(temp.find_last_not_of(" \n\r\t")+1);
- if(!temp.empty())
- if(stringToStruct(line, tempItem) == true){
- viewItemRow(tempItem);
- }
- }
- file.close();
- cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
- getch();
- system("cls");
- return;
- }
- void actionSearchItems(){
- system("cls");
- string query, temp;
- string queryParams[10] = {""}; // wieksza aby zmiescic wszystkie parametry razem
- string delimiter = " ";
- struct search {
- string add_date_from, add_date_to, name, location;
- } searchParams;
- cout << "----------------------------------------------------------------" << endl;
- cout << "|" << endl;
- cout << "| Wyszukaj pozycje" << endl;
- cout << "|" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Dostepne kryteria wyszukiwania: " << endl;
- cout << "| Nazwa: " << endl;
- cout << "| nazwa biurko (nazwa nie moze zawierac spacji)" << endl;
- cout << "| Lokalizacja: " << endl;
- cout << "| lokalizacja biuro (nazwa nie moze zawierac spacji)" << endl;
- cout << "| Od daty dodania: " << endl;
- cout << "| od RRRR-MM-DD" << endl;
- cout << "| Do daty dodania: " << endl;
- cout << "| od RRRR-MM-DD" << endl;
- cout << "| Wybrane kryteria mozna laczyc, np.:" << endl;
- cout << "| nazwa biurko od 2012-12-05" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Podaj kryteria wyszukiwania: " << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "Szukaj: ";
- cin.ignore();
- getline(cin, query);
- temp = query;
- size_t pos = 0;
- string param;
- int i = 0;
- while ((pos = query.find(delimiter)) != string::npos) {
- queryParams[i] = query.substr(0, pos);
- query.erase(0, pos + delimiter.length());
- i++;
- }
- queryParams[i] = query; // w ostatniej iteracji nie ma spacji
- for(i = 0; i < 10; i++){
- cout << queryParams[i] << endl;
- if(queryParams[i] == "od"){
- searchParams.add_date_from = queryParams[i + 1]; //nastepny element to wartosc dla tego parametru
- } else if(queryParams[i] == "do"){
- searchParams.add_date_to = queryParams[i + 1];
- } else if(queryParams[i] == "lokalizacja"){
- searchParams.location = queryParams[i + 1];
- } else if(queryParams[i] == "nazwa"){
- searchParams.name = queryParams[i + 1];
- }
- }
- system("cls");
- item tempItem;
- cout << "----------------------------------------------------------------" << endl;
- cout << "|" << endl;
- cout << "| Wyniki wyszukiwania: " << temp << endl;
- cout << "|" << endl;
- cout << "----------------------------------------------------------------" << endl;
- ifstream file(DATABASE_FILENAME, ios::in);
- if (!file){
- cout << "Nieudane otwarcie pliku" << endl;
- return;
- }
- string line;
- cout << endl;
- viewRowHeader();
- i = 0;
- time_t from, to;
- from = strToTime(searchParams.add_date_from);
- to = strToTime(searchParams.add_date_to);
- while (file.good()){
- getline(file, line);
- temp = line;
- temp.erase(temp.find_last_not_of(" \n\r\t")+1);
- if(!temp.empty()){
- if(stringToStruct(line, tempItem) == true){
- if(searchParams.name != "" && (tempItem.name.find(searchParams.name) == string::npos)){
- continue;
- }
- if(searchParams.location != "" && (tempItem.location.find(searchParams.location) == string::npos)){
- continue;
- }
- if(searchParams.add_date_from != "" && (strToTime(tempItem.add_date) < from)){
- continue;
- }
- if(searchParams.add_date_to != "" && (strToTime(tempItem.add_date) > to)){
- continue;
- }
- viewItemRow(tempItem);
- i++;
- }
- }
- }
- file.close();
- cout << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Znaleziono " << i << " pozycji" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << endl << "Wcisnij dowolny klawisz aby kontynuowac";
- getch();
- system("cls");
- return;
- }
- void deleteOldItems(){
- item tempItem;
- ifstream file(DATABASE_FILENAME, ios::in);
- if (!file){
- return;
- }
- string line, temp;
- int i = 1;
- time_t today;
- today = strToTime(getDate());
- bool deleted = false;
- while (file.good()){
- deleted = false;
- getline(file, line);
- temp = line;
- temp.erase(temp.find_last_not_of(" \n\r\t")+1);
- if(!temp.empty())
- if(stringToStruct(line, tempItem) == true){
- cout << strToTime(tempItem.del_date) << endl;
- if(strToTime(tempItem.del_date) < today){
- cout << "usun" << i << " id " << tempItem.id << endl;
- deleteItem(i);
- deleted = true;
- }
- }
- if(deleted == false)
- i++;
- }
- file.close();
- return;
- }
- void menu(){
- string action;
- while(1){
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Inwentarz" << endl;
- cout << "| Autor: Rafal Kot" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| Wybierz akcje do wykonania:" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| dodaj - utworzenie nowej pozycji" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| lista - lista wszystkich pozycji" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| szukaj - wyszukiwarka pozycji" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| pokaz ID - wyswietla pozycje o wskazanym numerze, np. pokaz 23" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| edytuj ID - edycja pozycji o wskazanym numerze, np. edytuj 23" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "| usun ID - usuniecie pozycji o wskazanym numerze, np. usun 23" << endl;
- cout << "----------------------------------------------------------------" << endl;
- cout << "Akcja: ";
- cin >> action;
- if(action == ACTION_INSERT_ITEM){
- actionInsertItem();
- } else if(action == ACTION_UPDATE_ITEM){
- int id;
- cin >> id;
- actionUpdateItem(id);
- } else if(action == ACTION_VIEW_ITEM){
- int id;
- cin >> id;
- actionViewItem(id);
- } else if(action == ACTION_DELETE_ITEM){
- int id;
- cin >> id;
- actionDeleteItem(id);
- } else if(action == ACTION_LIST_ITEMS){
- actionListItems();
- } else if(action == ACTION_SEARCH_ITEMS){
- actionSearchItems();
- } else if(action == ACTION_EXIT){
- break;
- }
- }
- return;
- }
- int main(int argc, char** argv) {
- deleteOldItems();
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement