language: C++ 4.7.2 (gcc-4.7.2)
date: 121 days 10 hours ago
link:
visibility: public
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
#include <iostream>
#include <string>
#include <map>
 
class Item {
    public:
                Item(){};
                Item(int is, float upp, float usp) :
                        in_stock(is), unit_purchase_price(upp),
                        unit_sale_price(usp) {};
                int in_stock;
                float unit_purchase_price;
                float unit_sale_price;
};
 
class Inventory {
        private:
                typedef std::map<std::string,Item> item_map;
                typedef std::map<std::string,Item>::const_iterator im_i;
                item_map m;
        public:
                void update_item(std::string const & item, int update_quantity,
                                        float buy_price, float sell_price){
 
                        Item i(update_quantity, buy_price, sell_price);
                        m[item] = i;
                }
                void print_items(void) const{
                        std::cout << "Item,In_Stock,Purchase_Price,Sale_Price" << std::endl;
                        for( im_i i = m.begin();
                                i!= m.end();
                                ++i)
                                std::cout << i->first << "," << i->second.in_stock << ","
                                        << i->second.unit_purchase_price << "," 
                                        << i->second.unit_sale_price << std::endl;
                }
                float profit(std::string const & a_thing) const{
                        im_i it = m.find(a_thing);
                        if( it == m.end() ){
                                std::cout << "No such item" << std::endl; // well shit
                                return 0.0;
                        }
                        return static_cast<float>( it->second.in_stock)*
                                (it->second.unit_sale_price - it->second.unit_purchase_price);
                }
};
 
 
int main(){
        Inventory i;
        i.update_item("A fucking toast", 1, 0.5, 100.0);
        i.update_item("oranges", 10, 1.5, 10.0);
        i.update_item("Unruly children", 6, 25, 130.50);
        i.update_item("Dwarves", 1.5, 5550, 51000.00);
        i.print_items();
        std::cout << "Profit for \"A fucking toast\": " << i.profit("A fucking toast") << std::endl;
}