1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
    This source code does not compile on Turbo C++. If you are working on that
    compiler, please, use the translate button selecting the option Dynamic array in C.
*/

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#ifdef __linux__
    #include <termios.h>
    #include <unistd.h>
    #define CLEAR_SCREEN "clear"
    int _getch ();
#else
    #include <conio.h>
    #define CLEAR_SCREEN "cls"
#endif
using namespace std;

class Book {
public:
    string ISBN;
    string title;
    string author;
    string publisher;
    int number_of_pages;
    int year;

    Book() {}
    ~Book() {}

    bool operator==(const Book &book) const
    {
        return this==&book || this->ISBN==book.ISBN;
    }

    bool operator!=(const Book &book) const
    {
        return this!=&book && this->ISBN!=book.ISBN;
    }

    bool operator<(const Book &book) const
    {
        return this->ISBN<book.ISBN;
    }

    Book& operator=(const Book &book)
    {
        if (this!=&book)
        {
            this->ISBN = book.ISBN;
            this->title = book.title;
            this->author = book.author;
            this->publisher = book.publisher;
            this->number_of_pages = book.number_of_pages;
            this->year = book.year;
        }
        return *this;
    }

    static void print (Book &book, int *counter);
    static void print_on_file (Book &book, ostream *file_stream);
};

void   pause_screen (const char *message);
string read_str (const char *message);
int    read_integer (const char *message);
bool   read_field (istream &file_stream, char *field, char delimiter);

const char *path = "books.tsv";

int main ()
{
    vector<Book> books;
    vector<Book>::iterator i;
    Book *datum, book;
    int counter=0, main_option, suboption;
    char field[255];
    ifstream file_stream (path);
    if (file_stream!=NULL)
    {
        while (read_field (file_stream, field, '\t'))
        {
            book.ISBN = field;
            read_field (file_stream, field, '\t');
            book.title = field;
            read_field (file_stream, field, '\t');
            book.author = field;
            read_field (file_stream, field, '\t');
            book.publisher = field;
            read_field (file_stream, field, '\t');
            book.number_of_pages = atoi (field);
            read_field (file_stream, field, '\n');
            book.year = atoi (field);
            books.push_back (book);
        }
        file_stream.close();
    }
    do {
        cout << endl;
        system (CLEAR_SCREEN);
        cout << "MENU" << endl;
        cout << "1.- Insertions" << endl;
        cout << "2.- Queries" << endl;
        cout << "3.- Updates" << endl;
        cout << "4.- Deletions" << endl;
        cout << "5.- Sort records" << endl;
        cout << "6.- List records" << endl;
        cout << "7.- Exit" << endl;
        cout << "Select an option: ";
        do
            main_option = _getch ();
        while (main_option<'1' || main_option>'7');
        cout << (char)main_option << endl << endl;
        if (books.empty() && main_option!='1' && main_option!='7')
        {
            pause_screen ("There are not records.\n");
            continue;
        }
        if (main_option<'5')
        {
            book.ISBN = read_str ("Enter the ISBN of the book");
            i = find (books.begin(), books.end(), book);
            datum = i != books.end() ? &books[i - books.begin()] : NULL;
            if (datum!=NULL)
            {
                cout << endl;
                Book::print (*datum, &counter);
            }
        }
        if (main_option=='1' && datum!=NULL)
            cout << "The record already exists." << endl;
        else if (main_option>='2' && main_option<='4' && datum==NULL)
            cout << endl << "Record not found." << endl;
        else switch (main_option)
        {
            case '1':
                book.title = read_str ("Enter the title");
                book.author = read_str ("Enter the author");
                book.publisher = read_str ("Enter the publisher");
                book.number_of_pages = read_integer ("Enter the number of pages");
                book.year = read_integer ("Enter the year");
                books.push_back (book);
                cout << endl << "Record added correctly." << endl;
                break;
            case '3':
                cout << "Menu of fields updating" << endl;
                cout << "1.- title" << endl;
                cout << "2.- author" << endl;
                cout << "3.- publisher" << endl;
                cout << "4.- number of pages" << endl;
                cout << "5.- year" << endl;
                do {
                    suboption = read_integer ("Select a field number to be updated");
                    if (suboption<1 || suboption>5)
                        printf ("Incorrect option.\n");
                } while (suboption<1 || suboption>5);
                switch (suboption)
                {
                    case 1:
                        datum->title = read_str ("Enter the new title");
                        break;
                    case 2:
                        datum->author = read_str ("Enter the new author");
                        break;
                    case 3:
                        datum->publisher = read_str ("Enter the new publisher");
                        break;
                    case 4:
                        datum->number_of_pages = read_integer ("Enter the new number of pages");
                        break;
                    case 5:
                        datum->year = read_integer ("Enter the new year");
                        break;
                }
                cout << endl << "Record updated correctly." << endl;
                break;
            case '4':
                books.erase (find (books.begin(), books.end(), *datum));
                cout << "Record removed correctly." << endl;
                break;
            case '5':
                sort (books.begin(), books.end());
                cout << "Records sorted correctly." << endl;
                break;
            case '6':
                counter = 0;
                for (i=books.begin(); i!=books.end(); i++)
                    Book::print (*i, &counter);
                cout << "Total of records: " << counter << "." << endl;
                break;
        }
        if (main_option!='7')
            pause_screen ("");
    } while (main_option!='7');
    ofstream output_stream (path);
    if (output_stream!=NULL)
    {
        for (i=books.begin(); i!=books.end(); i++)
            Book::print_on_file (*i, &output_stream);
        output_stream.close();
    }
    return EXIT_SUCCESS;
}

void Book::print (Book &book, int *counter)
{
    cout << "ISBN           : " << book.ISBN.c_str() << endl;
    cout << "title          : " << book.title.c_str() << endl;
    cout << "author         : " << book.author.c_str() << endl;
    cout << "publisher      : " << book.publisher.c_str() << endl;
    cout << "number of pages: " << book.number_of_pages << endl;
    cout << "year           : " << book.year << endl;
    cout << endl;
    (*counter)++;
}

void Book::print_on_file (Book &book, ostream *file_stream)
{
    *file_stream << book.ISBN.c_str() << "\t";
    *file_stream << book.title.c_str() << "\t";
    *file_stream << book.author.c_str() << "\t";
    *file_stream << book.publisher.c_str() << "\t";
    *file_stream << book.number_of_pages << "\t";
    *file_stream << book.year << endl;
}

string read_str (const char *message)
{
    char string_var[255];
    cout << message << ": ";
    cin.get (string_var, sizeof (string_var), '\n');
    cin.get();
    string str(string_var);
    return str;
}

int read_integer (const char *message)
{
    int integer_var;
    cout << message << ": ";
    cin >> integer_var;
    cin.get();
    return integer_var;
}

bool read_field (istream &file_stream, char *str, char delimiter)
{
    file_stream.get (str, 255, delimiter);
    file_stream.get();
    return !file_stream.eof();
}

void pause_screen (const char *message)
{
    cout << message << endl << "Press any key to continue . . . ";
    _getch ();
}

#ifdef __linux__

int _getch ()
{
    int ch;
    struct termios oldt, newt;
    tcgetattr (STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr (STDIN_FILENO, TCSANOW, &newt);
    ch = getchar();
    tcsetattr (STDIN_FILENO, TCSANOW, &oldt);
    return ch;
}

#endif