-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDB.cpp
More file actions
183 lines (157 loc) · 4.51 KB
/
SDB.cpp
File metadata and controls
183 lines (157 loc) · 4.51 KB
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
#include "SDB.h"
/**
* @brief To check if the identifier is not a duplicate
* @param (id) parameter to pass student id
* @param (list) pointer to struct student
* @retval return 1 if the id is existed and return 0 if the id not found.
*/
static bool ID_CHECK(uint32 id, student *List)
{
bool status=true;
student *TempNode=List;
if(NULL==List)
{
status=false;
}
else
{
while( (NULL!=TempNode) && (id!=TempNode->Student_ID) )
{
TempNode=TempNode->NodeLink;
}
if(NULL == TempNode )
{
status=false;
}
else
{
show_message("Duplicate ID","This ID already exists. Please enter the ID again");
}
}
return status;
}
/**************************************************************************************/
bool SDB_AddEntry(student **List, uint32 id, const char* name, uint32 year) {
student *TempNode = new student();
if (!TempNode) return false;
if (ID_CHECK(id, *List)) {
delete TempNode;
return false;
}
TempNode->Student_ID = id;
strncpy(TempNode->Student_Name, name, MAX_NAME_LENGTH-1);
TempNode->Student_Name[MAX_NAME_LENGTH-1] = '\0';
TempNode->Student_year = year;
TempNode->English = 0;
TempNode->Arabic = 0;
TempNode->Maths = 0;
TempNode->Total = 0;
TempNode->NodeLink = NULL;
if (*List == NULL) {
*List = TempNode;
} else {
student *LastNode = *List;
while (LastNode->NodeLink != NULL) {
LastNode = LastNode->NodeLink;
}
LastNode->NodeLink = TempNode;
}
repo_save(List);
return true;
}
bool SDB_DeletEntry(student **List, uint32 id) {
if (NULL==*List) {
show_message("Error","the data is empty !!!");
return false;
}
student *TempNode = *List;
student *PrevNode = NULL;
while (TempNode != NULL && TempNode->Student_ID != id) {
PrevNode = TempNode;
TempNode = TempNode->NodeLink;
}
if (TempNode == NULL) {
show_message("Error", "Student ID not found!");
return false;
}
if (PrevNode == NULL) {
*List = TempNode->NodeLink;
} else {
PrevNode->NodeLink = TempNode->NodeLink;
}
delete TempNode;
show_message("Success","the data is delete successfully");
repo_save(List);
return true;
}
bool SDB_ReadEntry(student *List, uint32 id, student* result) {
student *TempNode = List;
while (TempNode != NULL) {
if (TempNode->Student_ID == id) {
*result = *TempNode;
return true;
}
TempNode = TempNode->NodeLink;
}
return false;
}
bool SDB_GetList(student *List, uint32* idArray, uint32* count) {
*count = 0;
student *TempNode = List;
while (TempNode != NULL) {
idArray[(*count)++] = TempNode->Student_ID;
TempNode = TempNode->NodeLink;
}
return (*count > 0);
}
bool SDB_IsIdExist(student *List, uint32 id) {
student *TempNode = List;
while (TempNode != NULL) {
if (TempNode->Student_ID == id) {
return true;
}
TempNode = TempNode->NodeLink;
}
return false;
}
void SDB_GetUsedSize(student *List, uint32* size) {
*size = 0;
student *TempNode = List;
while (TempNode != NULL) {
(*size)++;
TempNode = TempNode->NodeLink;
}
}
bool EnterMarks(student **List, uint32 id, uint32 english, uint32 arabic, uint32 maths) {
student *TempNode = *List;
while (TempNode != NULL) {
if (TempNode->Student_ID == id) {
TempNode->English = english;
TempNode->Arabic = arabic;
TempNode->Maths = maths;
TempNode->Total = english + arabic + maths;
repo_save(List);
return true;
}
TempNode = TempNode->NodeLink;
}
return false;
}
bool DisplayMarks(student *List, uint32 id, uint32* english, uint32* arabic,
uint32* maths, uint32* total, char* name, uint32* year) {
student *TempNode = List;
while (TempNode != NULL) {
if (TempNode->Student_ID == id) {
*english = TempNode->English;
*arabic = TempNode->Arabic;
*maths = TempNode->Maths;
*total = TempNode->Total;
strncpy(name, TempNode->Student_Name, MAX_NAME_LENGTH-1);
name[MAX_NAME_LENGTH-1] = '\0';
*year = TempNode->Student_year;
return true;
}
TempNode = TempNode->NodeLink;
}
return false;
}