fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. class Item {
  6. public:
  7. Item(){};
  8. Item(int is, float upp, float usp) :
  9. in_stock(is), unit_purchase_price(upp),
  10. unit_sale_price(usp) {};
  11. int in_stock;
  12. float unit_purchase_price;
  13. float unit_sale_price;
  14. };
  15.  
  16. class Inventory {
  17. private:
  18. typedef std::map<std::string,Item> item_map;
  19. typedef std::map<std::string,Item>::const_iterator im_i;
  20. item_map m;
  21. public:
  22. void update_item(std::string const & item, int update_quantity,
  23. float buy_price, float sell_price){
  24.  
  25. Item i(update_quantity, buy_price, sell_price);
  26. m[item] = i;
  27. }
  28. void print_items(void) const{
  29. std::cout << "Item,In_Stock,Purchase_Price,Sale_Price" << std::endl;
  30. for( im_i i = m.begin();
  31. i!= m.end();
  32. ++i)
  33. std::cout << i->first << "," << i->second.in_stock << ","
  34. << i->second.unit_purchase_price << ","
  35. << i->second.unit_sale_price << std::endl;
  36. }
  37. float profit(std::string const & a_thing) const{
  38. im_i it = m.find(a_thing);
  39. if( it == m.end() ){
  40. std::cout << "No such item" << std::endl; // well shit
  41. return 0.0;
  42. }
  43. return static_cast<float>( it->second.in_stock)*
  44. (it->second.unit_sale_price - it->second.unit_purchase_price);
  45. }
  46. };
  47.  
  48.  
  49. int main(){
  50. Inventory i;
  51. i.update_item("A fucking toast", 1, 0.5, 100.0);
  52. i.update_item("oranges", 10, 1.5, 10.0);
  53. i.update_item("Unruly children", 6, 25, 130.50);
  54. i.update_item("Dwarves", 1.5, 5550, 51000.00);
  55. i.print_items();
  56. std::cout << "Profit for \"A fucking toast\": " << i.profit("A fucking toast") << std::endl;
  57. }
  58.  
Success #stdin #stdout 0.02s 2864KB
stdin
Standard input is empty
stdout
Item,In_Stock,Purchase_Price,Sale_Price
A fucking toast,1,0.5,100
Dwarves,1,5550,51000
Unruly children,6,25,130.5
oranges,10,1.5,10
Profit for "A fucking toast": 99.5