Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<stdlib.h>
- #include<list>
- #include<iterator>
- #include<string>
- #include<fstream>
- #define _CRT_SECURE_NO_WARNING
- using namespace std;
- class Cadou {
- private:
- string nume;
- double pret;
- public:
- Cadou(string nume, double pret) {
- this->nume = nume;
- this->pret = pret;
- }
- virtual void afisare() {
- cout << nume <<" " << pret << " ";
- }
- string get_nume() { return nume; }
- double get_pret() { return pret; }
- };
- class Haina : public Cadou {
- private:
- int marime;
- string culoare;
- public:
- Haina(string nume, double pret, string culoare, int marime) : Cadou(nume, pret) {
- this->culoare = culoare;
- this->marime = marime;
- }
- void afisare() {
- Cadou::afisare();
- cout << culoare << " " << marime << endl;
- }
- };
- class Bijuteri :public Cadou {
- private:
- string material;
- public:
- Bijuteri(string nume, double pret, string material) : Cadou(nume, pret) {
- this->material = material;
- }
- void afisare() {
- Cadou::afisare();
- cout << material << endl;
- }
- };
- class Jucarie: public Cadou
- {
- protected:
- string brand;
- public:
- Jucarie(string nume, double pret, string brand) : Cadou(nume, pret) {
- this->brand = brand;
- }
- void afisare() {
- Cadou::afisare();
- cout << brand << endl;
- }
- };
- list <Cadou *> listaCadou;
- list <Cadou *>::iterator it;
- void init()
- {
- int marime;
- double pret;
- string nume, brand, material, culoare;
- char tip;
- cout << "\nCe fel de cadou H- haina, B- Bijutire, -J -Jucarie: "; cin >> tip;
- cout << "Nume: "; cin >> nume;
- cout << "Pret: "; cin >> pret;
- if (tip == 'H') {
- cout << "Culoare: "; cin >> culoare;
- cout << "Marime: "; cin >> marime;
- if (listaCadou.empty())
- listaCadou.push_back(new Haina(nume, pret, culoare, marime));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Haina(nume, pret, culoare, marime));
- }
- }
- if (tip == 'B') {
- cout << "Material: "; cin >> material;
- if (listaCadou.empty())
- listaCadou.push_back(new Bijuteri(nume, pret, material));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Bijuteri(nume, pret, material));
- }
- }
- if (tip == 'J') {
- cout << "brand: "; cin >> brand;
- if (listaCadou.empty())
- listaCadou.push_back(new Jucarie(nume, pret, brand));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Jucarie(nume, pret, brand));
- }
- }
- }
- void stergere() {
- string nume;
- cout << "\nNume cadou: ";
- cin >> nume;
- for (it = listaCadou.begin(); it != listaCadou.end(); it++)
- if ((*it)->get_nume() == nume) {
- listaCadou.erase(it);
- cout << " Cadoul " << nume << " a fost sters!" << endl;
- return;
- }
- cout << " Cadoul" << nume << " nu se afla in baza de date" << endl;
- }
- void calcul_pret_cadouri() {
- double suma = 0.f;
- for (it = listaCadou.begin(); it != listaCadou.end(); it++) suma = suma + (*it)->get_pret();
- }
- void cautareCadou() {
- bool ok = 0;
- string nume;
- cout << " Care este denumirea cadoului?: ";
- cin >> nume;
- for (it = listaCadou.begin(); it != listaCadou.end(); it++)
- if ((*it)->get_nume() == nume) {
- (*it)->afisare();
- return;
- }
- cout << "\n Caodul nu se afla pe lista";
- }
- void citire_fisier(string numeFiser)
- {
- ifstream f(numeFiser);
- int marime;
- double pret;
- string nume, brand, material, culoare;
- char tip;
- if (!f) {
- cout << "\n Nu exista fiserul " << numeFiser;
- return;
- }
- while (!f.eof()) {
- f >> tip;
- f >> nume;
- f >> pret;
- if (tip == 'H') {
- f >> culoare;
- f >> marime;
- if (listaCadou.empty())
- listaCadou.push_back(new Haina(nume, pret, culoare, marime));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Haina(nume, pret, culoare, marime));
- }
- }
- if (tip == 'B') {
- f >> material;
- if (listaCadou.empty())
- listaCadou.push_back(new Bijuteri(nume, pret, material));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Bijuteri(nume, pret, material));
- }
- }
- if (tip == 'J') {
- f >> brand;
- if (listaCadou.empty())
- listaCadou.push_back(new Jucarie(nume, pret, brand));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Jucarie(nume, pret, brand));
- }
- }
- }
- cout << "\n Datele au fost citite cu succes!";
- f.close();
- //else throw "Eroare la citirea din fisier";
- }
- void show() {
- cout << "=============================" << endl;
- for (it = listaCadou.begin(); it != listaCadou.end(); it++) {
- (*it)->afisare();
- cout << "----------------------------- \n";
- }
- cout << "=============================" << endl;
- }
- int main() {
- int opt;
- do {
- cout << "\n\n";
- cout << "1.Citire cadouuri din fiser\n";
- cout << "2.Afis cadouri \n";
- cout << "3.Afisare date despre un cadou \n";
- cout << "4.Calculul facutri pentru cadouri\n";
- cout << "5.Stergerea unui cadou de pe lista\n";
- cout << "6. Adaugare cadou de la tastautra\n";
- cout << "0.Iesire \n";
- cout << "Dati optiunea dvs: ";
- cin >> opt;
- system("cls");
- switch (opt) {
- case 1:
- citire_fisier("cadou.txt");
- break;
- case 2:
- show();
- break;
- case 3:
- cautareCadou();
- break;
- case 4:
- calcul_pret_cadouri();
- break;
- case 5:
- stergere();
- break;
- case 6:
- init();
- break;
- case 0:
- return 0;
- break;
- default:
- cout << "\nOptiune Invalida";
- break;
- }
- } while (opt != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement