Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #define MAX 100
- using namespace std;
- enum typeC{standard=0,loyal=1,vip=2};
- class UserExistsException{
- private:
- char poraka[100];
- public:
- UserExistsException(const char *poraka="")
- {
- strcpy(this->poraka,poraka);
- }
- void print()
- {
- cout<<poraka<<endl;
- }
- };
- class Customer{
- private:
- char ime[50];
- char eadresa[50];
- typeC vid;
- static int osnoven_popust;
- const static int dopolnitelen_popust;
- int broj_kupeni;
- public:
- Customer(const char *ime="",const char *eadresa="",typeC vid=(typeC)0,int broj_kupeni=0)
- {
- strcpy(this->ime,ime);
- strcpy(this->eadresa,eadresa);
- this->vid=vid;
- this->broj_kupeni=broj_kupeni;
- }
- typeC getVid()
- {
- return vid;
- }
- void setVid(int number)
- {
- vid=(typeC)number;
- }
- int getKupeni()
- {
- return broj_kupeni;
- }
- static void setDiscount1(int number)
- {
- osnoven_popust=number;
- }
- static int getPopust()
- {
- return osnoven_popust;
- }
- friend ostream &operator << (ostream &out,Customer &c)
- {
- out<<c.ime<<endl<<c.eadresa<<endl<<c.broj_kupeni<<endl;
- if(c.getVid()==0)
- {
- out<<"standard"<<" "<<"0"<<endl;
- }
- else if(c.getVid()==1)
- {
- out<<"loyal"<<" "<<c.osnoven_popust<<endl;
- }
- else if(c.getVid()==2)
- {
- out<<"vip"<<" "<<c.dopolnitelen_popust+c.osnoven_popust<<endl;
- }
- return out;
- }
- char *getAdresa()
- {
- return eadresa;
- }
- };
- int Customer::osnoven_popust=10;
- const int Customer::dopolnitelen_popust=20;
- class FINKI_bookstore{
- private:
- Customer *kupuvaci;
- int broj;
- public:
- FINKI_bookstore()
- {
- this->kupuvaci = new Customer[0];
- this->broj=0;
- }
- void setCustomers(Customer *novi,int n)
- {
- this->kupuvaci = new Customer[n];
- this->broj=n;
- for(int i=0;i<broj;i++)
- {
- this->kupuvaci[i]=novi[i];
- }
- }
- FINKI_bookstore &operator+=(Customer &arg)
- {
- int flag=0;
- for(int i=0;i<broj;i++)
- {
- if(strcmp(kupuvaci[i].getAdresa(),arg.getAdresa())==0)
- {
- throw UserExistsException("The user already exists in the list!");
- flag=1;
- }
- }
- if(flag==0)
- {
- Customer *tmp = new Customer[broj+1];
- for(int i=0;i<broj;i++)
- {
- tmp[i]=kupuvaci[i];
- }
- tmp[broj++]=arg;
- delete [] kupuvaci;
- kupuvaci = tmp;
- }
- return *this;
- }
- void update()
- {
- for(int i=0;i<broj;i++)
- {
- if(kupuvaci[i].getVid()==0)
- {
- if(kupuvaci[i].getKupeni()>5)
- {
- kupuvaci[i].setVid((typeC)1);
- }
- }
- else if(kupuvaci[i].getVid()==1)
- {
- if(kupuvaci[i].getKupeni()>10)
- {
- kupuvaci[i].setVid((typeC)2);
- }
- }
- }
- }
- friend ostream &operator <<(ostream &out,FINKI_bookstore &fb)
- {
- for(int i=0;i<fb.broj;i++)
- {
- out<<fb.kupuvaci[i];
- }
- return out;
- }
- };
- int main(){
- int testCase;
- cin >> testCase;
- char name[MAX];
- char email[MAX];
- int tC;
- int discount;
- int numProducts;
- if (testCase == 1){
- cout << "===== Test Case - Customer Class ======" << endl;
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- cout << "===== CONSTRUCTOR ======" << endl;
- Customer c(name, email, (typeC) tC, numProducts);
- cout << c;
- }
- if (testCase == 2){
- cout << "===== Test Case - Static Members ======" << endl;
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- cout << "===== CONSTRUCTOR ======" << endl;
- Customer c(name, email, (typeC) tC, numProducts);
- cout << c;
- c.setDiscount1(5);
- cout << c;
- }
- if (testCase == 3){
- cout << "===== Test Case - FINKI-bookstore ======" << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << fc <<endl;
- }
- if (testCase == 4){
- cout << "===== Test Case - operator+= ======" << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << "OPERATOR +=" << endl;
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- try{
- fc+=c;
- }
- catch(UserExistsException &exc)
- {
- exc.print();
- }
- cout << fc;
- }
- if (testCase == 5){
- cout << "===== Test Case - operator+= (exception) ======" << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << "OPERATOR +=" << endl;
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- try {
- fc+=c;
- }
- catch (UserExistsException &ex) {
- ex.print();
- }
- cout << fc;
- }
- if (testCase == 6){
- cout << "===== Test Case - update method ======" << endl << endl;
- FINKI_bookstore fc;
- int n;
- cin >> n;
- Customer customers[MAX];
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,MAX);
- cin.getline(email,MAX);
- cin >> tC;
- cin >> numProducts;
- Customer c(name, email, (typeC) tC, numProducts);
- customers[i] = c;
- }
- fc.setCustomers(customers, n);
- cout << "Update:" << endl;
- fc.update();
- cout << fc;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement