forked from Danielbet21/Amusement_Park
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShop.c
253 lines (201 loc) · 5.18 KB
/
Shop.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
253
#include "Shop.h"
int initShop(Shop* shop, char* name, eShopType type, Time openHour, Time closeHour, int isNameDynamic) {
if (!isValidShop(name, type, openHour, closeHour)) {
return 0;
}
shop->name = name;
shop->type = type;
shop->openHour = openHour;
shop->closeHour = closeHour;
shop->isNameDynamic = isNameDynamic;
return 1;
}
int isValidShop(char* name, eShopType type, Time openHour, Time closeHour) {
// check name is not NULL
if (name == NULL || name == "") {
return 0;
}
// check type si valid
if (type < 0 || type >= eNofShopTypes) {
return 0;
}
// check openHour and closeHour are valid
if (!isValidTime(openHour.hour, openHour.minute) || !isValidTime(closeHour.hour, closeHour.minute)) {
return 0;
}
// check openHour is before closeHour
if (compareTime(&openHour, &closeHour) > 0) {
return 0;
}
return 1;
}
//printf("----------------------------\n");
//printf("Name: %s\nCategory: %s\nHeight Limitation:\n\t-Min Height: %d\n\t-Max Height: %d\n", facility->name, facilityTypeTilte[facility->category], facility->minHeight, facility->maxHeight);
//printf("----------------------------\n");
void printShop(const void* pShop) {
const Shop* shop = (Shop*)pShop;
if (!isValidShop(shop->name, shop->type, shop->openHour, shop->closeHour)) {
printf("Invalid shop\n");
return;
}
printf("----------------------------\n");
printf("Name: %s\nCategory: %s\n", shop->name, ShopTypeStr[shop->type]);
printf("Opening hours: \n");
printf("\tOpen hour: ");
printTime(&shop->openHour);
printf("\n\tClose hour: ");
printTime(&shop->closeHour);
printf("\n");
printf("----------------------------\n");
}
int compareShopsByName(const Shop* shop1, const Shop* shop2) {
return strcmp(shop1->name, shop2->name);
}
void freeShop(void* pShop) {
Shop* shop = (Shop*)pShop;
if (shop->isNameDynamic) {
free(shop->name);
}
}
void printShopTypes() {
for (int i = 0; i < eNofShopTypes; i++) {
printf("%d - %s\n", i + 1, ShopTypeStr[i]);
}
}
void initShopByUser(Shop* shop) {
int flag = 0;
eShopType type = -1;
Time openHour = { -1, 0 };
Time closeHour = { -1, 0 };
char* name = NULL;
do
{
if (flag > 0) {
printf("Invalid shop values, Please try again\n");
free(name);
}
name = getStrExactName("Please enter shop name: ");
printf("Please enter shop type:\n");
printShopTypes();
scanf("%d", &type);
printf("Please enter shop open time\n");
initTimeByUser(&openHour);
printf("Please enter shop close time\n");
initTimeByUser(&closeHour);
flag = 1;
} while (!initShop(shop, name, type - 1, openHour, closeHour, 1));
}
// save and load functions
int saveShopToTextFile(const Shop* shop, FILE* fp) {
IS_FILE_NULL(fp);
if (!isValidShop(shop->name, shop->type, shop->openHour, shop->closeHour)) {
return 0;
}
// save name to file
if (writeStringToTextFile(fp, shop->name) == 0) {
return 0;
}
// save shop type
if (writeIntToTextFile(fp, shop->type) == 0) {
return 0;
}
if (saveTimeToTextFile(&shop->openHour, fp) == 0) {
return 0;
}
if (saveTimeToTextFile(&shop->closeHour, fp) == 0) {
return 0;
}
return 1;
}
int loadShopFromTextFile(Shop* shop, FILE* fp) {
IS_FILE_NULL(fp);
char buffer[MAX_BUFFER_SIZE];
// read name from file
char* bufferName = readStringFromTextFile(fp, buffer, MAX_BUFFER_SIZE);
if (bufferName == NULL) {
return 0;
}
// read shop type
int intTempType;
if (readIntFromTextFile(fp, &intTempType) == 0) {
return 0;
}
eShopType tempType = intTempType;
// read open hour and close hour
Time tempOpenHour;
if (loadTimeFromTextFile(&tempOpenHour, fp) == 0) {
return 0;
}
// read close hour
Time tempCloseHour;
if (loadTimeFromTextFile(&tempCloseHour, fp) == 0) {
return 0;
}
// aloocate memory for shop name
char* tempName = (char*)malloc(strlen(bufferName) + 1);
if (tempName == NULL) {
return 0;
}
strcpy(tempName, bufferName);
// init shop
if (!initShop(shop, tempName, tempType, tempOpenHour, tempCloseHour, 1)) {
free(tempName);
return 0;
}
return 1;
}
int saveShopToBinFile(const Shop* shop, FILE* fp) {
IS_FILE_NULL(fp);
if (!isValidShop(shop->name, shop->type, shop->openHour, shop->closeHour)) {
return 0;
}
// save name to file
if (writeStringTobinFile(fp, shop->name) == 0) {
return 0;
}
// save shop type
if (writeGeneralToBinFile(fp, &shop->type, sizeof(eShopType)) == 0) {
return 0;
}
// save open hour
if (saveTimeToBinFile(&shop->openHour, fp) == 0) {
return 0;
}
// save close hour
if (saveTimeToBinFile(&shop->closeHour, fp) == 0) {
return 0;
}
return 1;
}
int loadShopFromBinFile(Shop* shop, FILE* fp) {
IS_FILE_NULL(fp);
// read name from file
char* tempName = readStringFromBinFile(fp);
if (tempName == NULL) {
return 0;
}
// read shop type
eShopType tempType;
if (readGeneralFromBinFile(fp, &tempType, sizeof(eShopType)) == 0) {
free(tempName);
return 0;
}
// read open hour
Time tempOpenHour;
if (loadTimeFromBinFile(&tempOpenHour, fp) == 0) {
free(tempName);
return 0;
}
// read close hour
Time tempCloseHour;
if (loadTimeFromBinFile(&tempCloseHour, fp) == 0) {
free(tempName);
return 0;
}
// init shop
if (!initShop(shop, tempName, tempType, tempOpenHour, tempCloseHour, 1)) {
free(tempName);
return 0;
}
return 1;
}