Advertisement
LegoDrifter

OOP - Zadaca 2

Jun 28th, 2020
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #define MAX 100
  4.  
  5. using namespace std;
  6.  
  7. enum typeC{standard=0,loyal=1,vip=2};
  8. class UserExistsException{
  9. private:
  10.     char poraka[100];
  11. public:
  12.     UserExistsException(const char *poraka="")
  13.     {
  14.         strcpy(this->poraka,poraka);
  15.     }
  16.     void print()
  17.     {
  18.         cout<<poraka<<endl;
  19.     }
  20.  
  21.  
  22. };
  23. class Customer{
  24. private:
  25.     char ime[50];
  26.     char eadresa[50];
  27.     typeC vid;
  28.     static int osnoven_popust;
  29.     const static int dopolnitelen_popust;
  30.     int broj_kupeni;
  31. public:
  32.     Customer(const char *ime="",const char *eadresa="",typeC vid=(typeC)0,int broj_kupeni=0)
  33.     {
  34.         strcpy(this->ime,ime);
  35.         strcpy(this->eadresa,eadresa);
  36.         this->vid=vid;
  37.         this->broj_kupeni=broj_kupeni;
  38.     }
  39.     typeC getVid()
  40.     {
  41.         return vid;
  42.     }
  43.     void setVid(int number)
  44.     {
  45.         vid=(typeC)number;
  46.     }
  47.     int getKupeni()
  48.     {
  49.         return broj_kupeni;
  50.     }
  51.     static void setDiscount1(int number)
  52.     {
  53.         osnoven_popust=number;
  54.     }
  55.     static int getPopust()
  56.     {
  57.         return osnoven_popust;
  58.     }
  59.     friend ostream &operator << (ostream &out,Customer &c)
  60.     {
  61.         out<<c.ime<<endl<<c.eadresa<<endl<<c.broj_kupeni<<endl;
  62.         if(c.getVid()==0)
  63.         {
  64.             out<<"standard"<<" "<<"0"<<endl;
  65.         }
  66.         else if(c.getVid()==1)
  67.         {
  68.             out<<"loyal"<<" "<<c.osnoven_popust<<endl;
  69.         }
  70.         else if(c.getVid()==2)
  71.         {
  72.             out<<"vip"<<" "<<c.dopolnitelen_popust+c.osnoven_popust<<endl;
  73.         }
  74.         return out;
  75.     }
  76.     char *getAdresa()
  77.     {
  78.         return eadresa;
  79.     }
  80.  
  81.  
  82. };
  83. int Customer::osnoven_popust=10;
  84. const int Customer::dopolnitelen_popust=20;
  85. class FINKI_bookstore{
  86. private:
  87.     Customer *kupuvaci;
  88.     int broj;
  89. public:
  90.     FINKI_bookstore()
  91.     {
  92.         this->kupuvaci = new Customer[0];
  93.         this->broj=0;
  94.     }
  95.     void setCustomers(Customer *novi,int n)
  96.     {
  97.         this->kupuvaci = new Customer[n];
  98.         this->broj=n;
  99.         for(int i=0;i<broj;i++)
  100.         {
  101.             this->kupuvaci[i]=novi[i];
  102.         }
  103.     }
  104.     FINKI_bookstore &operator+=(Customer &arg)
  105.     {
  106.    
  107.         int flag=0;
  108.         for(int i=0;i<broj;i++)
  109.         {
  110.             if(strcmp(kupuvaci[i].getAdresa(),arg.getAdresa())==0)
  111.             {
  112.                 throw UserExistsException("The user already exists in the list!");
  113.                 flag=1;
  114.             }
  115.         }
  116.         if(flag==0)
  117.         {
  118.         Customer *tmp = new Customer[broj+1];
  119.         for(int i=0;i<broj;i++)
  120.         {
  121.             tmp[i]=kupuvaci[i];
  122.         }
  123.         tmp[broj++]=arg;
  124.         delete [] kupuvaci;
  125.         kupuvaci = tmp;
  126.        
  127.         }
  128.          return *this;
  129.     }
  130.     void update()
  131.     {
  132.         for(int i=0;i<broj;i++)
  133.         {
  134.             if(kupuvaci[i].getVid()==0)
  135.             {
  136.                 if(kupuvaci[i].getKupeni()>5)
  137.                 {
  138.                     kupuvaci[i].setVid((typeC)1);
  139.                 }
  140.             }
  141.             else if(kupuvaci[i].getVid()==1)
  142.             {
  143.                 if(kupuvaci[i].getKupeni()>10)
  144.                 {
  145.                     kupuvaci[i].setVid((typeC)2);
  146.                 }
  147.             }
  148.         }
  149.     }
  150.     friend ostream &operator <<(ostream &out,FINKI_bookstore &fb)
  151.     {
  152.         for(int i=0;i<fb.broj;i++)
  153.         {
  154.             out<<fb.kupuvaci[i];
  155.         }
  156.         return out;
  157.     }
  158.  
  159.  
  160. };
  161. int main(){
  162.   int testCase;
  163.   cin >> testCase;
  164.  
  165.   char name[MAX];
  166.   char email[MAX];
  167.   int tC;
  168.   int discount;
  169.   int numProducts;
  170.  
  171.  
  172.   if (testCase == 1){
  173.     cout << "===== Test Case - Customer Class ======" << endl;
  174.     cin.get();
  175.     cin.getline(name,MAX);
  176.     cin.getline(email,MAX);
  177.     cin >> tC;
  178.     cin >> numProducts;
  179.     cout << "===== CONSTRUCTOR ======" << endl;
  180.     Customer c(name, email, (typeC) tC, numProducts);
  181.     cout << c;
  182.  
  183.   }
  184.  
  185.   if (testCase == 2){
  186.     cout << "===== Test Case - Static Members ======" << endl;
  187.     cin.get();
  188.     cin.getline(name,MAX);
  189.     cin.getline(email,MAX);
  190.     cin >> tC;
  191.     cin >> numProducts;
  192.     cout << "===== CONSTRUCTOR ======" << endl;
  193.     Customer c(name, email, (typeC) tC, numProducts);
  194.     cout << c;
  195.  
  196.     c.setDiscount1(5);
  197.  
  198.     cout << c;
  199.   }
  200.  
  201.   if (testCase == 3){
  202.     cout << "===== Test Case - FINKI-bookstore ======" << endl;
  203.     FINKI_bookstore fc;
  204.     int n;
  205.     cin >> n;
  206.     Customer customers[MAX];
  207.     for(int i = 0; i < n; ++i) {
  208.       cin.get();
  209.       cin.getline(name,MAX);
  210.       cin.getline(email,MAX);
  211.       cin >> tC;
  212.       cin >> numProducts;
  213.       Customer c(name, email, (typeC) tC, numProducts);
  214.       customers[i] = c;
  215.     }
  216.  
  217.     fc.setCustomers(customers, n);
  218.  
  219.     cout << fc <<endl;
  220.   }
  221.  
  222.   if (testCase == 4){
  223.     cout << "===== Test Case - operator+= ======" << endl;
  224.     FINKI_bookstore fc;
  225.     int n;
  226.     cin >> n;
  227.     Customer customers[MAX];
  228.     for(int i = 0; i < n; ++i) {
  229.       cin.get();
  230.       cin.getline(name,MAX);
  231.       cin.getline(email,MAX);
  232.       cin >> tC;
  233.       cin >> numProducts;
  234.       Customer c(name, email, (typeC) tC, numProducts);
  235.       customers[i] = c;
  236.     }
  237.  
  238.     fc.setCustomers(customers, n);
  239.     cout << "OPERATOR +=" << endl;
  240.     cin.get();
  241.     cin.getline(name,MAX);
  242.     cin.getline(email,MAX);
  243.     cin >> tC;
  244.     cin >> numProducts;
  245.     Customer c(name, email, (typeC) tC, numProducts);
  246.     try{
  247.     fc+=c;
  248.     }
  249.     catch(UserExistsException &exc)
  250.     {
  251.         exc.print();
  252.     }
  253.  
  254.     cout << fc;
  255.   }
  256.  
  257.   if (testCase == 5){
  258.     cout << "===== Test Case - operator+= (exception) ======" << endl;
  259.     FINKI_bookstore fc;
  260.     int n;
  261.     cin >> n;
  262.     Customer customers[MAX];
  263.     for(int i = 0; i < n; ++i) {
  264.       cin.get();
  265.       cin.getline(name,MAX);
  266.       cin.getline(email,MAX);
  267.       cin >> tC;
  268.       cin >> numProducts;
  269.       Customer c(name, email, (typeC) tC, numProducts);
  270.       customers[i] = c;
  271.         }
  272.     fc.setCustomers(customers, n);
  273.     cout << "OPERATOR +=" << endl;
  274.     cin.get();
  275.     cin.getline(name,MAX);
  276.     cin.getline(email,MAX);
  277.     cin >> tC;
  278.     cin >> numProducts;
  279.           Customer c(name, email, (typeC) tC, numProducts);
  280.       try {
  281.     fc+=c;
  282.       }
  283.          catch (UserExistsException &ex) {
  284.       ex.print();
  285.       }
  286.     cout << fc;
  287.   }
  288.  
  289.   if (testCase == 6){
  290.     cout << "===== Test Case - update method  ======" << endl << endl;
  291.     FINKI_bookstore fc;
  292.     int n;
  293.     cin >> n;
  294.     Customer customers[MAX];
  295.     for(int i = 0; i < n; ++i) {
  296.       cin.get();
  297.       cin.getline(name,MAX);
  298.       cin.getline(email,MAX);
  299.       cin >> tC;
  300.       cin >> numProducts;
  301.       Customer c(name, email, (typeC) tC, numProducts);
  302.       customers[i] = c;
  303.     }
  304.  
  305.     fc.setCustomers(customers, n);
  306.  
  307.     cout << "Update:" << endl;
  308.     fc.update();
  309.     cout << fc;
  310.   }
  311.   return 0;
  312.    
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement