Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <vector>
- #include <map>
- using namespace std;
- class Product {
- public:
- string name;
- double price;
- string producer;
- Product(string name, double price, string producer);
- friend ostream& operator<< (ostream &out, const Product &product);
- //ofera polimorfism la runtime
- virtual int getProfit() {
- cout <<"General profit."<<endl;
- return 100;
- }
- virtual int applyTVA() {
- cout << "Aplic TVA CLASIC de 21 la suta pentru orice produs"<<endl;
- price = price + 0.21 * price;
- }
- };
- //supraincarcarea operatorului de afisare pentru clasa Produs
- ostream& operator<< (ostream &out, const Product &product) {
- out <<"("<<"Nume: " << product.name <<", " << "Pret: " << product.price <<", "
- << "Producator: " << product.producer <<")\n";
- }
- Product::Product(string name, double price, string producer) {
- this->name = string(name);
- this->price = price;
- this->producer = string(producer);
- }
- class Milk : public Product {
- public:
- using Product::Product;
- int getProfit() {
- cout<<"Milk profit." <<endl;
- return 50;
- }
- int applyTVA() {
- cout << "Aplic TVA 19 la suta pentru lapte"<<endl;
- price = price + 0.19 * price;
- }
- };
- class Egg : public Product {
- public:
- using Product::Product;
- //metoda cu polimorfismm, care este virtuala; da un alt profit in functie de
- //tipul clasei --ou, lapte sau produs general
- int getProfit() {
- cout <<"Egg profit." <<endl;
- return 10;
- }
- int applyTVA() {
- cout << "Aplic TVA 30 la suta pentru oua"<<endl;
- price = price + 0.30 * price;
- }
- };
- class Store {
- private:
- string name;
- string address;
- string time_table;
- vector<Product> products;
- double profit;
- //un map care tine pereche string, int - adica (nume produs - numarul de
- //produse din stock)
- map<string, int> stocks;
- public:
- Store(string name, string address, string time_table);
- friend ostream& operator<< (ostream &out, const Store &store);
- void addProduct(string name, double price, string producer);
- void addProduct(string name, double price, string producer, int stock);
- void addProduct(Product p);
- void addProduct(Product p, int stock);
- void addProduct(Product* p, int stock);
- void removeProduct(string name);
- void sell(string name);
- void sell(string name, int amount);
- void sell(Product* p, int amount);
- void update(string name);
- void update(string name, int amount);
- void rename(Product p, string rename);
- void rename(string name, string rename);
- int getStock(string name);
- int getStock(Product p);
- void showProducts();
- void showProduct(string name);
- void showProfit();
- };
- //incarcarea operatorului de afisare --pentru clasa Store
- ostream& operator<<(ostream &out, const Store &store) {
- out << "Name: " << store.name <<endl<< "Address: " << store.address <<endl<<
- "Time table: " << store.time_table << "\n"<<endl;
- return out;
- }
- void Store::showProfit() {
- cout<<"Profit: "<< profit<<endl;
- }
- Store::Store(string name, string address, string time_table) {
- this->name = string(name);
- this->address = string(address);
- this->time_table = string(time_table);
- this->profit = 0;
- }
- //adaugarea unui produs, doar ca acum se lucreaza cu map-ul
- //pentru stock -- adica stocks[name] = 1; sau = amount, depinde
- void Store::addProduct(string name, double price, string producer) {
- Product p(name, price, producer);
- products.push_back(p);
- stocks[name] = 1;
- cout<<"Product added."<<endl;
- }
- void Store::addProduct(string name, double price, string producer, int stock) {
- Product p(name, price, producer);
- products.push_back(p);
- stocks[name] = stock;
- cout<<"Product added x" <<stock<<endl;
- }
- void Store::addProduct(Product p) {
- products.push_back(p);
- stocks[p.name] = 1;
- cout<<"Product added."<<endl;
- }
- void Store::addProduct(Product p, int stock) {
- products.push_back(p);
- stocks[p.name] = stock;
- cout<<"Product added x."<<stock<<endl;
- }
- //adaugarea unui produs..numai ca adauga un pointer
- //metoda de push_back adauga valoarea pointerului, nu pointerul efectiv
- void Store::addProduct(Product *p, int stock) {
- products.push_back(*p);
- stocks[p->name] = stock;
- cout<<"Product added x."<<stock<<endl;
- }
- //metoda de stergere a produsului
- void Store::removeProduct(string name) {
- vector<Product>::iterator it;
- for (it = products.begin(); it != products.end(); ++it) {
- if (it->name.compare(name) == 0) {
- break;
- }
- }
- if (it == products.end()) {
- cout<<"This product doesn't exist."<<endl;
- }
- else {
- products.erase(it);
- cout<<"Product " <<name <<" removed"<<endl;
- }
- stocks.erase(name);
- }
- //metoda sell care ne ajuta sa vedem virtualizarea..primeste un pointer,
- void Store::sell(Product *p, int amount) {
- if (stocks[p->name] > amount) {
- stocks[p->name] = stocks[p->name] - amount;
- cout<< "Sold "<< amount<<" products"<<endl;
- profit = profit + amount * p->getProfit();
- }
- else {
- cout<<"Can't sell. Not enough products in the stock"<<endl;
- }
- }
- //scadem stock-ul folosind map-ul -- doar cu nume, se vinde un singur produs
- void Store::sell(string name) {
- if (stocks[name] > 1) {
- stocks[name]--;
- cout<<"Product Sold."<<endl;
- vector<Product>::iterator it;
- for (it = products.begin(); it != products.end(); ++it) {
- if (it->name.compare(name) == 0) {
- profit += it->getProfit();
- break;
- }
- }
- }
- else if (stocks[name] == 1) {
- stocks[name]--;
- cout<<"Product Sold. 0 " << name <<"s remaining. You should update the stock."<<endl;
- vector<Product>::iterator it;
- for (it = products.begin(); it != products.end(); ++it) {
- if (it->name.compare(name) == 0) {
- profit += it->getProfit();
- break;
- }
- }
- }
- else {
- cout<<"You can't sell this product. The product is out of the stock."<<endl;
- }
- }
- //scadem stock-ul folosind map-ul - supraincarcata cu nume si <amount>
- void Store::sell(string name, int amount) {
- if (stocks[name] > amount) {
- stocks[name] = stocks[name] - amount;
- vector<Product>::iterator it;
- for (it = products.begin(); it != products.end(); ++it) {
- if (it->name.compare(name) == 0) {
- profit = profit + amount * it->getProfit();
- break;
- }
- }
- cout<< "Sold "<< amount<<" products"<<endl;
- }
- else {
- cout<<"Can't sell. Not enough products in the stock"<<endl;
- }
- }
- //supraincarcare - se updateaza stock-ul doar cu un singur produs
- void Store::update(string name) {
- stocks[name]++;
- cout<<"Stock updated by 1"<<endl;
- }
- //se updateaza stock-ul cu <amount> produse
- void Store::update(string name, int amount) {
- stocks[name] = stocks[name] + amount;
- cout<<"Stock updated by " <<amount<<endl;
- }
- //rename - supraincarcare
- void Store::rename(Product p, string rename) {
- int stock = stocks[p.name];
- stocks.erase(p.name);
- p.name = rename;
- stocks[p.name] = stock;
- cout<<"Name changed."<<endl;
- }
- //rename - schimba numele unui produs
- void Store::rename(string name, string rename) {
- //se salveaza stock-ul actual
- int stock = stocks[name];
- //se sterge intrarea cu numele vechi din stock
- stocks.erase(name);
- //se adauga o noua intrare cu noul nume si vechiul stock
- stocks[rename] = stock;
- //se cauta produsul in toate produsele, iar cand se gaseste, se schimba numele
- vector<Product>::iterator it;
- for (it = products.begin(); it != products.end(); ++it) {
- if (it->name.compare(name) == 0) {
- it->name = rename;
- break;
- }
- }
- cout<<"Name changed."<<endl;
- }
- //supraincarcare - afiseaza stock-ul unui produs dupa Produs
- int Store::getStock(Product p) {
- return stocks[p.name];
- }
- //afiseaza stock-ul unui produs - dupa nume
- int Store::getStock(string name) {
- return stocks[name];
- }
- //arata un anumit produs - in functie de nume
- void Store::showProduct(string name) {
- vector<Product>::iterator it;
- for (it = products.begin(); it != products.end(); ++it) {
- if (it->name.compare(name) == 0) {
- cout << (*it) << "with Stock: " << stocks[name] <<endl;
- break;
- }
- }
- }
- //arata produsele din magazin
- void Store::showProducts() {
- vector<Product>::iterator it;
- for (it = products.begin(); it != products.end(); ++it) {
- cout<<(*it);
- }
- cout<<endl;
- }
- int main() {
- Store store("AFI", "Iuliu Maniu 1-3", "L-V 8-22");
- cout<<store;
- store.addProduct(string("geanta"), 320.32, string("LV"));
- store.addProduct(string("suc Fanta"), 3.2, string("Coca-Cola"), 20);
- cout<<"\nBefore remove:\n";
- store.showProducts();
- store.removeProduct(string("suc Fanta"));
- cout<<"\nAfter remove:\n";
- store.showProducts();
- store.addProduct(string("ball"), 20, string("NIKE"), 5);
- store.showProducts();
- store.sell(string("ball"));
- store.showProfit();
- cout<<store.getStock(string("ball")) << endl;
- store.update(string("ball"));
- cout<<store.getStock(string("ball"))<<endl;
- store.update(string("ball"),33);
- cout<<store.getStock(string("ball"))<<endl;
- store.sell(string("ball"));
- store.showProfit();
- cout<<store.getStock(string("ball"))<<endl;
- store.sell(string("ball"),10);
- store.showProfit();
- cout<<store.getStock(string("ball"))<<endl;
- store.rename(string("ball"),string("minge de fotbal"));
- store.showProduct(string("minge de fotbal"));
- //functie virtuala
- Egg *egg = new Egg("egg", 30, string("Ferma"));
- store.addProduct(egg, 20);
- store.showProducts();
- store.sell(egg, 10);
- store.showProfit();
- // functie virtuala
- Milk *milk = new Milk("milk", 50, string("Ferma"));
- store.addProduct(milk, 50);
- store.showProducts();
- store.sell(milk, 13);
- store.showProfit();
- //polimorfism TVA
- cout <<"\n";
- Product* product;
- Milk sana("sana", 55, string("vaca"));
- Egg ou_de_rata("ou de rata", 120, string("rata"));
- product = &sana;
- cout<< *product;
- product->applyTVA();
- product = &ou_de_rata;
- cout << *product;
- product->applyTVA();
- cout << *product;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement