Advertisement
SS_B-Rabbit

Original

Feb 22nd, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. class Carti
  7. {
  8. protected:
  9.     static int count;
  10.     string nume;
  11.     int pret, id;
  12. public:
  13.     Carti(string nume, int pret);
  14.     string get_nume() { return nume; }
  15.     int get_pret() { return pret; }
  16.     virtual void afisare() = 0;
  17.     bool operator <(const Carti& other) const
  18.     {
  19.         return pret < other.pret;
  20.     }
  21.     int operator +(const int scumpire)
  22.     {
  23.         return this->pret += scumpire;
  24.     }
  25. };
  26.  
  27. Carti::Carti(string nume, int pret)
  28.     :nume(nume), pret(pret) {
  29.     count++;
  30.     this->id = count;
  31. }
  32.  
  33. class Poker : public Carti
  34. {
  35.     string design;
  36. public:
  37.     Poker(string nume, int pret, string design) : Carti(nume, pret)
  38.     {
  39.         this->design = design;
  40.     }
  41.     string get_design() { return design; }
  42.     void afisare() override
  43.     {
  44.         cout << id << ".Pachetul " << nume << " cu design " << design << " este la pretul de " << pret << " lei." << endl;
  45.     }
  46. };
  47.  
  48. class Magie : public Carti
  49. {
  50.     string efect;
  51. public:
  52.     Magie(string nume, int pret, string efect) : Carti(nume, pret)
  53.     {
  54.         this->efect = efect;
  55.     }
  56.     string get_efect() { return efect; }
  57.     void afisare() override
  58.     {
  59.         cout << id << ".Pachetul " << nume << " ce realizeaza efectul " << efect << " este la pretul de " << pret << " lei." << endl;
  60.     }
  61. };
  62.  
  63. int Carti::count = 0;
  64. list <Carti*> deck;
  65. list <Carti*> :: iterator it;
  66.  
  67. void citire()
  68. {
  69.     int pret;
  70.     bool option;
  71.     string nume, efect, stil;
  72.  
  73.     cout << "Nume:";
  74.     cin  >> nume;
  75.     cout << "Pret:";
  76.     cin >> pret;
  77.     cout << "De care pachet? \n[0]. Poker\n[1]. Magie" << endl;
  78.     cin >> option;
  79.     if (!option)
  80.     {
  81.         cout << "Stil:";
  82.         cin >> stil;
  83.         if (deck.empty())
  84.             deck.push_back(new Poker(nume, pret, stil));
  85.         else
  86.         {
  87.             it = deck.begin();
  88.             while (it != deck.end() && (*it)->get_nume() < nume)
  89.                 advance(it, 1);
  90.             deck.emplace(it, new Poker(nume, pret, stil));
  91.         }
  92.     }
  93.     else
  94.     {
  95.         cout << "Efect:";
  96.         cin >> efect;
  97.         if (deck.empty())
  98.             deck.push_back(new Magie(nume, pret, efect));
  99.         else
  100.         {
  101.             it = deck.begin();
  102.             while (it != deck.end() && (*it)->get_nume() < nume)
  103.                 advance(it, 1);
  104.             deck.emplace(it, new Magie(nume, pret, efect));
  105.         }
  106.     }
  107. }
  108.  
  109. void afisare()
  110. {
  111.     int option;
  112.     cout << "Ce afisam?\n[1].Poker\n[2].Magie" << endl;
  113.     cin >> option;
  114.     cout << endl;
  115.     for (auto q = deck.begin(); q != deck.end(); q++)
  116.     {
  117.         Poker* pok = dynamic_cast<Poker*>(*q);
  118.         Magie* mag = dynamic_cast<Magie*>(*q);
  119.         switch (option)
  120.         {
  121.         case 1:
  122.             if (pok)
  123.                 pok->afisare();
  124.             break;
  125.         case 2:
  126.             if (mag)
  127.                 mag->afisare();
  128.             break;
  129.         }
  130.     }
  131. }
  132.  
  133. void ex(int nota)
  134. {
  135.     if (nota < 8)
  136.         throw "Gresit!";
  137.     cout << "Gracias very much!";
  138.     cin.get();
  139.     exit(0);
  140. }
  141.  
  142. void stergere(string& nume)
  143. {
  144.     bool ok = 0;
  145.     it = deck.begin();
  146.     while (it != deck.end())
  147.     {
  148.         if ((*it)->get_nume() == nume)
  149.         {
  150.             it = deck.erase(it);
  151.             ok = 1;
  152.         }
  153.         else
  154.             it++;
  155.     }
  156.     if (!ok) cout << "Tivadar detected!" << endl;
  157. }
  158.  
  159. void cautare(string& nume, int pret)
  160. {
  161.     bool ok = 0;
  162.     for (it = deck.begin(); it != deck.end(); it++)
  163.     {
  164.         Poker* pok = dynamic_cast<Poker*>(*it);
  165.         Magie* mag = dynamic_cast<Magie*>(*it);
  166.         if (pok)
  167.             if (pok->get_nume() == nume && pok->get_pret() == pret)
  168.             {
  169.                 pok->afisare();
  170.                 ok = 1;
  171.             }
  172.         if (mag)
  173.             if (mag->get_nume() == nume && mag->get_pret() == pret)
  174.             {
  175.                 mag->afisare();
  176.                 ok = 1;
  177.             }
  178.     }
  179.     if (!ok) cout << "Tivadar strikes again!" << std::endl;
  180. }
  181.  
  182. void scumpire(string& nume, int lei)
  183. {
  184.     for (it = deck.begin(); it != deck.end(); it++)
  185.     {
  186.         Poker* pok = dynamic_cast<Poker*>(*it);
  187.         Magie* mag = dynamic_cast<Magie*>(*it);
  188.         if (pok)
  189.             if (pok->get_nume() == nume)
  190.             {
  191.                 (*pok) + lei;
  192.             }
  193.         if (mag)
  194.             if (mag->get_nume() == nume)
  195.             {
  196.                 (*mag) + lei;
  197.             }
  198.     }
  199. }
  200.  
  201.     int main()
  202.     {
  203.         int option, pret;
  204.         string nume;
  205.         do {
  206.             cout << "1. Adaugare pachet" << endl;
  207.             cout << "2. Afisare pachete" << endl;
  208.             cout << "3. Eliminare pachet" << endl;
  209.             cout << "4. Cautare pachet" << endl;
  210.             cout << "5. Sortare dupa pret" << endl;
  211.             cout << "6. Scumpire pachet" << endl;
  212.             cout << "99. Spala fereastra!" << endl;
  213.             cout << "10. Acordati nota" << endl;
  214.             cout << "0. Out Out Out!" << endl;
  215.             cin >> option;
  216.             switch (option)
  217.             {
  218.             case 1:
  219.                 citire();
  220.                 break;
  221.             case 2:
  222.                 afisare();
  223.                 break;
  224.             case 3:
  225.                 cout << "Ce pachet lichidam? ";
  226.                 cin >> nume;
  227.                 stergere(nume);
  228.                 break;
  229.             case 4:
  230.                 cout << "Numele pachetului cautat:";
  231.                 cin >> nume;
  232.                 cout << "Si pretul:";
  233.                 cin >> pret;
  234.                 cautare(nume, pret);
  235.                 break;
  236.             case 5:
  237.                  deck.sort();
  238.                  afisare();
  239.                 break;
  240.             case 6: cout << "Ce pachet scumpim?";
  241.                 cin >> nume;
  242.                 cout << "Cu cati lei?";
  243.                 cin >> pret;
  244.                 scumpire(nume, pret);
  245.                 break;
  246.             case 10:
  247.                 try
  248.                 {
  249.                     int nota;
  250.                     cout << "Introduceti nota:";
  251.                     cin >> nota;
  252.                     ex(nota);
  253.                 }
  254.                 catch (...) { cout << "Nota este prea mica!" << endl; }
  255.                 break;
  256.             case 99:
  257.                 system("cls");
  258.                 break;
  259.             case 0:
  260.                 exit(0);
  261.             }
  262.         } while (option != 0);
  263.         afisare();
  264.         cin.get();
  265.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement