# Calculating shipping cost as percentage of the total cost of online shopping

Calculate the shipping cost as percentage of the total cost of the online purchases of some customers, depending on the weights of the items in their shopping carts.

```#include <iostream> #include <vector> #include <tuple> using namespace std; struct StoreItem { string item_name; double weight; double price; }; class ShoppingCart { private: double items_bought_weight; vector<string> items_bought; public: void add_items(vector<string> items) { items_bought = items; } string get_items() { string items; size_t sz = items_bought.size(); for(size_t i = 0; i < sz; ++i) { if(i != sz - 1) {items += ", ";} items += items_bought[i]; } return items; } double get_total_price(vector<tuple<string, double, double>> items) { string item_name; double price, res = 0; for(const auto item_bought : items_bought) { for(const auto item : items) { item_name = get<0>(item); price = get<2>(item); if(item_name == item_bought) {res += price;} } } return res; } double get_total_weight(vector<tuple<string, double, double>> items) { string item_name; double weight, res = 0; for(const auto item_bought : items_bought) { for(const auto item : items) { item_name = get<0>(item); weight = get<1>(item); if(item_name == item_bought) {res += weight;} } } return res; } }; class ShippingCost { private: double weight_kg; public: ShippingCost(double w) : weight_kg(w) {} double calculate() { vector<tuple<int, int, int>> from_to_weight_price = { make_tuple(0, 5, 4), make_tuple(5, 10, 10), make_tuple(10, 20, 22), make_tuple(20, 30, 34), make_tuple(30, 40, 46), make_tuple(40, 50, 60), make_tuple(50, 60, 72), make_tuple(60, 70, 84), make_tuple(70, 80, 96), make_tuple(80, 90, 108), make_tuple(90, 100, 120), }; int from_weight, to_weight, shipping_cost; for(const auto fw_tw_pr : from_to_weight_price) { from_weight = get<0>(fw_tw_pr); to_weight = get<1>(fw_tw_pr); shipping_cost = get<2>(fw_tw_pr); if(from_weight < weight_kg && weight_kg < to_weight) { return shipping_cost; } } return 0; } }; class Customer { private: string first_name, last_name; public: Customer(string fn, string ln) : first_name(fn), last_name(ln) {} string get_name() { return first_name + " " + last_name; } }; int main() { vector<Customer> customers = { {"Jonathan", "Knightley"}, {"Fabio", "Sanetti"}, {"Felippe", "Cesari"}, {"John", "Spencer"}, {"Brian", "Hoffman"} }; vector<tuple<string, double, double>> items = { make_tuple("TV", 18, 1200), make_tuple("sofa", 45, 690), make_tuple("washing machine", 90, 780), make_tuple("memory foam mattress", 16, 235), make_tuple("chainsaw", 8, 110), make_tuple("kitchen table", 25, 290), make_tuple("picture", 1, 90), make_tuple("curtains", 4.5, 25), make_tuple("10kg paper", 10, 18), make_tuple("road bike", 9, 900) }; vector<vector<string>> items_to_buy = { {"washing machine", "curtains"}, {"kitchen table", "picture", "10kg paper"}, {"sofa", "memory foam mattress"}, {"road bike", "TV"}, {"TV", "kitchen table"} }; for(size_t i = 0; i < customers.size(); ++i) { ShoppingCart shopping_cart; shopping_cart.add_items(items_to_buy[i]); double products_price = shopping_cart.get_total_price(items); double shopping_cart_item_weight = shopping_cart.get_total_weight(items); ShippingCost shipping_cost(shopping_cart_item_weight); double shipping_cost_value = shipping_cost.calculate(); double total_price = products_price + shipping_cost_value; cout << customers[i].get_name() << endl << "Cost of all items: \$" << products_price << endl << "Cost of shipping: \$" << shipping_cost_value << endl << "Shipping cost as % of total cost: " << (shipping_cost_value / total_price) * 100 << "%" << endl << endl; } /* Jonathan Knightley Cost of all items: \$805 Cost of shipping: \$120 Shipping cost as % of total cost: 12.973% Fabio Sanetti Cost of all items: \$398 Cost of shipping: \$46 Shipping cost as % of total cost: 10.3604% Felippe Cesari Cost of all items: \$925 Cost of shipping: \$84 Shipping cost as % of total cost: 8.32507% John Spencer Cost of all items: \$2100 Cost of shipping: \$34 Shipping cost as % of total cost: 1.59325% Brian Hoffman Cost of all items: \$1490 Cost of shipping: \$60 Shipping cost as % of total cost: 3.87097% */ return 0; }```

It looks like John Spencer, who bought a road bike and a TV has the lowest shipping cost relative to the cost of the items purchased.