+/EE312

software design

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

Joseph RyanProject 76241299

master
4.2 KiB156 linesraw
1/*
2 * Name: Joseph Ryan
3 * EID: jbr2558
4 * Project 7, CRM part 2
5 * EE 312, Spring 2017
6 */
7
8#include <stdio.h>
9#include <iostream>
10#include "String.h"
11#include "Customer.h"
12#include "CustomerDB.h"
13
14using namespace std;
15
16void readString(String &);
17void readNum(int &);
18
19CustomerDB database;
20
21int num_bottles = 0;
22int num_rattles = 0;
23int num_diapers = 0;
24
25/* clear the inventory and reset the customer database to empty */
26void reset(void) {
27    database.clear();
28    num_bottles = 0;
29    num_rattles = 0;
30    num_diapers = 0;
31}
32
33/*
34 * selectInventItem is a convenience function that allows you
35 * to obtain a pointer to the inventory record using the item type name
36 * word must be "Bottles", "Diapers" or "Rattles"
37 * for example the expression *selectInventItem("Bottles") returns the
38 * current global variable for the number of bottls in the inventory
39 */
40int* selectInventItem(String word) {
41    if (word == "Bottles") {
42        return &num_bottles;
43    } else if (word == "Diapers") {
44        return &num_diapers;
45    } else if (word == "Rattles") {
46        return &num_rattles;
47    }
48
49    /* NOT REACHED */
50    return 0;
51}
52
53/*
54 * this overload of selectInventItem is similar to selectInventItem above, however
55 * this overload takes a Customer as a second argument and selects the data member
56 * of that Customer that matches "word". The function returns a pointer to one of the three data
57 * members from the specified customer.
58 */
59int* selectInventItem(String word, Customer &cust) {
60    if (word == "Bottles") {
61        return &cust.bottles;
62    } else if (word == "Diapers") {
63        return &cust.diapers;
64    } else if (word == "Rattles") {
65        return &cust.rattles;
66    }
67
68    /* NOT REACHED */
69    return 0;
70}
71
72/*
73 * findMax searches through the CustomerDB "database" and returns the Customer
74 * who has purchased the most items of the specified type.
75 * type must be one of "Bottles", "Rattles" or "Diapers".
76 *
77 * Note: if two or more Customers are tied for having purchased the most of that item type
78 * then findMax returns the first Customer in the CustomerDB who has purchased that maximal
79 * quantity.
80 *
81 * Note: in the special case (invalid case) where there are zero Customers in the
82 * CustomerDB, fundMax returns a null pointer (0)
83 */
84Customer* findMax(String type) {
85    Customer* result = 0;
86    int max = 0;
87    for (int k = 0; k < database.size(); k += 1) {
88        Customer &cust = database[k];
89        int* p = selectInventItem(type, cust);
90        if (*p > max) {
91            max = *p;
92            result = &cust;
93        }
94    }
95    return result;
96}
97
98bool itemExists(String item) {
99    auto found = false;
100    for(auto s : {
101                "Bottles", "Diapers", "Rattles"
102            }) {
103        if(String(s) == item) {
104            found = true;
105        }
106    }
107    return found;
108}
109
110void processPurchase() {
111    String name, type;
112    int amt;
113    readString(name);
114    readString(type);
115    readNum(amt);
116    if(!itemExists(type) || amt <= 0) {
117        return;
118    }
119    auto dbamt = selectInventItem(type);
120    if(amt > *dbamt) {
121        cout << "Sorry " << name.c_str() << ", we only have " << *dbamt << " " <<
122             type.c_str() << endl;
123        return;
124    }
125    *dbamt -= amt;
126    *selectInventItem(type, database[name]) += amt;
127}
128
129void processSummarize() {
130    cout << "There are " << num_bottles << " Bottles, " << num_diapers <<
131         " Diapers and " << num_rattles << " Rattles in inventory" << endl <<
132         "we have had a total of " << database.size() << " different customers"
133         << endl;
134    for(auto s : {
135                "Bottles", "Diapers", "Rattles"
136            }) {
137        auto type = String(s);
138        auto temp = findMax(type);
139        if(temp) {
140            cout << temp->name.c_str() << " has purchased the most " << s <<
141                 " (" << *selectInventItem(type, *temp) << ")" << endl;
142            continue;
143        }
144        cout << "no one has purchased any " << s << endl;
145    }
146}
147
148void processInventory() {
149    String type;
150    int amt;
151    readString(type);
152    readNum(amt);
153    if(amt > 0 && itemExists(type)) {
154        *selectInventItem(type) += amt;
155    }
156}