Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- using namespace std;
- const char * crt = "\n------------------------------------------------------\n";
- template<class T1, class T2>
- class Kolekcija
- {
- T1* _elementi1;
- T2* _elementi2;
- int _trenutno;
- public:
- Kolekcija()
- {
- _trenutno = 0;
- _elementi1 = nullptr;
- _elementi2 = nullptr;
- }
- Kolekcija(const Kolekcija & obj) : _trenutno(obj._trenutno)
- {
- _elementi1 = new T1[_trenutno];
- _elementi2 = new T2[_trenutno];
- for (size_t i = 0; i < _trenutno; i++)
- {
- _elementi1[i] = obj._elementi1[i];
- _elementi2[i] = obj._elementi2[i];
- }
- }
- ~Kolekcija()
- {
- delete[] _elementi1;
- delete[] _elementi2;
- _elementi1 = nullptr;
- _elementi2 = nullptr;
- }
- void Reset()
- {
- _trenutno = 0;
- }
- Kolekcija & operator = (const Kolekcija & right)
- {
- if (this != &right)
- {
- delete[] _elementi1;
- delete[] _elementi2;
- _trenutno = right._trenutno;
- _elementi1 = new T1[_trenutno];
- _elementi2 = new T2[_trenutno];
- for (size_t i = 0; i < _trenutno; i++)
- {
- _elementi1[i] = right._elementi1[i];
- _elementi2[i] = right._elementi2[i];
- }
- }
- return *this;
- }
- void DodajElemente(T1& el1, T2& el2)
- {
- T1* temp1 = new T1[_trenutno + 1];
- T2* temp2 = new T2[_trenutno + 1];
- for (int i = 0; i < _trenutno; i++)
- {
- temp1[i] = _elementi1[i];
- temp2[i] = _elementi2[i];
- }
- delete[] _elementi1;
- delete[] _elementi2;
- _elementi1 = temp1;
- _elementi2 = temp2;
- _elementi1[_trenutno] = el1;
- _elementi2[_trenutno] = el2;
- _trenutno++;
- }
- T1 & GetElement_1(int indeks)const
- {
- if (indeks >= 0 && indeks < _trenutno)
- return _elementi1[indeks];
- }
- T1 &operator[](int indeks)const
- {
- if (indeks >= 0 && indeks < _trenutno)
- return _elementi1[indeks];
- }
- T2 & GetElement_2(int indeks)const
- {
- if (indeks >= 0 && indeks < _trenutno)
- return _elementi2[indeks];
- }
- int GetTrenutno() const { return _trenutno; }
- friend ostream& operator<<<>(ostream&, const Kolekcija&);
- };
- template<class T1, class T2>
- ostream& operator<<<>(ostream& cout, const Kolekcija<T1, T2>& k)
- {
- for (int i = 0; i < k._trenutno; i++)
- {
- cout << k._elementi1[i] << "\t" << k._elementi2[i] << endl;
- }
- return cout;
- }
- //U programu implementirati nasljeđivanje između odgovarajućih klasa
- //Klasu Osoba deklarisati kao apstraktnu.
- class Osoba
- {
- protected:
- char* _ime;
- char* _prezime;
- char* _adresa;
- char _telefon[15];
- public:
- Osoba(char* ime, char* prezime, char* adresa, char* telefon)
- {
- int vel = strlen(ime) + 1;
- _ime = new char[vel];
- strcpy_s(_ime, vel, ime);
- vel = strlen(prezime) + 1;
- _prezime = new char[vel];
- strcpy_s(_prezime, vel, prezime);
- vel = strlen(adresa) + 1;
- _adresa = new char[vel];
- strcpy_s(_adresa, vel, adresa);
- strcpy_s(_telefon, 15, telefon);
- }
- Osoba(const Osoba & obj)
- {
- int size = strlen(obj._ime) + 1;
- _ime = new char[size];
- strcpy_s(_ime, size, obj._ime);
- size = strlen(obj._prezime) + 1;
- _prezime = new char[size];
- strcpy_s(_prezime, size, obj._prezime);
- size = strlen(obj._adresa) + 1;
- _adresa = new char[size];
- strcpy_s(_adresa, size, obj._adresa);
- strcpy_s(_telefon, 15, obj._telefon);
- }
- virtual ~Osoba()
- {
- delete[] _ime;
- delete[] _prezime;
- delete[] _adresa;
- _ime = nullptr;
- _prezime = nullptr;
- _adresa = nullptr;
- }
- Osoba & operator = (const Osoba & right)
- {
- if (this != &right)
- {
- delete[] _ime;
- delete[] _prezime;
- delete[] _adresa;
- int size = strlen(right._ime) + 1;
- _ime = new char[size];
- strcpy_s(_ime, size, right._ime);
- size = strlen(right._prezime) + 1;
- _prezime = new char[size];
- strcpy_s(_prezime, size, right._prezime);
- size = strlen(right._adresa) + 1;
- _adresa = new char[size];
- strcpy_s(_adresa, size, right._adresa);
- strcpy_s(_telefon, 15, right._telefon);
- }
- return *this;
- }
- const char * GetIme()const
- {
- return _ime;
- }
- const char * GetPrezime()const
- {
- return _prezime;
- }
- virtual void Info() = 0;
- };
- class Proizvod
- {
- char* _naziv;
- char* _opis;
- double _cijena;
- public:
- Proizvod(char* naziv, char* opis, double cijena)
- {
- int vel = strlen(naziv) + 1;
- _naziv = new char[vel];
- strcpy_s(_naziv, vel, naziv);
- vel = strlen(opis) + 1;
- _opis = new char[vel];
- strcpy_s(_opis, vel, opis);
- _cijena = cijena;
- }
- Proizvod(const Proizvod & obj)
- {
- int size = strlen(obj._naziv) + 1;
- _naziv = new char[size];
- strcpy_s(_naziv, size, obj._naziv);
- size = strlen(obj._opis) + 1;
- _opis = new char[size];
- strcpy_s(_opis, size, obj._opis);
- _cijena = obj._cijena;
- }
- ~Proizvod()
- {
- delete[] _naziv;
- delete[] _opis;
- _naziv = nullptr;
- _opis = nullptr;
- }
- const char * GetNaziv()const
- {
- return _naziv;
- }
- const char * GetOpis()const
- {
- return _opis;
- }
- double GetCijena()const
- {
- return _cijena;
- }
- Proizvod & operator = (const Proizvod & right)
- {
- if (this != &right)
- {
- delete[] _naziv;
- delete[] _opis;
- int size = strlen(right._naziv) + 1;
- _naziv = new char[size];
- strcpy_s(_naziv, size, right._naziv);
- size = strlen(right._opis) + 1;
- _opis = new char[size];
- strcpy_s(_opis, size, right._opis);
- _cijena = right._cijena;
- }
- return *this;
- }
- friend ostream& operator<<(ostream&, const Proizvod&);
- friend ostream& operator<<(ostream&, const Proizvod*);
- friend bool operator ==(const Proizvod &, const Proizvod &);
- };
- ostream& operator<<(ostream& cout, const Proizvod& p)
- {
- cout << "Naziv: " << p._naziv << endl;
- cout << "Opis: " << p._opis << endl;
- cout << "Cijena: " << p._cijena << "KM" << endl;
- return cout;
- }
- ostream& operator<<(ostream& cout, const Proizvod* p)
- {
- cout << "Naziv: " << p->_naziv << endl;
- cout << "Opis: " << p->_opis << endl;
- cout << "Cijena: " << p->_cijena << "KM" << endl;
- return cout;
- }
- bool operator ==(const Proizvod & left, const Proizvod & right)
- {
- if (strcmp(left._naziv, right._naziv) != 0)
- return false;
- else if (strcmp(left._opis, right._opis) != 0)
- return false;
- else if (left._cijena != right._cijena)
- return false;
- return true;
- }
- class Narudzba
- {
- char _datum[12]; //Format: dd.MM.yyyy.
- Kolekcija<Proizvod*, int> _proizvodi; //Podaci o naručenim proizvodima i količinama
- public:
- //Potrebne osnovne funkcije za rad sa klasom
- Narudzba()
- {
- strcpy_s(_datum, "<N/A>");
- }
- Narudzba(char * datum)
- {
- strncpy_s(_datum, datum, _TRUNCATE);
- }
- Narudzba(const Narudzba & obj)
- {
- strcpy_s(_datum, obj._datum);
- for (size_t i = 0; i < obj._proizvodi.GetTrenutno(); i++)
- {
- Proizvod * noviProizvod = new Proizvod(*obj._proizvodi.GetElement_1(i));
- int kol = obj._proizvodi.GetElement_2(i);
- _proizvodi.DodajElemente(noviProizvod, kol);
- }
- }
- Narudzba & operator = (const Narudzba & right)
- {
- if (this != &right)
- {
- for (size_t i = 0; i < _proizvodi.GetTrenutno(); i++)
- {
- delete _proizvodi.GetElement_1(i);
- }
- strcpy_s(_datum, right._datum);
- _proizvodi.Reset();
- for (size_t i = 0; i < right._proizvodi.GetTrenutno(); i++)
- {
- Proizvod * noviProizvod = new Proizvod(*right._proizvodi.GetElement_1(i));
- int kol = right._proizvodi.GetElement_2(i);
- _proizvodi.DodajElemente(noviProizvod, kol);
- }
- }
- return *this;
- }
- ~Narudzba()
- {
- for (size_t i = 0; i < _proizvodi.GetTrenutno(); i++)
- {
- delete _proizvodi.GetElement_1(i);
- _proizvodi.GetElement_1(i) = nullptr;
- }
- }
- int GetMjesecNarudzbe()const
- {
- char datum[3] = { _datum[3], _datum[4], '\0' };
- return atoi(datum);
- }
- void DodajProizvod(const Proizvod & proizvod, int kolicina = 1)
- {
- for (size_t i = 0; i < _proizvodi.GetTrenutno(); i++)
- {
- if (*_proizvodi.GetElement_1(i) == proizvod)
- {
- _proizvodi.GetElement_2(i)++; // povecaj kolicinu za jedan ako su proizvodi isti
- return;
- }
- }
- Proizvod * proizvodPtr = new Proizvod(proizvod);
- _proizvodi.DodajElemente(proizvodPtr, kolicina);
- }
- //Funkciju GetIznosNarudzbe koja vraća ukupan iznos narudžbe na osnovu
- //podataka o cijenama proizvoda i naručenim količinama. Funkcija prima neobavezni parametar popust
- //koji inicijalno ima vrijednost 0, što znači da ne postoji popust na postojeće cijene proizvoda.
- double GetIznosNarudzbe(float popust = 0)const
- {
- if (popust >= 1) // ako se popust ne unese u decimali
- popust /= 100;
- double suma = 0;
- for (size_t i = 0; i < _proizvodi.GetTrenutno(); i++)
- suma += _proizvodi.GetElement_1(i)->GetCijena() * _proizvodi.GetElement_2(i);
- return suma -= (suma * popust);
- }
- //Funkciju za ispis svih vrijednosti atributa klase.
- void Info()
- {
- cout << crt << "\t\tDatum narudzbe: " << _datum << crt;
- if (_proizvodi.GetTrenutno() > 0)
- {
- cout << "Ukupno proizvoda: " << _proizvodi.GetTrenutno() << endl;
- cout << "\n\t\t:::LISTA PROIZVODA:::\n\n";
- for (size_t i = 0; i < _proizvodi.GetTrenutno(); i++)
- cout << _proizvodi.GetElement_1(i) << "Kolicina: " << _proizvodi.GetElement_2(i) << endl << endl;
- }
- else
- cout << "Narudzba trenutno nema proizvoda";
- cout << "------------------------------------------------------\n";
- }
- };
- class Klijent : public Osoba
- {
- protected:
- int _id;
- char* _email;
- static int BROJ_ID;
- Kolekcija<Narudzba, double> _narudzbe; //Podaci o narudžbama sa ukupnim utrošenim iznosom u KM
- public:
- //Potrebne konstruktor i destruktor funkcije.
- Klijent(char* ime, char* prezime, char* adresa, char* telefon, char * email) : Osoba(ime, prezime, adresa, telefon), _id(BROJ_ID++)
- {
- int size = strlen(email) + 1;
- _email = new char[size];
- strcpy_s(_email, size, email);
- }
- Klijent(const Klijent & obj) : Osoba(obj), _id(obj._id)
- {
- int size = strlen(obj._email) + 1;
- _email = new char[size];
- strcpy_s(_email, size, obj._email);
- _narudzbe = obj._narudzbe;
- }
- ~Klijent()
- {
- delete[]_email;
- _email = nullptr;
- }
- Klijent & operator= (const Klijent & right)
- {
- if (this != &right)
- {
- delete[] _email;
- Osoba::operator=(right);
- _id = right._id;
- int size = strlen(right._email) + 1;
- _email = new char[size];
- strcpy_s(_email, size, right._email);
- _narudzbe = right._narudzbe;
- }
- return *this;
- }
- //Funkciju za evidenciju nove narudžbe.
- virtual void EvidentirajNarudzbu(const Narudzba & narudzba)
- {
- Narudzba order(narudzba);
- double ukupnoPotroseno = narudzba.GetIznosNarudzbe();
- _narudzbe.DodajElemente(order, ukupnoPotroseno);
- }
- //Funkciju koja vraća logičku vrijednost true ukoliko je klijent za odabrani mjesec u godini imao
- //potrošnju veću od zadate. Prototip funkcije: bool IsPotrosnjaVeca(int mjesec, double potrosnja);
- //Za pronalazak potrosnje po mjesecima koristiti podatak o datumu narudžbe.
- bool IsPotrosnjaVeca(int mjesec, double potrosnja)
- {
- for (size_t i = 0; i < _narudzbe.GetTrenutno(); i++)
- {
- if (mjesec == _narudzbe.GetElement_1(i).GetMjesecNarudzbe())
- if (_narudzbe.GetElement_2(i) >= potrosnja)
- return true;
- }
- return false;
- }
- //Funkciju za izmjenu e-mail adrese.
- bool SetEmail(const char * email)
- {
- if (email != nullptr)
- {
- delete[]_email;
- int size = strlen(email) + 1;
- _email = new char[size];
- strcpy_s(_email, size, email);
- return true;
- }
- return false;
- }
- //Funkciju za ispis svih vrijednosti atributa klase.
- void Info()
- {
- cout << "ID: " << _id << endl;
- cout << "Ime i prezime: " << _ime << " " << _prezime << endl;
- cout << "Adresa: " << _adresa << endl;
- cout << "Telefon: " << _telefon << endl;
- cout << "E-mail: " << _email;
- cout << crt << "\t:::PODACI O NARUDZBAMA:::\n";
- if (_narudzbe.GetTrenutno() > 0)
- {
- cout << "Broj evidentiranih narudzbi: " << _narudzbe.GetTrenutno() << endl;
- for (size_t i = 0; i < _narudzbe.GetTrenutno(); i++)
- {
- cout << "\t\t>> Narudzba [" << i + 1 << "/" << _narudzbe.GetTrenutno() << "] <<";
- _narudzbe.GetElement_1(i).Info();
- cout << "Ukupno potroseno: " << _narudzbe.GetElement_2(i) << "KM";
- }
- }
- else
- cout << "Klijent [" << _ime << " " << _prezime << "] nema evidentiranih narudzbi" << endl;
- }
- };
- int Klijent::BROJ_ID = 1;
- class VIPKlijent : public Klijent
- {
- char* _parkingOznaka;
- double _popust;
- public:
- //Potrebne konstruktor i destruktor funkcije
- VIPKlijent(char* ime, char* prezime, char* adresa, char* telefon, char * email, char * parkingOznaka, double popust) : Klijent(ime, prezime, adresa, telefon, email)
- {
- int size = strlen(parkingOznaka) + 1;
- _parkingOznaka = new char[size];
- strcpy_s(_parkingOznaka, size, parkingOznaka);
- _popust = popust;
- }
- VIPKlijent(const VIPKlijent & obj) : Klijent(obj)
- {
- int size = strlen(obj._parkingOznaka) + 1;
- _parkingOznaka = new char[size];
- strcpy_s(_parkingOznaka, size, obj._parkingOznaka);
- _popust = obj._popust;
- }
- ~VIPKlijent()
- {
- delete[]_parkingOznaka;
- _parkingOznaka = nullptr;
- }
- VIPKlijent & operator = (const VIPKlijent & right)
- {
- if (this != &right)
- {
- delete[]_parkingOznaka;
- Klijent::operator=(right);
- int size = strlen(right._parkingOznaka) + 1;
- _parkingOznaka = new char[size];
- strcpy_s(_parkingOznaka, size, right._parkingOznaka);
- _popust = right._popust;
- }
- return *this;
- }
- //Funkciju za evidenciju nove narudžbe sa zaračunatim popustom na sve proizvode.
- void EvidentirajNarudzbu(const Narudzba & narudzba)
- {
- Narudzba order(narudzba);
- double ukupnoPotroseno = narudzba.GetIznosNarudzbe(_popust);
- _narudzbe.DodajElemente(order, ukupnoPotroseno);
- }
- //Funkciju koja posljednjoj narudžbi klijenta dodaje gratis proizvod proslijeđen kao parametar.
- bool DodajGratisProizvod(const Proizvod & proizvod)
- {
- if (_narudzbe.GetTrenutno() == 0)
- {
- cout << "Ne postoji nijedna evidentirana narudzba" << endl;
- return false;
- }
- _narudzbe[_narudzbe.GetTrenutno() - 1].DodajProizvod(proizvod);
- return true;
- }
- //Funkciju za ispis svih vrijednosti atributa klase
- void Info()
- {
- cout << "ID: " << _id << endl;
- cout << "Ime i prezime: " << _ime << " " << _prezime << endl;
- cout << "Adresa: " << _adresa << endl;
- cout << "Telefon: " << _telefon << endl;
- cout << "E-mail: " << _email << endl;
- cout << "Parking oznaka: " << _parkingOznaka << endl;
- cout << "Popust: " << _popust << "%";
- cout << crt << "\t:::PODACI O NARUDZBAMA:::\n";
- if (_narudzbe.GetTrenutno() > 0)
- {
- cout << "Broj evidentiranih narudzbi: " << _narudzbe.GetTrenutno() << endl;
- for (size_t i = 0; i < _narudzbe.GetTrenutno(); i++)
- {
- cout << "\t\t>> Narudzba [" << i + 1 << "/" << _narudzbe.GetTrenutno() << "] <<";
- _narudzbe.GetElement_1(i).Info();
- cout << "Ukupno potroseno: " << _narudzbe.GetElement_2(i) << "KM\n";
- }
- }
- else
- cout << "Klijent [" << _ime << " " << _prezime << "] nema evidentiranih narudzbi" << endl;
- }
- };
- const char * sastaviMail(const char * ime, const char * prezime)
- {
- int size = strlen(ime) + 1;
- char * temp_ime = new char[size];
- strcpy_s(temp_ime, size, ime);
- size = strlen(prezime) + 1;
- char * temp_prezime = new char[size];
- strcpy_s(temp_prezime, size, prezime);
- for (size_t i = 0; i < strlen(ime); i++)
- {
- temp_ime[i] = tolower(ime[i]);
- for (size_t j = 0; j < prezime[j]; j++)
- temp_prezime[j] = tolower(prezime[j]);
- }
- char temp[100];
- strcpy_s(temp, temp_ime);
- strcat_s(temp, ".");
- strcat_s(temp, temp_prezime);
- strcat_s(temp, "@firma.ba");
- size = strlen(temp) + 1;
- char * email = new char[size];
- strcpy_s(email, size, temp);
- return email;
- }
- //Funkcija treba da pronađe sve one klijente koji su za odabrani mjesec ostvarili potrošnju veću
- //od zadate. Ispisati njihove podatke i vratiti ukupan broj pronađenih klijenata.
- int PronadjiKlijentePoPotrosnji(Osoba* niz[], int length)
- {
- int mjesec = 0;
- int brojacKlijenata = 0;
- double potrosnja = 0;
- Klijent * klijent = nullptr;
- cout << "Unesite mjesec: ";
- cin >> mjesec;
- cin.ignore();
- cout << "Unesite potrosnju: ";
- cin >> potrosnja;
- cin.ignore();
- for (size_t i = 0; i < length; i++)
- {
- klijent = dynamic_cast<Klijent*>(niz[i]);
- if (klijent != nullptr)
- {
- if (klijent->IsPotrosnjaVeca(mjesec, potrosnja))
- {
- klijent->Info();
- brojacKlijenata++;
- }
- }
- }
- return brojacKlijenata;
- }
- //Pronaći sve VIP klijente, te im dodati gratis proizvod proslijeđen kao parametar.
- void UkljuciGratisProizvodVIPKlijentima(Osoba* niz[], int length, const Proizvod& proizvod)
- {
- VIPKlijent * vip_klijent = nullptr;
- for (size_t i = 0; i < length; i++)
- {
- vip_klijent = dynamic_cast<VIPKlijent *>(niz[i]);
- if (vip_klijent != nullptr)
- vip_klijent->DodajGratisProizvod(proizvod);
- }
- }
- //Svim klijentima postaviti mail adresu u formatu: ime.prezime@firma.ba.
- void GenerisiMailAdreseKlijentima(Osoba* niz[], int length)
- {
- Klijent * klijent = nullptr;
- for (size_t i = 0; i < length; i++)
- {
- klijent = dynamic_cast<Klijent *>(niz[i]);
- if (klijent != nullptr)
- {
- if (typeid(Klijent) == typeid(*klijent))
- klijent->SetEmail(sastaviMail(klijent->GetIme(), klijent->GetPrezime()));
- }
- }
- }
- //Omogućiti pretragu klijenata po imenu ili prezimenu (ispisati informacije
- //o svim klijentima koji sadrže uneseni tekst u dijelu imena ili prezimena.
- void PronadjiKlijentePoImenuPrezimenu(Osoba* niz[], int length)
- {
- Klijent * klijent = nullptr;
- char temp[50];
- cout << "Unesite ime ili prezime klijenta: ";
- cin.getline(temp, 50);
- for (size_t i = 0; i < length; i++)
- {
- klijent = dynamic_cast<Klijent*>(niz[i]);
- if (klijent != nullptr)
- {
- if (strstr(klijent->GetIme(), temp) != 0 || strstr(klijent->GetPrezime(), temp) != 0)
- klijent->Info();
- }
- }
- }
- void main()
- {
- const int length = 5;
- Osoba * niz[length];
- niz[0] = new Klijent("Nisvet", "Mujkic", "Ahmeta Fetahagica bb", "060/302-5434", "nisvet.mujkic@edu.fit.ba");
- niz[1] = new Klijent("Eman", "Basic", "Ulica Marsala Tita 14", "061/551-3421", "eman.basic@tu.au");
- niz[2] = new Klijent("Johnny", "Bravo", "Ulica Dzemala Bijedica", "065/652-5984", "johnny.bravo@gov.us");
- niz[3] = new VIPKlijent("Mirnes", "Cerimovic", "Tuzlanska bb", "061/777-3333", "mirnes.cerimovic@untz.ba", "MC_1", 50);
- niz[4] = new VIPKlijent("Haris", "Kudic", "Ilidza 12", "062/919-8732", "haris.kudic@edu.burch.ba", "HK_2", 0.1);
- Proizvod sok("Cappy", "Tropsko voce", 1.3);
- Proizvod cokolada("Milka", "Cokolada sa nougatom", 1.7);
- Proizvod cips("Frank Chips XXL", "Slani cips", 2.4);
- Proizvod sladoled("Macho", "Vanilija", 1.5);
- Narudzba order_1("11/12/2016");
- order_1.DodajProizvod(sok, 10);
- order_1.DodajProizvod(cokolada);
- order_1.DodajProizvod(cokolada);
- Narudzba order_2("04/7/2016");
- order_2.DodajProizvod(cips, 15);
- order_2.DodajProizvod(sok, 4);
- Klijent * k1 = dynamic_cast<Klijent*>(niz[0]);
- if (k1 != nullptr)
- k1->EvidentirajNarudzbu(order_1);
- VIPKlijent * k2 = dynamic_cast<VIPKlijent*>(niz[3]);
- if (k2 != nullptr)
- k2->EvidentirajNarudzbu(order_1);
- GenerisiMailAdreseKlijentima(niz, length);
- UkljuciGratisProizvodVIPKlijentima(niz, length, cips);
- PronadjiKlijentePoImenuPrezimenu(niz, length);
- PronadjiKlijentePoPotrosnji(niz, length);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement