-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
98 lines (84 loc) · 2.19 KB
/
vm.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
/*****************************************************************************
* COSC1283/1284 - Programming Techniques
* Semester 2 2011 Assignment #2 - Vending Machine
* Full Name : Alyssa Biasi
* Course Code : COSC1284
* Program Code : BP215
******************************************************************************/
#include "vm.h"
#include "vm_options.h"
#include "vm_utility.h"
int main(int argc, char *argv[])
{
VendingMachineType vm;
int finished = FALSE;
int opt = 0;
int initFlag, dataFlag;
/* Checking arguments. */
if(argc != 3) {
printf("Incorrect number of arguments.\n\n");
exit(EXIT_FAILURE);
}
/* Initialise the vending machine to a safe empty state. */
initFlag = systemInit(&vm);
dataFlag = loadData(&vm, argv[1], argv[2]);
/* Testing to see if both systemInit(.) and loadData(.) are ok */
if (initFlag == FAILURE || dataFlag == FAILURE) {
exit(EXIT_FAILURE);
}
printf("\n");
do {
opt = 0;
printf("Main Menu:\n");
printf("1) Purchase Product\n");
printf("2) Display Products\n");
printf("3) Save and Exit\n");
printf("Supplier-Only Menu:\n");
printf("4) Add Product\n");
printf("5) Remove Product\n");
printf("6) Display Coins\n");
printf("7) Re-stock Products\n");
printf("8) Re-stock Coins\n");
printf("9) Abort\n");
do {
printf("\nSelect your option (1-9): ");
opt = readInt();
switch(opt) {
case 1:
purchaseProduct(&vm);
break;
case 2:
displayProducts(&vm);
break;
case 3:
saveData(&vm, argv[1], argv[2]);
finished = TRUE;
break;
case 4:
addProduct(&vm);
break;
case 5:
removeProduct(&vm);
break;
case 6:
displayCoins(&vm);
break;
case 7:
restockProducts(&vm);
break;
case 8:
restockCoins(&vm);
break;
case 9:
printf("All new data has been lost.\n\n");
finished = TRUE;
break;
default:
break;
}
}while(opt < 1 || opt > 9);
} while(!finished);
/* Deallocate all dynamically allocated memory. */
systemFree(&vm);
exit(EXIT_SUCCESS);
}