+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanProject 40c27270

master
5.3 KiB195 linesraw
1// CRM Project4.cpp
2// EE 312 Project 4 submission by
3// Joseph Ryan
4// JBR2558
5// Slip days used: 0
6// Spring 2018
7
8#include <stdio.h>
9#include <assert.h>
10#include "MyString.h"
11#include "Invent.h"
12
13#define MAX_CUSTOMERS 1000
14Customer customers[MAX_CUSTOMERS];
15int num_customers = 0;
16
17/* you'll probably need several more global variables */
18/* and of course, you have a few functions to write */
19int bottles, diapers, rattles;
20
21Customer* mostBottles() {
22    int most = 0;
23    Customer* who = NULL;
24    for(int i = 0; i < num_customers; ++i) {
25        if(customers[i].bottles > most) {
26            who = &customers[i];
27            most = who->bottles;
28        }
29    }
30    return who;
31}
32
33Customer* mostDiapers() {
34    int most = 0;
35    Customer* who = NULL;
36    for(int i = 0; i < num_customers; ++i) {
37        if(customers[i].diapers > most) {
38            who = &customers[i];
39            most = who->diapers;
40        }
41    }
42    return who;
43}
44
45Customer* mostRattles() {
46    int most = 0;
47    Customer* who = NULL;
48    for(int i = 0; i < num_customers; ++i) {
49        if(customers[i].rattles > most) {
50            who = &customers[i];
51            most = who->rattles;
52        }
53    }
54    return who;
55}
56
57/* clear the inventory and reset the customer database to empty */
58void reset(void) {
59    for(int i = 0; i < num_customers; ++i) {
60        StringDestroy(&customers[i].name);
61    }
62    bottles = rattles = diapers = num_customers = 0;
63}
64
65void processSummarize() {
66    printf("There are %d Bottles, %d Diapers and %d Rattles in inventory\n",
67           bottles, diapers, rattles);
68    printf("we have had a total of %d different customers\n", num_customers);
69    if(num_customers) {
70        Customer* most = mostBottles();
71        if(most) {
72            StringPrint(&most->name);
73            printf(" has purchased the most Bottles (%d)\n", most->bottles);
74        } else {
75            printf("no one has purchased any Bottles\n");
76        }
77        most = mostDiapers();
78        if(most) {
79            StringPrint(&most->name);
80            printf(" has purchased the most Diapers (%d)\n", most->diapers);
81        } else {
82            printf("no one has purchased any Diapers\n");
83        }
84        most = mostRattles();
85        if(most) {
86            StringPrint(&most->name);
87            printf(" has purchased the most Rattles (%d)\n", most->rattles);
88        } else {
89            printf("no one has purchased any Rattles\n");
90        }
91    }
92}
93
94Customer* getCustomer(String* name) {
95    Customer* c = NULL;
96    for(int i = 0; i < num_customers; ++i) {
97        if(StringIsEqualTo(name, &customers[i].name)) {
98            c = &customers[i];
99            break;
100        }
101    }
102    if(!c) {
103        c = &customers[num_customers++];
104        c->name = StringDup(name);
105        c->bottles = c->diapers = c->rattles = 0;
106    }
107    return c;
108}
109
110void processPurchase() {
111    String bstr = StringCreate("Bottles");
112    String dstr = StringCreate("Diapers");
113    String rstr = StringCreate("Rattles");
114
115    String name, type;
116    Customer* c = NULL;
117    readString(&name);
118    readString(&type);
119    int ammount;
120    readNum(&ammount);
121
122    if(ammount > 0) {
123        if(StringIsEqualTo(&type, &bstr)) {
124            if(ammount > bottles) {
125                printf("Sorry ");
126                StringPrint(&name);
127                printf(", we only have %d Diapers\n", diapers);
128            } else {
129                c = getCustomer(&name);
130                bottles -= ammount;
131                c->bottles += ammount;
132            }
133        } else if(StringIsEqualTo(&type, &dstr)) {
134            if(ammount > diapers) {
135                printf("Sorry ");
136                StringPrint(&name);
137                printf(", we only have %d Diapers\n", diapers);
138            } else {
139                c = getCustomer(&name);
140                diapers -= ammount;
141                c->diapers += ammount;
142            }
143        } else if(StringIsEqualTo(&type, &rstr)) {
144            if(ammount > rattles) {
145                printf("Sorry ");
146                StringPrint(&name);
147                printf(", we only have %d Rattles\n", rattles);
148            } else {
149                c = getCustomer(&name);
150                rattles -= ammount;
151                c->rattles += ammount;
152            }
153        }
154    }
155
156    StringDestroy(&name);
157    StringDestroy(&type);
158    StringDestroy(&bstr);
159    StringDestroy(&dstr);
160    StringDestroy(&rstr);
161}
162
163void processInventory() {
164    String bstr = StringCreate("Bottles");
165    String dstr = StringCreate("Diapers");
166    String rstr = StringCreate("Rattles");
167
168    String input;
169    readString(&input);
170    int ammount;
171    readNum(&ammount);
172    int* field;
173    if(StringIsEqualTo(&input, &bstr)) {
174        field = &bottles;
175    } else if(StringIsEqualTo(&input, &dstr)) {
176        field = &diapers;
177    } else if(StringIsEqualTo(&input, &rstr)) {
178        field = &rattles;
179    } else {
180        StringDestroy(&input);
181        StringDestroy(&bstr);
182        StringDestroy(&dstr);
183        StringDestroy(&rstr);
184        return;
185    }
186
187    if(ammount > 0) {
188        *field += ammount;
189    }
190
191    StringDestroy(&input);
192    StringDestroy(&bstr);
193    StringDestroy(&dstr);
194    StringDestroy(&rstr);
195}