Guest User

Untitled

a guest
Jan 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. #include <cctype>
  5. #include <memory>
  6.  
  7. // كلاس الأصناف
  8. class Product
  9. {
  10. public:
  11. Product(std::string n, float p, int s) : name(n), price(p), max_stock(s) {}
  12.  
  13. Product(){};
  14. std::string get_name() const
  15. {
  16. return this->name;
  17. }
  18.  
  19. float get_price() const
  20. {
  21. return this->price;
  22. }
  23.  
  24. int get_stock() const
  25. {
  26. return this->max_stock;
  27. }
  28.  
  29. bool isExisted() const
  30. {
  31. if (this->max_stock > 0)
  32. {
  33. return true;
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }
  40.  
  41. int took()
  42. {
  43. return --this->max_stock;
  44. }
  45.  
  46. private:
  47. std::string name;
  48. float price;
  49. int max_stock;
  50. };
  51.  
  52. // كلاس السلة لأضافة الأصناف وكميتها
  53. class Backet
  54. {
  55. public:
  56. Backet(Product *_p, int _q) : product(_p), quantity(_q){};
  57.  
  58. Product *get_product() const
  59. {
  60. return product;
  61. }
  62.  
  63. int get_quantity() const
  64. {
  65. return quantity;
  66. }
  67.  
  68. private:
  69. Product *product;
  70. int quantity;
  71. };
  72.  
  73. // كلاس الطلب لحساب سعر الصنف مع كميته وإنهاء الطلب
  74. class Order
  75. {
  76. public:
  77. Order()
  78. {
  79. final_price = 1;
  80. }
  81.  
  82. bool addToBacket(Product *p, int _quantity)
  83. {
  84. if (_quantity > 0 && _quantity <= p->get_stock())
  85. {
  86. // ضيف الصنف لسلة
  87. Backet b(p, _quantity);
  88. array_of_products.emplace_back(b);
  89. final_price = _quantity * p->get_price();
  90. return true;
  91. }
  92. else
  93. {
  94. return false;
  95. }
  96. }
  97.  
  98. float getThePrice() const
  99. {
  100. return final_price;
  101. }
  102. // في حالة زبون يبي يتم الطلب نقصوا الكمية من المخزن ونرجعوا قيمة الفاتورة النهائية
  103. float finishTheOrder()
  104. {
  105. final_price = 0;
  106. for (auto &item : array_of_products)
  107. {
  108. // نقصوا الكمية من المخزن
  109. for (int i = 0; i < item.get_quantity(); i++)
  110. {
  111. item.get_product()->took();
  112. // نجمعوا قيمة نهائية للطلب مع كل صنف نلقوه في السلة
  113. final_price += item.get_product()->get_price();
  114. }
  115. }
  116.  
  117. // نفرغوا السلة
  118. array_of_products.clear();
  119. return final_price;
  120. }
  121.  
  122. // عرض الفاتورة للمشتري
  123. void printOrder() const
  124. {
  125. for (const auto &item : array_of_products)
  126. {
  127. std::cout << "Name: " << item.get_product()->get_name() << "\tPrice: " << this->getThePrice()
  128. << "\tQuantity: " << item.get_quantity() << '\n';
  129. }
  130. }
  131.  
  132. // تفريغ السلة
  133. void emptyTheBacket()
  134. {
  135. array_of_products.clear();
  136. }
  137.  
  138. private:
  139. std::vector<Backet> array_of_products;
  140. int quantity;
  141. float final_price;
  142. };
  143.  
  144. int main(int argc, char const *argv[])
  145. {
  146. /* code */
  147. int choice, _q;
  148. char choice_y_n;
  149. float money;
  150.  
  151. std::vector<Product> arr_of_p;
  152. // أضف الأصناف
  153. arr_of_p.emplace_back(Product("Water", 0.5, 20));
  154. arr_of_p.emplace_back(Product("Pepsi", 2, 15));
  155. arr_of_p.emplace_back(Product("Biscuit", 1.5, 25));
  156. arr_of_p.emplace_back(Product("Choc", 4, 2));
  157. arr_of_p.emplace_back(Product("Coffe", 1.5, 20));
  158. arr_of_p.emplace_back(Product("Milk", 3.75, 10));
  159. arr_of_p.emplace_back(Product("Juice", 1, 20));
  160. arr_of_p.emplace_back(Product("Bravo", 4, 15));
  161.  
  162. // كائن الطلب ويفضل يكون واحد لأن لكل مشتري عنده كائن واحد
  163. Order ord1;
  164.  
  165. // تبدا عملية البيع
  166. while (1)
  167. {
  168. // العرض الأصناف المتاحة مع كميتها
  169. std::cout << "No." << '\t' << "Name"
  170. << "\t"
  171. << "Price" << '\t' << "Quantity" << '\t' << '\n';
  172. int i = 0;
  173. for (auto &item : arr_of_p)
  174. {
  175. std::cout << ++i << '\t' << item.get_name() << "\t" << item.get_price() << '\t' << item.get_stock() << '\t' << '\n';
  176. }
  177.  
  178. // بدء عملية البيع
  179. std::cout << std::endl;
  180. std::cout << "Enter a Number of Product Do you want to buy :";
  181. std::cin >> choice;
  182.  
  183. // نتأكدوا إنه يختار الصنف الصح
  184. if (choice > 0 && choice < 9)
  185. {
  186. // نشوفوا لو قاعد في المخزن
  187. if (arr_of_p[choice - 1].isExisted())
  188. {
  189. std::cout << "Enter a Quantity of Product you selected :";
  190. std::cin >> _q;
  191.  
  192. // نضيفوا للسلة
  193. if (ord1.addToBacket(&arr_of_p[choice - 1], _q))
  194. {
  195. std::cout << std::endl;
  196. std::cout << "The Order Your requested is \n";
  197. ord1.printOrder();
  198.  
  199. std::cout << std::endl;
  200. std::cout << "Do you want to complete the process [Y/N]: ";
  201. std::cin >> choice_y_n;
  202. if (std::toupper(choice_y_n) == 'Y')
  203. {
  204. std::cout << "pay " << ord1.getThePrice() << ": ";
  205. std::cin >> money;
  206.  
  207. if (money >= ord1.getThePrice())
  208. {
  209. // نتموا الطلبية
  210. float price = ord1.finishTheOrder();
  211. float returned = money > price ? money - price : 0;
  212. std::cout << "you purchased " << money << " and the returned money back is " << returned << std::endl;
  213. }
  214. else
  215. {
  216. std::cout << "error, your money isn't suffinient for that " << std::endl;
  217. ord1.emptyTheBacket();
  218. }
  219. }
  220. }
  221. else
  222. {
  223. std::cout << "you ordered unreasonable quantity, " << std::endl;
  224. std::cin.get();
  225. }
  226. }
  227. else
  228. {
  229. std::cout << "The product you ordered is out of stock, sorry" << std::endl;
  230. std::cin.get();
  231. }
  232. }
  233. }
  234.  
  235. return 0;
  236. }
Add Comment
Please, Sign In to add comment