-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
130 lines (106 loc) · 3.46 KB
/
main.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
#include <stdio.h>
#include <string.h>
typedef struct {
char name[16];
float price;
} dish;
struct order {
char name[16];
unsigned long count;
};
// enums create a "constant" (f.e. like const EXIT = 0)
enum command {
UNKNOWN = -1,
EXIT = 0,
ORDER = 1,
CLOSE_ORDER = 2
};
const dish DISHES[] = {
{"sake maki", 5.99f },
{"sake nigiri", 4.99f }
};
void print_welcome() {
puts("\n( ` 3 `)\n Welcome to the fabulous sunshine sushi manager!\n");
puts(" Available commands:");
puts(" exit - exits this program");
puts(" order <number> <dish> - adds a dish to the current order");
puts(" close order - closes the current order and prints the bill");
puts("\n Use Ctrl+C or 'exit' to quit.\n");
}
enum command get_command(char *input) {
if (strstr(input, "exit") == input) {
return EXIT;
}
if (strstr(input, "order") == input) {
return ORDER;
}
if (strstr(input, "close order") == input) {
return CLOSE_ORDER;
}
return UNKNOWN;
}
const dish *get_dish_by_name(char *name) {
int dishes_count = sizeof(DISHES) / sizeof(dish);
for (int i = 0; i < dishes_count; i++) {
if (strcmp(name, DISHES[i].name) == 0) {
return &DISHES[i];
}
}
return NULL;
}
int main() {
setbuf(stdout, 0); // fix to make the input/output work with attached debugger - please ignore this
char input[64] = "";
int order_count = 0;
struct order orders[10] = {};
enum command current_command = EXIT;
int received = 0;
float sum = 0.0f;
print_welcome();
do {
fputs("> ", stdout); // Sends the string "> " to the standard output
fflush(stdout); // Enforces the full output of the buffer
// gets: returns 0 if there occurred an error
if (gets(input) == 0) {
puts("error: input failure.");
}
current_command = get_command(input);
switch (current_command) {
case ORDER:
received = sscanf(input,
"order %lu %15[ a-zA-Z]",
&orders[order_count].count,
orders[order_count].name);
if (received == 2) {
order_count++;
printf("Order position %d added.\n", order_count);
}
break;
case CLOSE_ORDER:
sum = 0.0f;
puts("\nBill\n----");
for (int i = 0; i < order_count; i++) {
const dish *curr_dish = get_dish_by_name(orders[i].name);
if (curr_dish == 0) {
printf("Dish '%s' could not be found. Order discarded.\n", orders[i].name);
order_count = 0;
continue;
}
sum = sum + (float) orders[i].count * curr_dish->price;
printf("%d. %lu x %s: %f EUR\n",
i + 1,
orders[i].count,
orders[i].name,
(float) orders[i].count * curr_dish->price);
}
order_count = 0;
printf("Total: %f EUR\n", sum);
break;
case UNKNOWN:
printf("Unknown command '%s'\n", input);
break;
}
} while (current_command != EXIT);
puts("bye.");
return 0;
}