Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <iterator>
- #include <conio.h>
- #include <string>
- #include <exception>
- #include <fstream>
- #define _CRT_SECURE_NO_WARNING
- using namespace std;
- class MyException : public exception {
- private:
- string mesajEroare;
- public:
- MyException(string mesajEroare) {
- this->mesajEroare = mesajEroare;
- }
- const char* what() const throw() {
- if (mesajEroare == "out_limit") return "Valoarea este inafara intervalului";
- if (mesajEroare == "lista_goala") return "lista este goala";
- return "Eroare neprevazuta";
- }
- };
- ostream & linie(ostream &out) {
- out << "------------------------\n";
- return out;
- }
- class Pachet;
- class Cazare {
- int tipDerivat;
- string nume;
- string adresa;
- double pret;
- public:
- Cazare(int tipDerivat, string nume, string adresa, double pret)
- {
- this->tipDerivat = tipDerivat;
- this->nume = nume;
- this->adresa = adresa;
- this->pret = pret;
- }
- int getTip() { return tipDerivat; }
- virtual int getStele() = 0;
- virtual void afisare_cazare()
- {
- cout << tipDerivat << "| " << nume << " " << adresa << " " << pret << " ";
- }
- virtual void SalvareFiser(fstream &out) {
- out << nume << endl;
- out << adresa << endl;
- out << pret << endl;
- out << tipDerivat << endl;
- }
- void setNume(string nume) {
- this->nume = nume;
- }
- virtual Cazare & operator++() = 0;
- virtual Cazare & operator--() = 0;
- void modificare_nume(string denumire)
- {
- nume = denumire;
- }
- string getNume() { return nume; }
- string getAdressa() { return adresa; }
- };
- class Hotel : public Cazare
- {
- int nrStele;
- string personalPremium;
- static int stele[8];
- public:
- Hotel(int tipDerivat, string nume, string adresa, double pret, int nrStele) : Cazare(tipDerivat, nume, adresa, pret)
- {
- this->nrStele = nrStele;
- if (this->nrStele <= 3) personalPremium = "NU";
- else personalPremium = "DA";
- stele[nrStele]++;
- }
- static void AfisareStele() { for (int i = 7; i > 0; i--)cout << i << " stele " << stele[i] << " unitati" << endl; }
- void setPersonal(string text) {
- personalPremium = text;
- }
- int getStele() { return nrStele; };
- void afisare_cazare()
- {
- Cazare::afisare_cazare();
- cout << nrStele << " " << personalPremium << endl;
- }
- void SalvareFiser(fstream &out) {
- Cazare::SalvareFiser(out);
- out << nrStele << endl;
- }
- Cazare & operator++()
- {
- if (nrStele == 7) {
- cout << "Nu se poate da mai mult de 7 stele" << endl;
- return *this;
- }
- if (nrStele > 3) setPersonal("DA");
- stele[nrStele]--;
- nrStele++;
- stele[nrStele]++;
- return *this;
- }
- Cazare& operator--()
- {
- if (nrStele == 1) {
- cout << "Nu se poate da mai putin de o stea" << endl;
- return *this;
- }
- stele[nrStele]--;
- nrStele--;
- stele[nrStele]++;
- if (nrStele > 3) setPersonal("NU");
- return *this;
- }
- };
- int Hotel::stele[8] = {};
- istream & operator>>(istream &in, Hotel *&h) {
- string nume;
- string adresa;
- double pret;
- int nrStele;
- bool ok;
- cout << "Nume: "; in >> nume;
- cout << "Adresa: "; in >> adresa;
- cout << "Pret: "; in >> pret;
- do {
- try {
- cout << "Numar de stele: "; in >> nrStele;
- if (nrStele < 1 || nrStele > 7) throw MyException("out_limit");
- ok = true;
- }
- catch (MyException &e) {
- cerr << e.what() << endl;
- ok = false;
- }
- } while (ok == false);
- h = new Hotel(0, nume, adresa, pret, nrStele);
- return in;
- }
- class Pensiune : public Cazare {
- int nrMargarete;
- string tipPensiune;
- static int margarete[4];
- public:
- Pensiune(int tipDerivat, string nume, string adresa, double pret, int nrMargarete, string tipPensiune) :Cazare(tipDerivat, nume, adresa, pret) {
- this->nrMargarete = nrMargarete;
- this->tipPensiune = tipPensiune;
- margarete[nrMargarete]++;
- }
- static void AfisareStele() { for (int i = 3; i > 0; i--)cout << i << " margarete " << margarete[i] << " unitati" << endl; }
- void afisare_cazare()
- {
- Cazare::afisare_cazare();
- cout << nrMargarete << " " << tipPensiune << endl;
- }
- int getStele() { return nrMargarete; }
- void SalvareFiser(fstream &out) {
- Cazare::SalvareFiser(out);
- out << nrMargarete << endl;
- out << tipPensiune << endl;
- }
- Cazare& operator++()
- {
- if (nrMargarete == 3) {
- cout << "Nu se poate da mai multe de 3 margarte" << endl;
- return *this;
- }
- nrMargarete++;
- return *this;
- }
- Cazare& operator--()
- {
- if (nrMargarete == 1) {
- cout << "Nu se poate da mai putin de o margareta" << endl;
- return *this;
- }
- nrMargarete--;
- return *this;
- }
- };
- int Pensiune::margarete[4] = {};
- istream & operator>>(istream &in, Pensiune *&p) {
- string nume;
- string adresa;
- double pret;
- int nrMargarete;
- string tipPensiune;
- bool ok;
- cout << "Nume: "; in >> nume;
- cout << "Adresa: "; in >> adresa;
- cout << "Pret: "; in >> pret;
- do {
- try {
- cout << "Numar de margarete: "; in >> nrMargarete;
- if (nrMargarete < 1 || nrMargarete > 3) throw MyException("out_limit");
- ok = true;
- }
- catch (MyException &e) {
- cerr << e.what();
- ok = false;
- }
- } while (ok == false);
- cout << "Tip de pensiune: "; in >> tipPensiune;
- p = new Pensiune(1, nume, adresa, pret, nrMargarete, tipPensiune);
- return in;
- }
- class Pachet {
- string denumire;
- string zona;
- string tipOferta;
- list <Cazare*> listaCazare;
- public:
- Pachet(string denumire, string zona, string tipOferta)
- {
- this->denumire = denumire;
- this->zona = zona;
- this->tipOferta = tipOferta;
- }
- void Afisare() {
- cout << "Nume: " << denumire << endl;
- cout << "Zona: " << zona << endl;
- cout << "Tip de oferta: " << tipOferta << endl;
- for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)(*it)->afisare_cazare();
- }
- bool Cautare(string adresa) {
- for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)
- if ((*it)->getAdressa() == adresa) {
- (*it)->afisare_cazare();
- return true;
- }
- return false;
- }
- bool Schimbare(string nume) {
- for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)
- if ((*it)->getNume() == nume) {
- string nume2;
- cout << "Nume nou: "; cin >> nume2;
- (*it)->setNume(nume2);
- (*it)->afisare_cazare();
- return true;
- }
- return false;
- }
- void adaugare_cazare() {
- int tip;
- Hotel *h = NULL;
- Pensiune *p = NULL;
- Cazare *c = NULL;
- cout << "Tip de cazare [Hotel- 0 , Pensiune - 1]: "; cin >> tip;
- if (tip) {
- cin >> p;
- c = p;
- }
- else
- {
- cin >> h;
- c = h;
- }
- list<Cazare *>::iterator it = listaCazare.begin();
- while (it != listaCazare.end() && (*it)->getStele() > c->getStele()) it++;
- listaCazare.emplace(it, c);
- }
- void Sterge(int tip, int nrstele) {
- if (listaCazare.empty()) return;
- for (auto it = listaCazare.begin(); it != listaCazare.end(); it++) {
- if ((*it)->getTip() == tip && (*it)->getStele() == nrstele) {
- listaCazare.erase(it);
- it = listaCazare.begin();
- }
- }
- }
- bool modificareNumarStele(string nume, int mod) {
- for (auto it = listaCazare.begin(); it != listaCazare.end(); it++)
- if ((*it)->getTip() == 0 && (*it)->getNume() == nume) {
- if (mod == 0)
- cout << "Inainte de incrementare " << endl;
- else
- cout << "Inainte de decrementare" << endl;
- (*it)->afisare_cazare();
- if (mod == 0)(*it)->operator++();
- else (*it)->operator--();
- cout << "Dupa " << endl;
- (*it)->afisare_cazare();
- return true;
- }
- return false;
- }
- string getNume() { return denumire; }
- friend fstream &operator<<(fstream &out, Pachet *&p);
- friend fstream &operator>>(fstream &in, Pachet *&p);
- };
- istream &operator>>(istream &in, Pachet *&p) {
- string denumire;
- string zona;
- string tipOferta;
- cout << "Denumire: "; in >> denumire;
- cout << "Zona: "; in >> zona;
- cout << "Tip Oferta: "; in >> tipOferta;
- p = new Pachet(denumire, zona, tipOferta);
- return in;
- }
- ostream &operator<<(ostream &out, Pachet *&p) {
- out << linie;
- p->Afisare();
- return out;
- }
- fstream &operator<<(fstream &out, Pachet *&p) {
- out << p->denumire << endl;
- out << p->zona << endl;
- out << p->tipOferta << endl;
- for (auto it = p->listaCazare.begin(); it != p->listaCazare.end(); it++) (*it)->SalvareFiser(out);
- out << endl;
- return out;
- }
- fstream &operator>>(fstream &in, Pachet *&p) {
- string denumire;
- string zona;
- string tipOferta;
- in >> denumire;
- in >> zona;
- in >> tipOferta;
- p = new Pachet(denumire, zona, tipOferta);
- string nume;
- string adresa;
- double pret;
- int tipDerivat;
- Cazare *c = NULL;
- if (in.eof()) return in;
- while (getline(in, nume))
- {
- getline(in, nume);
- if (in.eof() || nume == "\n" || nume == "") return in;
- in >> adresa;
- in >> pret;
- in >> tipDerivat;
- if (tipDerivat) {
- int nrMargarete;
- string tipPensiune;
- in >> nrMargarete >> tipPensiune;
- c = new Pensiune(tipDerivat, nume, adresa, pret, nrMargarete, tipPensiune);
- }
- else
- {
- int nrStele;
- in >> nrStele;
- c = new Hotel(tipDerivat, nume, adresa, pret, nrStele);
- }
- list<Cazare *>::iterator it = p->listaCazare.begin();
- while (it != p->listaCazare.end() && (*it)->getNume() < c->getNume()) it++;
- p->listaCazare.emplace(it, c);
- }
- return in;
- }
- int main() {
- int opt, nrCazari;
- list<Pachet *> listaPachet;
- list<Pachet *>::iterator it;
- Pachet *p = NULL;
- fstream f;
- string adresa, nume;
- bool gasit = 0;
- int nrStele;
- do {
- cout << "0. Iesire\n";
- cout << "1. Adaugare tastatura \n";
- cout << "2. Afisare Tastatura \n";
- cout << "3. Citire Fiser \n";
- cout << "4. Cautare dupa Adresa \n";
- cout << "5. Stergere\n";
- cout << "6. Incrementare \n";
- cout << "7. Decrementare\n";
- cout << "8. Schimbare nume\n";
- cout << "9. Afisare numar stele \n";
- cout << "Optiunea Aleasa "; cin >> opt;
- system("cls");
- switch (opt)
- {
- case 0: exit(0);
- case 1:
- cin >> p;
- cout << "Numar de cazari: "; cin >> nrCazari;
- for (int i = 1; i <= nrCazari; i++) {
- cout << "Cazarea " << i << endl;
- p->adaugare_cazare();
- system("cls");
- }
- it = listaPachet.begin();
- while (it != listaPachet.end() && (*it)->getNume() < p->getNume()) it++;
- listaPachet.emplace(it, p);
- break;
- case 2:
- for (it = listaPachet.begin(); it != listaPachet.end(); it++)cout << (*it);
- cout << linie;
- break;
- case 3:
- f.open("in.txt", ios::in);
- if (f.is_open() == false) break;
- cout << "Datele citie din fiserul in.txt sunt: " << endl;
- while (!f.eof()) {
- f >> p;
- cout << endl;
- if (p->getNume() == "") {
- delete p;
- break;
- }
- it = listaPachet.begin();
- while (it != listaPachet.end() && (*it)->getNume() < p->getNume()) it++;
- listaPachet.emplace(it, p);
- p->Afisare();
- }
- f.close();
- break;
- case 4:
- cout << "Adresa: "; cin >> adresa;
- for (it = listaPachet.begin(); it != listaPachet.end(); it++)
- if ((*it)->Cautare(adresa)) {
- gasit = 1;
- break;
- }
- if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
- gasit = 0;
- break;
- case 5:
- cout << "Stergere dupa numarul de [stele - 0, margarte -1]: "; cin >> gasit;
- if (gasit) {
- cout << "Numar de margarete: "; cin >> nrStele;
- for (it = listaPachet.begin(); it != listaPachet.end(); it++) (*it)->Sterge(1, nrStele);
- }
- else {
- cout << "Numar de stele: "; cin >> nrStele;
- for (it = listaPachet.begin(); it != listaPachet.end(); it++) (*it)->Sterge(0, nrStele);
- }
- gasit = 0;
- break;
- case 6:
- cout << "nume: "; cin >> nume;
- for (it = listaPachet.begin(); it != listaPachet.end(); it++)
- if ((*it)->modificareNumarStele(nume, 0)) {
- gasit = 1;
- break;
- }
- if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
- gasit = 0;
- break;
- case 7:
- cout << "nume: "; cin >> nume;
- for (it = listaPachet.begin(); it != listaPachet.end(); it++)
- if ((*it)->modificareNumarStele(nume, 1)) {
- gasit = 1;
- break;
- }
- if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
- gasit = 0;
- break;
- case 8:
- cout << "nume: "; cin >> nume;
- for (it = listaPachet.begin(); it != listaPachet.end(); it++)
- if ((*it)->Schimbare(nume)) {
- gasit = 1;
- break;
- }
- if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
- gasit = 0;
- break;
- case 9:
- Hotel::AfisareStele();
- Pensiune::AfisareStele();
- break;
- default:
- break;
- }
- } while (1);
- return 0;
- }
RAW Paste Data