Alx09

Untitled

Feb 8th, 2021
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <iterator>
  4. #include <conio.h>
  5. #include <string>
  6. #include <exception>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. class MyException : public exception {
  12. private:
  13.     string mesajEroare;
  14. public:
  15.  
  16.     MyException(string mesajEroare) {
  17.  
  18.         this->mesajEroare = mesajEroare;
  19.     }
  20.     const char* what() const throw() {
  21.         if (mesajEroare == "out_limit") return "Valoarea este inafara intervalului";
  22.         if (mesajEroare == "lista_goala") return "lista este goala";
  23.         return "Eroare neprevazuta";
  24.     }
  25. };
  26. ostream & linie(ostream &out) {
  27.     out << "------------------------\n";
  28.     return out;
  29. }
  30.  
  31. class Cazare {
  32.     int tipDerivat;
  33.     string nume;
  34.     string adresa;
  35.     double pret;
  36. public:
  37.     Cazare(int tipDerivat, string nume, string adresa, double pret)
  38.     {
  39.         this->tipDerivat = tipDerivat;
  40.         this->nume = nume;
  41.         this->adresa = adresa;
  42.         this->pret = pret;
  43.     }
  44.     int getTip() { return tipDerivat; }
  45.     virtual int getStele() = 0;
  46.     virtual void afisare_cazare()
  47.     {
  48.         cout << tipDerivat << "| " << nume << " " << adresa << " " << pret << " ";
  49.     }
  50.    
  51.     void setNume(string nume) {
  52.         this->nume = nume;
  53.     }
  54.     virtual Cazare & operator++() = 0;
  55.     virtual Cazare & operator--() = 0;
  56.     void modificare_nume(string denumire)
  57.     {
  58.         nume = denumire;
  59.     }
  60.     string getNume() { return nume; }
  61.     string getAdressa() { return adresa; }
  62.  
  63. };
  64.  
  65.  
  66. class Hotel : public Cazare
  67. {
  68.     int nrStele;
  69.     string personalPremium;
  70.     static int stele[8];
  71. public:
  72.     Hotel(int tipDerivat, string nume, string adresa, double pret, int nrStele) : Cazare(tipDerivat, nume, adresa, pret)
  73.     {
  74.         this->nrStele = nrStele;
  75.         if (this->nrStele <= 3)  personalPremium = "NU";
  76.         else personalPremium = "DA";
  77.         stele[nrStele]++;
  78.  
  79.     }
  80.     static void AfisareStele() { for (int i = 7; i > 0; i--)cout <<i<<" stele "<< stele[i] << " unitati" << endl; }
  81.     void setPersonal(string text) {
  82.         personalPremium = text;
  83.     }
  84.     int getStele() { return nrStele; };
  85.     void afisare_cazare()
  86.     {
  87.         Cazare::afisare_cazare();
  88.         cout << nrStele << " " << personalPremium << endl;
  89.     }  
  90.     Cazare & operator++()
  91.     {
  92.         if (nrStele == 7) {
  93.             cout << "Nu se poate da mai mult de 7 stele" << endl;
  94.             return *this;
  95.         }
  96.         if (nrStele > 3) setPersonal("DA");
  97.         stele[nrStele]--;
  98.         nrStele++;
  99.         stele[nrStele]++;
  100.  
  101.         return *this;
  102.     }
  103.  
  104.     Cazare& operator--()
  105.     {
  106.         if (nrStele == 1) {
  107.             cout << "Nu se poate da mai putin de  o stea" << endl;
  108.             return *this;
  109.         }
  110.         stele[nrStele]--;
  111.         nrStele--;
  112.         stele[nrStele]++;
  113.         if (nrStele > 3) setPersonal("NU");
  114.        
  115.         return *this;
  116.     }
  117.  
  118. };
  119. int Hotel::stele[8] = {};
  120. istream & operator>>(istream &in, Hotel *&h) {
  121.     string nume;
  122.     string adresa;
  123.     double pret;
  124.     int nrStele;
  125.     bool ok;
  126.     cout << "Nume: "; in >> nume;
  127.     cout << "Adresa: "; in >> adresa;
  128.     cout << "Pret: "; in >> pret;
  129.     do {
  130.         try {
  131.             cout << "Numar de stele: "; in >> nrStele;
  132.             if (nrStele < 1 || nrStele > 7) throw MyException("out_limit");
  133.             ok = true;
  134.         }
  135.         catch (MyException &e) {
  136.             cerr << e.what() << endl;
  137.             ok = false;
  138.         }
  139.     } while (ok == false);
  140.     h = new Hotel(0, nume, adresa, pret, nrStele);
  141.     return in;
  142. }
  143.  
  144.  
  145. class Pensiune : public Cazare {
  146.     int nrMargarete;
  147.     string tipPensiune;
  148.     static int margarete[4];
  149. public:
  150.     Pensiune(int tipDerivat, string nume, string adresa, double pret, int nrMargarete, string tipPensiune) :Cazare(tipDerivat, nume, adresa, pret) {
  151.         this->nrMargarete = nrMargarete;
  152.         this->tipPensiune = tipPensiune;
  153.         margarete[nrMargarete]++;
  154.     }
  155.     static void AfisareStele() { for (int i = 3; i > 0; i--)cout << i<< " margarete "<< margarete[i] << " unitati" << endl; }
  156.     void afisare_cazare()
  157.     {
  158.         Cazare::afisare_cazare();
  159.         cout << nrMargarete << " " << tipPensiune << endl;
  160.     }
  161.     int getStele() { return nrMargarete; }
  162.    
  163.  
  164.     Cazare& operator++()
  165.     {
  166.         if (nrMargarete == 3) {
  167.             cout << "Nu se poate da mai multe de 3 margarte" << endl;
  168.             return *this;
  169.        }
  170.         nrMargarete++;
  171.         return *this;
  172.     }
  173.  
  174.     Cazare& operator--()
  175.     {
  176.         if (nrMargarete == 1) {
  177.             cout << "Nu se poate da mai  putin de o margareta" << endl;
  178.             return *this;
  179.         }
  180.         nrMargarete--;
  181.         return *this;
  182.     }
  183. };
  184. int Pensiune::margarete[4] = {};
  185.  
  186. istream & operator>>(istream &in, Pensiune *&p) {
  187.     string nume;
  188.     string adresa;
  189.     double pret;
  190.     int nrMargarete;
  191.     string tipPensiune;
  192.     bool ok;
  193.     cout << "Nume: "; in >> nume;
  194.     cout << "Adresa: "; in >> adresa;
  195.     cout << "Pret: "; in >> pret;
  196.     do {
  197.         try {
  198.             cout << "Numar de margarete: "; in >> nrMargarete;
  199.             if (nrMargarete < 1 || nrMargarete > 3) throw MyException("out_limit");
  200.             ok = true;
  201.         }
  202.         catch (MyException &e) {
  203.             cerr << e.what();
  204.             ok = false;
  205.         }
  206.     } while (ok == false);
  207.     cout << "Tip de pensiune: "; in >> tipPensiune;
  208.  
  209.     p = new Pensiune(1, nume, adresa, pret, nrMargarete, tipPensiune);
  210.     return in;
  211. }
  212.  
  213.  
  214. class Pachet {
  215.     string denumire;
  216.     string zona;
  217.     string tipOferta;
  218.     list <Cazare*> listaCazare;
  219. public:
  220.     Pachet(string denumire, string zona, string tipOferta)
  221.     {
  222.         this->denumire = denumire;
  223.         this->zona = zona;
  224.         this->tipOferta = tipOferta;
  225.     }
  226.     void Afisare() {
  227.         cout << "Nume: " << denumire << endl;
  228.         cout << "Zona: " << zona << endl;
  229.         cout << "Tip de oferta: " << tipOferta << endl;
  230.         for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)(*it)->afisare_cazare();
  231.     }
  232.     bool Cautare(string adresa) {
  233.         for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)
  234.             if ((*it)->getAdressa() == adresa) {
  235.                 (*it)->afisare_cazare();
  236.                     return true;
  237.             }
  238.         return false;
  239.     }
  240.     bool Schimbare(string nume) {
  241.         for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)
  242.             if ((*it)->getNume() == nume) {
  243.                 string nume2;
  244.                 cout << "Nume nou: "; cin >> nume2;
  245.                 (*it)->setNume(nume2);
  246.                 (*it)->afisare_cazare();
  247.                 return true;
  248.             }
  249.         return false;
  250.     }
  251.     void adaugare_cazare() {
  252.         int tip;
  253.         Hotel *h = NULL;
  254.         Pensiune *p = NULL;
  255.         Cazare *c = NULL;
  256.         cout << "Tip de cazare [Hotel- 0 , Pensiune - 1]: "; cin >> tip;
  257.         if (tip) {
  258.             cin >> p;
  259.             c = p;
  260.         }
  261.         else
  262.         {
  263.             cin >> h;
  264.             c = h;
  265.         }
  266.  
  267.         list<Cazare *>::iterator it = listaCazare.begin();
  268.         while (it != listaCazare.end() && (*it)->getStele() > c->getStele()) it++;
  269.         listaCazare.emplace(it, c);
  270.     }
  271.     void Sterge(int tip, int nrstele) {
  272.         if (listaCazare.empty()) return;
  273.         for (auto it = listaCazare.begin(); it != listaCazare.end(); it++){
  274.             if ((*it)->getTip() == tip && (*it)->getStele() == nrstele)
  275.                 listaCazare.erase(it);
  276.             if (listaCazare.empty()) return;
  277.                 if (it == listaCazare.end()) return;
  278.             }
  279.     }
  280.     bool modificareNumarStele(string nume, int mod) {
  281.         for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)
  282.             if ( (*it)->getTip() == 0 &&  (*it)->getNume() == nume) {
  283.                 if (mod == 0)
  284.                     cout << "Inainte de incrementare " << endl;
  285.                 else
  286.                     cout << "Inainte de decrementare" << endl;
  287.            
  288.                 (*it)->afisare_cazare();
  289.                 if(mod== 0)(*it)->operator++();
  290.                 else (*it)->operator--();
  291.                 cout << "Dupa " << endl;
  292.                 (*it)->afisare_cazare();
  293.  
  294.                 return true;
  295.             }
  296.         return false;
  297.     }
  298.     string getNume() { return denumire; }
  299.  
  300.     friend fstream &operator>>(fstream &in, Pachet *&p);
  301. };
  302. istream &operator>>(istream &in, Pachet *&p) {
  303.     string denumire;
  304.     string zona;
  305.     string tipOferta;
  306.     cout << "Denumire: "; in >> denumire;
  307.     cout << "Zona: "; in >> zona;
  308.     cout << "Tip Oferta: "; in >> tipOferta;
  309.     p = new Pachet(denumire, zona, tipOferta);
  310.     return in;
  311. }
  312. ostream &operator<<(ostream &out, Pachet *&p) {
  313.     out << linie;
  314.     p->Afisare();
  315.     return out;
  316. }
  317.  
  318. fstream &operator>>(fstream &in, Pachet *&p) {
  319.     string denumire;
  320.     string zona;
  321.     string tipOferta;
  322.     in >> denumire;
  323.     in >> zona;
  324.     in >> tipOferta;
  325.     p = new Pachet(denumire, zona, tipOferta);
  326.     string nume;
  327.     string adresa;
  328.     double pret;
  329.     int tipDerivat;
  330.     Cazare *c = NULL;
  331.     if (in.eof()) return in;
  332.     while (getline(in, nume))
  333.     {
  334.         getline(in, nume);
  335.         if (in.eof() || nume == "\n" || nume == ""  ) return in;
  336.  
  337.         in >> adresa;
  338.         in >> pret;
  339.         in >> tipDerivat;
  340.         if (tipDerivat) {
  341.             int nrMargarete;
  342.             string tipPensiune;
  343.             in >> nrMargarete >> tipPensiune;
  344.             c = new Pensiune(tipDerivat, nume, adresa, pret, nrMargarete, tipPensiune);
  345.         }
  346.         else
  347.         {
  348.             int nrStele;
  349.             in >> nrStele;
  350.             c = new Hotel(tipDerivat, nume, adresa, pret, nrStele);
  351.         }
  352.         list<Cazare *>::iterator it = p->listaCazare.begin();
  353.         while (it != p->listaCazare.end() && (*it)->getNume() < c->getNume()) it++;
  354.         p->listaCazare.emplace(it, c);
  355.        
  356.     }
  357.     return in;
  358. }
  359.  
  360. int main() {
  361.     int opt, nrCazari;
  362.     list<Pachet *> listaPachet;
  363.     list<Pachet *>::iterator it;
  364.     Pachet *p = NULL;
  365.     fstream f;
  366.     string adresa, nume;
  367.     bool gasit = 0;
  368.     int nrStele;
  369.     do {
  370.         cout << "0. Iesire\n";
  371.         cout << "1. Adaugare tastatura \n";
  372.         cout << "2. Afisare Tastatura \n";
  373.         cout << "3. Citire Fiser \n";
  374.         cout << "4. Cautare dupa Adresa \n";
  375.         cout << "5. Stergere\n";
  376.         cout << "6. Incrementare \n";
  377.         cout << "7. Decrementare\n";
  378.         cout << "8. Schimbare nume\n";
  379.         cout << "9. Afisare numar stele \n";
  380.         cout << "Optiunea Aleasa "; cin >> opt;
  381.         system("cls");
  382.         switch (opt)
  383.         {
  384.         case 0: exit(0);
  385.         case 1:
  386.             cout << "Oferta noua -1, sau adaugare cazari - 0 "; cin >> gasit;
  387.             if (gasit) {
  388.                 cin >> p;
  389.             }
  390.             else {
  391.                 cout << "Nume oferta: "; cin >> nume;
  392.                 p = NULL;
  393.                 for (it = listaPachet.begin(); it != listaPachet.end(); it++) {
  394.                     if ((*it)->getNume() == nume) {
  395.                         p = *it;
  396.                        
  397.                         break;
  398.                     }
  399.                 }
  400.                 if (p == NULL) { cout << "Ofaerta nu a fost gasita!\n"; break; }
  401.             }
  402.             cout << "Numar de cazari: "; cin >> nrCazari;
  403.             for (int i = 1; i <= nrCazari; i++) {
  404.                 cout << "Cazarea " << i << endl;
  405.                 p->adaugare_cazare();
  406.                 system("cls");
  407.             }
  408.             it = listaPachet.begin();
  409.             while (it != listaPachet.end() && (*it)->getNume() < p->getNume()) it++;
  410.             listaPachet.emplace(it, p);
  411.             break;
  412.         case 2:
  413.             for (it = listaPachet.begin(); it != listaPachet.end(); it++)cout << (*it);
  414.             cout << linie;
  415.             break;
  416.         case 3:
  417.             f.open("in.txt", ios::in);
  418.             if (f.is_open() == false) break;
  419.             cout << "Datele citie din fiserul in.txt sunt: " << endl;
  420.             while(!f.eof()){
  421.                 f >> p;
  422.                 if (p->getNume() == "") {
  423.                     delete p;
  424.                     break;
  425.                 }
  426.                 it = listaPachet.begin();
  427.                 while (it != listaPachet.end() && (*it)->getNume() < p->getNume()) it++;
  428.                 listaPachet.emplace(it, p);
  429.                 p->Afisare();
  430.             }
  431.             f.close();
  432.             break;
  433.    
  434.         case 4:
  435.        
  436.             cout << "Adresa: "; cin >> adresa;
  437.             for (it = listaPachet.begin(); it != listaPachet.end(); it++)
  438.                 if ((*it)->Cautare(adresa)) {
  439.                     gasit = 1;
  440.                     break;
  441.                 }
  442.             if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
  443.             gasit = 0;
  444.             break;
  445.         case 5:
  446.             cout << "Stergere dupa numarul de [stele - 0, margarte -1]: "; cin >> gasit;
  447.  
  448.             if (gasit) {
  449.                 cout << "Numar de margarete: "; cin >> nrStele;
  450.                 for (it = listaPachet.begin(); it != listaPachet.end(); it++) (*it)->Sterge(1, nrStele);
  451.             }
  452.             else{
  453.                 cout << "Numar de stele: "; cin >> nrStele;
  454.                 for (it = listaPachet.begin(); it != listaPachet.end(); it++) (*it)->Sterge(0, nrStele);
  455.             }
  456.             gasit = 0;
  457.             break;
  458.         case 6:
  459.             cout << "nume: "; cin >> nume;
  460.             for (it = listaPachet.begin(); it != listaPachet.end(); it++)
  461.                 if ((*it)->modificareNumarStele(nume, 0)) {
  462.                     gasit = 1;
  463.                     break;
  464.                 }
  465.             if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
  466.             gasit = 0;
  467.             break;
  468.         case 7:
  469.             cout << "nume: "; cin >> nume;
  470.             for (it = listaPachet.begin(); it != listaPachet.end(); it++)
  471.                 if ((*it)->modificareNumarStele(nume, 1)) {
  472.                     gasit = 1;
  473.                     break;
  474.                 }
  475.             if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
  476.             gasit = 0;
  477.             break;
  478.         case 8:
  479.             cout << "nume: "; cin >> nume;
  480.             for (it = listaPachet.begin(); it != listaPachet.end(); it++)
  481.                 if ((*it)->Schimbare(nume)) {
  482.                     gasit = 1;
  483.                     break;
  484.                 }
  485.             if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
  486.             gasit = 0;
  487.             break;
  488.         case 9:
  489.             Hotel::AfisareStele();
  490.             Pensiune::AfisareStele();
  491.             break;
  492.         default:
  493.             break;
  494.         }
  495.  
  496.  
  497.     } while (1);
  498.     return 0;
  499. }
Advertisement
Add Comment
Please, Sign In to add comment