Advertisement
zeita

Untitled

Jan 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <vector>
  5. #include <map>
  6. using namespace std;
  7.  
  8. class Product {
  9. public:
  10. string name;
  11. double price;
  12. string producer;
  13. Product(string name, double price, string producer);
  14. friend ostream& operator<< (ostream &out, const Product &product);
  15. //ofera polimorfism la runtime
  16. virtual int getProfit() {
  17. cout <<"General profit."<<endl;
  18. return 100;
  19. }
  20. virtual int applyTVA() {
  21. cout << "Aplic TVA CLASIC de 21 la suta pentru orice produs"<<endl;
  22. price = price + 0.21 * price;
  23. }
  24. };
  25. //supraincarcarea operatorului de afisare pentru clasa Produs
  26. ostream& operator<< (ostream &out, const Product &product) {
  27. out <<"("<<"Nume: " << product.name <<", " << "Pret: " << product.price <<", "
  28. << "Producator: " << product.producer <<")\n";
  29. }
  30.  
  31.  
  32.  
  33. Product::Product(string name, double price, string producer) {
  34. this->name = string(name);
  35. this->price = price;
  36. this->producer = string(producer);
  37. }
  38.  
  39. class Milk : public Product {
  40. public:
  41. using Product::Product;
  42. int getProfit() {
  43. cout<<"Milk profit." <<endl;
  44. return 50;
  45. }
  46. int applyTVA() {
  47. cout << "Aplic TVA 19 la suta pentru lapte"<<endl;
  48. price = price + 0.19 * price;
  49. }
  50. };
  51.  
  52. class Egg : public Product {
  53. public:
  54. using Product::Product;
  55. //metoda cu polimorfismm, care este virtuala; da un alt profit in functie de
  56. //tipul clasei --ou, lapte sau produs general
  57. int getProfit() {
  58. cout <<"Egg profit." <<endl;
  59. return 10;
  60. }
  61. int applyTVA() {
  62. cout << "Aplic TVA 30 la suta pentru oua"<<endl;
  63. price = price + 0.30 * price;
  64. }
  65. };
  66.  
  67. class Store {
  68. private:
  69. string name;
  70. string address;
  71. string time_table;
  72. vector<Product> products;
  73. double profit;
  74. //un map care tine pereche string, int - adica (nume produs - numarul de
  75. //produse din stock)
  76. map<string, int> stocks;
  77. public:
  78. Store(string name, string address, string time_table);
  79. friend ostream& operator<< (ostream &out, const Store &store);
  80. void addProduct(string name, double price, string producer);
  81. void addProduct(string name, double price, string producer, int stock);
  82. void addProduct(Product p);
  83. void addProduct(Product p, int stock);
  84. void addProduct(Product* p, int stock);
  85. void removeProduct(string name);
  86. void sell(string name);
  87. void sell(string name, int amount);
  88. void sell(Product* p, int amount);
  89. void update(string name);
  90. void update(string name, int amount);
  91. void rename(Product p, string rename);
  92. void rename(string name, string rename);
  93. int getStock(string name);
  94. int getStock(Product p);
  95. void showProducts();
  96. void showProduct(string name);
  97. void showProfit();
  98. };
  99.  
  100. //incarcarea operatorului de afisare --pentru clasa Store
  101. ostream& operator<<(ostream &out, const Store &store) {
  102. out << "Name: " << store.name <<endl<< "Address: " << store.address <<endl<<
  103. "Time table: " << store.time_table << "\n"<<endl;
  104. return out;
  105. }
  106.  
  107.  
  108. void Store::showProfit() {
  109. cout<<"Profit: "<< profit<<endl;
  110. }
  111.  
  112. Store::Store(string name, string address, string time_table) {
  113. this->name = string(name);
  114. this->address = string(address);
  115. this->time_table = string(time_table);
  116. this->profit = 0;
  117. }
  118.  
  119. //adaugarea unui produs, doar ca acum se lucreaza cu map-ul
  120. //pentru stock -- adica stocks[name] = 1; sau = amount, depinde
  121. void Store::addProduct(string name, double price, string producer) {
  122. Product p(name, price, producer);
  123. products.push_back(p);
  124. stocks[name] = 1;
  125. cout<<"Product added."<<endl;
  126. }
  127.  
  128. void Store::addProduct(string name, double price, string producer, int stock) {
  129. Product p(name, price, producer);
  130. products.push_back(p);
  131. stocks[name] = stock;
  132. cout<<"Product added x" <<stock<<endl;
  133. }
  134.  
  135. void Store::addProduct(Product p) {
  136. products.push_back(p);
  137. stocks[p.name] = 1;
  138. cout<<"Product added."<<endl;
  139. }
  140.  
  141. void Store::addProduct(Product p, int stock) {
  142. products.push_back(p);
  143. stocks[p.name] = stock;
  144. cout<<"Product added x."<<stock<<endl;
  145. }
  146.  
  147.  
  148. //adaugarea unui produs..numai ca adauga un pointer
  149. //metoda de push_back adauga valoarea pointerului, nu pointerul efectiv
  150. void Store::addProduct(Product *p, int stock) {
  151. products.push_back(*p);
  152. stocks[p->name] = stock;
  153. cout<<"Product added x."<<stock<<endl;
  154. }
  155.  
  156. //metoda de stergere a produsului
  157. void Store::removeProduct(string name) {
  158. vector<Product>::iterator it;
  159. for (it = products.begin(); it != products.end(); ++it) {
  160. if (it->name.compare(name) == 0) {
  161. break;
  162. }
  163. }
  164.  
  165. if (it == products.end()) {
  166. cout<<"This product doesn't exist."<<endl;
  167. }
  168. else {
  169. products.erase(it);
  170. cout<<"Product " <<name <<" removed"<<endl;
  171. }
  172. stocks.erase(name);
  173. }
  174.  
  175. //metoda sell care ne ajuta sa vedem virtualizarea..primeste un pointer,
  176. void Store::sell(Product *p, int amount) {
  177. if (stocks[p->name] > amount) {
  178. stocks[p->name] = stocks[p->name] - amount;
  179. cout<< "Sold "<< amount<<" products"<<endl;
  180. profit = profit + amount * p->getProfit();
  181. }
  182. else {
  183. cout<<"Can't sell. Not enough products in the stock"<<endl;
  184. }
  185. }
  186.  
  187. //scadem stock-ul folosind map-ul -- doar cu nume, se vinde un singur produs
  188. void Store::sell(string name) {
  189. if (stocks[name] > 1) {
  190. stocks[name]--;
  191. cout<<"Product Sold."<<endl;
  192. vector<Product>::iterator it;
  193. for (it = products.begin(); it != products.end(); ++it) {
  194. if (it->name.compare(name) == 0) {
  195. profit += it->getProfit();
  196. break;
  197. }
  198. }
  199. }
  200. else if (stocks[name] == 1) {
  201. stocks[name]--;
  202. cout<<"Product Sold. 0 " << name <<"s remaining. You should update the stock."<<endl;
  203. vector<Product>::iterator it;
  204. for (it = products.begin(); it != products.end(); ++it) {
  205. if (it->name.compare(name) == 0) {
  206. profit += it->getProfit();
  207. break;
  208. }
  209. }
  210. }
  211. else {
  212. cout<<"You can't sell this product. The product is out of the stock."<<endl;
  213. }
  214. }
  215.  
  216. //scadem stock-ul folosind map-ul - supraincarcata cu nume si <amount>
  217. void Store::sell(string name, int amount) {
  218. if (stocks[name] > amount) {
  219. stocks[name] = stocks[name] - amount;
  220. vector<Product>::iterator it;
  221. for (it = products.begin(); it != products.end(); ++it) {
  222. if (it->name.compare(name) == 0) {
  223. profit = profit + amount * it->getProfit();
  224. break;
  225. }
  226. }
  227. cout<< "Sold "<< amount<<" products"<<endl;
  228. }
  229. else {
  230. cout<<"Can't sell. Not enough products in the stock"<<endl;
  231. }
  232. }
  233.  
  234. //supraincarcare - se updateaza stock-ul doar cu un singur produs
  235. void Store::update(string name) {
  236. stocks[name]++;
  237. cout<<"Stock updated by 1"<<endl;
  238. }
  239.  
  240. //se updateaza stock-ul cu <amount> produse
  241. void Store::update(string name, int amount) {
  242. stocks[name] = stocks[name] + amount;
  243. cout<<"Stock updated by " <<amount<<endl;
  244. }
  245.  
  246. //rename - supraincarcare
  247. void Store::rename(Product p, string rename) {
  248. int stock = stocks[p.name];
  249. stocks.erase(p.name);
  250. p.name = rename;
  251. stocks[p.name] = stock;
  252. cout<<"Name changed."<<endl;
  253. }
  254.  
  255. //rename - schimba numele unui produs
  256. void Store::rename(string name, string rename) {
  257. //se salveaza stock-ul actual
  258. int stock = stocks[name];
  259. //se sterge intrarea cu numele vechi din stock
  260. stocks.erase(name);
  261. //se adauga o noua intrare cu noul nume si vechiul stock
  262. stocks[rename] = stock;
  263.  
  264. //se cauta produsul in toate produsele, iar cand se gaseste, se schimba numele
  265. vector<Product>::iterator it;
  266. for (it = products.begin(); it != products.end(); ++it) {
  267. if (it->name.compare(name) == 0) {
  268. it->name = rename;
  269. break;
  270. }
  271. }
  272. cout<<"Name changed."<<endl;
  273. }
  274.  
  275. //supraincarcare - afiseaza stock-ul unui produs dupa Produs
  276. int Store::getStock(Product p) {
  277. return stocks[p.name];
  278. }
  279.  
  280. //afiseaza stock-ul unui produs - dupa nume
  281. int Store::getStock(string name) {
  282. return stocks[name];
  283. }
  284.  
  285. //arata un anumit produs - in functie de nume
  286. void Store::showProduct(string name) {
  287. vector<Product>::iterator it;
  288. for (it = products.begin(); it != products.end(); ++it) {
  289. if (it->name.compare(name) == 0) {
  290. cout << (*it) << "with Stock: " << stocks[name] <<endl;
  291. break;
  292. }
  293. }
  294. }
  295.  
  296. //arata produsele din magazin
  297. void Store::showProducts() {
  298. vector<Product>::iterator it;
  299. for (it = products.begin(); it != products.end(); ++it) {
  300. cout<<(*it);
  301. }
  302. cout<<endl;
  303. }
  304.  
  305. int main() {
  306. Store store("AFI", "Iuliu Maniu 1-3", "L-V 8-22");
  307. cout<<store;
  308.  
  309. store.addProduct(string("geanta"), 320.32, string("LV"));
  310. store.addProduct(string("suc Fanta"), 3.2, string("Coca-Cola"), 20);
  311.  
  312. cout<<"\nBefore remove:\n";
  313. store.showProducts();
  314. store.removeProduct(string("suc Fanta"));
  315. cout<<"\nAfter remove:\n";
  316. store.showProducts();
  317.  
  318. store.addProduct(string("ball"), 20, string("NIKE"), 5);
  319. store.showProducts();
  320.  
  321. store.sell(string("ball"));
  322. store.showProfit();
  323. cout<<store.getStock(string("ball")) << endl;
  324.  
  325. store.update(string("ball"));
  326. cout<<store.getStock(string("ball"))<<endl;
  327.  
  328. store.update(string("ball"),33);
  329. cout<<store.getStock(string("ball"))<<endl;
  330.  
  331. store.sell(string("ball"));
  332. store.showProfit();
  333. cout<<store.getStock(string("ball"))<<endl;
  334.  
  335. store.sell(string("ball"),10);
  336. store.showProfit();
  337. cout<<store.getStock(string("ball"))<<endl;
  338.  
  339. store.rename(string("ball"),string("minge de fotbal"));
  340. store.showProduct(string("minge de fotbal"));
  341.  
  342. //functie virtuala
  343. Egg *egg = new Egg("egg", 30, string("Ferma"));
  344. store.addProduct(egg, 20);
  345. store.showProducts();
  346. store.sell(egg, 10);
  347. store.showProfit();
  348.  
  349. // functie virtuala
  350. Milk *milk = new Milk("milk", 50, string("Ferma"));
  351. store.addProduct(milk, 50);
  352. store.showProducts();
  353. store.sell(milk, 13);
  354. store.showProfit();
  355.  
  356. //polimorfism TVA
  357. cout <<"\n";
  358. Product* product;
  359. Milk sana("sana", 55, string("vaca"));
  360. Egg ou_de_rata("ou de rata", 120, string("rata"));
  361. product = &sana;
  362. cout<< *product;
  363. product->applyTVA();
  364. product = &ou_de_rata;
  365. cout << *product;
  366. product->applyTVA();
  367. cout << *product;
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement