-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlager.c
430 lines (354 loc) · 10.3 KB
/
lager.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tree.h"
#include "list.h"
#include "dblib.h"
struct goods
{
char *Name;
char *Desc;
int Price;
list_t *shelves; //pekar inte på shelf, utan på listan som innehåller alla shelves.
};
struct have_tree
{
tree_t *old; //Trädet innan ändring
tree_t *new; // Trädet efter ändring. Undo gör att *new = *old.
};
struct action
{
int type; // NOTHING = 0, ADD = 1, REMOVE = 2, EDIT = 3
goods_t *orig;
goods_t *copy;
};
typedef struct action action_t;
typedef void(goods_func)(goods_t *elem, action_t *undo);
#define QUIT 'Q'
#define LIST 'L'
#define LEFT 'L'
#define SHELVES 'L'
#define UNDO 'U'
#define PRODUCT 'U'
#define REMOVE 'R'
#define RIGHT 'R'
#define PRICE 'R'
#define ADD 'A'
#define AMOUNT 'A'
#define EDIT 'E'
#define EXIT 'E'
#define DESCRIPTION 'E'
//------------------------------- All functions in this file: -----------------------
void add_goods(tree_t *tree);
shelf_t *make_shelf(int amount, char* hylla);
char *ask_question_key(char *question, tree_t *tree);
goods_t *new_goods(char *name, char *desc, int price, list_t *shelves);
void list_goods(tree_t *tree, goods_func, action_t *undo);
void list_shelves (goods_t *goods);
void show_goods(tree_t *tree, K name);
void index_action(tree_t *tree, int page, goods_func function, action_t *undo);
goods_t *copy_goods(goods_t *goods);
// ------------------------------list good ---------------------------------------
void list_shelves (goods_t *goods)
{
list_t *shelves = goods->shelves;
int length = list_length(shelves);
for (int i = 0; i != length; i++)
{
shelf_t *cur_shelf = (shelf_t *) list_get(shelves, i);
printf("%d. Shelf: %s Amount: %d \n",(i+1), cur_shelf->Hylla, cur_shelf->Antal);
}
return;
}
void show_goods(tree_t *tree, K name)
{
goods_t *goodet = (goods_t *) tree_get(tree, name);
// TODO: gör om picked_goods till en goods_t
printf("Name: %s \n", goodet->Name);
printf("Description: %s \n", goodet->Desc);
printf("Price: %d \n", goodet->Price);
list_shelves(goodet);
}
//-------------------------------Add goods ----------------------------
list_t *make_shelves_list(int shelf_amount)
{
list_t *new_l = list_new();
while (shelf_amount != 0)
{
char *hylla = ask_question_hylla("Place goods on what shelf?\n");
int amount = ask_question_int("Amount of goods on this shelf?\n");
shelf_t *new_shelf = make_shelf(amount, hylla);
list_prepend(new_l, new_shelf);
--shelf_amount;
}
return new_l;
}
void add_goods(tree_t *tree)
{
K name = ask_question_key("Name goods\n", tree); //två objekt får ej ha samma namn.
char *desc = ask_question_string("Describe goods\n");
int price = ask_question_int("Price of goods\n");
int hyllor = ask_question_int("place on how many shelves?");
list_t *shelves_list = make_shelves_list(hyllor);
goods_t *new_good = new_goods(name, desc, price, shelves_list);
tree_insert(tree, name, new_good);
return;
}
shelf_t *make_shelf(int amount, char* hylla) //Måste frigöras!
{
shelf_t *new_shelf = calloc(1, sizeof(shelf_t));
new_shelf->Hylla = hylla;
new_shelf->Antal = amount;
return new_shelf;
}
goods_t *new_goods(K name, char *desc, int price, list_t *shelves)
{
goods_t *new_g = calloc(1, sizeof(goods_t));
new_g->Name = name;
new_g->Desc = desc;
new_g->Price = price;
new_g->shelves = shelves; // TODO - kan inte placera varor på tagna hyllor.
return new_g;
}
//----------------------------------------------------
char *ask_question_key(char *question, tree_t *tree)
{
K answer;
answer = ask_question_string(question);
bool already_key = tree_has_key(tree, answer);
if (already_key == false)
{
return answer;
}
else
{
printf("\'%s\' already exists. Choose a different name!\n", answer);
return ask_question_key(question, tree);
}
}
//------------------------------------------------------------------------------------
void remove_goods(tree_t *tree);
//--------------------------------- edit goods ---------------------------------------
void edit_shelves(goods_t *vara)
{
int index = ask_question_int("Choose index of shelf name to edit!\n");
shelf_t *shelf = (shelf_t *) list_get(vara->shelves, (index-1));
char *hylla = ask_question_hylla("What is the new shelf?\n");
shelf->Hylla = hylla;
}
void edit_shelves_amount(goods_t *vara)
{
int index = ask_question_int("Choose index of shelf amount to edit!\n");
shelf_t *shelf = (shelf_t *) list_get(vara->shelves, (index-1));
int amount = ask_question_int("What is the new amount on shelf?\n");
shelf->Antal = amount;
}
void edit_goods(goods_t *vara, action_t *undo)
{
undo->type = 3;
undo->copy = copy_goods(vara);
undo->orig = vara;
while (true)
{
char choice = ask_question_menu("\nWhat do you want to edit?\n"
"D[E]scription\n"
"P[R]ice\n"
"She[L}ves\n"
"[A]mount\n"
"[Q]uit from editor\n");
switch(choice)
{
case DESCRIPTION:
printf("Current description: %s\n", vara->Desc);
puts("\n-----------------------------------------\n");
char *new_desc = ask_question_string("New description:\n");
vara->Desc = new_desc;
break;
case PRICE:
printf("Curren price: %d\n", vara->Price);
puts("\n-----------------------------------------\n");
int new_price = ask_question_int("New price:\n");
vara->Price = new_price;
break;
case SHELVES:
list_shelves(vara);
edit_shelves(vara);
break;
case AMOUNT:
list_shelves(vara);
edit_shelves_amount(vara);
case QUIT:
return;
}
}
return;
}
// ----------------------------------list goods ---------------------------------------
void does_nothing(goods_t *elem, action_t *undo)
{
return;
}
void list_helper(tree_t *tree, int page) //20 per page. page index from 0. //
{
K *buf = tree_keys(tree);
int goods_amount = tree_size(tree);
int i = 0 + 20 * page;
for (; i != goods_amount && i < (20 + 20 * page); i++)
{
K current = buf[i];
printf("\n%d.%s\n",(i%20)+1, current);
}
return;
}
K index_to_key(tree_t *tree, int index, int page)//tar in korrekt index, inte vad väljaren skriver!!
{
K *buf = tree_keys(tree);
int goods_amount = tree_size(tree);
index = index + 20 * page;
if (index < goods_amount)
{
K choice = buf[index]; //-1 as list is indexed from 1, not 0.
return choice;
}
else
{
puts("Your number is out of bounds, please choose a nr from available goods");
return NULL;
}
}
void index_action(tree_t *tree, int page, goods_func function, action_t *undo)
{
int num = ask_question_int("Choose product nr:\n");
if (tree_size(tree) == 0)
{
printf("\nThe catalogue is Empty, please add a product and try again\n");
return;
}
K key_name = index_to_key(tree, (num-1), page);
show_goods(tree, key_name);
goods_t *elem = (goods_t *) tree_get(tree, key_name);
function(elem, undo);
}
void list_goods(tree_t *tree, goods_func function, action_t *undo)
{
int tot_pages = tree_size(tree) / 20 + (tree_size(tree)%20 > 0 ? 1 : 0); // amount of pages as needed + 1 page if amount is not devisable by 20.
int first_page = 0;
int *cur_page = &first_page;
list_helper(tree, *cur_page);
while(true)
{
printf("\nTotalt antal varor: %d\n", tree_size(tree));
char choice = ask_question_menu("\n\n[L]eft <-\t"
"Choose prod[u]t"
"-> [R]ight\t"
"[E]xit\n");
switch(choice)
{
case RIGHT:
(*cur_page)++;
if (*cur_page < tot_pages)
{
list_helper(tree, *cur_page);
}
else
{
(*cur_page)--;
puts("You have come to the end of the list");
list_helper(tree, *cur_page);
}
break;
case LEFT:
(*cur_page)--;
if (*cur_page >= 0)
{
list_helper(tree, *cur_page);
}
else
{
(*cur_page)++;
puts("You are at the beginning of the list");
list_helper(tree, *cur_page);
}
break;
case PRODUCT:
index_action(tree, *cur_page, function, undo);
list_helper(tree, *cur_page);
break;
case EXIT:
return;
}
}
return;
}
//----------------------------------undo action ---------------------------------------
void undo_action(tree_t *tree, action_t *action)
{
if (action->type == 3)
{
*action->orig = *action->copy;
puts("\nYour editing is undone!\n");
}
else
{
puts("There is nothing to undo\n");
}
action->type = 0;
}
goods_t *copy_goods(goods_t *goods)
{
char *name = goods->Name;
char *desc = goods->Desc;
int pris = goods->Price;
list_t *shelf = goods->shelves;
goods_t *copy_g = new_goods(name, desc, pris, shelf);
return copy_g;
}
//-------------------------------------------------------------------------------------
void exit_program()
{
puts("Thank you, and goodbye!");
}
//------------------------------------------------------------------------------------
int event_loop(tree_t *tree)
{
action_t undo = (action_t) {.type = 0};
while (true)
{
char choice = ask_question_menu("\n[A]dd Item to database\n"
"[R]emove item from database\n"
"[E]dit database\n"
"[U]ndo last change\n"
"[L]ist database\n"
"[Q]uit\n\n"
"What would you like to do today?\n");
switch(choice)
{
case ADD:
add_goods(tree);
break;
case REMOVE:
//remove_goods();
break;
case EDIT:
list_goods(tree, edit_goods, &undo);
break;
case UNDO:
undo_action(tree, &undo);
break;
case LIST:
list_goods(tree, does_nothing, &undo);
break;
case QUIT:
//puts("Bye bye");
return 0;
break;
}
}
return 0;
}
int main()
{
tree_t *tree = tree_new();
event_loop(tree);
return 0;
}