Alx09

Ex 23 POO versiunea finala

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