-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshopping.c
More file actions
213 lines (177 loc) · 5.48 KB
/
shopping.c
File metadata and controls
213 lines (177 loc) · 5.48 KB
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
//Siddharth Vasu
//CPSC 223C, Spring 2023
//Assignment 5
//siddharth.vasu@csu.fullerton.edu
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//Function prototypes
void menu();
void show_inventory(char * inventoryfilename);
void view_cart(char * cartfilename);
void purchase_item(char * inventoryfilename, char * cartfilename);
void prepare_receipt(char * cartfilename, char * receiptfilename);
//Main function
int main(void) {
printf("Welcome to Fullerton Farmer's Market\n");
//Inputting inventory file name
char inventory[64];
printf("Enter name of inventory file: ");
fgets(inventory, 64, stdin);
inventory[strlen(inventory)-1] = '\0';
FILE * inventoryfile = fopen(inventory, "r");
while(inventoryfile == NULL) {
printf("File was not found, try again: ");
fgets(inventory, 64, stdin);
inventory[strlen(inventory)-1] = '\0';
inventoryfile = fopen(inventory, "r");
}
fclose(inventoryfile);
//Initializing the customer plus shopping cart and receipt files
int customer_counter = 1;
char cartfilename[20];
sprintf(cartfilename, "cart%d.txt", customer_counter);
FILE * cartfile = fopen(cartfilename, "w");
fclose(cartfile);
char receiptfilename[20];
strcpy(receiptfilename, "ffm.txt");
FILE * receiptfile = fopen(receiptfilename, "w");
fclose(receiptfile);
char selection = 'R';
//Runs a function when the user makes a valid selection
while (toupper(selection) != 'X') {
switch (toupper(selection)) {
//Repeats the menu
case 'R':
menu();
break;
//Displays the inventory
case 'D':
show_inventory(inventory);
break;
//Views the cart
case 'V':
view_cart(cartfilename);
break;
//Purchases an item from inventory and puts into cart
case 'P':
purchase_item(inventory, cartfilename);
break;
//Handles invalid selections
default:
printf("Invalid selection.");
break;
}
//Prompts the user for a selection
printf("\n\nPlease make a selection: ");
selection = fgetc(stdin);
if (selection == '\n') {
selection = fgetc(stdin);
}
}
//Prepares the receipt, closes program
prepare_receipt(cartfilename, receiptfilename);
printf("\nYour receipt has been generated\n");
printf("Please view your receipt in the file: ffm.txt");
printf("\nThank you for shopping with us!\n");
return 0;
}
//Function implementations
void menu() {
printf("\nHere is our menu:\n");
printf("D = Display list of merchandise in inventory\n");
printf("P = Purchase an item\n");
printf("R = Repeat this menu\n");
printf("V = View shopping cart\n");
printf("X = Exit and generate receipt");
}
void show_inventory(char * inventoryfilename) {
FILE * inventory = fopen(inventoryfilename, "r");
char line[1024];
printf("\n");
while(fgets(line, 1024, inventory) != NULL) {
fputs(line, stdout);
}
fclose(inventory);
}
void view_cart(char * cartfilename) {
FILE * cart = fopen(cartfilename, "r");
char line[1024];
printf("\n");
while (fgets(line, 1024, cart) != NULL) {
fputs(line, stdout);
}
fclose(cart);
}
void purchase_item (char * inventoryfilename, char * cartfilename) {
FILE * inventory = fopen(inventoryfilename, "r");
FILE * cart = fopen(cartfilename, "a");
char item[128];
char avail[128];
int found = 0;
int quantity = 0;
printf("\nEnter the name of the item (must match on the first 8 characters): ");
fgets(item, 128, stdin);
if (item[0] == '\n') {
fgets(item, 128, stdin);
}
item[strlen(item)-1] = '\0';
//Checks inventory for the item the user inputs
while ((fgets(avail, 128, inventory)) != NULL && !found) {
avail[strlen(avail)-1] = '\0';
/*
Compares user input and current inventory item to see
if they match on the first 8 characters
*/
found = (strncmp(item, avail, 8) == 0);
//Case if found, asks for quantity and adds to cart
if (found) {
printf("Please enter quantity: ");
scanf("%d", &quantity);
if (quantity > 0) {
fprintf(cart, "%dx %s\n", quantity, avail);
printf("Item was added to shopping cart");
}
//Doesn't add to cart if input for quantity is invalid
else {
printf("\nInvalid amount");
}
}
}
//Case if the item isn't found
if (!found) {
printf("\nSorry that item is out of stock");
}
fclose(inventory);
fclose(cart);
}
void prepare_receipt(char * cartfilename, char * receiptfilename) {
FILE * cart = fopen(cartfilename, "r");
FILE * checkout = fopen(receiptfilename, "w");
//Stores the current date and time
time_t current_linux_time = time(NULL);
struct tm *broken = localtime(¤t_linux_time);
//Initial receipt dialog with store name, date, and slogan
fprintf(checkout, "Fullerton Farmer's Market ");
fprintf(checkout, "%02d/%02d/%04d\n", broken->tm_mon + 1, broken->tm_mday, broken->tm_year + 1900);
fprintf(checkout, "The customer is always first\n\n");
double total = 0.0;
char temp[128];
char * token;
char * ptr;
long quantity;
//Puts the contents of cart into checkout, adds prices
while (fgets(temp, 128, cart) != NULL) {
fprintf(checkout, "%s", temp);
quantity = strtol(temp, &ptr, 10);
token = strstr(temp, "$");
token++;
total += quantity * strtod(token, &ptr);
}
fprintf(checkout, "\nYour total is: $%1.2lf", total);
fprintf(checkout, "\nThank you for shopping with us!");
fclose(cart);
fclose(checkout);
}