Skip to content

Commit d9d425a

Browse files
authored
implemented basic database operations
functions added: adding new records, editing existing ones, removing records, searching and sorting by a specific key
1 parent 33d2055 commit d9d425a

File tree

6 files changed

+331
-12
lines changed

6 files changed

+331
-12
lines changed

DB.dbf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
1
2+
1
3+
16 Melon 2.99
4+
0 Carrots 0.99
5+
1 Asparagus 9.00
6+
2 Tomatoes 9.00
7+
3 Parsley 2.25
8+
4 Pepper 1.25
9+
5 Mushrooms 2.25
10+
6 Celery 5.19
11+
7 Artichoke 4.99
12+
8 Beans 1.00
13+
9 Spinach 3.49
14+
10 Beet 1.49
15+
11 Cauliflower 4.25
16+
12 Chayote 1.99
17+
13 Chilis 1.00
18+
14 Cilantro 1.99
19+
15 Cucumber 1.99
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Start testing: Feb 22 11:31 Central European Standard Time
1+
Start testing: Feb 23 12:45 Central European Standard Time
22
----------------------------------------------------------
3-
End testing: Feb 22 11:31 Central European Standard Time
3+
End testing: Feb 23 12:45 Central European Standard Time

cmake-build-debug/cppdb.exe

150 KB
Binary file not shown.

main.cpp

Lines changed: 310 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <iostream>
22
#include <fstream>
3+
#include <cstdio>
4+
#include <algorithm>
35

46
typedef unsigned long long loong;
57
loong currentid, dbsize;
@@ -15,16 +17,23 @@ struct RecordNode {
1517
RecordNode* nextNode;
1618
};
1719

18-
void SaveToFile(RecordNode* &db, Record);
20+
// Operating on files
1921
bool LoadFromFile(RecordNode* &db);
20-
void AddToDatabase(RecordNode* &db, Record);
22+
void SaveToFile(RecordNode* &db);
23+
24+
// Displaying, adding, removing and modifying records
2125
void DisplayDatabase(RecordNode*);
2226
void DisplayRecord(Record);
23-
void FindRecord(RecordNode*);
24-
void SortDatabase(RecordNode* &db, int);
25-
Record CreateNewRecord();
27+
void AddToDatabase(RecordNode* &db, Record);
2628
void EditRecord(RecordNode*, int);
2729
void RemoveRecord(RecordNode* &db, int);
30+
void FindRecord(RecordNode*, int);
31+
void FindRecord(RecordNode*, std::string);
32+
void FindRecord(RecordNode*, float);
33+
void SortDatabase(RecordNode* &db, int);
34+
Record CreateNewRecord();
35+
36+
// Option menus
2837
int Menu();
2938
int SearchMenu();
3039
int SortMenu();
@@ -60,18 +69,80 @@ template <class T> T RequestValueFromUser(std::string message, std::string error
6069
}
6170

6271
int main() {
63-
RecordNode* db = nullptr;
72+
RecordNode* db = NULL;
6473
dbsize = 0;
74+
if (!LoadFromFile(db)) std::cout << "Unable to load a database file. Creating new." << std::endl;
6575
int choice = Menu();
6676
while (choice != 8) {
6777
switch (choice) {
68-
case 1:
78+
case 1: {
6979
DisplayDatabase(db);
7080
break;
71-
case 2:
81+
}
82+
case 2: {
7283
int search_choice = SearchMenu();
84+
std::cin.ignore();
85+
switch (search_choice) {
86+
case 1: {
87+
int id;
88+
std::cout << "ID: " << std::endl;
89+
std::cin >> id;
90+
FindRecord(db, id);
91+
break;
92+
}
93+
case 2: {
94+
std::string name;
95+
std::cout << "Name: " << std::endl;
96+
std::cin >> name;
97+
FindRecord(db, name);
98+
break;
99+
}
100+
case 3: {
101+
float price;
102+
std::cout << "Price: " << std::endl;
103+
std::cin >> price;
104+
FindRecord(db, price);
105+
break;
106+
}
107+
}
108+
break;
109+
}
110+
case 3: {
111+
AddToDatabase(db, CreateNewRecord());
112+
break;
113+
}
114+
case 4: {
115+
int id;
116+
std::cout << "Enter id of the record you wish to modify: ";
117+
std::cin >> id;
118+
EditRecord(db, id);
119+
break;
120+
}
121+
case 5: {
122+
int id;
123+
std::cout << "Enter id of the record you wish to remove: ";
124+
std::cin >> id;
125+
RemoveRecord(db, id);
126+
break;
127+
}
128+
case 6: {
129+
int sort_choice = SortMenu();
130+
SortDatabase(db, sort_choice);
131+
break;
132+
}
133+
case 7: {
134+
SaveToFile(db);
135+
break;
136+
}
73137
}
138+
choice = Menu();
139+
}
140+
while (db != NULL) {
141+
RecordNode* tmp = db->nextNode;
142+
delete db;
143+
db = tmp;
74144
}
145+
return 0;
75146
}
76147

77148
bool LoadFromFile(RecordNode* &db) {
@@ -101,13 +172,234 @@ bool LoadFromFile(RecordNode* &db) {
101172
}
102173
}
103174

175+
176+
void SaveToFile(RecordNode* &db) {
177+
std::ifstream file("DB.dbf");
178+
std::string error;
179+
if (file.good()) {
180+
rename("DB.dbf", "DB.transact.dbf");
181+
file.close();
182+
}
183+
try {
184+
std::ofstream output("DB.dbf", std::ios::trunc);
185+
output << currentid << std::endl << dbsize << std::endl;
186+
RecordNode* tmp = db;
187+
if (tmp != NULL) {
188+
while (tmp != NULL) {
189+
output << tmp->record.id << " " << tmp->record.name << " " << tmp->record.price << std::endl;
190+
tmp = tmp->nextNode;
191+
}
192+
}
193+
output.close();
194+
}
195+
catch (std::string &error) {
196+
std::cout << error << std::endl;
197+
remove("DB.dbf");
198+
rename("DB.transact.dbf", "DB.dbf");
199+
}
200+
remove("DB.transact.dbf");
201+
}
202+
203+
void DisplayDatabase(RecordNode* db) {
204+
int i = 0;
205+
RecordNode* tmp = db;
206+
if (tmp != NULL) {
207+
while (tmp != NULL) {
208+
std::cout << ">> Element number " << i << std::endl;
209+
DisplayRecord(tmp->record);
210+
i++;
211+
tmp = tmp->nextNode;
212+
}
213+
}
214+
else {
215+
std::cout << "Record not found." << std::endl;
216+
}
217+
}
218+
219+
void DisplayRecord(Record record) {
220+
std::cout << "> ID: " << record.id << std::endl;
221+
std::cout << "> Name: " << record.name << std::endl;
222+
std::cout << "> Price: " << record.price << std::endl;
223+
}
224+
225+
void AddToDatabase(RecordNode* &db, Record record) {
226+
RecordNode* tmp = db;
227+
RecordNode* newRecord = new RecordNode;
228+
newRecord->record = record;
229+
newRecord->nextNode = NULL;
230+
if (tmp == NULL) db = newRecord;
231+
else {
232+
while (tmp->nextNode != NULL) tmp = tmp->nextNode;
233+
tmp->nextNode = newRecord;
234+
}
235+
dbsize++;
236+
}
237+
238+
void EditRecord(RecordNode* db, int id) {
239+
RecordNode* tmp = db;
240+
while (tmp != NULL) {
241+
if (tmp->record.id == id) {
242+
int tmpid = id;
243+
tmp->record = CreateNewRecord();
244+
tmp->record.id = tmpid;
245+
return;
246+
}
247+
tmp = tmp->nextNode;
248+
}
249+
std::cout << "Record not found." << std::endl;
250+
}
251+
252+
void RemoveRecord(RecordNode* &db, int id) {
253+
RecordNode* tmp = db;
254+
if (db == NULL) return;
255+
if (db->record.id == id) {
256+
tmp = NULL;
257+
if (db->nextNode != NULL) {
258+
tmp = db->nextNode;
259+
}
260+
delete db;
261+
db = tmp;
262+
dbsize--;
263+
return;
264+
}
265+
if (db != NULL) {
266+
while (tmp->nextNode->record.id != id) {
267+
if (tmp->nextNode == NULL) return;
268+
tmp = tmp->nextNode;
269+
}
270+
RecordNode* temp = tmp->nextNode;
271+
tmp->nextNode = temp->nextNode;
272+
delete temp;
273+
dbsize--;
274+
}
275+
}
276+
277+
void FindRecord(RecordNode* db, int id) {
278+
RecordNode* tmp = db;
279+
if (tmp != NULL) {
280+
while (tmp != NULL) {
281+
if (tmp->record.id == id) {
282+
DisplayRecord(tmp->record);
283+
return;
284+
}
285+
tmp = tmp->nextNode;
286+
}
287+
}
288+
else {
289+
std::cout << "Record not found." << std::endl;
290+
}
291+
}
292+
293+
void FindRecord(RecordNode* db, std::string name) {
294+
RecordNode* tmp = db;
295+
if (tmp != NULL) {
296+
while (tmp != NULL) {
297+
if (tmp->record.name == name) {
298+
DisplayRecord(tmp->record);
299+
return;
300+
}
301+
tmp = tmp->nextNode;
302+
}
303+
}
304+
else {
305+
std::cout << "Record not found." << std::endl;
306+
}
307+
}
308+
309+
void FindRecord(RecordNode* db, float price) {
310+
RecordNode* tmp = db;
311+
if (tmp != NULL) {
312+
while (tmp != NULL) {
313+
if (tmp->record.price == price) {
314+
DisplayRecord(tmp->record);
315+
return;
316+
}
317+
tmp = tmp->nextNode;
318+
}
319+
}
320+
else {
321+
std::cout << "Record not found." << std::endl;
322+
}
323+
}
324+
325+
void SortDatabase(RecordNode* &db, int mode) {
326+
if (db == NULL) {
327+
std::cout << "Unable to sort empty database" << std::endl;
328+
return;
329+
}
330+
Record* table = new Record[dbsize];
331+
RecordNode* tmp = db;
332+
RecordNode* tmp2 = db;
333+
int iterator = 0;
334+
while (tmp != NULL) {
335+
table[iterator] = tmp->record;
336+
tmp2 = tmp->nextNode;
337+
delete tmp;
338+
tmp = tmp2;
339+
iterator++;
340+
}
341+
switch (mode) {
342+
case 1: {
343+
bool p = true;
344+
for (loong i = dbsize - 1; i > 0; i--) {
345+
for (loong j = 0; j < i; j++) {
346+
if (table[j].id > table[j + 1].id) std::swap(table[j], table[j + 1]);
347+
p = false;
348+
}
349+
if (p) break;
350+
}
351+
break;
352+
}
353+
case 2: {
354+
bool p = true;
355+
for (loong i = dbsize - 1; i > 0; i--) {
356+
for (loong j = 0; j < i; j++) {
357+
if (table[j].name > table[j + 1].name) std::swap(table[j], table[j + 1]);
358+
p = false;
359+
}
360+
if (p) break;
361+
}
362+
break;
363+
}
364+
case 3: {
365+
bool p = true;
366+
for (loong i = dbsize - 1; i > 0; i--) {
367+
for (loong j = 0; j < i; j++) {
368+
if (table[j].price > table[j + 1].price) std::swap(table[j], table[j + 1]);
369+
p = false;
370+
}
371+
if (p) break;
372+
}
373+
break;
374+
}
375+
}
376+
db = NULL;
377+
loong temp = dbsize;
378+
dbsize = 0;
379+
for (int i = 0; i < temp; i++) AddToDatabase(db, table[i]);
380+
std::cout << "Database sorted." << std::endl;
381+
return;
382+
}
383+
384+
Record CreateNewRecord() {
385+
Record newRecord;
386+
std::cout << "Please provide information: " << std::endl;
387+
std::cout << "> Name: " << std::endl;
388+
std::cin >> newRecord.name;
389+
newRecord.price = RequestValueFromUser<float>("> Price:", "Enter numeric value!");
390+
std::cout << "Assigned id: " << currentid << std::endl;
391+
newRecord.id = currentid;
392+
currentid++;
393+
return newRecord;
394+
}
395+
104396
int Menu() {
105397
std::cout << "MENU\n"
106398
"1. Display all records in the database\n"
107399
"2. Search for a record\n"
108400
"3. Add a new record\n"
109401
"4. Edit existing record\n"
110-
"5. Delete record\n"
402+
"5. Remove record\n"
111403
"6. Sort database\n"
112404
"7. Save changes\n"
113405
"8. Exit" << std::endl;
@@ -119,5 +411,13 @@ int SearchMenu() {
119411
"1. Search by id\n"
120412
"2. Search by name\n"
121413
"3. Search by price" << std::endl;
122-
return RequestValueFromUser<int>("\nSelect an option: ", "Wrong option! Select a value fromr 1 to 4!");
414+
return RequestValueFromUser<int>("\nSelect an option: ", "Wrong option! Select a value from 1 to 4!");
415+
}
416+
417+
int SortMenu() {
418+
std::cout << "SORT MENU\n"
419+
"1. Sort by id\n"
420+
"2. Sort by name\n"
421+
"3. Sort by price" << std::endl;
422+
return RequestValueFromUser<int>("Select an option: ", "Enter numeric value ", 1, 3);
123423
}

main.exe

159 KB
Binary file not shown.

0 commit comments

Comments
 (0)