-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.c
174 lines (147 loc) · 4.72 KB
/
product.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
/*
* product.c
*
* functions for inventory product data item
*
* CSCI 352, Spring 2017, Assignment 3
*
* David Bover, May 2017
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "product.h"
#include "tokenizer.h"
// create and return a new product_t data item from the parameters
product_t new_product(char *code,
char *desc,
price_t price,
category_t category,
int stock,
int sales[MONTHS]) {
product_t product;
strncpy(product.code, code, PRODUCT_CODE_LENGTH - 1);
strncpy(product.desc, desc, PRODUCT_DESC_LENGTH - 1);
product.price = price;
product.category = category;
product.stock = stock;
int i;
for (i=0; i<MONTHS; i++)
product.sales[i] = sales[i];
product.sales_updated = 0;
product.next = 0;
return product;
}
// display product details to file fp, preceded by before, followed by after
void write_product(FILE *fp, char *before, product_t product, char *after) {
fprintf(fp, "%s", before);
fprintf(fp, "%s: '%s' ", product.code, product.desc);
write_price(fp, "", product.price, "");
write_category(fp, " ", product.category, "");
fprintf(fp, " %d", product.stock);
int i;
fprintf(fp, " (%d", product.sales[0]);
for (i=1; i<MONTHS; i++)
fprintf(fp, " %d", product.sales[i]);
fprintf(fp, ") next:%d", (int)product.next);
fprintf(fp, "%s", after);
}
// display product category to file fp, preceded by before, followed by after
void write_category(FILE *fp, char *before, category_t category, char *after) {
fprintf(fp, "%s", before);
switch(category) {
case JACKET: fprintf(fp, "Jacket"); break;
case SHOES: fprintf(fp, "Shoes"); break;
case SHIRT: fprintf(fp, "Shirt"); break;
case SWEATER: fprintf(fp, "Sweater"); break;
case PANTS: fprintf(fp, "Pants"); break;
case ACCESSORY: fprintf(fp, "Accessory"); break;
default: fprintf(fp, "Unknown"); break;
}
fprintf(fp, "%s", after);
}
// internal function to determine category type from a string
static category_t category_from_string(char *name) {
if (strcasecmp(name, "JACKET") == 0)
return JACKET;
if (strcasecmp(name, "SHOES") == 0)
return SHOES;
if (strcasecmp(name, "SHIRT") == 0)
return SHIRT;
if (strcasecmp(name, "SWEATER") == 0)
return SWEATER;
if (strcasecmp(name, "PANTS") == 0)
return PANTS;
if (strcasecmp(name, "ACCESSORY") == 0)
return ACCESSORY;
return UNKNOWN;
}
// parse new product line from transaction file
// to extract product data fields, create product_t p
// there are no sales or stock figures
int read_new_product(char *line, product_ptr product){
char *c;
char **tokens;
int dollars, cents;
int i;
// get the product code
c = &(line[0]);
strncpy(product->code, c, PRODUCT_CODE_LENGTH-1);
product->code[PRODUCT_CODE_LENGTH-1] = '\0';
// get the product description
c += PRODUCT_CODE_LENGTH - 1;
strncpy(product->desc, c, PRODUCT_DESC_LENGTH-1);
product->desc[PRODUCT_DESC_LENGTH-1] = '\0';
// tokenize the rest of the line
c += PRODUCT_DESC_LENGTH - 1;
tokens = tokenize(c, " ");
if (token_count(tokens) < 2) return 0;
// get the price dollars and cents
if (sscanf(tokens[0], "%d.%d", &dollars, ¢s) < 2) return 0;
product->price = new_price(dollars, cents, 0);
// get the category, stock and sales for the past MONTHS
product->category = category_from_string(tokens[1]);
// initialize other fields to zero
product->stock = 0;
for (i=0; i<MONTHS; i++)
product->sales[i] = 0;
product->sales_updated = 0;
product->next = 0;
return 1;
}
// parse line from inventory file to extract product data fields, create product_t p
int read_product(char *line, product_ptr product){
char *c;
char **tokens;
int dollars, cents;
int i;
// get the product code
c = &(line[0]);
strncpy(product->code, c, PRODUCT_CODE_LENGTH-1);
product->code[PRODUCT_CODE_LENGTH-1] = '\0';
// get the product description
c += PRODUCT_CODE_LENGTH - 1;
strncpy(product->desc, c, PRODUCT_DESC_LENGTH-1);
product->desc[PRODUCT_DESC_LENGTH-1] = '\0';
// tokenize the rest of the line
c += PRODUCT_DESC_LENGTH - 1;
tokens = tokenize(c, " ");
if (token_count(tokens) < 15) return 0;
// get the price dollars and cents
if (sscanf(tokens[0], "%d.%d", &dollars, ¢s) < 2) return 0;
product->price = new_price(dollars, cents, 0);
// get the category, stock and sales for the past MONTHS
product->category = category_from_string(tokens[1]);
// first check that the remaining tokens are all integers
for (i = 2; i <= 2+MONTHS; i++)
if (strspn(tokens[i], "0123456789") != strlen(tokens[i])) return 0;
product->stock = atoi(tokens[2]);
for (i=0; i<MONTHS; i++){
product->sales[i] = atoi(tokens[3+i]);
}
// initialize other fields to zero
product->sales_updated = 0;
product->next = 0;
return 1;
}