forked from sagelga/ComPro_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.c
252 lines (244 loc) · 11.6 KB
/
category.c
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
// Welcome to the program. The declaration of the functions and the library used is in .h file
#include "main.h"
int categorySelectById (unsigned int id, char *name) {
int numberOfRecords; // Number of the records in a table
numberOfRecords = RecordCount.category;
for ( int i = 0; i < numberOfRecords; i++ ) {
if ( Category[i].id == id ) {
// Return all values back by reference
strcpy (name, Category[i].name);
return 1; // Found a record
}
}
return 0; // Not found the given `id` in the records
}
int categoryInsert (char *name) {
int tailIndex = RecordCount.category;
// To confirm that `name` is unique
for ( int i = 0; i < tailIndex; i++ ) {
if ( strcmp (Category[i].name, name) == 0 )
return 0; // Error: Category name already exists
}
Category[tailIndex].id = tailIndex; // Auto-increment
strcpy (Category[tailIndex].name, name);
RecordCount.category++; // Update the amount of records
categoryFileWrite (); // Save to a Database file
return 1; // Operation Success
}
int categoryUpdateName (unsigned int id, char *name) {
int numberOfRecords; // Number of the records in a table
numberOfRecords = RecordCount.category;
for ( int i = 0; i < numberOfRecords; i++ ) {
if ( Category[i].id == id ) {
strcpy (Category[i].name, name);
categoryFileWrite (); // Save to a Database file
return 1; // Record successfully updated
}
}
return 0; // Not found the given `id` in the records
}
void displayCategory (int page) {
screenClear ();
int allPage = (int) ceil (RecordCount.category / 32) + 1;
bannerFullBorder ();
printf (":: ID | Category Name ::\n");
bannerFullBorder ();
if ( page == allPage ) {
for ( int i = (page - 1) * 32; i < RecordCount.category; ++i )
printf (":: %2u | %-110s ::\n", Category[i].id, Category[i].name);
for ( int i = 0; i < 32 - (RecordCount.category % 32); ++i )
printf (":: | ::\n");
} else {
for ( int i = (page - 1) * 32; i < page * 32/*(32*page)*/; ++i )
printf (":: %2u | %-110s ::\n", Category[i].id, Category[i].name);
}
bannerBlankBorderTextCen ("Enter Page Number | Type 'B' to back");
printf (":: << < ( Page %d of %d ) > >> ::\n",
page, allPage);
bannerFullBorder ();
bannerUserInput ();
}
void categoryDatabaseInterface () {
char handling;
int pageIn = 1, CheckPage;
displayCategory (1);
for ( int i = 0; i >= 0; ++i ) {
scanf (" %c", &handling);
if ((handling == 'B') || (handling == 'b')) {
screenClear ();
inventorySwitchHub ();
} else if ( isdigit (handling)) {
CheckPage = (int) handling - 48;
if ((CheckPage <= ((int) ceil (RecordCount.inventory / 32) + 1)) && (CheckPage >= 1)) {
pageIn = (int) handling - 48;
displayCategory (pageIn);
} else {
displayCategory (pageIn);
printf ("Oops! Page not found, Please enter correct page: ");
}
} else {
displayCategory (pageIn);
printf ("Oops! Input Error, Please enter correctly: ");
}
}
}
void categoryAdd () {
screenClear ();
char nameIN[MAX_LNG_TEXT];
char handling;
bannerFullBorder ();
bannerBlankBorderTextCen ("Insert Category");
bannerFullBorder ();
printf (":: ID | Category Name ::\n");
bannerFullBorder ();
for ( int i = 0; i < RecordCount.category; ++i )
printf (":: %2u | %-110s ::\n", Category[i].id, Category[i].name);
for ( int i = 0; i < (33 - RecordCount.category - 2); ++i )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Type 'Q' to stop | ALTERNATE RESPONSE | Type 'B' to back");
bannerFullBorder ();
for ( int i = 0; i >= 0; ++i ) {
printf ("\n");
printf ("Enter the Category name to add >>> ");
scanf (" %[^\n]", nameIN);
if ((strcmp (nameIN, "B") == 0) || (strcmp (nameIN, "b") == 0)) {
inventorySwitchHub ();
} else if ((strcmp (nameIN, "Q") == 0) || (strcmp (nameIN, "q") == 0)) {
terminate ();
} else {
if ( categoryInsert (nameIN)) {
screenClear ();
bannerFullBorder ();
bannerBlankBorderTextCen ("Insert Category");
bannerFullBorder ();
printf (":: ID | Category Name ::\n");
bannerFullBorder ();
for ( int i = 0; i < RecordCount.category; ++i )
printf (":: %2u | %-110s ::\n", Category[i].id, Category[i].name);
for ( int i = 0; i < (32 - RecordCount.category - 3); ++i )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Successful!!! The category has been inserted.");
bannerBlankBorder ();
bannerBlankBorderTextCen ("Type 'Q' to stop | ALTERNATE RESPONSE | Type 'B' to back");
bannerFullBorder ();
} else {
screenClear ();
bannerFullBorder ();
bannerBlankBorderTextCen ("Insert Category");
bannerFullBorder ();
printf (":: ID | Category Name ::\n");
bannerFullBorder ();
for ( int i = 0; i < RecordCount.category; ++i )
printf (":: %2u | %-110s ::\n", Category[i].id, Category[i].name);
for ( int i = 0; i < 32 - RecordCount.category - 3; ++i )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Error!!! Category name already exists");
bannerBlankBorder ();
bannerBlankBorderTextCen ("Type 'Q' to stop | ALTERNATE RESPONSE | Type 'B' to back");
bannerFullBorder ();
}
}
}
}
void categoryEdit () {
char bufferHead[140];
sprintf (bufferHead, "%-60s|%s", "ID", "CategoryName");
screenClear ();
bannerFullBorder ();
bannerBlankBorderTextCen ("Category Database");
bannerFullBorder ();
bannerBlankBorder ();
bannerBlankBorderTextLeft (bufferHead);
bannerFullBorderSection ();
bannerBlankBorder ();
bannerBlankBorderTextLeft ("->");
bannerBlankBorder ();
bannerFullBorder ();
printf (":: ID | Category Name ::\n");
bannerFullBorder ();
for ( int i = 0; i < RecordCount.category; ++i )
printf (":: %2u | %-110s ::\n", Category[i].id, Category[i].name);
for ( int i = 0; i < (27 - (RecordCount.category - 1 - 4)); i++ )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Please type the Category ID");
bannerBlankBorder ();
bannerBlankBorderTextCen ("Type 'Q' to stop | ALTERNATE RESPONSE | Type 'B' to back");
bannerFullBorder ();
bannerUserInput ();
char id[MAX_LNG_TEXT], name[MAX_LNG_TEXT], buffer[MAX_LNG_SCREEN], flag[MAX_LNG_TEXT];
while ( 1 ) {
scanf ("%s", id);
if ( tolower (id[0]) == 'b' ) {
inventorySwitchHub ();
} else if ( tolower (id[0]) == 'q' ) {
terminate ();
} else {
if ( categorySelectById (strtoul (id, NULL, 10), name)) {
screenClear ();
sprintf (buffer, "%-60s %s", id, name);
bannerFullBorder ();
bannerBlankBorderTextCen ("Category Database");
bannerFullBorder ();
bannerBlankBorder ();
bannerBlankBorderTextLeft (bufferHead);
bannerFullBorderSection ();
bannerBlankBorder ();
bannerBlankBorderTextLeft (buffer);
bannerBlankBorderTextCen (" ");
for ( int i = 0; i < 27; i++ )
bannerBlankBorder ();
bannerBlankBorderTextCen (
"Type another name to change category name... | Press Enter to set by default");
bannerFullBorder ();
bannerUserInput ();
printf ("Default CategoryName: (%s) >>> ", name);
if ( superscanf (flag) != 0 )
categoryUpdateName (strtoul (id, NULL, 10), flag);
screenClear ();
bannerFullBorder ();
bannerBlankBorderTextCen ("Category Database");
bannerFullBorder ();
bannerBlankBorder ();
bannerBlankBorderTextLeft (bufferHead);
bannerFullBorderSection ();
bannerBlankBorder ();
categorySelectById (strtoul (id, NULL, 10), name);
sprintf (buffer, "-> %-57s %s", id, name);
bannerBlankBorderTextLeft (buffer);
bannerBlankBorder ();
bannerFullBorder ();
printf (":: ID | Category Name ::\n");
bannerFullBorder ();
for ( int i = 0; i < RecordCount.category; ++i )
printf (":: %2u | %-110s ::\n", Category[i].id, Category[i].name);
for ( int i = 0; i < 25 - RecordCount.category - 3; i++ )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Category has been updated");
bannerBlankBorderTextCen ("Type Next CategoryID to Update Or Type 'B' to Back");
bannerBlankBorderTextCen ("Type 'Q' to stop | ALTERNATE RESPONSE | Type 'B' to back");
bannerFullBorder ();
bannerUserInput ();
} else {
screenClear ();
bannerFullBorder ();
bannerBlankBorderTextCen ("Category Database");
bannerFullBorder ();
bannerBlankBorder ();
bannerBlankBorderTextLeft (bufferHead);
bannerFullBorderSection ();
bannerBlankBorder ();
bannerBlankBorderTextLeft ("->");
for ( int i = 0; i < 10; i++ )
bannerBlankBorder ();
bannerBlankBorder ();
bannerBlankBorderTextCen ("CategoryID dosen't exist.");
bannerBlankBorderTextCen ("Type Next CategoryID Or Type 'B' to Back");
for ( int i = 0; i < 16; i++ )
bannerBlankBorder ();
bannerBlankBorderTextCen ("Type 'Q' to stop | ALTERNATE RESPONSE | Type 'B' to back");
bannerFullBorder ();
bannerUserInput ();
}
}
}
}